-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCFG.C
More file actions
133 lines (122 loc) · 3.36 KB
/
TCFG.C
File metadata and controls
133 lines (122 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*-----------------------------------------------------------------------------
定義ファイル操作パッケージ 簡易版 (c)TORO 2000
-----------------------------------------------------------------------------*/
#define STRICT
#include <windows.h>
#include <string.h>
#include "torowin.h"
#include "tcfg.h"
#define MARGINE 64
//======================================================================== 雑用
/*-------------------------------------
行末(\0,\a,\d)か?
RET: ==0 :EOL !=0 :Code
--------------------------------------*/
BYTE USEFASTCALL IsEOLA(const char **str)
{
BYTE code;
code = **str;
if ( code == '\0' ) return '\0';
if ( code == 0xd ){
if ( *(*str + 1) == 0x0a ) (*str)++;
return '\0';
}
if ( code == 0xa ){
if ( *(*str + 1) == 0x0d ) (*str)++;
return '\0';
}
return code;
}
#define IsEOL IsEOLW
/*-------------------------------------
空白(space,tab)をスキップする
RET: ==0 :EOL !=0 :Code
--------------------------------------*/
BYTE USEFASTCALL SkipSpaceA(const char **str)
{
BYTE code;
for ( ;; ){
code = IsEOLA(str);
if ( (code != ' ') && (code != '\t') ) break;
(*str)++;
}
return code;
}
//------------------------------------- 行末空白を除去(b:先頭,p:末尾)
void USEFASTCALL SkipSpaceB(char *b, char *p)
{
while( b < p ){
if ( (*p == ' ') || (*p == '\t') ){
p--;
continue;
}
break;
}
*(p + 1) = '\0';
}
//============================================================ 定義ファイル操作
//------------------------------------- 定義ファイルを読み込む
BOOL CFGOpen(CFG *cfg,WCHAR *filename)
{
HANDLE hFile;
cfg->bottom = NULL;
cfg->size = 0;
cfg->write = 0;
// tstrcpy(cfg->filename,filename);
hFile = CreateFileW(filename, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if ( hFile == INVALID_HANDLE_VALUE ) return FALSE;
cfg->size = GetFileSize(hFile, NULL);
cfg->bottom = (char *)HeapAlloc(GetProcessHeap(), 0, cfg->size + MARGINE);
ReadFile(hFile, cfg->bottom, cfg->size, &cfg->size, NULL);
CloseHandle(hFile);
memset(cfg->bottom + cfg->size, 0, MARGINE);
return TRUE;
}
//------------------------------------- 定義ファイルを閉じる
BOOL CFGClose(CFG *cfg)
{
HeapFree(GetProcessHeap(), 0 , cfg->bottom);
return TRUE;
}
/*-------------------------------------
指定の offset から、一行分を読み込み
string に書き込む。
offset には次の行の先頭を書く
-------------------------------------*/
BOOL USEFASTCALL CFGGetLine(CFG *cfg, DWORD *offset, char *string, int maxlen)
{
char *bottom, *ptr, *end = NULL, *maxptr = string + maxlen - 1;
bottom = cfg->bottom + *offset;
if ( *bottom == '\0' ) return FALSE;
// 先頭の空白を削除 ---------------
SkipSpaceA(&bottom);
ptr = bottom;
// 行末&コメントの検索 ------------
for ( ; *ptr != '\0' ; ptr++ ){
#if 0
if ( (*ptr == ';') && (end == NULL) ){ // コメント
end = ptr;
continue;
}
#endif
if ( (*ptr == '\r') || (*ptr == '\n') ){ // CR/LF
if ( end == NULL ) end = ptr;
ptr++;
while ( (*ptr == '\r') || (*ptr == '\n') ) ptr++;
break;
}
}
*offset = (DWORD)(ptr - cfg->bottom);
if ( end == NULL ) end = ptr;
// 行末の空白を削除 ---------------
while ( (end > bottom) && (*(end - 1) == ' ') ) end--;
// 複写 & TAB 除去 ----------------
for ( ; bottom < end ; bottom++ ){
if ( *bottom == '\t' ) continue;
*string++ = *bottom;
if ( string >= maxptr ) break;
}
*string = '\0';
return TRUE;
}