2 * PROJECT: ReactOS TXT to NLS Converter
3 * LICENSE: GNU General Public License Version 2.0 or any later version
4 * FILE: devutils/txt2nls/txt.c
5 * COPYRIGHT: Copyright 2016 Dmitry Chapyshev <dmitry@reactos.org>
11 txt_get_header(const char *file_path
, NLS_FILE_HEADER
*header
)
21 file
= fopen(file_path
, "r");
24 printf("Unable to read TXT file.\n");
28 /* Find CODEPAGE entry */
30 while (fgets(buf
, sizeof(buf
), file
))
34 p
= strstr(buf
, "CODEPAGE");
37 /* Length of CODEPAGE string is 8 chars */
40 /* Skip all spaces after CODEPAGE */
41 while (isspace(*p
)) ++p
;
43 /* Convert string to uint32_t */
44 val
= strtoul(p
, &p
, 10);
46 /* Validate codepage value */
49 printf("Wrong codepage: %u (line: %u)\n", val
, line
);
53 header
->CodePage
= (uint16_t)val
;
62 printf("CODEPAGE not found.\n");
66 /* Find CPINFO entry */
68 while (fgets(buf
, sizeof(buf
), file
))
72 p
= strstr(buf
, "CPINFO");
75 /* Length of CPINFO string is 6 chars */
78 /* Skip all spaces after CPINFO */
79 while (isspace(*p
)) ++p
;
81 /* Convert string to uint32_t */
82 val
= strtoul(p
, &p
, 10);
85 if (val
!= 1 && val
!= 2)
87 printf("Wrong character size: %u (line: %u)\n", val
, line
);
91 header
->MaximumCharacterSize
= (uint16_t)val
;
93 /* Skip all spaces after character size */
94 while (isspace(*p
)) ++p
;
96 /* Convert string to uint32_t */
97 val
= strtoul(p
, &p
, 16);
98 header
->DefaultChar
= (uint16_t)val
;
99 header
->TransDefaultChar
= (uint16_t)val
;
101 /* Skip all spaces after default char */
102 while (isspace(*p
)) ++p
;
104 /* Convert string to uint32_t */
105 val
= strtoul(p
, &p
, 16);
106 header
->UniDefaultChar
= (uint16_t)val
;
107 header
->TransUniDefaultChar
= (uint16_t)val
;
116 printf("CPINFO not found.\n");
120 header
->HeaderSize
= sizeof(NLS_FILE_HEADER
) / sizeof(uint16_t);
131 txt_get_mb_table(const char *file_path
, uint16_t uni_default_char
)
143 table
= malloc(0xFF * sizeof(uint16_t));
146 printf("Memory allocation failure\n");
150 /* Set default value for all table items */
151 for (index
= 0; index
<= 0xFF; index
++)
152 table
[index
] = uni_default_char
;
154 file
= fopen(file_path
, "r");
157 printf("Unable to read TXT file.\n");
161 /* Find MBTABLE entry */
163 while (fgets(buf
, sizeof(buf
), file
))
167 p
= strstr(buf
, "MBTABLE");
173 while (isspace(*p
)) ++p
;
175 count
= strtoul(p
, &p
, 10);
176 if (count
== 0 || count
> 256)
178 printf("Wrong MBTABLE size: %u (line: %u)\n", count
, line
);
189 printf("MBTABLE not found.\n");
193 /* Parse next line */
194 while (fgets(buf
, sizeof(buf
), file
) && count
)
204 while (isspace(*p
)) ++p
;
206 if (!*p
|| p
[0] == ';')
209 cp_char
= strtoul(p
, &p
, 16);
212 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
217 while (isspace(*p
)) ++p
;
219 uni_char
= strtoul(p
, &p
, 16);
220 if (uni_char
> 0xFFFF)
222 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
226 table
[cp_char
] = uni_char
;
245 txt_get_wc_table(const char *file_path
, uint16_t default_char
, int is_dbcs
)
257 table
= malloc(0xFFFF * (is_dbcs
? sizeof(uint16_t) : sizeof(uint8_t)));
260 printf("Memory allocation failure\n");
264 /* Set default value for all table items */
265 for (index
= 0; index
<= 0xFFFF; index
++)
270 uint16_t *tmp
= (uint16_t*)table
;
271 tmp
[index
] = default_char
;
276 uint8_t *tmp
= (uint8_t*)table
;
277 tmp
[index
] = default_char
;
281 file
= fopen(file_path
, "r");
284 printf("Unable to read TXT file.\n");
288 /* Find WCTABLE entry */
290 while (fgets(buf
, sizeof(buf
), file
))
294 p
= strstr(buf
, "WCTABLE");
300 while (isspace(*p
)) ++p
;
302 count
= strtoul(p
, &p
, 10);
303 if (count
== 0 || count
> 65536)
305 printf("Wrong WCTABLE size: %u (line: %u)\n", count
, line
);
316 printf("WCTABLE not found.\n");
320 /* Parse next line */
321 while (fgets(buf
, sizeof(buf
), file
) && count
)
331 while (isspace(*p
)) ++p
;
333 if (!*p
|| p
[0] == ';')
336 uni_char
= strtoul(p
, &p
, 16);
337 if (uni_char
> 0xFFFF)
339 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
344 while (isspace(*p
)) ++p
;
346 cp_char
= strtoul(p
, &p
, 16);
347 if ((is_dbcs
&& cp_char
> 0xFFFF) || (!is_dbcs
&& cp_char
> 0xFF))
349 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
356 uint16_t *tmp
= (uint16_t*)table
;
357 tmp
[uni_char
] = cp_char
;
362 uint8_t *tmp
= (uint8_t*)table
;
363 tmp
[uni_char
] = cp_char
;
384 txt_get_glyph_table(const char *file_path
, uint16_t uni_default_char
)
396 table
= malloc(0xFF * sizeof(uint16_t));
399 printf("Memory allocation failure\n");
403 /* Set default value for all table items */
404 for (index
= 0; index
<= 0xFF; index
++)
405 table
[index
] = uni_default_char
;
407 file
= fopen(file_path
, "r");
410 printf("Unable to read TXT file.\n");
414 /* Find GLYPHTABLE entry */
416 while (fgets(buf
, sizeof(buf
), file
))
420 p
= strstr(buf
, "GLYPHTABLE");
426 while (isspace(*p
)) ++p
;
428 count
= strtoul(p
, &p
, 10);
429 if (count
== 0 || count
> 256)
431 printf("Wrong GLYPHTABLE size: %u (line: %u)\n", count
, line
);
442 printf("GLYPHTABLE not found.\n");
446 /* Parse next line */
447 while (fgets(buf
, sizeof(buf
), file
) && count
)
457 while (isspace(*p
)) ++p
;
459 if (!*p
|| p
[0] == ';')
462 cp_char
= strtoul(p
, &p
, 16);
465 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
470 while (isspace(*p
)) ++p
;
472 uni_char
= strtoul(p
, &p
, 16);
473 if (uni_char
> 0xFFFF)
475 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
479 table
[cp_char
] = uni_char
;