mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-09 15:28:21 -04:00
Avoid pointless check for if options.txt exists
This commit is contained in:
parent
33a6992cca
commit
c26e463a05
@ -1005,10 +1005,8 @@ static struct CheckResourcesScreen {
|
|||||||
|
|
||||||
static void CheckResourcesScreen_Yes(void* w, int idx) { FetchResourcesScreen_SetActive(); }
|
static void CheckResourcesScreen_Yes(void* w, int idx) { FetchResourcesScreen_SetActive(); }
|
||||||
static void CheckResourcesScreen_Next(void* w, int idx) {
|
static void CheckResourcesScreen_Next(void* w, int idx) {
|
||||||
static const cc_string optionsTxt = String_FromConst("options.txt");
|
|
||||||
Http_ClearPending();
|
Http_ClearPending();
|
||||||
|
if (Options_LoadResult != ReturnCode_FileNotFound) {
|
||||||
if (File_Exists(&optionsTxt)) {
|
|
||||||
MainScreen_SetActive();
|
MainScreen_SetActive();
|
||||||
} else {
|
} else {
|
||||||
ChooseModeScreen_SetActive(true);
|
ChooseModeScreen_SetActive(true);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
struct StringsBuffer Options;
|
struct StringsBuffer Options;
|
||||||
static struct StringsBuffer changedOpts;
|
static struct StringsBuffer changedOpts;
|
||||||
|
cc_result Options_LoadResult;
|
||||||
|
|
||||||
void Options_Free(void) {
|
void Options_Free(void) {
|
||||||
StringsBuffer_Clear(&Options);
|
StringsBuffer_Clear(&Options);
|
||||||
@ -35,8 +36,8 @@ static cc_bool Options_LoadFilter(const cc_string* entry) {
|
|||||||
void Options_Load(void) {
|
void Options_Load(void) {
|
||||||
/* Increase from max 512 to 2048 per entry */
|
/* Increase from max 512 to 2048 per entry */
|
||||||
StringsBuffer_SetLengthBits(&Options, 11);
|
StringsBuffer_SetLengthBits(&Options, 11);
|
||||||
EntryList_Load(&Options, "options-default.txt", '=', NULL);
|
Options_LoadResult = EntryList_Load(&Options, "options-default.txt", '=', NULL);
|
||||||
EntryList_Load(&Options, "options.txt", '=', NULL);
|
Options_LoadResult = EntryList_Load(&Options, "options.txt", '=', NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Options_Reload(void) {
|
void Options_Reload(void) {
|
||||||
@ -52,7 +53,7 @@ void Options_Reload(void) {
|
|||||||
StringsBuffer_Remove(&Options, i);
|
StringsBuffer_Remove(&Options, i);
|
||||||
}
|
}
|
||||||
/* Load only options which have not changed */
|
/* Load only options which have not changed */
|
||||||
EntryList_Load(&Options, "options.txt", '=', Options_LoadFilter);
|
Options_LoadResult = EntryList_Load(&Options, "options.txt", '=', Options_LoadFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SaveOptions(void) {
|
static void SaveOptions(void) {
|
||||||
|
@ -87,6 +87,7 @@
|
|||||||
|
|
||||||
struct StringsBuffer;
|
struct StringsBuffer;
|
||||||
extern struct StringsBuffer Options;
|
extern struct StringsBuffer Options;
|
||||||
|
extern cc_result Options_LoadResult;
|
||||||
/* Frees any memory allocated in storing options. */
|
/* Frees any memory allocated in storing options. */
|
||||||
void Options_Free(void);
|
void Options_Free(void);
|
||||||
|
|
||||||
|
@ -357,48 +357,68 @@ int File_Exists(const cc_string* path) {
|
|||||||
return attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY);
|
return attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static cc_result Directory_EnumCore(const cc_string* dirPath, const cc_string* file, DWORD attribs,
|
||||||
|
void* obj, Directory_EnumCallback callback) {
|
||||||
|
cc_string path; char pathBuffer[MAX_PATH + 10];
|
||||||
|
/* ignore . and .. entry */
|
||||||
|
if (file->length == 1 && file->buffer[0] == '.') return 0;
|
||||||
|
if (file->length == 2 && file->buffer[0] == '.' && file->buffer[1] == '.') return 0;
|
||||||
|
|
||||||
|
String_InitArray(path, pathBuffer);
|
||||||
|
String_Format2(&path, "%s/%s", dirPath, file);
|
||||||
|
|
||||||
|
if (attribs & FILE_ATTRIBUTE_DIRECTORY) return Directory_Enum(&path, obj, callback);
|
||||||
|
callback(&path, obj);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
|
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
|
||||||
cc_string path; char pathBuffer[MAX_PATH + 10];
|
cc_string path; char pathBuffer[MAX_PATH + 10];
|
||||||
WCHAR str[NATIVE_STR_LEN];
|
WCHAR str[NATIVE_STR_LEN];
|
||||||
WCHAR* src;
|
WIN32_FIND_DATAW eW;
|
||||||
WIN32_FIND_DATAW entry;
|
WIN32_FIND_DATAA eA;
|
||||||
|
int i, ansi = false;
|
||||||
HANDLE find;
|
HANDLE find;
|
||||||
cc_result res;
|
cc_result res;
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Need to append \* to search for files in directory */
|
/* Need to append \* to search for files in directory */
|
||||||
String_InitArray(path, pathBuffer);
|
String_InitArray(path, pathBuffer);
|
||||||
String_Format1(&path, "%s\\*", dirPath);
|
String_Format1(&path, "%s\\*", dirPath);
|
||||||
Platform_EncodeUtf16(str, &path);
|
Platform_EncodeUtf16(str, &path);
|
||||||
|
|
||||||
find = FindFirstFileW(str, &entry);
|
find = FindFirstFileW(str, &eW);
|
||||||
if (find == INVALID_HANDLE_VALUE) return GetLastError();
|
if (!find || find == INVALID_HANDLE_VALUE) {
|
||||||
|
if ((res = GetLastError()) != ERROR_CALL_NOT_IMPLEMENTED) return res;
|
||||||
|
ansi = true;
|
||||||
|
|
||||||
do {
|
/* Windows 9x does not support W API functions */
|
||||||
path.length = 0;
|
Platform_Utf16ToAnsi(str);
|
||||||
String_Format1(&path, "%s/", dirPath);
|
find = FindFirstFileA((LPCSTR)str, &eA);
|
||||||
|
if (find == INVALID_HANDLE_VALUE) return GetLastError();
|
||||||
|
}
|
||||||
|
|
||||||
/* ignore . and .. entry */
|
if (ansi) {
|
||||||
src = entry.cFileName;
|
do {
|
||||||
if (src[0] == '.' && src[1] == '\0') continue;
|
path.length = 0;
|
||||||
if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue;
|
for (i = 0; i < MAX_PATH && eA.cFileName[i]; i++) {
|
||||||
|
String_Append(&path, Convert_CodepointToCP437(eA.cFileName[i]));
|
||||||
for (i = 0; i < MAX_PATH && src[i]; i++) {
|
}
|
||||||
/* TODO: UTF16 to codepoint conversion */
|
if ((res = Directory_EnumCore(dirPath, &path, eA.dwFileAttributes, obj, callback))) return res;
|
||||||
String_Append(&path, Convert_CodepointToCP437(src[i]));
|
} while (FindNextFileA(find, &eA));
|
||||||
}
|
} else {
|
||||||
|
do {
|
||||||
if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
path.length = 0;
|
||||||
res = Directory_Enum(&path, obj, callback);
|
for (i = 0; i < MAX_PATH && eW.cFileName[i]; i++) {
|
||||||
if (res) { FindClose(find); return res; }
|
/* TODO: UTF16 to codepoint conversion */
|
||||||
} else {
|
String_Append(&path, Convert_CodepointToCP437(eW.cFileName[i]));
|
||||||
callback(&path, obj);
|
}
|
||||||
}
|
if ((res = Directory_EnumCore(dirPath, &path, eW.dwFileAttributes, obj, callback))) return res;
|
||||||
} while (FindNextFileW(find, &entry));
|
} while (FindNextFileW(find, &eW));
|
||||||
|
}
|
||||||
|
|
||||||
res = GetLastError(); /* return code from FindNextFile */
|
res = GetLastError(); /* return code from FindNextFile */
|
||||||
FindClose(find);
|
FindClose(find);
|
||||||
return res == ERROR_NO_MORE_FILES ? 0 : GetLastError();
|
return res == ERROR_NO_MORE_FILES ? 0 : res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static cc_result DoFile(cc_file* file, const cc_string* path, DWORD access, DWORD createMode) {
|
static cc_result DoFile(cc_file* file, const cc_string* path, DWORD access, DWORD createMode) {
|
||||||
@ -1565,7 +1585,7 @@ void Platform_Utf16ToAnsi(void* data) {
|
|||||||
WCHAR* src = (WCHAR*)data;
|
WCHAR* src = (WCHAR*)data;
|
||||||
char* dst = (char*)data;
|
char* dst = (char*)data;
|
||||||
|
|
||||||
while (*src) { *dst++ = *src++; }
|
while (*src) { *dst++ = (char)(*src++); }
|
||||||
*dst = '\0';
|
*dst = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
src/Utils.c
11
src/Utils.c
@ -222,7 +222,7 @@ int Convert_FromBase64(const char* src, int len, cc_uint8* dst) {
|
|||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*--------------------------------------------------------EntryList--------------------------------------------------------*
|
*--------------------------------------------------------EntryList--------------------------------------------------------*
|
||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
void EntryList_Load(struct StringsBuffer* list, const char* file, char separator, EntryList_Filter filter) {
|
cc_result EntryList_Load(struct StringsBuffer* list, const char* file, char separator, EntryList_Filter filter) {
|
||||||
cc_string entry; char entryBuffer[1024];
|
cc_string entry; char entryBuffer[1024];
|
||||||
cc_string path;
|
cc_string path;
|
||||||
cc_string key, value;
|
cc_string key, value;
|
||||||
@ -236,8 +236,8 @@ void EntryList_Load(struct StringsBuffer* list, const char* file, char separator
|
|||||||
maxLen = list->_lenMask ? list->_lenMask : STRINGSBUFFER_DEF_LEN_MASK;
|
maxLen = list->_lenMask ? list->_lenMask : STRINGSBUFFER_DEF_LEN_MASK;
|
||||||
|
|
||||||
res = Stream_OpenFile(&stream, &path);
|
res = Stream_OpenFile(&stream, &path);
|
||||||
if (res == ReturnCode_FileNotFound) return;
|
if (res == ReturnCode_FileNotFound) return res;
|
||||||
if (res) { Logger_SysWarn2(res, "opening", &path); return; }
|
if (res) { Logger_SysWarn2(res, "opening", &path); return res; }
|
||||||
|
|
||||||
/* ReadLine reads single byte at a time */
|
/* ReadLine reads single byte at a time */
|
||||||
Stream_ReadonlyBuffered(&buffered, &stream, buffer, sizeof(buffer));
|
Stream_ReadonlyBuffered(&buffered, &stream, buffer, sizeof(buffer));
|
||||||
@ -274,10 +274,11 @@ void EntryList_Load(struct StringsBuffer* list, const char* file, char separator
|
|||||||
|
|
||||||
res = stream.Close(&stream);
|
res = stream.Close(&stream);
|
||||||
if (res) { Logger_SysWarn2(res, "closing", &path); }
|
if (res) { Logger_SysWarn2(res, "closing", &path); }
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntryList_UNSAFE_Load(struct StringsBuffer* list, const char* file) {
|
cc_result EntryList_UNSAFE_Load(struct StringsBuffer* list, const char* file) {
|
||||||
EntryList_Load(list, file, '\0', NULL);
|
return EntryList_Load(list, file, '\0', NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntryList_Save(struct StringsBuffer* list, const char* file) {
|
void EntryList_Save(struct StringsBuffer* list, const char* file) {
|
||||||
|
@ -59,10 +59,10 @@ typedef cc_bool (*EntryList_Filter)(const cc_string* entry);
|
|||||||
/* Loads the entries from disc. */
|
/* Loads the entries from disc. */
|
||||||
/* NOTE: If separator is \0, does NOT check for duplicate keys when loading. */
|
/* NOTE: If separator is \0, does NOT check for duplicate keys when loading. */
|
||||||
/* filter can be used to optionally skip loading some entries from the file. */
|
/* filter can be used to optionally skip loading some entries from the file. */
|
||||||
CC_NOINLINE void EntryList_Load(struct StringsBuffer* list, const char* file, char separator, EntryList_Filter filter);
|
CC_NOINLINE cc_result EntryList_Load(struct StringsBuffer* list, const char* file, char separator, EntryList_Filter filter);
|
||||||
/* Shortcut for EntryList_Load with separator of \0 and filter of NULL */
|
/* Shortcut for EntryList_Load with separator of \0 and filter of NULL */
|
||||||
/* NOTE: Does NOT check for duplicate keys */
|
/* NOTE: Does NOT check for duplicate keys */
|
||||||
CC_NOINLINE void EntryList_UNSAFE_Load(struct StringsBuffer* list, const char* file);
|
CC_NOINLINE cc_result EntryList_UNSAFE_Load(struct StringsBuffer* list, const char* file);
|
||||||
/* Saves the entries in the given list to disc. */
|
/* Saves the entries in the given list to disc. */
|
||||||
CC_NOINLINE void EntryList_Save(struct StringsBuffer* list, const char* file);
|
CC_NOINLINE void EntryList_Save(struct StringsBuffer* list, const char* file);
|
||||||
/* Removes the entry whose key caselessly equals the given key. */
|
/* Removes the entry whose key caselessly equals the given key. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user