Mercurial > hg > sidinfo
annotate sidutil.c @ 357:199284ba94ea
Cleanup and few comments added.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 27 Jan 2020 02:33:13 +0200 |
parents | 811eb6c6695e |
children | a5131cd64110 |
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 |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
165 #define SI_PUTCH(xch) th_strbuf_putch(&outBuf, &outSize, &outLen, (xch)) |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
166 |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
167 |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
168 // |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
169 // Convert _ONLY_ FROM ISO-8859-1 encoding to few other encodings |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
170 // |
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
|
171 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
|
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 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
|
174 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
|
175 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
|
176 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
|
177 |
b3d46806787d
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 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
|
179 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
|
180 |
b3d46806787d
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 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
|
182 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
|
183 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
|
184 |
b3d46806787d
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 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
|
186 { |
b3d46806787d
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 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
|
188 { |
b3d46806787d
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 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
|
190 // 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
|
191 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
|
192 { |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
193 if (!SI_PUTCH(*srcPtr)) |
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
|
194 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
|
195 } |
b3d46806787d
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 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
|
197 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
|
198 { |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
199 if (!SI_PUTCH(0xC2) || |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
200 !SI_PUTCH(*srcPtr)) |
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
|
201 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
|
202 } |
b3d46806787d
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 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
|
204 { |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
205 if (!SI_PUTCH(0xC3) || |
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
206 !SI_PUTCH(*srcPtr - 0x40)) |
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
|
207 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
|
208 } |
b3d46806787d
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_ISO88591: |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
212 if (!SI_PUTCH(*srcPtr)) |
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 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
|
214 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
|
215 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 // 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
|
220 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
|
221 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
|
222 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
|
223 |
b3d46806787d
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 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
232 if (!SI_PUTCH(outByte)) |
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
|
233 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
|
234 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
|
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 } |
b3d46806787d
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 |
357
199284ba94ea
Cleanup and few comments added.
Matti Hamalainen <ccr@tnsp.org>
parents:
356
diff
changeset
|
238 if (!SI_PUTCH(*srcPtr)) |
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
|
239 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
|
240 |
b3d46806787d
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 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
|
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 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
|
244 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
|
245 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
|
246 } |
b3d46806787d
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 |
b3d46806787d
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 #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
|
249 |
b3d46806787d
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 |
b3d46806787d
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 // 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
|
252 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
|
253 { |
b3d46806787d
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 #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
|
255 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
|
256 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
|
257 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
|
258 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
|
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 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
|
261 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
|
262 |
b3d46806787d
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 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
|
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 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
|
266 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
|
267 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
|
268 } |
b3d46806787d
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 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
|
271 #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
|
272 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
|
273 #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
|
274 } |
b3d46806787d
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 |
b3d46806787d
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 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
|
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 (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
|
280 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
|
281 |
b3d46806787d
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 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
|
283 |
b3d46806787d
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 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
|
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 // 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
|
287 // 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
|
288 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
|
289 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
|
290 |
b3d46806787d
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 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
|
292 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
|
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 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
|
295 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
|
296 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
|
297 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 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
|
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 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
|
302 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
|
303 } |
b3d46806787d
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->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
|
305 |
b3d46806787d
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 #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
|
307 // 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
|
308 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
|
309 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
|
310 #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
|
311 // 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
|
312 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
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 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
|
320 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
|
321 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
|
322 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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 |
b3d46806787d
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 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
|
333 #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
|
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 |
b3d46806787d
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 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
|
337 } |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 |
b3d46806787d
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 |
b3d46806787d
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 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
|
341 { |
b3d46806787d
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 #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
|
343 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
|
344 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
|
345 #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
|
346 #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
|
347 |
b3d46806787d
Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 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
|
349 } |
349
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
350 |
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 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
|
353 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
354 (void) err; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
355 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
|
356 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
357 |
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 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
|
360 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
361 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
|
362 int res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
363 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
364 // Initialize SLDB |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
365 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
|
366 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
367 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
|
368 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
|
369 goto out; |
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 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
372 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
|
373 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
374 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
|
375 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
376 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
|
377 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
|
378 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
379 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
380 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
381 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
|
382 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
383 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
|
384 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
|
385 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
386 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
387 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
388 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
|
389 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
390 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
|
391 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
|
392 goto out; |
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 out: |
355
9924d4518932
Adjust to th-libs ioctx API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
396 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
|
397 return res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
398 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
399 |
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 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
|
402 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
403 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
|
404 int res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
405 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
406 // Initialize STILDB |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
407 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
|
408 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
409 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
|
410 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
|
411 goto out; |
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 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
414 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
|
415 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
416 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
|
417 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
418 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
|
419 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
|
420 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
421 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
422 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
423 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
|
424 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
425 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
|
426 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
|
427 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
428 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
429 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
430 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
|
431 { |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
432 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
|
433 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
|
434 goto out; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
435 } |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
436 |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
437 out: |
355
9924d4518932
Adjust to th-libs ioctx API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
438 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
|
439 return res; |
a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
Matti Hamalainen <ccr@tnsp.org>
parents:
334
diff
changeset
|
440 } |