• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

OSX/Linux filesystem functions?

Status
Not open for further replies.

Deleted member 219079

D

Deleted member 219079

I used Linux for a while and swore to god never going to Windows. I googled "replacement for Visual Studio"... Now I'm on Windows. I'd need Linux version for these functions:
Code:
#include <stdbool.h>

struct OS_DIR_ENUM_DATA;
static inline char* dir_enum_first(const char* path, struct OS_DIR_ENUM_DATA* data);
static inline bool dir_enum_next(struct OS_DIR_ENUM_DATA* data);
static inline char* dir_enum_fname(struct OS_DIR_ENUM_DATA* data);
static inline char* dir_enum_end(struct OS_DIR_ENUM_DATA* data);
static inline bool dir_enum_isdir(struct OS_DIR_ENUM_DATA* data);
static inline bool dir_exists(const char* path);
static inline bool get_app_dir(char* dest);
static inline bool get_exec_dir(char* dest);

Here's Windows - implementation:
Code:
#ifdef _WIN32

#include <Windows.h>
#define OS_MAX_PATH MAX_PATH

struct OS_DIR_ENUM_DATA { HANDLE h; WIN32_FIND_DATAA ffd; };

static inline char* get_error_string();
static inline char* get_error_string_ex(DWORD);

static inline bool get_app_dir(char* dest) {
    HMODULE hModule = GetModuleHandle(NULL);
    DWORD findLen = GetModuleFileNameA(hModule, dest, OS_MAX_PATH);
    return !!findLen;
}

static inline bool get_exec_dir(char* dest) {
    DWORD findLen = GetCurrentDirectoryA(OS_MAX_PATH, dest);
    return !!findLen;
}

static inline char* dir_enum_first(const char* path, struct OS_DIR_ENUM_DATA* data) {
    data->h = FindFirstFileA(path, &data->ffd);
    if (data->h == INVALID_HANDLE_VALUE) return get_error_string();
    return NULL;
}

static inline char* dir_enum_end(struct OS_DIR_ENUM_DATA* data) {
    DWORD dwError = GetLastError();
    if (dwError != ERROR_NO_MORE_FILES) return get_error_string_ex(dwError);
    FindClose(data->h);
    return NULL;
}

static inline char* dir_enum_fname(struct OS_DIR_ENUM_DATA* data) {    return data->ffd.cFileName;    }
static inline bool dir_enum_isdir(struct OS_DIR_ENUM_DATA* data) {    return data->ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY; }
static inline bool dir_enum_next(struct OS_DIR_ENUM_DATA* data) {    return (FindNextFileA(data->h, &data->ffd) != 0); }

// author: Jamin Grey @http://stackoverflow.com/a/17387176/5945178
static inline char* get_error_string_ex(DWORD errorMessageID) {
    LPSTR messageBuffer = NULL;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &messageBuffer, 0, NULL);
    char* message = malloc(size + 1);
    memcpy(message, messageBuffer, size);
    message[size] = 0;
    LocalFree(messageBuffer);
    return message;
}

// author: Jamin Grey @http://stackoverflow.com/a/17387176/5945178
static inline char* get_error_string() {
    DWORD errorMessageID = GetLastError();
    if (errorMessageID == 0) return NULL;
    return get_error_string_ex(errorMessageID);
}

// author: retrodrone @http://stackoverflow.com/a/6218445/5945178
static inline bool dir_exists(const char* path)
{
    DWORD dwAttrib = GetFileAttributesA(path);

    return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
        (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

#endif

Critique such as "you need to do it type *myPointer, not type* myPointer" isn't really needed, I was following some WIN32 tutorial and it had the variables so and my whole project is following that "convention" now...

If you use OS X, you can provide that as well. Altough idk how I can compile for it lol.
 

Deleted member 219079

D

Deleted member 219079

It's actually C. And no not for SharpCraft, this is my other ambitious project >:)

Ah yes I forgot about dirent. I could use it.

What about OS X?
 

Deleted member 219079

D

Deleted member 219079

I rely heavily on StackOverflow for solving any issues. C++ folks don't prioritize performance and I get answers involving some stupid class.

If I ended up using C++, I'd still rely on C functionality most of the time, then you'd come and ask "Why not use C?".

C++ is for weird enthusiasts in my eyes, I will use C#/Java if I want more abstract functions. Now there might come a time a library I'd require uses C++, I'd look for Java/C# solution and transfer over.
 

Deleted member 219079

D

Deleted member 219079

I don't like sweet snacks. I must like salty snacks.

C introduces little bureaucracy. I like it. There now you know why I like the language.
 
just use boost libraries for that purpose, they eliminate the need to write OS specific functions for filesystems:

Boost Filesystem Library

you can use a wrapper to compile it as an static library for C.

You can also use the C POSIX Library. Have in mind, that OS X and most Linux distributions complie with the POSIX standard but Windows doesn't unless you use wrappers or the unix subsystem in windows 10 developer.
 
Last edited:

Deleted member 219079

D

Deleted member 219079

I'm resorting to sole Win32 as I realize that unicode support is a must for front end application and I'm uncertain of how those wrappers handle it. I want my program to be able to support e.g. cyrillic characters. (Also have to use C++ as this error code passing was fucking irritating.. But that's not implying I like it. CUZ I DONT)
 
Level 13
Joined
Sep 13, 2010
Messages
550
Doesn't C++ have a cross platform file system class nowadays? I remember them talking about that years ago.

Either way, I used this in the past and it worked well.

Yeah it was suggested and planned to get implemented into c++ for a while. It is a part of c++17 and as it seems it's not in experimental state anymore. The docs are still half missing tho. MSVS2015 itself only has the experimental libs, meanwhile gcc and clang seem to have c++17 support, not sure if they left experimental phase.

As far as I can see it supports unicode encodings so unless you wanna make stuff for yourself maybe it is recommended to check it out for yourself.

Filesystem library - cppreference.com
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,199
When building on Linux using GCC it is important to declare that the build is large file system aware otherwise it will fail on large multi-TB partitions.

C++ file systems API is an attempt to give feature level parity with Java which already has a file systems API for many years. The only purpose of a file systems API is so that one can create file systems from a variety of sources such as an MPQ file system which will transparently work as if it was the standard file system.
 
Status
Not open for further replies.
Top