Mercurial > hg > sidinfo
annotate sidutil.c @ 356:811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 27 Jan 2020 02:32:59 +0200 |
parents | 9924d4518932 |
children | 199284ba94ea |
rev | line source |
---|---|
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * SIDLib common utility functions |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * (C) Copyright 2014-2020 Tecnic Software productions (TNSP) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 */ |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 #include "sidutil.h" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "th_file.h" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_string.h" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_datastruct.h" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 |
316
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
12 char * sidutil_escape_string(const char *str, const char *escchars) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
13 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
14 size_t len, size; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
15 char *buf; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
16 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
17 if (str == NULL) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
18 return NULL; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
19 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
20 if (escchars == NULL) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
21 return th_strdup(str); |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
22 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
23 size = strlen(str) + 1; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
24 if ((buf = th_malloc(size)) == NULL) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
25 return NULL; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
26 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
27 for (len = 0; *str; str++) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
28 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
29 if (strchr(escchars, *str) != NULL || *str == '\\') |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
30 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
31 if (!th_strbuf_putch(&buf, &size, &len, '\\')) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
32 goto err; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
33 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
34 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
35 if (!th_strbuf_putch(&buf, &size, &len, *str)) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
36 goto err; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
37 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
38 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
39 if (!th_strbuf_putch(&buf, &size, &len, 0)) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
40 goto err; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
41 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
42 return buf; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
43 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
44 err: |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
45 th_free(buf); |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
46 return NULL; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
47 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
48 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
49 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
50 void sidutil_print_string_escaped(FILE *outFile, const char *str) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
51 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
52 while (*str) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
53 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
54 if (*str == '\\') |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
55 switch (*(++str)) |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
56 { |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
57 case 'n': fputc('\n', outFile); break; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
58 case 'r': fputc('\r', outFile); break; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
59 case 't': fputc('\r', outFile); break; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
60 case '\\': fputc('\\', outFile); break; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
61 default: fputc(*str, outFile); break; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
62 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
63 else |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
64 fputc(*str, outFile); |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
65 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
66 str++; |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
67 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
68 } |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
69 |
b0c844b39516
Move and rename siEscapeString() to sidutil_escape_string() and
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
70 |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 void sidutil_print_license(void) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 printf("%s - %s\n%s\n", th_prog_name, th_prog_desc, th_prog_author); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 printf( |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 "\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 "Redistribution and use in source and binary forms, with or without\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 "modification, are permitted provided that the following conditions\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 "are met:\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 "\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 " 1. Redistributions of source code must retain the above copyright\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 " notice, this list of conditions and the following disclaimer.\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 "\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 " 2. Redistributions in binary form must reproduce the above copyright\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 " notice, this list of conditions and the following disclaimer in\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 " the documentation and/or other materials provided with the\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 " distribution.\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 "\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 " 3. The name of the author may not be used to endorse or promote\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 " products derived from this software without specific prior written\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 " permission.\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 "\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 "THIS SOFTWARE IS PROVIDED BY THE AUTHOR \"AS IS\" AND ANY EXPRESS OR\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 "ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 "INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 "(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 "SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 "STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 "IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 "POSSIBILITY OF SUCH DAMAGE.\n" |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 ); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 const char *sidutil_strip_hvsc_path(const char *hvscPath, const char *filename) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 if (hvscPath != NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 const char *hvsc = hvscPath, *fptr = filename; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 // Compare until end of string(s) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 for (; *hvsc != 0 && *fptr != 0 && *hvsc == *fptr; hvsc++, fptr++); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
115 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 // Full match? |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 if (*hvsc == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 return fptr - 1; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 return filename; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
123 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 char *sidutil_check_hvsc_file(const char *hvscPath, const char *filebase, const char *fext) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 th_stat_data sdata; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 char *npath = th_strdup_printf("%s%c%s%c%s%s", |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 hvscPath, TH_DIR_SEPARATOR_CHR, |
318
f3ba2ba894b1
Rename few HVSC related #defines to have SIDUTIL_ prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
316
diff
changeset
|
130 SIDUTIL_HVSC_DOCUMENTS, TH_DIR_SEPARATOR_CHR, |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 filebase, fext != NULL ? fext : ""); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
132 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 if (npath != NULL && |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 th_stat_path(npath, &sdata) && |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 (sdata.flags & TH_IS_READABLE) && |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 (sdata.flags & TH_IS_DIR) == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 return npath; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 th_free(npath); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 return NULL; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
141 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
144 #ifndef HAVE_ICONV |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 static const uint8_t sidutil_lang_iso88591_to_cp850[16*6] = { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 0xff, 0xad, 0xbd, 0x9c, 0xcf, 0xbe, 0xdd, 0xf5, 0xf9, 0xb8, 0xa6, 0xae, 0xaa, 0xf0, 0xa9, 0xee, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 0xf8, 0xf1, 0xfd, 0xfc, 0xef, 0xe6, 0xf4, 0xfa, 0xf7, 0xfb, 0xa7, 0xaf, 0xac, 0xab, 0xf3, 0xa8, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 0xb7, 0xb5, 0xb6, 0xc7, 0x8e, 0x8f, 0x92, 0x80, 0xd4, 0x90, 0xd2, 0xd3, 0xde, 0xd6, 0xd7, 0xd8, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 0xd1, 0xa5, 0xe3, 0xe0, 0xe2, 0xe5, 0x99, 0x9e, 0x9d, 0xeb, 0xe9, 0xea, 0x9a, 0xed, 0xe8, 0xe1, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
151 0x85, 0xa0, 0x83, 0xc6, 0x84, 0x86, 0x91, 0x87, 0x8a, 0x82, 0x88, 0x89, 0x8d, 0xa1, 0x8c, 0x8b, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 0xd0, 0xa4, 0x95, 0xa2, 0x93, 0xe4, 0x94, 0xf6, 0x9b, 0x97, 0xa3, 0x96, 0x81, 0xec, 0xe7, 0x98, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 }; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 static const uint8_t sidutil_lang_iso88591_to_cp437[16*6] = { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 0xff, 0xad, 0x9b, 0x9c, 0x00, 0x9d, 0x00, 0x00, 0x00, 0x00, 0xa6, 0xae, 0xaa, 0x00, 0x00, 0x00, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 0xf8, 0xf1, 0xfd, 0x00, 0x00, 0xe6, 0x00, 0xfa, 0x00, 0x00, 0xa7, 0xaf, 0xac, 0xab, 0x00, 0xa8, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 0x00, 0x00, 0x00, 0x00, 0x8e, 0x8f, 0x92, 0x80, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 0x00, 0xa5, 0x00, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9a, 0x00, 0x00, 0xe1, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 0x85, 0xa0, 0x83, 0x00, 0x84, 0x86, 0x91, 0x87, 0x8a, 0x82, 0x88, 0x89, 0x8d, 0xa1, 0x8c, 0x8b, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 0x00, 0xa4, 0x95, 0xa2, 0x93, 0x00, 0x94, 0xf6, 0x00, 0x97, 0xa3, 0x96, 0x81, 0x00, 0x00, 0x98, |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 }; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 static char *sidutil_chconv_internal(SIDUtilChConvCtx *ctx, const char *src) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 // Fallback conversion of ISO-8859-1 to X |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 const uint8_t *srcPtr = (const uint8_t *) src; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 const uint8_t *convTable; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 size_t outSize, outLen; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 char *outBuf, outByte; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 if (src == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 return NULL; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 outSize = strlen(src) + 1; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 if ((outBuf = th_malloc(outSize)) == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 return NULL; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 for (outLen = 0; *srcPtr; srcPtr++) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 switch (ctx->outLangID) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 case TH_LANG_UTF8: |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 // Not 100% correct really, but close enough |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 if (*srcPtr < 0x80) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, *srcPtr)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
189 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 if (*srcPtr < 0xBF) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, 0xC2) || |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 !th_strbuf_putch(&outBuf, &outSize, &outLen, *srcPtr)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, 0xC3) || |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
201 !th_strbuf_putch(&outBuf, &outSize, &outLen, *srcPtr - 0x40)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 break; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 case TH_LANG_ISO88591: |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, *srcPtr)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 break; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
210 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 case TH_LANG_CP850: |
356
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
212 case TH_LANG_CP858: |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 case TH_LANG_CP437: |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 // Not 100% correct either, but close enough |
356
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
215 convTab = (ctx->outLangID == TH_LANG_CP437) ? |
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
216 sidutil_lang_iso88591_to_cp437 : |
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
217 sidutil_lang_iso88591_to_cp850; |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
219 if (*srcPtr < 0x7f) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 outByte = *srcPtr; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 if (*srcPtr >= 0xA0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 outByte = convTable[*srcPtr - 0xA0]; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
224 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
225 outByte = '?'; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
226 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, outByte)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
228 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 break; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 if (!th_strbuf_putch(&outBuf, &outSize, &outLen, *srcPtr)) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 goto err; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 return outBuf; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 err: |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 th_free(outBuf); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 return NULL; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
241 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
242 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
243 #endif |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 // NOTICE! Only call this function IF ctx->enabled == TRUE |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 char * sidutil_chconv_convert(SIDUtilChConvCtx *ctx, const char *src) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 #ifdef HAVE_ICONV |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 size_t srcLeft = strlen(src) + 1; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 size_t outLeft = srcLeft * 2; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 char *srcPtr = (char *) src; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 char *outBuf, *outPtr; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 if ((outBuf = outPtr = th_malloc(outLeft + 1)) == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 return NULL; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 while (srcLeft > 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 size_t ret = iconv(ctx->iconvCtx, &srcPtr, &srcLeft, &outPtr, &outLeft); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 if (ret == (size_t) -1) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 break; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 return (char *) outBuf; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 #else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 return sidutil_chconv_internal(ctx, src); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 #endif |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 int sidutil_chconv_init(SIDUtilChConvCtx *ctx, const char *outLang) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 if (ctx == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 return THERR_NULLPTR; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
277 memset(ctx, 0, sizeof(*ctx)); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 if (outLang != NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
280 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
281 // Get the character encoding part (e.g. "UTF-8" etc.) and |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
282 // strip out and lowercase everything (e.g. "utf8") |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
283 size_t i; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 char *ptr; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 if ((ctx->outLang = th_strdup(outLang)) == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 return THERR_MALLOC; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
288 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
289 if ((ptr = strchr(ctx->outLang, '.')) == NULL) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 ptr = ctx->outLang; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 ptr++; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 for (i = 0; *ptr; ptr++) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 if (*ptr != '-') |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 ctx->outLang[i++] = th_tolower(*ptr); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 ctx->outLang[i] = 0; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 #ifdef HAVE_ICONV |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
302 // Initialize iconv, check if we have language/charset |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 ctx->iconvCtx = iconv_open(ctx->outLang, "iso88591"); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 ctx->enabled = (ctx->iconvCtx != (iconv_t) -1); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 #else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 // Check if we can use our fallback converter |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 if (strcmp(ctx->outLang, "utf8") == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 ctx->outLangID = TH_LANG_UTF8; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 if (strcmp(ctx->outLang, "iso88591") == 0 || |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 strcmp(ctx->outLang, "cp819") == 0 || |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
312 strcmp(ctx->outLang, "latin1") == 0 || |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
313 strcmp(ctx->outLang, "cp28591") == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 ctx->outLangID = TH_LANG_ISO88591; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
315 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 if (strcmp(ctx->outLang, "cp850") == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
317 ctx->outLangID = TH_LANG_CP850; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
318 else |
356
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
319 if (strcmp(ctx->outLang, "cp858") == 0) |
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
320 ctx->outLangID = TH_LANG_CP858; |
811eb6c6695e
Add "support" for CP858 output in fallback converter as it is essentially the same as CP850.
Matti Hamalainen <ccr@tnsp.org>
parents:
355
diff
changeset
|
321 else |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 if (strcmp(ctx->outLang, "cp437") == 0) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
323 ctx->outLangID = TH_LANG_CP437; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 ctx->outLangID = TH_LANG_ISO88591; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 ctx->enabled = ctx->outLangID != TH_LANG_ISO88591; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 #endif |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 return THERR_OK; |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
332 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 void sidutil_chconv_close(SIDUtilChConvCtx *ctx) |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
336 { |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 #ifdef HAVE_ICONV |
334
1e7ffcaeb8ad
Check SIDUtilChConvCtx::enabled instead of SIDUtilChConvCtx::iconvCtx != -1 when calling iconv_close().
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
338 if (ctx->enabled) |
313
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 iconv_close(ctx->iconvCtx); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 #else |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 #endif |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
343 th_free(ctx->outLang); |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
344 } |
349
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
345 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
346 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
347 void sidutil_ioctx_error(th_ioctx *fh, const int err, const char *msg) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
348 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
349 (void) err; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
350 THERR("[%s:%" PRIu_SIZE_T "] %s\n", fh->filename, fh->line, msg); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
351 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
352 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
353 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
354 int sidutil_read_sldb_file(const char *filename, SIDLibSLDB **pdbh) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
355 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
356 th_ioctx *infh = NULL; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
357 int res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
358 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
359 // Initialize SLDB |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
360 if ((res = th_io_fopen(&infh, &th_stdio_io_ops, filename, "r")) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
361 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
362 THERR("Could not open SLDB '%s': %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
363 filename, th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
364 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
365 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
366 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
367 th_io_set_handlers(infh, sidutil_ioctx_error, NULL); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
368 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
369 if ((res = sidlib_sldb_new(pdbh)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
370 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
371 THERR("Could not allocate SLDB database structure: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
372 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
373 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
374 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
375 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
376 if ((res = sidlib_sldb_read(infh, *pdbh)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
377 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
378 THERR("Error parsing SLDB: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
379 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
380 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
381 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
382 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
383 if ((res = sidlib_sldb_build_index(*pdbh)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
384 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
385 THERR("Error building SLDB index: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
386 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
387 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
388 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
389 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
390 out: |
355
9924d4518932
Adjust to th-libs ioctx API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
391 th_io_close(infh); |
349
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
392 return res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
393 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
394 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
395 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
396 int sidutil_read_stildb_file(const char *filename, SIDLibSTILDB **pdbh, SIDLibChConvCtx *chconv) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
397 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
398 th_ioctx *infh = NULL; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
399 int res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
400 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
401 // Initialize STILDB |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
402 if ((res = th_io_fopen(&infh, &th_stdio_io_ops, filename, "r")) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
403 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
404 THERR("Could not open STIL database '%s': %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
405 filename, th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
406 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
407 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
408 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
409 th_io_set_handlers(infh, sidutil_ioctx_error, NULL); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
410 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
411 if ((res = sidlib_stildb_new(pdbh)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
412 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
413 THERR("Could not allocate STIL database structure: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
414 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
415 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
416 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
417 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
418 if ((res = sidlib_stildb_read(infh, *pdbh, chconv)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
419 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
420 THERR("Error parsing STIL: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
421 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
422 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
423 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
424 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
425 if ((res = sidlib_stildb_build_index(*pdbh)) != THERR_OK) |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
426 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
427 THERR("Error building STIL index: %s\n", |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
428 th_error_str(res)); |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
429 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
430 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
431 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
432 out: |
355
9924d4518932
Adjust to th-libs ioctx API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
433 th_io_close(infh); |
349
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
434 return res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
435 } |