217 lines
4.4 KiB
C
217 lines
4.4 KiB
C
/*
|
|
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to
|
|
* deal in the Software without restriction, including without limitation the
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies of the Software, its documentation and marketing & publicity
|
|
* materials, and acknowledgment shall be given in the documentation, materials
|
|
* and software packages that this Software was used.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "parse.h"
|
|
#include "util.h"
|
|
|
|
const char *
|
|
atword(const char *s, int num)
|
|
{
|
|
int cnt, i;
|
|
|
|
if (!s)
|
|
return NULL;
|
|
cnt = 0;
|
|
i = 0;
|
|
|
|
while (s[i])
|
|
{
|
|
if ((s[i] != ' ') && (s[i] != '\t'))
|
|
{
|
|
if (i == 0)
|
|
cnt++;
|
|
else if ((s[i - 1] == ' ') || (s[i - 1] == '\t'))
|
|
cnt++;
|
|
if (cnt == num)
|
|
return &s[i];
|
|
}
|
|
i++;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
void
|
|
word(const char *s, int num, char *wd)
|
|
{
|
|
int cnt, i;
|
|
const char *start, *finish, *ss;
|
|
|
|
if (!s)
|
|
return;
|
|
if (!wd)
|
|
return;
|
|
if (num <= 0)
|
|
{
|
|
*wd = 0;
|
|
return;
|
|
}
|
|
cnt = 0;
|
|
i = 0;
|
|
start = NULL;
|
|
finish = NULL;
|
|
ss = NULL;
|
|
|
|
while (s[i])
|
|
{
|
|
if ((cnt == num) && ((s[i] == ' ') || (s[i] == '\t')))
|
|
{
|
|
finish = &s[i];
|
|
break;
|
|
}
|
|
if ((s[i] != ' ') && (s[i] != '\t'))
|
|
{
|
|
if (i == 0)
|
|
{
|
|
cnt++;
|
|
if (cnt == num)
|
|
start = &s[i];
|
|
}
|
|
else if ((s[i - 1] == ' ') || (s[i - 1] == '\t'))
|
|
{
|
|
cnt++;
|
|
if (cnt == num)
|
|
start = &s[i];
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
if (cnt == num)
|
|
{
|
|
if ((start) && (finish))
|
|
{
|
|
for (ss = start; ss < finish; ss++)
|
|
*wd++ = *ss;
|
|
}
|
|
else if (start)
|
|
{
|
|
for (ss = start; *ss != 0; ss++)
|
|
*wd++ = *ss;
|
|
}
|
|
*wd = 0;
|
|
}
|
|
}
|
|
|
|
/* gets word number [num] in the string [s] and copies it into [wd] */
|
|
/* wd is NULL terminated. If word [num] does not exist wd = "" */
|
|
/* NB: this function now handles quotes so for a line: */
|
|
/* Hello to "Welcome sir - may I Help" Shub Foo */
|
|
/* Word 1 = Hello */
|
|
/* Word 2 = to */
|
|
/* Word 3 = Welcome sir - may I Help */
|
|
/* Word 4 = Shub */
|
|
/* word 5 = Foo */
|
|
void
|
|
fword(char *s, int num, char *wd)
|
|
{
|
|
char *cur, *start, *end;
|
|
int count, inword, inquote, len;
|
|
|
|
if (!s)
|
|
return;
|
|
if (!wd)
|
|
return;
|
|
*wd = 0;
|
|
if (num <= 0)
|
|
return;
|
|
cur = s;
|
|
count = 0;
|
|
inword = 0;
|
|
inquote = 0;
|
|
start = NULL;
|
|
end = NULL;
|
|
while ((*cur) && (count < num))
|
|
{
|
|
if (inword)
|
|
{
|
|
if (inquote)
|
|
{
|
|
if (*cur == '"')
|
|
{
|
|
inquote = 0;
|
|
inword = 0;
|
|
end = cur;
|
|
count++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (isspace(*cur))
|
|
{
|
|
end = cur;
|
|
inword = 0;
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!isspace(*cur))
|
|
{
|
|
if (*cur == '"')
|
|
{
|
|
inquote = 1;
|
|
start = cur + 1;
|
|
}
|
|
else
|
|
start = cur;
|
|
inword = 1;
|
|
}
|
|
}
|
|
if (count == num)
|
|
break;
|
|
cur++;
|
|
}
|
|
if (!start)
|
|
return;
|
|
if (!end)
|
|
end = cur;
|
|
if (end <= start)
|
|
return;
|
|
len = (int)(end - start);
|
|
if (len > 4000)
|
|
len = 4000;
|
|
if (len > 0)
|
|
{
|
|
strncpy(wd, start, len);
|
|
wd[len] = 0;
|
|
}
|
|
}
|
|
|
|
char *
|
|
field(char *s, int fieldno)
|
|
{
|
|
char buf[4096];
|
|
|
|
buf[0] = 0;
|
|
fword(s, fieldno + 1, buf);
|
|
if (buf[0])
|
|
{
|
|
if ((!strcmp(buf, "NULL")) || (!strcmp(buf, "(null)")))
|
|
return NULL;
|
|
return Estrdup(buf);
|
|
}
|
|
return NULL;
|
|
}
|