00001
00002
00003
00004
00005
00006
00007
00008 #include "strutils.h"
00009 #include <ctype.h>
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <time.h>
00013
00014
00015 char * terminate_string( char * string, char c )
00016
00017
00018
00019
00020 {
00021 char * p;
00022
00023 if( string == NULL ) {
00024 return( NULL );
00025 }
00026
00027 p = string + strlen( string ) - 1;
00028 while( *p == '\r' || *p == '\n' ) {
00029 p--;
00030 }
00031 *(++p) = c;
00032 *(++p) = 0;
00033
00034 return( string );
00035 }
00036
00037 char * trim_string( char * string )
00038
00039
00040
00041 {
00042 char * p;
00043 char * q;
00044
00045 if( string == NULL )
00046 return( NULL );
00047
00048 p = string;
00049 q = string + strlen(string) - 1;
00050
00051 while( q >= p && isspace( *q ) )
00052 *(q--) = 0;
00053 while( isspace( *p ) )
00054 p++;
00055
00056 return( p );
00057 }
00058
00059 char * trim_string( char * string, char c )
00060
00061
00062 {
00063 char * p;
00064
00065 if ( string == NULL )
00066 return( NULL );
00067
00068 p = string;
00069
00070 while( *p != 0 && *p != c )
00071 p++;
00072
00073 while( *p )
00074 *(p++) = 0;
00075
00076 return( string );
00077 }
00078
00079 void replace_char( void * _string, char c1, char c2, int length )
00080
00081
00082
00083 {
00084 char *string = (char *)_string;
00085 for( int i=0; i < length; i++ )
00086 if( *(string+i) == c1 )
00087 *(string+i) = c2;
00088 }
00089
00090 char * strlwr( char * string )
00091
00092 {
00093 char * p = string;
00094 while( *p ) {
00095 *p = tolower( *p );
00096 p++;
00097 }
00098
00099 return( string );
00100 }
00101
00102 char * strupr( char * string)
00103
00104 {
00105 char *p = string;
00106 while ( *p )
00107 {
00108 *p = toupper( *p);
00109 p++;
00110 }
00111
00112 return( string );
00113 }
00114
00115
00116
00117 bool is_word(const char *p)
00118 {
00119 while (*p)
00120 {
00121 if(!isalnum(*p++))
00122 return false;
00123 }
00124
00125 return true;
00126 }
00127
00128
00129
00130
00131
00132 WvString hexdump_buffer(const void *_buf, size_t len)
00133 {
00134 const unsigned char *buf = (const unsigned char *)_buf;
00135 size_t count, count2, top;
00136 WvString out;
00137
00138 out.setsize(len / 16 * 80 + 80);
00139 char *cptr = out.edit();
00140
00141 for (count = 0; count < len; count+=16)
00142 {
00143 top = len-count < 16 ? len-count : 16;
00144 cptr += sprintf(cptr, "[%03X] ", (unsigned int)count);
00145
00146
00147 for (count2 = 0; count2 < top; count2++)
00148 {
00149 if (count2 && !(count2 % 4))
00150 *cptr++ = ' ';
00151 cptr += sprintf(cptr, "%02X", buf[count+count2]);
00152 }
00153
00154
00155 for (count2 = top; count2 < 16; count2++)
00156 {
00157 if (count2 && !(count2 % 4))
00158 {
00159 strcat(cptr, " ");
00160 cptr += 3;
00161 }
00162 else
00163 {
00164 strcat(cptr, " ");
00165 cptr += 2;
00166 }
00167 }
00168
00169 *cptr++ = ' ';
00170
00171
00172 for (count2 = 0; count2 < top; count2++)
00173 cptr += sprintf(cptr, "%c",
00174 (isprint(buf[count+count2])
00175 ? buf[count+count2] : '.'));
00176 *cptr++ = '\n';
00177 }
00178 *cptr = 0;
00179 return out;
00180 }
00181
00182
00183
00184 bool isnewline(char c)
00185 {
00186 return c=='\n' || c=='\r';
00187 }
00188
00189
00190
00191 void hexify(char *obuf, const void *_ibuf, size_t len)
00192 {
00193 const unsigned char *ibuf = (const unsigned char *)_ibuf;
00194
00195 while (len > 0)
00196 {
00197 sprintf(obuf, "%02x", *ibuf++);
00198 obuf += 2;
00199 len--;
00200 }
00201 }
00202
00203
00204
00205 void unhexify(void *_obuf, const char *ibuf)
00206 {
00207 unsigned char *obuf = (unsigned char *)_obuf;
00208 char lookup[] = "0123456789abcdef", *c, *c2;
00209
00210 if (strlen(ibuf) % 1)
00211 return;
00212
00213 while (*ibuf != 0)
00214 {
00215 c = strchr(lookup, *ibuf);
00216 c2 = strchr(lookup, *(ibuf+1));
00217 if (!c || !c2) return;
00218
00219 *obuf++ = ((c - lookup) << 4) | (c2 - lookup);
00220 ibuf += 2;
00221 }
00222 }
00223
00224
00225
00226 WvString web_unescape(const char *str)
00227 {
00228 const char *iptr;
00229 char *optr;
00230 char *idx1, *idx2;
00231 static char hex[] = "0123456789ABCDEF";
00232 WvString in, intmp(str), out;
00233
00234 in = trim_string(intmp.edit());
00235 out.setsize(strlen(in) + 1);
00236
00237 optr = out.edit();
00238 for (iptr = in, optr = out.edit(); *iptr; iptr++)
00239 {
00240 if (*iptr == '+')
00241 *optr++ = ' ';
00242 else if (*iptr == '%' && iptr[1] && iptr[2])
00243 {
00244 idx1 = strchr(hex, toupper((unsigned char) iptr[1]));
00245 idx2 = strchr(hex, toupper((unsigned char) iptr[2]));
00246
00247 if (idx1 && idx2)
00248 *optr++ = ((idx1 - hex) << 4) | (idx2 - hex);
00249
00250 iptr += 2;
00251 }
00252 else
00253 *optr++ = *iptr;
00254 }
00255
00256 *optr = 0;
00257
00258 return out;
00259 }
00260
00261
00262 WvString rfc822_date(time_t when)
00263 {
00264 WvString out;
00265 out.setsize(80);
00266
00267 if (when < 0)
00268 when = time(NULL);
00269
00270 struct tm *tmwhen = localtime(&when);
00271 strftime(out.edit(), 80, "%a, %d %b %Y %H:%M:%S %z", tmwhen);
00272
00273 return out;
00274 }
00275
00276
00277 WvString backslash_escape(const WvString &s1)
00278 {
00279
00280 if (!s1)
00281 return "";
00282
00283 WvString s2;
00284 s2.setsize(s1.len() * 2 + 1);
00285
00286 const char *p1 = s1;
00287 char *p2 = s2.edit();
00288 memset(p2, '\0', s2.len());
00289 while (*p1)
00290 {
00291 if (!isalnum(*p1))
00292 *p2++ = '\\';
00293 *p2++ = *p1++;
00294 }
00295
00296 return s2;
00297 }
00298
00299
00300
00301 int strcount(const WvString &s, const char c)
00302 {
00303 int n=0;
00304 const char *p = s;
00305 while ((p=strchr(p, c)) != NULL && p++)
00306 n++;
00307
00308 return n;
00309 }
00310
00311 WvString encode_hostname_as_DN(WvString &hostname)
00312 {
00313 WvString dn("cn=%s,",hostname);
00314
00315 WvStringList fqdnlist;
00316 WvStringList::Iter i(fqdnlist);
00317 fqdnlist.split(hostname,".");
00318 for (i.rewind();i.next();)
00319 {
00320 dn.append("dc=");
00321 dn.append(*i);
00322 dn.append(",");
00323 }
00324 char *ptr = dn.edit() + strlen(dn) - 1;
00325 *ptr = '\0';
00326
00327 return dn.unique();
00328 }
00329