00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __STRUTILS_H
00011 #define __STRUTILS_H
00012
00013 #include <sys/types.h>
00014 #include <time.h>
00015 #include "wvstring.h"
00016 #include "wvstringlist.h"
00017 #include "wvhex.h"
00018
00019 #ifdef _WIN32
00020 #define strncasecmp _strnicmp
00021 #define strcasecmp _stricmp
00022 #endif
00023
00024
00025
00026
00027
00028
00029
00030
00031 char *terminate_string(char *string, char c);
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 char *trim_string(char *string);
00042
00043
00044
00045
00046
00047 char *trim_string(char *string, char c);
00048
00049
00050
00051
00052
00053 char *non_breaking(char *string);
00054
00055
00056
00057
00058
00059 void replace_char(void *string, char c1, char c2, int length);
00060
00061
00062
00063
00064 char *snip_string(char *haystack, char *needle);
00065
00066 #ifndef _WIN32
00067
00068
00069
00070
00071 char *strlwr(char *string);
00072
00073
00074
00075
00076
00077 char *strupr(char *string);
00078
00079 #endif
00080
00081
00082 bool is_word(const char *string);
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 WvString hexdump_buffer(const void *buf, size_t len, bool charRep = true);
00093
00094
00095
00096
00097
00098 bool isnewline(char c);
00099
00100
00101
00102
00103
00104 WvString web_unescape(const char *str);
00105
00106
00107
00108
00109
00110
00111 WvString url_encode(WvStringParm stuff);
00112
00113
00114
00115
00116
00117
00118 WvString rfc822_date(time_t _when = -1);
00119
00120
00121 WvString rfc1123_date(time_t _when);
00122
00123
00124
00125
00126
00127 WvString passwd_crypt(const char *str);
00128
00129
00130
00131
00132
00133 WvString backslash_escape(WvStringParm s1);
00134
00135
00136 int strcount(WvStringParm s, const char c);
00137
00138
00139
00140
00141
00142 WvString encode_hostname_as_DN(WvStringParm hostname);
00143
00144
00145
00146
00147
00148
00149
00150 WvString nice_hostname(WvStringParm name);
00151
00152
00153
00154
00155
00156
00157 WvString getfilename(WvStringParm fullname);
00158 WvString getdirname(WvStringParm fullname);
00159
00160
00161
00162
00163
00164 WvString sizetoa(long long blocks, int blocksize=1);
00165
00166
00167
00168
00169
00170 int lookup(const char *str, const char * const *table,
00171 bool case_sensitive = false);
00172
00173
00174
00175
00176
00177
00178
00179
00180 template<class StringCollection>
00181 void strcoll_split(StringCollection &coll, WvStringParm _s,
00182 const char *splitchars = " \t", int limit = 0)
00183 {
00184 WvString s(_s);
00185 char *sptr = s.edit(), *eptr, oldc;
00186
00187 while (sptr && *sptr)
00188 {
00189 --limit;
00190 if (limit)
00191 {
00192 sptr += strspn(sptr, splitchars);
00193 eptr = sptr + strcspn(sptr, splitchars);
00194 }
00195 else
00196 {
00197 sptr += strspn(sptr, splitchars);
00198 eptr = sptr + strlen(sptr);
00199 }
00200
00201 oldc = *eptr;
00202 *eptr = 0;
00203
00204 WvString *newstr = new WvString(sptr);
00205 coll.add(newstr, true);
00206
00207 *eptr = oldc;
00208 sptr = eptr;
00209 }
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 template<class StringCollection>
00226 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s,
00227 const char *splitchars = " \t", int limit = 0)
00228 {
00229 WvString s(_s);
00230 char *sptr = s.edit(), *eptr, oldc;
00231
00232 bool start = true;
00233 while (sptr && *sptr)
00234 {
00235 int len = strspn(sptr,splitchars);
00236 sptr += len;
00237
00238 --limit;
00239
00240 for (bool unseen = true; len > 0 && limit; (len -= strlen(splitchars)),--limit)
00241 {
00242 if ((!start) && (unseen))
00243 { unseen = false; continue; }
00244
00245 coll.add(new WvString(""), true);
00246 }
00247
00248 start = false;
00249
00250 if (limit)
00251 eptr = sptr + strcspn(sptr,splitchars);
00252 else
00253 eptr = sptr + strlen(sptr);
00254
00255 oldc = *eptr;
00256 *eptr = '\0';
00257
00258 if (limit)
00259 coll.add(new WvString(sptr), true);
00260
00261 *eptr = oldc;
00262 sptr = eptr;
00263 }
00264 }
00265
00266
00267
00268
00269
00270
00271 template<class StringCollection>
00272 WvString strcoll_join(const StringCollection &coll,
00273 const char *joinchars = " \t")
00274 {
00275 size_t joinlen = strlen(joinchars);
00276 size_t totlen = 1;
00277 typename StringCollection::Iter s(
00278 const_cast<StringCollection&>(coll));
00279 for (s.rewind(); s.next(); )
00280 {
00281 if (s->cstr())
00282 totlen += strlen(s->cstr());
00283 totlen += joinlen;
00284 }
00285 totlen -= joinlen;
00286
00287 WvString total;
00288 total.setsize(totlen);
00289
00290 char *te = total.edit();
00291 te[0] = 0;
00292 bool first = true;
00293 for (s.rewind(); s.next(); )
00294 {
00295 if (first)
00296 first = false;
00297 else
00298 strcat(te, joinchars);
00299 if (s->cstr())
00300 strcat(te, s->cstr());
00301 }
00302 return total;
00303 }
00304
00305
00306
00307
00308
00309 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b);
00310
00311
00312
00313
00314 WvString undupe(WvStringParm s, char c);
00315
00316 WvString hostname();
00317
00318 WvString fqdomainname();
00319
00320
00321
00322
00323
00324 WvString metriculate(const off_t i);
00325
00326 #endif // __STRUTILS_H