私は INI ファイル パーサーを作成しました。Windows ではうまく機能しますが、Linux ではうまく機能しません。問題は memcmp 関数が原因です。必要なときに 0 が返されません。すでに printfと strlen でチェックしています (strncmp も使用しようとしました)。代わりに、別の値を返しましたが、それでも 0 とは異なります) が、問題の原因を見つけることができませんでした。
コードは次のとおりです。
#define INI_KEY_LENGTH 128
#define BEGIN '['
#define SEP '='
#define COMMENT ';'
static char configfilename[255];
static char * str_dup (const char * str){
char * dup = NULL;
if (str != NULL){
size_t size = strlen (str) + 1;
dup = malloc (size);
if (dup != NULL){
memcpy (dup, str, size);
}
}
return dup;
}
static void str_finalize (char * str){
char * p = strchr (str, '\n');
if (p != NULL){
*p = '\0';
}
}
static char * get (const char * filename, const char * section, const char * key){
char * ret = NULL;
char buff [INI_KEY_LENGTH];
FILE * file = NULL;
int opened = 0;
file = fopen (filename, "r");
if (file != NULL){
while ((fgets (buff, INI_KEY_LENGTH, file)) != NULL){
str_finalize (buff);
if (! opened && buff [0] == BEGIN){
char * p = buff;
// Don't work here
if (memcmp (p + 1, section, strlen (buff) - 2) == 0){
opened = 1;
continue;
}
}
else if (opened){
if (buff [0] == BEGIN){
opened = 0;
break;
}
if(buff [0] != COMMENT){
if (strstr (buff, key) != NULL){
char * p = strchr (buff, SEP);
if (p != NULL){
p++;
ret = str_dup (p);
break;
}
}
}
}
}
fclose (file);
}
return ret;
}
int init_iniFile (const char * filename){
int ret = 0;
FILE * file;
if ((file = fopen(filename, "r")) != NULL){
fclose(file);
strcpy(configfilename, filename);
ret = 1;
}
return ret;
}
char * get_string (const char * section, const char * key){
return get (configfilename, section, key);
}
エラーがばかげていると思いますが、私は C 初心者なので、ご迷惑をおかけして申し訳ありません。