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:
Here's Windows - implementation:
Critique such as "you need to do it
If you use OS X, you can provide that as well. Altough idk how I can compile for it lol.
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.