annotate sidutil.c @ 313:b3d46806787d

Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 11 Jan 2020 18:30:10 +0200
parents
children b0c844b39516
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
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 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
13 {
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 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
15 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
16 "\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
17 "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
18 "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
19 "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
20 "\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
21 " 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
22 " 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
23 "\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
24 " 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
25 " 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
26 " 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
27 " 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
28 "\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
29 " 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
30 " 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
31 " 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
32 "\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
33 "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
34 "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
35 "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
36 "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
37 "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
38 "(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
39 "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
40 "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
41 "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
42 "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
43 "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
44 );
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 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
49 {
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 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
51 {
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 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
53
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 // 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
55 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
56
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 // 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
58 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
59 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
60 }
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 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
63 }
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 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
67 {
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 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
69 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
70 hvscPath, TH_DIR_SEPARATOR_CHR,
b3d46806787d 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 SET_HVSC_DOCUMENTS, TH_DIR_SEPARATOR_CHR,
b3d46806787d 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 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
73
b3d46806787d 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 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
75 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
76 (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
77 (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
78 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
79
b3d46806787d 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 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
81 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
82 }
b3d46806787d 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
b3d46806787d 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
b3d46806787d 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 #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
86
b3d46806787d 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 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
88 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
89 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
90 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
91 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
92 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
93 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
94 };
b3d46806787d 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
b3d46806787d 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 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
97 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
98 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
99 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
100 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
101 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
102 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
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 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
107 {
b3d46806787d 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 // 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
109 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
110 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
111 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
112 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
113
b3d46806787d 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 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
115 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
116
b3d46806787d 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 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
118 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
119 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
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 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
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 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
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 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
126 // 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
127 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
128 {
b3d46806787d 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 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
130 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
131 }
b3d46806787d 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 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
133 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
134 {
b3d46806787d 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 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
136 !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
137 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
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 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
140 {
b3d46806787d 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 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
142 !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
143 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
144 }
b3d46806787d 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 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
146
b3d46806787d 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 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
148 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
149 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
150 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
151
b3d46806787d 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 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
153 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
154 // 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
155 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
156 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
157
b3d46806787d 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 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
159 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
160 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
161 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
162 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
163 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
164 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
165
b3d46806787d 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 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
167 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
168 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
169 }
b3d46806787d 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 }
b3d46806787d 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
b3d46806787d 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 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
173 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
174
b3d46806787d 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 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
176
b3d46806787d 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 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
178 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
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
b3d46806787d 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 #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
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
b3d46806787d 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 // 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
186 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
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 #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
189 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
190 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
191 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
192 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
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 ((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
195 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
196
b3d46806787d 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 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
198 {
b3d46806787d 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 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
200 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
201 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
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
b3d46806787d 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 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
205 #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
206 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
207 #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
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
b3d46806787d 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 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
212 {
b3d46806787d 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 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
214 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
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 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
217
b3d46806787d 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 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
219 {
b3d46806787d 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 // 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
221 // 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
222 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
223 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
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 ((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
226 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
227
b3d46806787d 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 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
229 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
230 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
231 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
232
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 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
234 {
b3d46806787d 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 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
236 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
237 }
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 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
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 #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
241 // 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
242 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
243 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
244 #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
245 // 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
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 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
264 #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
265 }
b3d46806787d 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
b3d46806787d Move a number of more or less generic helper functions into a separate sidutil.[ch] module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 return 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
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
b3d46806787d 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 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
272 {
b3d46806787d 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 #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
274 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
275 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
276 #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
277 #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
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 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
280 }