1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/* Place in the public domain by Sam Trenholme 2000 */
/* js_string routines that depend on the code page (encoding) that we
are in */
#include "JsStr.h"
/* js_newline_chars: returns a set of newline characters for a given
encoding
input: an empty js_string object we look at the codepage of
output: JS_ERROR (-1) on error, JS_SUCCESS (1) on success */
int js_newline_chars(js_string *js) {
/* Sanity check */
if(js_has_sanity(js) == -1)
return -1;
/* Handle different encodings differently */
switch(js->encoding) {
case JS_BINARY:
js->unit_count = 0;
return JS_SUCCESS;
case JS_US_ASCII:
case JS_ISO_8859_1:
if(js->unit_size != 1)
return -1; /* This string is not sane */
if(js_octets(js) < 2)
return -1; /* No overflows ever */
*(js->string) = '\r';
*(js->string + 1) = '\n';
js->unit_count = 2;
return JS_SUCCESS;
}
/* We only get here if it is an unhandled encoding */
return JS_ERROR;
}
/* js_space_chars: returns a set of whitespace characters for a given
encoding
input: an empty js_string object we look at the codepage of
output: JS_ERROR (-1) on error, JS_SUCCESS (1) on success */
int js_space_chars(js_string *js) {
/* Sanity check */
if(js_has_sanity(js) == -1)
return -1;
/* Handle different encodings differently */
switch(js->encoding) {
case JS_BINARY:
js->unit_count = 0;
return JS_SUCCESS;
case JS_US_ASCII:
case JS_ISO_8859_1:
if(js->unit_size != 1)
return -1; /* This string is not sane */
if(js_octets(js) < 3)
return -1; /* No overflows ever */
*(js->string) = ' ';
*(js->string + 1) = '\t';
if(js->encoding != JS_US_ASCII) {
*(js->string + 2) = 160; /* Non-breaking space */
js->unit_count = 3;
}
else
js->unit_count = 2;
return JS_SUCCESS;
}
/* We only get here if it is an unhandled encoding */
return JS_ERROR;
}
/* js_atoi: Convert a number, starting at a given offset, into an integer
input: js_string object to look at, place in string to start looking at
output: integer we find at the given point, 0 on error (ugh) */
unsigned int js_atoi(js_string *js, int offset) {
int value, sign;
if(js_has_sanity(js) == JS_ERROR)
return 0;
/* Return 0 if encoding unsupported */
if(js->encoding != JS_US_ASCII && js->encoding != JS_ISO_8859_1)
return 0;
/* All supported encodings use the same codes for numerical digits */
else {
if(offset >= js->unit_count)
return 0;
value = 0;
sign = 1;
while(offset < js->unit_count && *(js->string + offset) >= '0'
&& *(js->string + offset) <= '9') {
value *= 10;
value += *(js->string + offset) - '0';
offset++;
}
value *= sign;
return value;
}
return 0; /* We should never get here */
}
/* js_tolower: Convert a js_string object in to all lower case letters.
input: js_string object to convert
output: JS_ERROR on error, JS_SUCCESS on success */
int js_tolower(js_string *js) {
int counter;
if(js_has_sanity(js) == JS_ERROR)
return JS_ERROR;
if(js->unit_size != 1) /* All supported encodings have one octet
per character */
return JS_ERROR;
if(js->encoding == JS_US_ASCII) {
for(counter = 0; counter < js->unit_count; counter++)
if(*(js->string + counter) >= 'A' &&
*(js->string + counter) <= 'Z')
*(js->string + counter) += 32;
}
else if(js->encoding == JS_ISO_8859_1) {
for(counter = 0; counter < js->unit_count; counter++) {
if(*(js->string + counter) >= 'A' &&
*(js->string + counter) <= 'Z')
*(js->string + counter) += 32;
if(*(js->string + counter) >= 192 /* À */ &&
*(js->string + counter) <= 214 /* Ö */)
*(js->string + counter) += 32;
/* Why they had to put × and the corresponding ÷ smack dab in
the middle of the international letters is a mystery to me */
if(*(js->string + counter) >= 216 /* Ø */ &&
*(js->string + counter) <= 222 /* Þ */)
*(js->string + counter) += 32;
}
}
/* Return 0 if encoding unsupported */
else
return 0;
return JS_SUCCESS;
}
/* js_anq_chars: Give a list of alpha(numeric) characters for a given
encoding.
input: js_string object to fill with the data in question,
whether or not we include numbers and the _ symbol
(0 = no, 1 = yes, 2 = only numbers)
output: pointer to js_string obejct on success, 0 on error
*/
js_string *js_anq_chars(js_string *js, int do_nums) {
int place = 0,do_lets = 1;
unsigned char counter;
/* Sanity check */
if(js_has_sanity(js) == JS_ERROR)
return 0;
/* If we are doing only numbers, then we don't use letters */
if(do_nums == 2)
do_lets = 0;
/* Action is based on encoding */
switch(js->encoding) {
case JS_ISO_8859_1:
if(js_octets(js) < 192)
return 0; /* No overflows ever */
if(do_lets == 1) {
/* À to Ö */
for(counter=192;counter<=214;counter++) {
*(js->string + place) = counter;
place++;
}
/* Ø to ö */
for(counter=216;counter<=246;counter++) {
*(js->string + place) = counter;
place++;
}
/* ø to þ */
for(counter=248;counter<255;counter++) {
*(js->string + place) = counter;
place++;
}
/* ÿ ( We do it this way because counter is always <= 255 ) */
*(js->string + place) = 255;
place++;
}
case JS_US_ASCII:
if(js_octets(js) < 96)
return 0;
if(do_lets == 1) {
/* A to Z */
for(counter='A';counter<='Z';counter++) {
*(js->string + place) = counter;
place++;
}
/* a to z */
for(counter='a';counter<='z';counter++) {
*(js->string + place) = counter;
place++;
}
}
if(do_nums == 1 || do_nums == 2) {
/* 0 to 9 */
for(counter='0';counter<='9';counter++) {
*(js->string + place) = counter;
place++;
}
/* _ */
if(do_nums != 2) /* If not numbers only */ {
*(js->string + place) = '_';
place++;
}
}
js->unit_count = place;
break;
default:
return 0;
}
return js;
}
/* js_alpha_chars: Give a list of alphabetic characters for a given
encoding. (not including _)
input: js_string object to fill with the data in question,
output: pointer to js_string obejct on success, 0 on error
*/
js_string *js_alpha_chars(js_string *js) {
return js_anq_chars(js,0);
}
/* js_an_chars: Give a list of alphanumeric characters for a given
encoding. (including _)
input: js_string object to fill with the data in question,
output: pointer to js_string obejct on success, 0 on error
*/
js_string *js_an_chars(js_string *js) {
return js_anq_chars(js,1);
}
/* js_numbers: Give a list of numeric characters for a given
encoding. (no _)
input: js_string object to fill with the data in question,
output: pointer to js_string obejct on success, 0 on error
*/
js_string *js_numbers(js_string *js) {
return js_anq_chars(js,2);
}
|