代码页详细知识:
https://zh.wikipedia.org/wiki/%E4%BB%A3%E7%A0%81%E9%A1%B5
https://zh.wikipedia.org/wiki/%E5%AD%97%E7%AC%A6%E7%BC%96%E7%A0%81
int fgetline(FILE *fp, char *ch, int size = 1024)
{
int i;
for (i = 0; !feof(fp) && i < size; i++)
{
fread(ch + i, 1, 1, fp);
if (ch[i] == '\n')
{
ch[i - 1] = '\0';
return i + 1;
}
}
ch[i - 1] = '\0';
return i - 1;
}
int unicode_fgetline(FILE *fp, wchar_t *wch, int size = 1024)
{
int i;
for (i = 0; !feof(fp) && i < size; i++)
{
fread((char *)(wch + i), 2, 1, fp);
if (wch[i] == '\n')
{
wch[i - 1] = 0;
return i + 1;
}
}
wch[i - 1] = 0;
return i - 1;
}
int ReadConfFile(Setup &S,Item *pItem,int is_unicode)
{
FILE * fp = fopen(ConfFileName, "rb");
if (fp == NULL)
{
printf("同目录下无%s\n", ConfFileName);
return -1;
}
if (is_unicode)//如果配置文件是Unicode 格式
fseek(fp, 2, SEEK_SET);// 跳过unicode文本开头有两个字节0xFFFE(称作BOM,用于标识unicode编码)
wchar_t wch[1024];
char ch[2048];
int i;
char cht;
while (!feof(fp))
{
if (!is_unicode)
fgetline(fp, ch, 512);
else
{
unicode_fgetline(fp, wch, 1024);
WideCharToMultiByte(54936, 0, wch, -1, ch, 2048, NULL, NULL);//将Unicode 转为GB18030
}
if (strncmp(ch, "行数", 4) == 0)
S.row = atoi(ch + 5);
else if (strncmp(ch, "列数", 4) == 0)
S.col = atoi(ch + 5);
else if (strncmp(ch, "背景色", 6) == 0)
S.BgColor = atoi(ch + 7);
else if (strncmp(ch, "前景色", 6) == 0)
S.FgColor = atoi(ch + 7);
else if (strncmp(ch, "屏延时", 6) == 0)
S.ScreenSleep = atof(ch + 7);
else if (strncmp(ch, "条延时", 6) == 0)
S.ItemSleep = atof(ch + 7);
else if (strncmp(ch, "特效", 4) == 0)
{
i = atoi(ch + 4);
cht = ch[strlen(ch) - 1];
if (cht == 'y' || cht == 'Y')
S.SpecEffect[i] = true;
else
S.SpecEffect[i] = false;
}
else if (strncmp(ch, "item", 4) == 0)
{
i = atoi(ch + 4);
char *p = strstr(ch, "=");
if (strncmp(ch + 4+i/10+1, "_color",6)==0)
if (p[1] == 'x'||p[1]=='X')
pItem[i].color = 16;
else
pItem[i].color = atoi(p + 1);
else
{
pItem[i].content = new char[strlen(p + 1) + 1];
strcpy(pItem[i].content, p + 1);
}
}
}
fclose(fp);//关闭文件
return 0;
}