| 1 |
/** |
| 2 |
* Various string functions mainly used for controlling text output |
| 3 |
|
| 4 |
* Copyright (C) 2003 Shawn Betts |
| 5 |
* Copyright (C) 2004, 2007, 2008, 2009 Sylvain Beucler |
| 6 |
|
| 7 |
* This file is part of GNU FreeDink |
| 8 |
|
| 9 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 3 of the |
| 12 |
* License, or (at your option) any later version. |
| 13 |
|
| 14 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* General Public License for more details. |
| 18 |
|
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <ctype.h> |
| 29 |
#include <string.h> |
| 30 |
#include <strings.h> /* strcasecmp */ |
| 31 |
#include <stdlib.h> /* free */ |
| 32 |
#include <stdarg.h> /* va_start */ |
| 33 |
#include <stdio.h> /* vasprintf */ |
| 34 |
#include <xalloc.h> |
| 35 |
#include "str_util.h" |
| 36 |
#include "log.h" |
| 37 |
|
| 38 |
/** |
| 39 |
* Upcase the string |
| 40 |
*/ |
| 41 |
void |
| 42 |
strtoupper (char *s) |
| 43 |
{ |
| 44 |
for (; *s; s++) |
| 45 |
*s = toupper (*s); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Copy string w/o memory overlap |
| 50 |
*/ |
| 51 |
void strcpy_nooverlap(char *dst, char* src) |
| 52 |
{ |
| 53 |
char *tmp = strdup(src); |
| 54 |
strcpy(dst, tmp); |
| 55 |
free(tmp); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Utility - same as asprint, but appends to the specified buffer |
| 61 |
* |
| 62 |
* If strp points to a NULL pointer, it allocates a new buffer that |
| 63 |
* you'll have to free. |
| 64 |
*/ |
| 65 |
int asprintf_append(char **strp, const char* fmt, ...) |
| 66 |
{ |
| 67 |
va_list ap; |
| 68 |
|
| 69 |
if (*strp == NULL) |
| 70 |
{ |
| 71 |
*strp = (char*)malloc(1); |
| 72 |
*strp[0] = '\0'; |
| 73 |
} |
| 74 |
|
| 75 |
char *tmp = NULL; |
| 76 |
va_start(ap, fmt); |
| 77 |
int result = vasprintf(&tmp, fmt, ap); |
| 78 |
va_end(ap); |
| 79 |
|
| 80 |
*strp = (char*)realloc(*strp, strlen(*strp) + strlen(tmp) + 1); |
| 81 |
strcat(*strp, tmp); |
| 82 |
free(tmp); |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
void reverse(char *st) |
| 87 |
{ |
| 88 |
int i,ii; |
| 89 |
char don[255]; |
| 90 |
don[0] = 0; |
| 91 |
ii = strlen(st); |
| 92 |
for (i=ii; i > -1; i--) |
| 93 |
{ |
| 94 |
strchar(don, st[i]); |
| 95 |
} |
| 96 |
strcpy(st, don); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* This acts in the same way as strcat except it combines a string and |
| 101 |
* a single character, updating the null at the end. |
| 102 |
*/ |
| 103 |
void strchar(char *string, char ch) |
| 104 |
{ |
| 105 |
int last = strlen(string); |
| 106 |
string[last] = ch; |
| 107 |
string[last+1] = '\0'; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Split 'str' in words separated by _one_ 'sep', and copy the #'num' |
| 112 |
* one to 'return1'. The function does not alter 'str'. Return empty |
| 113 |
* string if not found. Several 'sep' enclose empty words |
| 114 |
* (e.g. separators are not collapsed, unlike 'get_word(...)'). |
| 115 |
* End of string behaves like a separator. |
| 116 |
*/ |
| 117 |
char* separate_string (char* line, int num, char sep) |
| 118 |
{ |
| 119 |
int l; |
| 120 |
int k; |
| 121 |
int line_len = strlen(line); |
| 122 |
|
| 123 |
l = 1; |
| 124 |
int start = 0; |
| 125 |
int end = 0; |
| 126 |
|
| 127 |
for (k = 0; k < line_len; k++) |
| 128 |
{ |
| 129 |
if (line[k] == sep) |
| 130 |
{ |
| 131 |
if (l == num) |
| 132 |
break; |
| 133 |
l++; |
| 134 |
start = end = (k + 1); |
| 135 |
} |
| 136 |
else /* (str[k] != sep) */ |
| 137 |
{ |
| 138 |
end++; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (l >= num) |
| 143 |
{ |
| 144 |
int size = end - start; |
| 145 |
char* result = (char*)xmalloc(size + 1); |
| 146 |
strncpy(result, line + start, size); |
| 147 |
result[size] = '\0'; |
| 148 |
|
| 149 |
replace_norealloc("\r", "", result); //Take the /r off it. |
| 150 |
replace_norealloc("\n", "", result); //Take the /n off it. |
| 151 |
return result; |
| 152 |
} |
| 153 |
else /* less than 'num' tokens */ |
| 154 |
{ |
| 155 |
return strdup(""); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Return the word number 'word' present in 'line'. If not present, |
| 161 |
* returns an empty string. Words are separated by one _or more_ |
| 162 |
* spaces and count from 1 (i.e. not 0). |
| 163 |
*/ |
| 164 |
char* get_word(char* line, int word) |
| 165 |
{ |
| 166 |
int cur_word = 1; |
| 167 |
|
| 168 |
/* find word */ |
| 169 |
char* pc = line; |
| 170 |
while (*pc != '\0') |
| 171 |
{ |
| 172 |
if (cur_word == word) |
| 173 |
break; |
| 174 |
if (*pc == ' ') |
| 175 |
{ |
| 176 |
cur_word++; |
| 177 |
while(*pc == ' ' && *pc != '\0') |
| 178 |
pc++; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
while(*pc != ' ' && *pc != '\0') |
| 183 |
pc++; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/* find end-of-word */ |
| 188 |
char* start = pc; |
| 189 |
while(*pc != '\0' && *pc != ' ') |
| 190 |
pc++; |
| 191 |
|
| 192 |
/* copy word - either we're on the right word and will copy it, |
| 193 |
either we're at the end of string and will copy an empty word */ |
| 194 |
int len = pc - start; |
| 195 |
char* result = (char*)xmalloc(len + 1); |
| 196 |
memcpy(result, start, len); |
| 197 |
result[len] = '\0'; |
| 198 |
|
| 199 |
return result; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Return whether 'orig' and 'comp' are the same string |
| 205 |
* (case-insensitive comparison). |
| 206 |
*/ |
| 207 |
/*bool*/int compare(char *orig, char *comp) |
| 208 |
{ |
| 209 |
return (strcasecmp(orig, comp) == 0); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Move chars between 'start' and the end of 'line' to the left, with |
| 214 |
* a postponement of 'shift' chars. Copy the trailing '\0'. |
| 215 |
*/ |
| 216 |
static void shift_left(char* line, int start, int shift) |
| 217 |
{ |
| 218 |
/* Beware of the direction so as not to overwrite */ |
| 219 |
int i = start; |
| 220 |
int max = strlen(line); |
| 221 |
for (; i <= max; i++) |
| 222 |
line[i-shift] = line[i]; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Move chars between 'start' and the end of 'line' to the right, with |
| 227 |
* a postponement of 'shift' chars. Copy the trailing '\0'. |
| 228 |
*/ |
| 229 |
static void shift_right(char* line, int start, int shift) |
| 230 |
{ |
| 231 |
/* Beware of the direction so as not to overwrite */ |
| 232 |
int i = strlen(line); |
| 233 |
for (; i >= start; i--) |
| 234 |
line[i+shift] = line[i]; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Replace word 'find' by word 'repl' in 'line', as many times as |
| 239 |
* possible. |
| 240 |
* |
| 241 |
* Note: |
| 242 |
* |
| 243 |
* - '*line_p' is xrealloc'd only if strlen(repl) > strlen(find), to |
| 244 |
make the calling code easier. |
| 245 |
*/ |
| 246 |
void replace(const char* find, const char* repl, char** line_p) |
| 247 |
{ |
| 248 |
int len_find = strlen(find); |
| 249 |
int len_repl = strlen(repl); |
| 250 |
char* line = *line_p; |
| 251 |
int len_line = strlen(line); |
| 252 |
|
| 253 |
int u = -1; |
| 254 |
int checker = 0; |
| 255 |
for (u = 0; u < len_line; u++) |
| 256 |
{ |
| 257 |
if (toupper(line[u]) == toupper(find[checker])) |
| 258 |
{ |
| 259 |
checker++; |
| 260 |
if (checker == len_find) |
| 261 |
{ |
| 262 |
int pos_repl = u + 1 - len_find; |
| 263 |
|
| 264 |
int len_newline = len_line + len_repl - len_find; |
| 265 |
/* Only change line_p if there's need to */ |
| 266 |
if (len_newline > len_line) |
| 267 |
{ |
| 268 |
line = xrealloc(line, len_newline + 1); |
| 269 |
*line_p = line; |
| 270 |
} |
| 271 |
|
| 272 |
/* Move what's after the replacement, if necessary */ |
| 273 |
if (len_repl < len_find) |
| 274 |
shift_left (line, pos_repl + len_find, len_find - len_repl); |
| 275 |
else if (len_repl > len_find) |
| 276 |
shift_right(line, pos_repl + len_find, len_repl - len_find); |
| 277 |
|
| 278 |
/* Actually replace */ |
| 279 |
strncpy(line + pos_repl, repl, len_repl); |
| 280 |
|
| 281 |
/* Prepare for next loop */ |
| 282 |
checker = 0; |
| 283 |
len_line += len_repl - len_find; |
| 284 |
u += len_repl - len_find; |
| 285 |
} |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
checker = 0; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Alias to 'replace' that emphasize that '&line' won't be modified |
| 296 |
* (i.e. realloc'd). Only valid if the replacement is shorter or as |
| 297 |
* long as the search term. |
| 298 |
*/ |
| 299 |
void replace_norealloc(const char* find, const char* repl, char* line) |
| 300 |
{ |
| 301 |
if (strlen(repl) > strlen(find)) |
| 302 |
{ |
| 303 |
log_fatal("Internal error: invalid string substitution"); |
| 304 |
exit(EXIT_FAILURE); |
| 305 |
} |
| 306 |
replace(find, repl, &line); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Convert Latin-1-encoded 'source' to UTF-8-encoded. Result will |
| 312 |
* always be NULL-terminated. |
| 313 |
*/ |
| 314 |
char* latin1_to_utf8(char* source) |
| 315 |
{ |
| 316 |
int cur_alloc_size = 512; |
| 317 |
const int step = 256; |
| 318 |
unsigned char* dest = (unsigned char*)xmalloc(cur_alloc_size); |
| 319 |
unsigned char* pcs = (unsigned char*)source; |
| 320 |
unsigned char* pcd = (unsigned char*)dest; |
| 321 |
unsigned char* pcd_limit = pcd + cur_alloc_size; |
| 322 |
while(*pcs != '\0') |
| 323 |
{ |
| 324 |
if (pcd == pcd_limit) |
| 325 |
{ |
| 326 |
cur_alloc_size += step; |
| 327 |
dest = xrealloc(dest, cur_alloc_size); |
| 328 |
pcd = dest + cur_alloc_size - step; |
| 329 |
pcd_limit = pcd + step; |
| 330 |
} |
| 331 |
if (*pcs < 128) |
| 332 |
{ |
| 333 |
*pcd = *pcs; |
| 334 |
pcs++; |
| 335 |
pcd++; |
| 336 |
} |
| 337 |
else |
| 338 |
{ |
| 339 |
*pcd = 0xc2 + ((*pcs - 128) / 64); |
| 340 |
pcd++; |
| 341 |
*pcd = 0x80 + ((*pcs - 128) % 64); |
| 342 |
pcd++; |
| 343 |
pcs++; |
| 344 |
} |
| 345 |
} |
| 346 |
*pcd = '\0'; |
| 347 |
return (char*)dest; |
| 348 |
} |
| 349 |
|
| 350 |
/* Here's a small Python script to explain the above formula: */ |
| 351 |
|
| 352 |
// # Static charset conversion table from Latin-1 to UTF-8: |
| 353 |
// print 'unsigned char conv[][2] = {\n', ',\n'.join( |
| 354 |
// ['\/*%d:\*/ {%s}' % ( |
| 355 |
// c, |
| 356 |
// ', '.join( |
| 357 |
// [hex(ord(i)) for i in chr(c).decode('ISO-8859-1').encode('utf-8')] |
| 358 |
// ) |
| 359 |
// ) for c in range(128,256)] |
| 360 |
// ), '};' |
| 361 |
// |
| 362 |
// # Test computed (!= static) table: |
| 363 |
// for c in range(128,256): |
| 364 |
// method1 = [ord(i) for i in chr(c).decode('ISO-8859-1').encode('utf-8')] |
| 365 |
// method2 = [0xc2 + ((c - 128) / 64), 0x80 + ((c - 128) % 64)] |
| 366 |
// #print method1, method2 |
| 367 |
// if method1[0] != method2[0] or method1[1] != method2[1]: |
| 368 |
// print "Mismatch at %c" |