Conversation
|
It looks good, except it's a bit of a pain having to redefine all the string handling functions to have cfgpath-specific variants. That will likely make it harder to integrate in with other code, not knowing which function to call. It's been a while since I've used these functions on Windows, but don't they automatically get replaced with wide variants if you are compiling in Unicode mode? Taking advantage of something like that would be ideal so that |
|
Even when compiling in Unicode mode, the C standard library functions The good news is, old code using plain The bad news is having to introduce an additional check in projects that are going to support Unicode Windows. Even if Consider this custom FILE *cfgpath__fopen(cfgpathchar_t *fn, cfgpathchar_t *mode) {
#if defined(WIN32) && defined(UNICODE)
return _wfopen(fn, mode);
#else
return fopen(fn, mode);
#endif
}
void test(void) {
FILE *fp;
cfgpathchar_t buffer[MAX_PATH];
// write config file test
get_user_config_file(buffer, sizeof(buffer)/sizeof(buffer[0]), CFGPATHTEXT("myapp"));
if (buffer[0] == 0) {
printf("get_user_config_file() fail\n");
return;
}
fp = cfgpath__fopen(buffer, CFGPATHTEXT("wb"));
if(fp==NULL) {
cfgpath__printf(CFGPATHTEXT("fopen(%s) fail\n"), buffer);
return;
}
fclose(fp);
cfgpath__printf(CFGPATHTEXT("wrote '%s'!\n"), buffer);
// write test file in config folder
get_user_config_folder(buffer, sizeof(buffer)/sizeof(buffer[0]), CFGPATHTEXT("myapp"));
if (buffer[0] == 0) {
printf("get_user_config_folder() fail\n");
return;
}
cfgpath__strcat(buffer, CFGPATHTEXT("test.txt"));
fp = cfgpath__fopen(buffer, CFGPATHTEXT("wb"));
if(fp==NULL) {
cfgpath__printf(CFGPATHTEXT("fopen(%s) fail\n"), buffer);
return;
}
fclose(fp);
cfgpath__printf(CFGPATHTEXT("wrote '%s'!\n"), buffer);
}Tested both with and without |
|
Nuk510:master |
This update introduces a type
cfgpathchar_twhich will be treated aswchar_tand usewchar_tstring functions if the constantUNICODEis defined when building for Windows;charand the C standard library will be used otherwise.The macro
CFGPATHTEXTis also introduced so string constants will be prefixed withLwhen the constantUNICODEis defined when building for Windows.Additionally, the usage examples display
sizeof(buf)/sizeof(buf[0])being passed in for themaxlenarguments becausecfgpathchar_tis not guaranteed to have a size of 1 (it will be at least 2 if building withUNICODEdefined on Windows).Now if a path contains more than just standard ASCII, Windows will still return a usable path. Consider the configuration file path...
C:\Documents and Settings\Name\Application Data\myapp.ini... where
Namecontains characters such asò,ü, etc. As long as the user has#define UNICODEbefore#include "cfgpath.h", the propagatedcfgpathchar_tcan be used with_wfopen.I have tested on Windows (both with and without
UNICODE) and Linux to confirm everything is still working.