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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*/
#include "wvbackslash.h"
#include "wvbuf.h"
#include "wvstream.h"
#include "wvstring.h"
#include "wvstringmask.h"
#include "wvtclstring.h"
#include <climits>
const WvStringMask WVTCL_NASTY_SPACES(WVTCL_NASTY_SPACES_STR);
const WvStringMask WVTCL_NASTY_NEWLINES(WVTCL_NASTY_NEWLINES_STR);
const WvStringMask WVTCL_SPLITCHARS(WVTCL_SPLITCHARS_STR);
static size_t wvtcl_escape(char *dst, const char *s, size_t s_len,
const WvStringMask &nasties, bool *verbatim = NULL)
{
if (verbatim) *verbatim = false;
// NULL strings remain such
if (s == NULL)
return 0;
// empty strings are just {}
if (s_len == 0)
{
if (dst)
{
dst[0] = '{';
dst[1] = '}';
}
return 2;
}
bool backslashify = false, inescape = false;
int len = 0, unprintables = 0, bracecount = 0;
const char *cptr, *cptr_end = s + s_len;
// figure out which method we need to use: backslashify or embrace.
// also count the number of unprintable characters we'll need to
// backslashify, if it turns out that's necessary.
for (cptr = s; cptr != cptr_end; cptr++)
{
// Assume we do nothing
if (dst) dst[len] = *cptr;
++len;
if (!inescape && *cptr == '{')
bracecount++;
else if (!inescape && *cptr == '}')
bracecount--;
if (bracecount < 0)
backslashify = true;
bool doit = false;
switch (*cptr)
{
case WVTCL_ALWAYS_NASTY_CASE:
doit = true;
break;
default:
if (nasties[*cptr])
doit = true;
}
if (doit)
unprintables++;
if (*cptr == '\\')
inescape = !inescape;
else
inescape = false;
}
// if the braces aren't balanced, backslashify
if (bracecount != 0 || inescape)
backslashify = true;
if (!backslashify && !unprintables)
{
if (verbatim) *verbatim = true;
return len; // no work needed!
}
if (backslashify)
{
if (dst)
{
len = 0;
for (cptr = s; cptr != cptr_end; ++cptr)
{
bool doit = false;
switch (*cptr)
{
case WVTCL_ALWAYS_NASTY_CASE:
doit = true;
break;
default:
if (nasties[*cptr])
doit = true;
}
if (doit)
dst[len++] = '\\';
dst[len++] = *cptr;
}
return len;
}
else return len+unprintables;
}
else
{
// the embrace method: just take the string and put braces around it
if (dst)
{
len = 0;
dst[len++] = '{';
for (cptr = s; cptr != cptr_end; ++cptr)
dst[len++] = *cptr;
dst[len++] = '}';
return len;
}
else return len+2;
}
}
WvString wvtcl_escape(WvStringParm s, const WvStringMask &nasties)
{
size_t s_len = s.len();
bool verbatim;
size_t len = wvtcl_escape(NULL, s, s_len, nasties, &verbatim);
if (verbatim) return s;
WvString result;
result.setsize(len);
char *e = result.edit();
e += wvtcl_escape(e, s, s_len, nasties);
*e = '\0';
return result;
}
static size_t wvtcl_unescape(char *dst, const char *s, size_t s_len,
bool *verbatim = NULL)
{
//printf(" unescape '%s'\n", (const char *)s);
// empty or NULL strings remain themselves
if (!s)
{
if (verbatim) *verbatim = true;
return 0;
}
if (verbatim) *verbatim = false;
// deal with embraced strings by simply removing the braces
if (s[0] == '{' && s[s_len-1] == '}')
{
if (dst) memcpy(dst, &s[1], s_len-2);
return s_len - 2;
}
bool skipquotes = false;
// deal with quoted strings by ignoring the quotes _and_ unbackslashifying.
if (s[0] == '"' && s[s_len-1] == '"')
skipquotes = true;
// otherwise, unbackslashify it.
const char *start = s, *end = &s[s_len];
if (skipquotes)
{
++start;
--end;
}
size_t len = 0;
bool inescape = false;
for (; start != end; ++start)
{
if (*start == '\\')
{
if (inescape)
{
if (dst) dst[len] = *start;
len++;
inescape = false;
}
else
inescape = true;
}
else
{
inescape = false;
if (dst) dst[len] = *start;
len++;
}
}
return len;
}
WvString wvtcl_unescape(WvStringParm s)
{
size_t s_len = s.len();
bool verbatim;
size_t len = wvtcl_unescape(NULL, s, s_len, &verbatim);
if (verbatim) return s;
WvString result;
result.setsize(len+1);
char *e = result.edit();
e += wvtcl_unescape(e, s, s_len);
*e = '\0';
return result;
}
WvString wvtcl_encode(WvList<WvString> &l, const WvStringMask &nasties,
const WvStringMask &splitchars)
{
int size = 0;
WvList<WvString>::Iter i(l);
int count = 0;
for (i.rewind(); i.next(); )
{
size += wvtcl_escape(NULL, *i, i->len(), nasties);
++count;
}
WvString result;
result.setsize(size+(count-1)+1);
char *p = result.edit();
int j;
for (i.rewind(), j=0; i.next(); ++j)
{
p += wvtcl_escape(p, *i, i->len(), nasties);
if (j < count - 1)
*p++ = splitchars.first();
}
*p = '\0';
return result;
}
const size_t WVTCL_GETWORD_NONE (UINT_MAX);
static size_t wvtcl_getword(char *dst, const char *s, size_t s_len,
const WvStringMask &splitchars,
bool do_unescape, size_t *end = NULL)
{
//printf(" used=%d\n", origsize);
if (!s_len) return WVTCL_GETWORD_NONE;
bool inescape = false, inquote = false, incontinuation = false;
int bracecount = 0;
const char *origend = s + s_len;
const char *sptr, *eptr;
// skip leading separators
for (sptr = s; sptr != origend; sptr++)
{
if (!splitchars[*sptr])
break;
}
if (sptr == origend) // nothing left
return WVTCL_GETWORD_NONE;
// detect initial quote
if (*sptr == '"')
{
inquote = true;
eptr = sptr+1;
}
else
eptr = sptr;
// loop over string until something satisfactory is found
for (; eptr != origend; eptr++)
{
char ch = *eptr;
incontinuation = false;
if (inescape)
{
if (ch == '\n')
{
// technically we've finished the line-continuation
// sequence, but we require at least one more character
// in order to prove that there's a next line somewhere
// in the buffer. Otherwise we might stop parsing before
// we're "really" done if we're given input line-by-line.
//
// A better way to do this would be for getword() to *never*
// return a string unless it contains a separator character;
// then we wouldn't need this weird special case. But it
// don't work like that; we'll return the last word in the
// buffer even if it *doesn't* end in a separator character.
incontinuation = true;
}
inescape = false;
}
else if (ch == '\\')
{
inescape = true;
// now we need a character to complete the escape
}
else // not an escape sequence
{
// detect end of a quoted/unquoted string
if (bracecount == 0)
{
if (inquote)
{
if (ch == '"')
{
eptr++;
break;
}
}
else if (splitchars[ch])
break;
}
// match braces
if (!inquote)
{
if (ch == '{')
bracecount++;
else if (bracecount > 0 && ch == '}')
bracecount--;
}
}
}
if (bracecount || sptr==eptr || inquote || inescape || incontinuation)
// not there yet...
return WVTCL_GETWORD_NONE;
//printf("len=%d, unget=%d\n", eptr - sptr, origend - eptr);
if (end) *end = eptr - s;
if (do_unescape)
return wvtcl_unescape(dst, sptr, eptr-sptr);
else
{
if (dst) memcpy(dst, sptr, eptr-sptr);
return eptr - sptr;
}
}
WvString wvtcl_getword(WvBuf &buf, const WvStringMask &splitchars,
bool do_unescape)
{
int origsize = buf.used();
const char *origptr = (const char *)buf.get(origsize);
size_t end;
size_t len = wvtcl_getword(NULL, origptr, origsize,
splitchars, do_unescape, &end);
if (len == WVTCL_GETWORD_NONE)
{
buf.unget(origsize);
return WvString::null;
}
WvString result;
result.setsize(len+1);
char *e = result.edit();
e += wvtcl_getword(e, origptr, origsize, splitchars, do_unescape);
*e = '\0';
buf.unget(origsize - end);
return result;
}
void wvtcl_decode(WvList<WvString> &l, WvStringParm _s,
const WvStringMask &splitchars, bool do_unescape)
{
const char *s = _s;
size_t s_len = _s.len();
for (;;)
{
size_t end;
size_t len = wvtcl_getword(NULL, s, s_len,
splitchars, do_unescape, &end);
if (len == WVTCL_GETWORD_NONE)
break;
WvString *word = new WvString();
word->setsize(len+1);
char *e = word->edit();
e += wvtcl_getword(e, s, s_len, splitchars, do_unescape);
*e = '\0';
l.append(word, true);
s += end;
s_len -= end;
}
}
|