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
|
/*
* string/chring movement functions
*
** G_strcpy (T, F)
** G_strncpy (T, F, n) copy F up to null or n, always copy null
** G_chrcpy (T, F, n)
** G_strmov (T, F)
** G_chrmov (T, F, n)
** G_strcat (T, F)
** G_chrcat (T, F, n)
** char *T, *F;
** int n;
*
* G_strcpy (T, F) copy F up to null, copy null
* G_chrcpy (T, F, n) copy F up to n, copy null
*
* G_strmov (T, F) copy F up to null
* G_chrmov (T, F, n) copy F up to n
*
* G_strcat (T, F) cat F up to null, copy null
* G_chrcat (T, F, n) cat F up to n, copy null
*
* the -cpy and -cat functions are for null-terminated destinations;
* the -mov functions are for non-null-terminated ('chring') destinations.
* all functions return 'T'.
*
* Author Dave Gerdes (USACERL)
*
*
* G_strcasecmp(a, b) char *a, *b;
* string compare ignoring case (upper or lower)
* returns: -1 a<b; 0 a==b; 1 a>b
*
* Author Michael Shapiro (USACERL)
*
*
* G_strstr(mainString, subString)
* Return a pointer to the first occurrence of subString
* in mainString, or NULL if no occurrences are found.
* G_strdup(string)
* Return a pointer to a string that is a duplicate of the string
* given to G_strdup. The duplicate is created using malloc.
* If unable to allocate the required space, NULL is returned.
*
* Author: Amit Parghi (USACERL), 1993 02 23
*
* G_strchg(char* bug, char character, char new) {
* replace all occurencies of character in string(inplace) with new
*
* Author: Bernhard Reiter (Intevation GmbH, Germany)
*
* char * G_str_replace(char* buffer, char* old_str, char* new_str)
* Replace all occurencies of old_str in buffer with new_str
* Author Beverly Wallace (LMCO)
*/
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include "gis.h"
#ifndef NULL
#define NULL 0
#endif
static char *G_strend (char *S)
{
while (*S)
S++;
return (S);
}
char *G_strcpy (char *T, const char *F)
{
char *d = T;
while ((*d++ = *F++))
;
return (T);
}
char *G_chrcpy (char *T, const char *F, int n)
{
char *d = T;
while (n--)
*d++ = *F++;
*d = '\0';
return (T);
}
char *G_strncpy (char *T, const char *F, int n)
{
char *d = T;
while (n-- && *F)
*d++ = *F++;
*d = '\0';
return (T);
}
char *G_strmov (char *T, const char *F)
{
char *d = T;
while (*F)
*d++ = *F++;
return (T);
}
char *G_chrmov (char *T, const char *F, int n)
{
char *d = T;
while (n--)
*d++ = *F++;
return (T);
}
char *G_strcat (char *T, const char *F)
{
G_strcpy (G_strend (T), F);
return (T);
}
char *G_chrcat (char *T, const char *F, int n)
{
G_chrcpy (G_strend (T), F, n);
return (T);
}
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;
}
char *G_strstr(char *mainString, const char *subString)
{
const char *p;
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 q;
}
}
char *G_strdup(const char *string)
{
char *p;
p = malloc(strlen(string) + 1);
if (p != NULL) {
strcpy(p, string);
}
return p;
}
char *G_strchg(char* bug, char character, char new) {
/* replace all occurencies of "character" in string(bug) with
* "new", returns new string */
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.
\return Returns the newly allocated string, input buffer is unchanged
Author Beverly Wallace (LMCO) 3/11/04, slightly modified RB/MN
Code example:
\verbatim
char *name;
name = G_str_replace ( inbuf, ".exe", "" );
...
free(name);
\endverbatim
--------------------------------------------------------------------*/
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;
}
|