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
|
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>
*/
/*
** Strings.c: various routines for dealing with strings
*/
#include "config.h"
#include <ctype.h>
#include "safemalloc.h"
#include "Strings.h"
void CopyString(char **dest, const char *source)
{
int len;
const char *start;
if (source == NULL)
{
*dest = NULL;
return;
}
/* set 'start' to the first character of the string,
skipping over spaces, but not newlines
(newline terminates the string) */
while ( isspace((unsigned char)*source) && (*source != '\n') )
{
source++;
}
start = source;
/* set 'len' to the length of the string, ignoring
trailing spaces */
len = 0;
while ( (*source != '\n') && (*source != 0) )
{
len++;
source++;
}
source--;
while( len > 0 && isspace((unsigned char)*source) )
{
len--;
source--;
}
/* TA: FIXME! xasprintf() */
*dest = fxmalloc(len+1);
strncpy(*dest,start,len);
(*dest)[len]=0;
}
void CopyStringWithQuotes(char **dest, const char *src)
{
while (src && src[0] == ' ')
{
src++;
}
if (src && src[0] == '"')
{
int len;
src++;
CopyString(dest, src);
len = strlen(*dest);
if (len > 0 && (*dest)[len - 1] == '"')
{
(*dest)[len - 1] = '\0';
}
}
else
{
CopyString(dest, src);
}
}
/*
*
* Copies a string into a new, malloc'ed string
* Strips leading spaces and trailing spaces and new lines
*
*/
char *stripcpy( const char *source )
{
const char* tmp;
char* ptr;
int len;
if(source == NULL)
{
return NULL;
}
while(isspace((unsigned char)*source))
{
source++;
}
len = strlen(source);
tmp = source + len -1;
while( (tmp >= source) && ((isspace((unsigned char)*tmp)) ||
(*tmp == '\n')) )
{
tmp--;
len--;
}
/* TA: FIXME! xasprintf() */
ptr = fxmalloc(len+1);
if (len)
{
strncpy(ptr,source,len);
}
ptr[len]=0;
return ptr;
}
int StrEquals( const char *s1, const char *s2 )
{
if (s1 == NULL && s2 == NULL)
{
return 1;
}
if (s1 == NULL || s2 == NULL)
{
return 0;
}
return strcasecmp(s1,s2) == 0;
}
int StrHasPrefix( const char* string, const char* prefix )
{
if ( prefix == NULL )
{
return 1;
}
if ( string == NULL )
{
return 0;
}
return strncasecmp( string, prefix, strlen(prefix) ) == 0;
}
/*
*
* Adds single quotes arround the string and escapes single quotes with
* backslashes. The result is placed in the given dest, not allocated.
* The end of destination, i.e. pointer to '\0' is returned.
* You should allocate dest yourself, at least strlen(source) * 2 + 3.
*
*/
char *QuoteString(char *dest, const char *source)
{
int i = 0;
*dest++ = '\'';
for(i = 0; source[i]; i++)
{
if (source[i] == '\'')
{
*dest++ = '\\';
}
*dest++ = source[i];
}
*dest++ = '\'';
*dest = '\0';
return dest;
}
/*
* Adds delim around the source and escapes all characters in escape with
* the corresponding escaper. The dest string must be preallocated.
* delim should be included in escape with a proper escaper.
* Returns a pointer to the end of dest.
*/
char *QuoteEscapeString(char *dest, const char *source, char delim,
const char *escape, const char *escaper)
{
*dest++ = delim;
while (*source)
{
char *esc;
esc = strchr(escape, *source);
if (esc != NULL)
{
*dest++ = escaper[(int)(esc-escape)];
}
*dest++ = *source++;
}
*dest++ = delim;
*dest = '\0';
return dest;
}
/*
* Calculates the lenght needed by a escaped by QuoteEscapeString
* the corresponding escaper.
*/
unsigned int QuoteEscapeStringLength(const char *source, const char *escape)
{
unsigned int len = 2;
while (*source)
{
if (strchr(escape, *source) != NULL)
{
len++;
}
len++;
source++;
}
return len;
}
|