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
|
#include "string.h"
#include "strparse.h"
#include "ctype.h"
void String_parse::skip_space()
{
while (string[pos] && isspace(string[pos])) {
pos = pos + 1;
}
}
char String_parse::peek()
{
return string[pos];
}
void String_parse::get_nonspace_quoted(char *field)
{
skip_space();
bool quoted = false;
if (string[pos] == '"') {
quoted = true;
*field++ = '"';
pos = pos + 1;
}
while (string[pos] && (quoted || !isspace(string[pos]))) {
if (string[pos] == '"') {
if (quoted) {
*field++ = '"';
pos = pos + 1;
}
*field = 0;
return;
}
if (string[pos] == '\\') {
pos = pos + 1;
}
if (string[pos]) {
*field++ = string[pos];
pos = pos + 1;
}
}
*field = 0;
}
char *escape_chars[] = {"\\n", "\\t", "\\\\", "\\r", "\\\""};
void string_escape(char *result, char *str, char *quote)
{
int length = (int) strlen(str);
if (quote[0]) {
*result++ = quote[0];
}
for (int i = 0; i < length; i++) {
if (!isalnum(str[i])) {
char *chars = "\n\t\\\r\"";
char *special = strchr(chars, str[i]);
if (special) {
*result++ = escape_chars[special - chars][0];
*result++ = escape_chars[special - chars][1];
} else {
*result++ = str[i];
}
} else {
*result++ = str[i] ;
}
}
*result++ = quote[0];
*result = 0;
}
|