annotate sidutil.c @ 318:f3ba2ba894b1

Rename few HVSC related #defines to have SIDUTIL_ prefix.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 11 Jan 2020 19:11:34 +0200
parents b0c844b39516
children 1e7ffcaeb8ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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:
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 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
213 // Not 100% correct either, 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
214 convTable = (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
215 sidutil_lang_iso88591_to_cp850 : sidutil_lang_iso88591_to_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
216
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224
b3d46806787d 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 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
226 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
227 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
228 }
b3d46806787d 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 }
b3d46806787d 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 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
232 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
233
b3d46806787d 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 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
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 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
237 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
238 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
239 }
b3d46806787d 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 #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
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
b3d46806787d 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 // 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
245 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
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 #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
248 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
249 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
250 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
251 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
252
b3d46806787d 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 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
254 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
255
b3d46806787d 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 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
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 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
259 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
260 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
261 }
b3d46806787d 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 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
264 #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
265 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
266 #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
267 }
b3d46806787d 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 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
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 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
273 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
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 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
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 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
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 // 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
280 // 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
281 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
282 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
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 ((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
285 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
286
b3d46806787d 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 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
288 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
289 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
290 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
291
b3d46806787d 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 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
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 != '-')
b3d46806787d 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 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
296 }
b3d46806787d 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] = 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
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 #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
300 // 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
301 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
302 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
303 #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
304 // 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321
b3d46806787d 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->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
323 #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
324 }
b3d46806787d 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
b3d46806787d 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 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
327 }
b3d46806787d 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
b3d46806787d 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 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
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 #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
333 if (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
334 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
335 #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
336 #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
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 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
339 }