Debian Codegraph @

terps/alan3/sysdep.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2 sysdep.c
3
4 This file contains necessary system dependent routines.
5
6 Notes on string handling:
7
8 - Native - means the "natural" character set/encoding for the
9 platform, nowadays often UTF-8, but that was not the case in the
10 beginning.
11
12 - Internal - is always ISO8859-1 in which encoding everything
13 internal should use (even dictionary entries, which is not certain
14 it does currently. TODO!
15
16 - Current - the compiler (currently) has options for different
17 charsets, if that is used all input files are considered to be in
18 that encoding, which might be different from the native encoding.
19 It (will) also auto-detect an UTF BOM and enforce UTF-8 for that
20 single file, which again might be different from native or the one
21 given using the -charset option.
22
23 \*----------------------------------------------------------------------*/
24
25 #include <time.h>
26 #include "sysdep.h"
27 #include "syserr.h"
28
29
30 #ifdef HAVE_GLK
31 #include "glk.h"
32 #endif
33
34
35 #ifdef HAVE_GLK
36
37 /* Note to Glk maintainers: 'native' characters are used for output, in this
38 case, Glk's Latin-1. ISO characters are Alan's internal representation,
39 stored in the .A3C file, and must be converted to native before printing.
40 Glk could just use the ISO routines directly, but its safer to maintain
41 its own tables to guard against future changes in either Alan or Glk (ie. a
42 move to Unicode).
43 */
44
45 static char spaceCharacters[] =
46 {
47 0x0A, /* linefeed */
48 0x20, /* space */
49 0xA0, /* non-breaking space */
50 0x00
51 };
52
53 static char lowerCaseCharacters[] =
54 {
55 0x61, /* a */ 0x62, /* b */ 0x63, /* c */ 0x64, /* d */
56 0x65, /* e */ 0x66, /* f */ 0x67, /* g */ 0x68, /* h */
57 0x69, /* i */ 0x6A, /* j */ 0x6B, /* k */ 0x6C, /* l */
58 0x6D, /* m */ 0x6E, /* n */ 0x6F, /* o */ 0x70, /* p */
59 0x71, /* q */ 0x72, /* r */ 0x73, /* s */ 0x74, /* t */
60 0x75, /* u */ 0x76, /* v */ 0x77, /* w */ 0x78, /* x */
61 0x79, /* y */ 0x7A, /* z */ 0xDF, /* ss <small sharp s> */
62 0xE0, /* a grave */ 0xE1, /* a acute */
63 0xE2, /* a circumflex */ 0xE3, /* a tilde */
64 0xE4, /* a diaeresis */ 0xE5, /* a ring */
65 0xE6, /* ae */ 0xE7, /* c cedilla */
66 0xE8, /* e grave */ 0xE9, /* e acute */
67 0xEA, /* e circumflex */ 0xEB, /* e diaeresis */
68 0xEC, /* i grave */ 0xED, /* i acute */
69 0xEE, /* i circumflex */ 0xEF, /* i diaeresis */
70 0xF0, /* <small eth> */ 0xF1, /* n tilde */
71 0xF2, /* o grave */ 0xF3, /* o acute */
72 0xF4, /* o circumflex */ 0xF5, /* o tilde */
73 0xF6, /* o diaeresis */ 0xF8, /* o slash */
74 0xF9, /* u grave */ 0xFA, /* u acute */
75 0xFB, /* u circumflex */ 0xFC, /* u diaeresis */
76 0xFD, /* y acute */ 0xFE, /* <small thorn> */
77 0xFF, /* y diaeresis */ 0x00
78 };
79
80 /* FIXME: ss <small sharp s> and y diaeresis have no UC analogues
81 Are they really considered LC?
82 */
83
84 static char upperCaseCharacters[] =
85 {
86 0x41, /* A */ 0x42, /* B */ 0x43, /* C */ 0x44, /* D */
87 0x45, /* E */ 0x46, /* F */ 0x47, /* G */ 0x48, /* H */
88 0x49, /* I */ 0x4A, /* J */ 0x4B, /* K */ 0x4C, /* L */
89 0x4D, /* M */ 0x4E, /* N */ 0x4F, /* O */ 0x50, /* P */
90 0x51, /* Q */ 0x52, /* R */ 0x53, /* S */ 0x54, /* T */
91 0x55, /* U */ 0x56, /* V */ 0x57, /* W */ 0x58, /* X */
92 0x59, /* Y */ 0x5A, /* Z */
93 0xC0, /* A grave */ 0xC1, /* A acute */
94 0xC2, /* A circumflex */ 0xC3, /* A tilde */
95 0xC4, /* A diaeresis */ 0xC5, /* A ring */
96 0xC6, /* AE */ 0xC7, /* C cedilla */
97 0xC8, /* E grave */ 0xC9, /* E acute */
98 0xCA, /* E circumflex */ 0xCB, /* E diaeresis */
99 0xCC, /* I grave */ 0xCD, /* I acute */
100 0xCE, /* I circumflex */ 0xCF, /* I diaeresis */
101 0xD0, /* <capital eth> */ 0xD1, /* N tilde */
102 0xD2, /* O grave */ 0xD3, /* O acute */
103 0xD4, /* O circumflex */ 0xD5, /* O tilde */
104 0xD6, /* O diaeresis */ 0xD8, /* O slash */
105 0xD9, /* U grave */ 0xDA, /* U acute */
106 0xDB, /* U circumflex */ 0xDC, /* U diaeresis */
107 0xDD, /* Y acute */ 0xDE, /* <capital thorn> */
108 0x00
109 };
110
111 #else
112
113 /* These work on internal (ISO8859-1) character sets */
114
115 static unsigned char spaceCharacters[] = " \t\n";
116
117 /* "abcdefghijklmnopqrstuvwxyz à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ø ù ú û ü ý þ ÿ"; */
118 static const char lowerCaseCharacters[] = "abcdefghijklmnopqrstuvwxyz\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF8\xF9\xFA\xFB\xFC\xFF\xFE\xFF";
119
120 /* "ABCDEFGHIJKLMNOPQRSTUVWXYZ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö Ø Ù Ú Û Û Ý Þ ß"; */
121 static const char upperCaseCharacters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF";
122
123 #endif
124
125 int isSpace(unsigned int c) /* IN - Internal character to test */
126 {
127 return (c != '\0' && strchr((char *)spaceCharacters, c) != 0);
128 }
129
130
131 int isLower(unsigned int c) /* IN - Internal character to test */
132 {
133 return (c != '\0' && strchr((char *)lowerCaseCharacters, c) != 0);
134 }
135
136
137 int isUpper(unsigned int c) /* IN - Internal character to test */
138 {
139 return (c != '\0' && strchr((char *)upperCaseCharacters, c) != 0);
140 }
141
142 int isLetter(unsigned int c) /* IN - Internal character to test */
143 {
144 return(c != '\0' && (isLower(c)? !0: isUpper(c)));
145 }
146
147
148 int toLower(unsigned int c) /* IN - Internal character to convert */
149 {
150 #ifdef HAVE_GLK
151 return glk_char_to_lower(c);
152 #else
153 return (isUpper(c)? c + ('a' - 'A'): c);
154 #endif
155 }
156
157 int toUpper(unsigned int c) /* IN - Internal character to convert */
158 {
159 #ifdef HAVE_GLK
160 return glk_char_to_upper(c);
161 #else
162 return (isLower(c)? c - ('a' - 'A'): c);
163 #endif
164 }
165
166 void stringToLowerCase(char string[]) /* INOUT - Internal string to convert */
167 {
168 char *s;
169
170 for (s = string; *s; s++)
171 *s = toLower(*s);
172 }
173
174
175 /*----------------------------------------------------------------------*/
176 bool equalStrings(char *str1, char *str2)
177 {
178 char *s1 = str1, *s2 = str2;
179
180 while (*s1 != '\0' && *s2 != '\0') {
181 if (toLower(*s1) != toLower(*s2))
182 return false;
183 s1++;
184 s2++;
185 }
186 return toLower(*s1) == toLower(*s2);
187 }
188
189
190 /*======================================================================*/
191 int littleEndian() {
192 int x = 1;
193 return (*(char *)&x == 1);
194 }
195
196
197 /*======================================================================*/
198 char *baseNameStart(char *fullPathName) {
199 static char *delimiters = "\\>]/:";
200 int i;
201
202 for (i = strlen(fullPathName)-1; i > 0; i--)
203 if (strchr(delimiters, fullPathName[i]) != NULL)
204 return &fullPathName[i+1];
205 return(fullPathName);
206 }