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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*!
* \file strings.c
*
* \brief GIS Library - string/chring movement functions
*
* \todo merge interesting functions from ../datetime/scan.c here
*
* (C) 1999-2008 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
* \author Dave Gerdes (USACERL), Michael Shapiro (USACERL), Amit
* Parghi (USACERL), Bernhard Reiter (Intevation GmbH, Germany) and
* many others
*/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <grass/gis.h>
#ifndef NULL
#define NULL 0
#endif
static char *G_strend(const char *S)
{
while (*S)
S++;
return (char *)S;
}
/*!
* \brief Copies characters from the string F into the string T.
*
* This function has undefined results if the strings overlap.
*
* \param[out] T target string
* \param[in] F source string
*
* \return pointer to T
*/
char *G_strcpy(char *T, const char *F)
{
char *d = T;
while ((*d++ = *F++)) ;
return (T);
}
/*!
* \brief Copies characters from the string F into the string T.
*
* Copies just the first n characters from the string F. At the end
* the null terminator is written into the string T.
*
* \param[out] T target string
* \param[in] F source string
* \param[in] n number of characters to copy
*
* \return T value
*/
char *G_chrcpy(char *T, const char *F, int n)
{
char *d = T;
while (n--)
*d++ = *F++;
*d = '\0';
return (T);
}
/*!
* \brief This function is similar to G_chrcpy() but always copies at least
* n characters into the string T.
*
* If the length of F is more than n, then copies just the first n
* characters. At the end the null terminator is written into the
* string T.
*
* \param[out] T target string
* \param[in] F source string
* \param[in] n number of characters to copy
*
* \return T value
*/
char *G_strncpy(char *T, const char *F, int n)
{
char *d = T;
while (n-- && *F)
*d++ = *F++;
*d = '\0';
return (T);
}
/*!
* \brief Copies characters from the string F (not including the
* terminating null character) into the string T.
*
* \param[out] T target string
* \param[in] F source string
*
* \return T value
*/
char *G_strmov(char *T, const char *F)
{
char *d = T;
while (*F)
*d++ = *F++;
return (T);
}
/*!
* \brief This copies characters from the string F (exactly n
* characters) into the string T.
*
* The terminating null character is not explicitly written into the
* string T.
*
* \param[out] T target string
* \param[in] F source string
* \param[in] n number of characters to copy
*
* \return T value
*/
char *G_chrmov(char *T, const char *F, int n)
{
char *d = T;
while (n--)
*d++ = *F++;
return (T);
}
/*!
* \brief This copies characters from the string F into the string T.
*
* This function is similar to G_strcpy(), except that the
* characters from F are concatenated or appended to the end of
* T, instead of overwriting it. That is, the first character from
* F overwrites the null character marking the end of T.
*
* \param[out] T target string
* \param[in] F source string
*
* \return T value
*/
char *G_strcat(char *T, const char *F)
{
G_strcpy(G_strend(T), F);
return (T);
}
/*!
* \brief This function is like G_strcat() except that not more than n
* characters from F are appended to the end of T.
*
* This function is similar to G_strcpy(), except that the
* characters from F are concatenated or appended to the end of
* T, instead of overwriting it. That is, the first character from
* F overwrites the null character marking the end of T.
*
* \param[out] T target string
* \param[in] F source string
* \param[in] n number of character to copy
*
* \return T value
*/
char *G_chrcat(char *T, const char *F, int n)
{
G_chrcpy(G_strend(T), F, n);
return (T);
}
/*!
* \brief String compare ignoring case (upper or lower)
*
* Returning a value that has the same sign as the difference between
* the first differing pair of characters
*
* \param[in] x first string to compare
* \param[in] y second string to compare
*
* \return 0 the two strings are equal
* \return -1, 1
*/
int G_strcasecmp(const char *x, const char *y)
{
int xx, yy;
if (!x)
return y ? -1 : 0;
if (!y)
return x ? 1 : 0;
while (*x && *y) {
xx = *x++;
yy = *y++;
if (xx >= 'A' && xx <= 'Z')
xx = xx + 'a' - 'A';
if (yy >= 'A' && yy <= 'Z')
yy = yy + 'a' - 'A';
if (xx < yy)
return -1;
if (xx > yy)
return 1;
}
if (*x)
return 1;
if (*y)
return -1;
return 0;
}
/*!
* \brief Finds the first occurrence of the character C in the
* null-terminated string beginning at mainString
*
* \param[in] mainString string where to find sub-string
* \param[in] subString sub-string
*
* \return a pointer to the first occurrence of subString in
* mainString
* \return NULL if no occurrences are found
*/
char *G_strstr(const char *mainString, const char *subString)
{
const char *p;
const char *q;
int length;
p = subString;
q = mainString;
length = strlen(subString);
do {
while (*q != '\0' && *q != *p) { /* match 1st subString char */
q++;
}
} while (*q != '\0' && strncmp(p, q, length) != 0 && q++);
/* Short-circuit evaluation is your friend */
if (*q == '\0') { /* ran off end of mainString */
return NULL;
}
else {
return (char *)q;
}
}
/*!
* \brief Copies the null-terminated string into a newly
* allocated string. The string is allocated using G_malloc().
*
* \param[in] string the string to duplicate
*
* \return pointer to a string that is a duplicate of the string
* given to G_strdup().
* \return NULL if unable to allocate the required space
*/
char *G_strdup(const char *string)
{
char *p;
p = G_malloc(strlen(string) + 1);
if (p != NULL) {
strcpy(p, string);
}
return p;
}
/*!
* \brief Replace all occurencies of character in string bug with new
*
* \param[in,out] bug base string
* \param[in] character character to replace
* \param[in] new new character
*
* \return bug string
*/
char *G_strchg(char *bug, char character, char new)
{
char *help = bug;
while (*help) {
if (*help == character)
*help = new;
help++;
}
return bug;
}
/*!
* \brief Replace all occurencies of old_str in buffer with new_str
*
* Code example:
* \code
* char *name;
* name = G_str_replace ( inbuf, ".exe", "" );
* ...
* G_free (name);
* \endcode
*
* \param[in,out] buffer main string
* \param[in] old_str string to replace
* \param[in] new_str new string
*
* \return the newly allocated string, input buffer is unchanged
*/
char *G_str_replace(char *buffer, const char *old_str, const char *new_str)
{
char *B, *R;
const char *N;
char *replace;
int count, len;
/* Make sure old_str and new_str are not NULL */
if (old_str == NULL || new_str == NULL)
return G_strdup(buffer);
/* Make sure buffer is not NULL */
if (buffer == NULL)
return NULL;
/* Make sure old_str occurs */
B = strstr(buffer, old_str);
if (B == NULL)
/* return NULL; */
return G_strdup(buffer);
if (strlen(new_str) > strlen(old_str)) {
/* Count occurences of old_str */
count = 0;
len = strlen(old_str);
B = buffer;
while (B != NULL && *B != '\0') {
B = G_strstr(B, old_str);
if (B != NULL) {
B += len;
count++;
}
}
len = count * (strlen(new_str) - strlen(old_str))
+ strlen(buffer);
}
else
len = strlen(buffer);
/* Allocate new replacement */
replace = G_malloc(len + 1);
if (replace == NULL)
return NULL;
/* Replace old_str with new_str */
B = buffer;
R = replace;
len = strlen(old_str);
while (*B != '\0') {
if (*B == old_str[0] && strncmp(B, old_str, len) == 0) {
N = new_str;
while (*N != '\0')
*R++ = *N++;
B += len;
}
else {
*R++ = *B++;
}
}
*R = '\0';
return replace;
}
/*!
* \brief Removes all leading and trailing white space from string.
*
* \param[in,out] buf buffer to be worked on
*
* \return 0
*/
int G_strip(char *buf)
{
register char *a, *b;
/* remove leading white space */
for (a = b = buf; *a == ' ' || *a == '\t'; a++) ;
if (a != b)
while ((*b++ = *a++)) ;
/* remove trailing white space */
for (a = buf; *a; a++) ;
if (a != buf) {
for (a--; *a == ' ' || *a == '\t'; a--) ;
a++;
*a = 0;
}
return 0;
}
/*!
* \brief Chop leading and trailing white spaces:
* \verbatim space, \f, \n, \r, \t, \v \endverbatim
*
* modified copy of G_squeeze(); RB March 2000 <Radim.Blazek@dhv.cz>
*
* \param line buffer to be worked on
*
* \return pointer to string
*/
char *G_chop(char *line)
{
register char *f = line, *t = line;
while (isspace(*f)) /* go to first non white-space char */
f++;
if (!*f) { /* no more chars in string */
*t = '\0';
return (line);
}
for (t = line; *t; t++) /* go to end */
;
while (isspace(*--t)) ;
*++t = '\0'; /* remove trailing white-spaces */
t = line;
while (*f) /* copy */
*t++ = *f++;
*t = '\0';
return (line);
}
/*!
* \brief Convert string to upper case
*
* \param[in,out] str pointer to string
*/
void G_str_to_upper(char *str)
{
int i = 0;
if (!str)
return;
while (str[i]) {
str[i] = toupper(str[i]);
i++;
}
}
/*!
* \brief Convert string to lower case
*
* \param[in,out] str pointer to string
*/
void G_str_to_lower(char *str)
{
int i = 0;
if (!str)
return;
while (str[i]) {
str[i] = tolower(str[i]);
i++;
}
}
/*!
* \brief Make string SQL compliant
*
* \param[in,out] str pointer to string
*
* \return number of changed characters
*/
int G_str_to_sql(char *str)
{
int count;
char *c;
count = 0;
if (!str || !*str)
return 0;
c = str;
while (*c) {
*c = toascii(*c);
if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z') &&
!(*c >= '0' && *c <= '9')) {
*c = '_';
count++;
}
c++;
}
c = str;
if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z')) {
*c = 'x';
count++;
}
return count;
}
|