Simple Virtual Filesystem in C/C++

前端 未结 6 1871
一个人的身影
一个人的身影 2020-12-31 15:02

I would like to implement a very simple Virtual Filesystem (VFS) which supports some basic file system operation like fwrite, fopen, fput, etc. The VFS is an abstraction lay

相关标签:
6条回答
  • 2020-12-31 15:31

    Maybe the simplest/cleanest implementation would be to create two separate libraries, one for windows and one for linux, without littering your code with cascaded if and switch statements. The libraries would implement the same functions, defined in a common header.

    Also, remember that the code inside your check OS == something would be compiled and linked on all systems, so e.g. the library compiled on linux should be able to resolve the windows system calls...

    I think separating things (different OS, different cpp files) is the simplest solution.

    EDIT:

    If you are using C++, why not just relying on streams? The standard library already provides the functionality you are trying to implement and is available on all platforms.
    Otherwise, here's a link to Windows File Management Functions.

    SECOND EDIT:

    If you want a cross-platform file system library, supporting among other things directory creation, you could check the boost filesystem library.

    0 讨论(0)
  • 2020-12-31 15:32

    I don't think you can compile a module for different OSes, the way you want.

    // Make the distinction at compile time,
    
    FILE VFS_File_Open( const unsigned char strFile, int flags )
    {
    #ifdef _WINDOWS
        //implement here the system calls required to open a file on a WIN OS
    #endif
    #ifdef _LINUX
        //implement here the system calls required to open a file on a Linux OS
    #endif
        etc
    }
    
    0 讨论(0)
  • 2020-12-31 15:38

    Standard C has no such feature. Notice that the notion of "concrete OS" is also a bit vague: are Windows XP and Windows Vista the same "concrete OS", or different ones? Are CentOS and Ubuntu the same OS, or different ones?

    Apparently, you are only looking for API differences, so in most cases, you can probably ignore version differences and distribution differences (although both Windows and Linux grow new system calls from time to time). In this case, it is best to preprocessor conditional compilation - since the code making Linux-specific calls won't even compile on Windows.

    Traditionally, the system compilers have various macros predefined. Here are a few such macros, on respective systems: _WIN32, __linux, __linux__, __AIX__, __hpux, ... If you need to identify a specific system, you should ask again on SO.

    0 讨论(0)
  • 2020-12-31 15:39

    Actually implemented this, so from experience here:

    The first thing to do is use classes. There is no fopen() equivalent. If there are flags, they're going to be an enum. Filenames are wchar_t nowadays.

    The second thing to do is factor out the OS-dependent parts of your file class. They should be in separate methods. You move these to a seperate file. For every OS you have, there will be a different file implementing the same methods. When you link your app, you know the target architecture and you can pick the correct versions.

    0 讨论(0)
  • 2020-12-31 15:42

    You could try PhysicsFS library.

    0 讨论(0)
  • 2020-12-31 15:42

    If you're looking for a way to abstract yourself from the properties of the filesystem you're using (path separators etc.), and if you're happy with a C++-only solution, take a look at Boost.Filesystem.

    Paths can be specified in the portable generic path format (basically POSIX format) and are automatically converted to the native format:

    path my_path( "some_dir/file.txt" );
    

    Elements can be concatenated onto the path using the / operator, and the result can then directly be used to open a file:

    ifstream file1( my_path / "foo/bar" );
    

    What's more, this functionality is part of Technical Report 2, meaning that it will likely make its way into the standard library.

    0 讨论(0)
提交回复
热议问题