|
| 1 | +/*----------------------------------------------------------------------------- |
| 2 | + 定義ファイル操作パッケージ 簡易版 (c)TORO 2017 |
| 3 | +-----------------------------------------------------------------------------*/ |
| 4 | +#define STRICT |
| 5 | +#include <windows.h> |
| 6 | +#include <string.h> |
| 7 | +#include "TOROWIN.H" |
| 8 | +#include "TCFG.H" |
| 9 | + |
| 10 | +#define MARGINE 64 |
| 11 | +//======================================================================== 雑用 |
| 12 | +/*------------------------------------- |
| 13 | + 行末(\0,\a,\d)か? |
| 14 | +
|
| 15 | +RET: ==0 :EOL !=0 :Code |
| 16 | +--------------------------------------*/ |
| 17 | +#ifndef UNICODE |
| 18 | +BYTE USEFASTCALL IsEOLA(const char **str) |
| 19 | +{ |
| 20 | + BYTE code; |
| 21 | + |
| 22 | + code = **str; |
| 23 | + if ( code == '\0' ) return '\0'; |
| 24 | + if ( code == 0xd ){ |
| 25 | + if ( *(*str + 1) == 0x0a ) (*str)++; |
| 26 | + return '\0'; |
| 27 | + } |
| 28 | + if ( code == 0xa ){ |
| 29 | + if ( *(*str + 1) == 0x0d ) (*str)++; |
| 30 | + return '\0'; |
| 31 | + } |
| 32 | + return code; |
| 33 | +} |
| 34 | +#endif |
| 35 | +WCHAR USEFASTCALL IsEOLW(const WCHAR **str) |
| 36 | +{ |
| 37 | + WCHAR code; |
| 38 | + |
| 39 | + code = **str; |
| 40 | + if ( code == '\0' ) return '\0'; |
| 41 | + if ( code == 0xd ){ |
| 42 | + if ( *(*str + 1) == 0x0a ) (*str)++; |
| 43 | + return '\0'; |
| 44 | + } |
| 45 | + if ( code == 0xa ){ |
| 46 | + if ( *(*str + 1) == 0x0d ) (*str)++; |
| 47 | + return '\0'; |
| 48 | + } |
| 49 | + return code; |
| 50 | +} |
| 51 | + |
| 52 | +/*------------------------------------- |
| 53 | + 空白(space,tab)をスキップする |
| 54 | +
|
| 55 | +RET: ==0 :EOL !=0 :Code |
| 56 | +--------------------------------------*/ |
| 57 | +#ifndef UNICODE |
| 58 | +BYTE USEFASTCALL SkipSpaceA(const char **str) |
| 59 | +{ |
| 60 | + BYTE code; |
| 61 | + |
| 62 | + for ( ;; ){ |
| 63 | + code = IsEOLA(str); |
| 64 | + if ( (code != ' ') && (code != '\t') ) break; |
| 65 | + (*str)++; |
| 66 | + } |
| 67 | + return code; |
| 68 | +} |
| 69 | +#endif |
| 70 | + |
| 71 | +WCHAR USEFASTCALL SkipSpaceW(const WCHAR **str) |
| 72 | +{ |
| 73 | + WCHAR code; |
| 74 | + |
| 75 | + for ( ;; ){ |
| 76 | + code = IsEOLW(str); |
| 77 | + if ( (code != ' ') && (code != '\t') ) break; |
| 78 | + (*str)++; |
| 79 | + } |
| 80 | + return code; |
| 81 | +} |
| 82 | + |
| 83 | +//------------------------------------- 行末空白を除去(b:先頭,p:末尾) |
| 84 | +void USEFASTCALL SkipSpaceB(const WCHAR *b, WCHAR *p) |
| 85 | +{ |
| 86 | + while ( b < p ){ |
| 87 | + if ( (*p == ' ') || (*p == '\t') ){ |
| 88 | + p--; |
| 89 | + continue; |
| 90 | + } |
| 91 | + break; |
| 92 | + } |
| 93 | + *(p + 1) = '\0'; |
| 94 | +} |
| 95 | +/*----------------------------------------------------------------------------- |
| 96 | + 一つ分のパラメータを抽出する。先頭と末尾の空白は除去する |
| 97 | +
|
| 98 | +RET: 先頭の文字(何もなかったら 0) |
| 99 | +-----------------------------------------------------------------------------*/ |
| 100 | +WCHAR GetLineParam(const WCHAR **str, WCHAR *param) |
| 101 | +{ |
| 102 | + WCHAR code, bottom; |
| 103 | + const WCHAR *src; |
| 104 | + WCHAR *dst; |
| 105 | + |
| 106 | + src = *str; |
| 107 | + dst = param; |
| 108 | + bottom = SkipSpaceW(&src); |
| 109 | + if ( bottom == '\0' ){ |
| 110 | + *str = src; |
| 111 | + return '\0'; |
| 112 | + } |
| 113 | + src++; |
| 114 | + if ( bottom == '\"' ){ |
| 115 | + bottom = *src; |
| 116 | + while ( '\0' != (code = IsEOLW(&src)) ){ |
| 117 | + src++; |
| 118 | + if ( (code == '\"') && (*src <= ' ') ) break; |
| 119 | + *dst++ = code; |
| 120 | + } |
| 121 | + } else{ |
| 122 | + *dst++ = bottom; |
| 123 | + while ( '\0' != (code = IsEOLW(&src)) ){ |
| 124 | + src++; |
| 125 | + if ( code <= ' ' ) break; |
| 126 | + *dst++ = code; |
| 127 | + } |
| 128 | + } |
| 129 | + *dst = '\0'; |
| 130 | + *str = src; |
| 131 | + return bottom; |
| 132 | +} |
| 133 | +//============================================================ 定義ファイル操作 |
| 134 | +//------------------------------------- UTF-8 定義ファイルを読み込む |
| 135 | +BOOL CFGOpen(CFG *cfg, WCHAR *filename) |
| 136 | +{ |
| 137 | + HANDLE hFile; |
| 138 | + DWORD filesize, fileoffset, cfglen; |
| 139 | + char *fileimage; |
| 140 | + |
| 141 | + cfg->bottom = NULL; |
| 142 | +// cfg->size = 0; |
| 143 | +// cfg->write = 0; |
| 144 | +// tstrcpy(cfg->filename,filename); |
| 145 | + |
| 146 | + hFile = CreateFileW(filename, GENERIC_READ, |
| 147 | + FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 148 | + if ( hFile == INVALID_HANDLE_VALUE ) return FALSE; |
| 149 | + |
| 150 | + filesize = GetFileSize(hFile, NULL); |
| 151 | + fileimage = (char *)HeapAlloc(GetProcessHeap(), 0, filesize + MARGINE); |
| 152 | + if ( fileimage == NULL ){ |
| 153 | + CloseHandle(hFile); |
| 154 | + return FALSE; |
| 155 | + } |
| 156 | + ReadFile(hFile, fileimage, filesize, &filesize, NULL); |
| 157 | + CloseHandle(hFile); |
| 158 | + |
| 159 | + fileoffset = memcmp(fileimage, UTF8HEADER, UTF8HEADERSIZE) ? 0 : UTF8HEADERSIZE; |
| 160 | + cfglen = MultiByteToWideChar(CP_UTF8, 0, fileimage + fileoffset, filesize - fileoffset, NULL, 0); |
| 161 | + cfg->bottom = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (cfglen * sizeof(WCHAR)) + MARGINE); |
| 162 | + if ( cfg->bottom == NULL ){ |
| 163 | + HeapFree(GetProcessHeap(), 0, fileimage); |
| 164 | + // cfg->size = 0; |
| 165 | + return FALSE; |
| 166 | + } |
| 167 | + memset((char *)(WCHAR *)(cfg->bottom + cfglen), 0, MARGINE); |
| 168 | + MultiByteToWideChar(CP_UTF8, 0, fileimage + fileoffset, filesize - fileoffset, cfg->bottom, cfglen); |
| 169 | + HeapFree(GetProcessHeap(), 0, fileimage); |
| 170 | + return TRUE; |
| 171 | +} |
| 172 | +//------------------------------------- 定義ファイルを閉じる |
| 173 | +BOOL CFGClose(CFG *cfg) |
| 174 | +{ |
| 175 | + if ( cfg->bottom != NULL ) HeapFree(GetProcessHeap(), 0, cfg->bottom); |
| 176 | + cfg->bottom = NULL; |
| 177 | +// cfg->size = 0; |
| 178 | + return TRUE; |
| 179 | +} |
| 180 | +/*------------------------------------- |
| 181 | + 指定の offset から、一行分を読み込み |
| 182 | + string に書き込む。 |
| 183 | + offset には次の行の先頭を書く |
| 184 | +-------------------------------------*/ |
| 185 | +BOOL USEFASTCALL CFGGetLine(CFG *cfg, DWORD *offset, WCHAR *string, int maxlen) |
| 186 | +{ |
| 187 | + WCHAR *bottom, *p, *end = NULL, *maxptr = string + maxlen - 1; |
| 188 | + |
| 189 | + bottom = cfg->bottom + *offset; |
| 190 | + if ( *bottom == '\0' ) return FALSE; |
| 191 | + // 先頭の空白を削除 --------------- |
| 192 | + SkipSpaceW((const WCHAR **)&bottom); |
| 193 | + p = bottom; |
| 194 | + // 行末&コメントの検索 ------------ |
| 195 | + for ( ; *p; p++ ){ |
| 196 | + #if 0 |
| 197 | + if ( (*p == ';') && (end == NULL) ){ // コメント |
| 198 | + end = p; |
| 199 | + continue; |
| 200 | + } |
| 201 | + #endif |
| 202 | + if ( (*p == '\r') || (*p == '\n') ){ // CR/LF |
| 203 | + if ( end == NULL ) end = p; |
| 204 | + p++; |
| 205 | + while ( (*p == '\r') || (*p == '\n') ) p++; |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
| 209 | + *offset = (DWORD)(p - cfg->bottom); |
| 210 | + if ( end == NULL ) end = p; |
| 211 | + // 行末の空白を削除 --------------- |
| 212 | + while ( (end > bottom) && (*(end - 1) == ' ') ) end--; |
| 213 | + // 複写 & TAB 除去 ---------------- |
| 214 | + for ( ; bottom < end; bottom++ ){ |
| 215 | + if ( *bottom == '\t' ) continue; |
| 216 | + *string++ = *bottom; |
| 217 | + if ( string >= maxptr ) break; |
| 218 | + } |
| 219 | + *string = '\0'; |
| 220 | + return TRUE; |
| 221 | +} |
| 222 | +//------------------------------------- パラメータを抽出する |
| 223 | +WCHAR *CFGFixParam(WCHAR *string, WCHAR **next) |
| 224 | +{ |
| 225 | + WCHAR *p, *q; |
| 226 | + |
| 227 | + SkipSpaceW((const WCHAR **)&string); |
| 228 | + p = string; |
| 229 | + while ( *p && (*p != ',') ) p++; |
| 230 | + q = p; |
| 231 | + if ( *p == ',' ) p++; |
| 232 | + while ( (q > string) && (*(q - 1) == ' ') ) q--; |
| 233 | + *q = '\0'; |
| 234 | + |
| 235 | + if ( next != '\0' ) *next = p; |
| 236 | + return string; |
| 237 | +} |
0 commit comments