abf4dd2c37c5f79821fb075d7068e0548182e598
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 /* By default set value as DefaultChar */
100 header
->TransDefaultChar
= (uint16_t)val
;
102 /* Skip all spaces after default char */
103 while (isspace(*p
)) ++p
;
105 /* Convert string to uint32_t */
106 val
= strtoul(p
, &p
, 16);
107 header
->UniDefaultChar
= (uint16_t)val
;
108 /* By default set value as UniDefaultChar */
109 header
->TransUniDefaultChar
= (uint16_t)val
;
118 printf("CPINFO not found.\n");
122 header
->HeaderSize
= sizeof(NLS_FILE_HEADER
) / sizeof(uint16_t);
133 txt_get_mb_table(const char *file_path
, uint16_t uni_default_char
)
145 table
= malloc(256 * sizeof(uint16_t));
148 printf("Memory allocation failure\n");
152 /* Set default value for all table items */
153 for (index
= 0; index
<= 255; index
++)
154 table
[index
] = uni_default_char
;
156 file
= fopen(file_path
, "r");
159 printf("Unable to read TXT file.\n");
163 /* Find MBTABLE entry */
165 while (fgets(buf
, sizeof(buf
), file
))
169 p
= strstr(buf
, "MBTABLE");
175 while (isspace(*p
)) ++p
;
177 count
= strtoul(p
, &p
, 10);
178 if (count
== 0 || count
> 256)
180 printf("Wrong MBTABLE size: %u (line: %u)\n", count
, line
);
191 printf("MBTABLE not found.\n");
195 /* Parse next line */
196 while (fgets(buf
, sizeof(buf
), file
) && count
)
206 while (isspace(*p
)) ++p
;
208 if (!*p
|| p
[0] == ';')
211 cp_char
= strtoul(p
, &p
, 16);
214 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
219 while (isspace(*p
)) ++p
;
221 uni_char
= strtoul(p
, &p
, 16);
222 if (uni_char
> 0xFFFF)
224 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
228 table
[cp_char
] = uni_char
;
247 txt_get_wc_table(const char *file_path
, uint16_t default_char
, int is_dbcs
)
259 table
= malloc(65536 * (is_dbcs
? sizeof(uint16_t) : sizeof(uint8_t)));
262 printf("Memory allocation failure\n");
266 /* Set default value for all table items */
267 for (index
= 0; index
<= 65535; index
++)
272 uint16_t *tmp
= (uint16_t*)table
;
273 tmp
[index
] = default_char
;
278 uint8_t *tmp
= (uint8_t*)table
;
279 tmp
[index
] = default_char
;
283 file
= fopen(file_path
, "r");
286 printf("Unable to read TXT file.\n");
290 /* Find WCTABLE entry */
292 while (fgets(buf
, sizeof(buf
), file
))
296 p
= strstr(buf
, "WCTABLE");
302 while (isspace(*p
)) ++p
;
304 count
= strtoul(p
, &p
, 10);
305 if (count
== 0 || count
> 65536)
307 printf("Wrong WCTABLE size: %u (line: %u)\n", count
, line
);
318 printf("WCTABLE not found.\n");
322 /* Parse next line */
323 while (fgets(buf
, sizeof(buf
), file
) && count
)
333 while (isspace(*p
)) ++p
;
335 if (!*p
|| p
[0] == ';')
338 uni_char
= strtoul(p
, &p
, 16);
339 if (uni_char
> 0xFFFF)
341 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
346 while (isspace(*p
)) ++p
;
348 cp_char
= strtoul(p
, &p
, 16);
349 if ((is_dbcs
&& cp_char
> 0xFFFF) || (!is_dbcs
&& cp_char
> 0xFF))
351 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
358 uint16_t *tmp
= (uint16_t*)table
;
359 tmp
[uni_char
] = cp_char
;
364 uint8_t *tmp
= (uint8_t*)table
;
365 tmp
[uni_char
] = cp_char
;
386 txt_get_glyph_table(const char *file_path
, uint16_t uni_default_char
)
398 table
= malloc(256 * sizeof(uint16_t));
401 printf("Memory allocation failure\n");
405 /* Set default value for all table items */
406 for (index
= 0; index
<= 255; index
++)
407 table
[index
] = uni_default_char
;
409 file
= fopen(file_path
, "r");
412 printf("Unable to read TXT file.\n");
416 /* Find GLYPHTABLE entry */
418 while (fgets(buf
, sizeof(buf
), file
))
422 p
= strstr(buf
, "GLYPHTABLE");
428 while (isspace(*p
)) ++p
;
430 count
= strtoul(p
, &p
, 10);
431 if (count
== 0 || count
> 256)
433 printf("Wrong GLYPHTABLE size: %u (line: %u)\n", count
, line
);
444 printf("GLYPHTABLE not found.\n");
448 /* Parse next line */
449 while (fgets(buf
, sizeof(buf
), file
) && count
)
459 while (isspace(*p
)) ++p
;
461 if (!*p
|| p
[0] == ';')
464 cp_char
= strtoul(p
, &p
, 16);
467 printf("Wrong char value: %u (line: %u)\n", cp_char
, line
);
472 while (isspace(*p
)) ++p
;
474 uni_char
= strtoul(p
, &p
, 16);
475 if (uni_char
> 0xFFFF)
477 printf("Wrong unicode char value: %u (line: %u)\n", uni_char
, line
);
481 table
[cp_char
] = uni_char
;