| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 1999 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: December 1999 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* String manipulation functions */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include <ctype.h> |
| 43 |
#include "cst_alloc.h" |
| 44 |
#include "cst_string.h" |
| 45 |
#include "cst_file.h" |
| 46 |
|
| 47 |
#ifdef UNDER_CE /* WinCE does not fully implement ANSI C */ |
| 48 |
|
| 49 |
cst_string *cst_strrchr(const cst_string *str, int c) |
| 50 |
{ |
| 51 |
cst_string *p = (const cst_string *)str + cst_strlen(str); |
| 52 |
while (p >= str) { |
| 53 |
if (*p == c) |
| 54 |
return p; |
| 55 |
--p; |
| 56 |
} |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
double cst_atof(const char *str) |
| 61 |
{ |
| 62 |
/* double f = 0.0; */ |
| 63 |
|
| 64 |
/* sscanf(str, "%f", &f); */ |
| 65 |
return atof(str); |
| 66 |
} |
| 67 |
|
| 68 |
#else /* Sane operating system */ |
| 69 |
|
| 70 |
cst_string *cst_strrchr(const cst_string *str, int c) |
| 71 |
{ |
| 72 |
return (cst_string *)strrchr((const char *)str, c); |
| 73 |
} |
| 74 |
|
| 75 |
double cst_atof(const char *str) |
| 76 |
{ |
| 77 |
return atof(str); |
| 78 |
} |
| 79 |
#endif /* WinCE */ |
| 80 |
|
| 81 |
cst_string *cst_strdup(const cst_string *str) |
| 82 |
{ |
| 83 |
cst_string *nstr = NULL; |
| 84 |
|
| 85 |
if (str) |
| 86 |
{ |
| 87 |
nstr = cst_alloc(cst_string,cst_strlen((const char *)str)+1); |
| 88 |
memmove(nstr,str,cst_strlen((const char *)str)+1); |
| 89 |
} |
| 90 |
return nstr; |
| 91 |
} |
| 92 |
|
| 93 |
cst_string *cst_strchr(const cst_string *s, int c) |
| 94 |
{ |
| 95 |
return (cst_string *)strchr((const char *)s,c); |
| 96 |
} |
| 97 |
|
| 98 |
char *cst_substr(const char *str,int start, int length) |
| 99 |
{ |
| 100 |
char *nstr = NULL; |
| 101 |
|
| 102 |
if (str) |
| 103 |
{ |
| 104 |
nstr = cst_alloc(char,length+1); |
| 105 |
strncpy(nstr,str+start,length); |
| 106 |
nstr[length] = '\0'; |
| 107 |
} |
| 108 |
return nstr; |
| 109 |
} |
| 110 |
|
| 111 |
char *cst_string_before(const char *s,const char *c) |
| 112 |
{ |
| 113 |
char *p; |
| 114 |
char *q; |
| 115 |
|
| 116 |
p = (char *)cst_strstr(s,c); |
| 117 |
if (p == NULL) |
| 118 |
return NULL; |
| 119 |
q = (char *)cst_strdup((cst_string *)s); |
| 120 |
q[cst_strlen(s)-cst_strlen(p)] = '\0'; |
| 121 |
return q; |
| 122 |
} |
| 123 |
|
| 124 |
cst_string *cst_downcase(const cst_string *str) |
| 125 |
{ |
| 126 |
cst_string *dc; |
| 127 |
int i; |
| 128 |
|
| 129 |
dc = cst_strdup(str); |
| 130 |
for (i=0; str[i] != '\0'; i++) |
| 131 |
{ |
| 132 |
if (isupper((int)str[i])) |
| 133 |
dc[i] = tolower((int)str[i]); |
| 134 |
} |
| 135 |
return dc; |
| 136 |
} |
| 137 |
|
| 138 |
cst_string *cst_upcase(const cst_string *str) |
| 139 |
{ |
| 140 |
cst_string *uc; |
| 141 |
int i; |
| 142 |
|
| 143 |
uc = cst_strdup(str); |
| 144 |
for (i=0; str[i] != '\0'; i++) |
| 145 |
{ |
| 146 |
if (islower((int)str[i])) |
| 147 |
uc[i] = toupper((int)str[i]); |
| 148 |
} |
| 149 |
return uc; |
| 150 |
} |
| 151 |
|
| 152 |
int cst_member_string(const char *str, const char * const *slist) |
| 153 |
{ |
| 154 |
const char * const *p; |
| 155 |
|
| 156 |
for (p = slist; *p; ++p) |
| 157 |
if (cst_streq(*p, str)) |
| 158 |
break; |
| 159 |
|
| 160 |
return *p != NULL; |
| 161 |
} |
| 162 |
|
| 163 |
char *cst_strcat(const char *a, const char *b) |
| 164 |
{ |
| 165 |
char *r; |
| 166 |
|
| 167 |
r = cst_alloc(char,cst_strlen(a)+cst_strlen(b)+1); |
| 168 |
cst_sprintf(r,"%s%s",a,b); |
| 169 |
return r; |
| 170 |
} |
| 171 |
|
| 172 |
char *cst_strcat3(const char *a, const char *b, const char *c) |
| 173 |
{ |
| 174 |
char *r; |
| 175 |
|
| 176 |
r = cst_alloc(char,cst_strlen(a)+cst_strlen(b)+cst_strlen(c)+1); |
| 177 |
cst_sprintf(r,"%s%s%s",a,b,c); |
| 178 |
return r; |
| 179 |
} |