📄 sflstr.c
字号:
{
ilen = getstrfldlen (strpattern, ifld, 0, ",");
if (!ilen)
break;
strpat = (char *)malloc (ilen + 1);
getstrfld (strpattern, ifld, 0, ",", strpat);
ifld++;
ilen = getstrfldlen (strpat, 0, 0, "|");
strsrch = (char *)malloc (ilen + 1);
getstrfld (strpat, 0, 0, "|", strsrch);
ilen = getstrfldlen (strpat, 1, 0, "|");
strrpl = (char *)malloc (ilen + 1);
getstrfld (strpat, 1, 0, "|", strrpl);
searchreplace (strbuf, strsrch, strrpl);
free (strsrch);
free (strrpl);
free (strpat);
}
return strbuf;
}
/* ---------------------------------------------------------------------[<]-
Function: wordwrapstr
Synopsis:
Function that does word wraping of a string at or less than iwid.
Breaks up a string on word boundaries by placing '\n' in the string.
Returns a pointer to head of the return buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
wordwrapstr (
char *strbuff,
int iwid)
{
char *strtmp = strbuff;
int icnt = 0;
ASSERT (strbuff);
replacechrswith (strbuff, "\n", ' ');
while (*strtmp)
{
if ((int)strlen (strtmp) > (int)iwid)
{
icnt = iwid;
while (*(strtmp + icnt))
{
if (strchr (" .?;!,", *(strtmp + icnt)))
{
ltrim ((strtmp + icnt));
insertchar (strtmp, '\n', icnt);
strtmp += icnt + 1;
break;
}
icnt--;
if (!icnt)
{
if (strchr (" .?;!,", *(strtmp + icnt)))
{
ltrim ((strtmp + iwid));
insertchar (strtmp, '\n', iwid);
strtmp += iwid + 1;
break;
}
}
}
}
else
break;
}
return strbuff;
}
/* ---------------------------------------------------------------------[<]-
Function: stricstr
Synopsis:
A case insensitive strstr. Returns a pointer to head of the str1.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
stricstr (
const char *str1,
const char *str2)
{
char *strtmp = (char *)str1;
int iret = 1;
ASSERT (str1);
ASSERT (str2);
while (*strtmp)
{
if (strlen (strtmp)>= strlen (str2))
{
iret = lexncmp (strtmp, str2, strlen (str2));
}
else
{
break;
}
if (!iret)
{
break;
}
strtmp++;
}
return !iret ? strtmp : (char *)NULL;
}
/* ---------------------------------------------------------------------[<]-
Function: strtempcmp
Synopsis:
Compares a string to a template.
Template chars and there functions:
# or 9 = Number.
A or _ = Alpha.
@ = Alphanumeric
\char = Literal. Char would be the literal to use. ie: "\%" -
looks for a % in that postion
Returns 0 if == to the template and 1 if != to the template.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
int
strtempcmp (
const char *str1,
const char *strPat)
{
int ires = 1;
ASSERT (str1);
ASSERT (strPat);
while (*str1 && *strPat)
{
switch ((int)*strPat)
{
case '#':
case '9':
ires = isdigit ((int)*str1);
break;
case 'A':
case '_':
ires = isalpha ((int)*str1);
break;
case '@':
ires = isalnum ((int)*str1);
break;
case ' ':
ires = isspace ((int)*str1);
break;
case '\\':
strPat++;
if (*str1 != *strPat)
{
ires = 1;
}
break;
default:
break;
}
if (!ires)
{
break;
}
str1++;
strPat++;
}
return ires ? 0 : 1;
}
/* ---------------------------------------------------------------------[<]-
Function: istoken
Synopsis:
Eats strToEat from strBuff only if it begins with contents of
strToEat, and returns a 0 or 1 to tell what it did.
Examples:
char strBuff[] = { "select * from mytbl;" };
int iWasToken;
istoken (&strBuff, "SELECT", &iWasToken);
On return here iWasToken would == 1, and strBuff would be:
" * from mytbl;"
If the token is not found, then strBuff will not be affected, and
a 0 will be returned.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
int
istoken (
char **strLine,
const char *strtoken,
int *iWasToken)
{
int iRet;
char cChar;
ASSERT (strLine);
ASSERT (strtoken);
ASSERT (iWasToken);
iRet = lexncmp (*strLine, strtoken, strlen (strtoken));
if (!iRet)
{
cChar = *(*strLine + strlen (strtoken));
if (!isalpha ((int)cChar)&& cChar != '_')
{
iRet = *iWasToken = 1;
strcpy (*strLine, (*strLine + strlen (strtoken)));
}
else
iRet = *iWasToken = 0;
}
else
iRet = *iWasToken = 0;
return iRet;
}
/* ---------------------------------------------------------------------[<]-
Function: eatstr
Synopsis:
Eats strToEat from strBuff only if it begins with contents of
strToEat.
Examples:
char strBuff[] = { "select * from mytbl;" };
eatstr (&strBuff, "SELECT");
On return here strBuff would be: " * from mytbl;"
If the token is not found, then strBuff will not be affected and
a NULL char * will be returned, but any white spaces on the left
of strBuff would be trimed.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
eatstr (
char **strBuff,
char *strToEat)
{
int iWasToken;
ASSERT (strBuff);
ASSERT (strToEat);
ltrim (*strBuff);
istoken (strBuff, strToEat, &iWasToken);
return iWasToken ? *strBuff : (char *)NULL;
}
/* ---------------------------------------------------------------------[<]-
Function: eatstrpast
Synopsis:
Eats chars past first occurrence of one of the chars contained in
strCharsToEatPast.
Examples:
char strBuff[] = { " , 456, 789" };
eatstrpast (&strBuff, ",");
On return here strBuff would be: " 456, 789".
Returns a pointer to head of the input buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
eatstrpast (
char **strBuff,
char *strCharsToEatPast)
{
ASSERT (strBuff);
ASSERT (strCharsToEatPast);
ltrim (*strBuff);
while (**strBuff && strchr (strCharsToEatPast, **strBuff))
deletechar (*strBuff, 0);
return *strBuff;
}
/* ---------------------------------------------------------------------[<]-
Function: movestrpast
Synopsis:
Eats chars past first occurrence of one of the chars contained in
strCharsToEatPast.
Examples:
char strBuff[] = { "123, 456, 789" };
movestrpast (&strBuff, ",");
On return here strBuff would be: " 456, 789".
Returns a pointer to head of the input buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
movestrpast (
char **strBuff,
char cCharToEatPast)
{
ASSERT (strBuff);
ltrim (*strBuff);
while (**strBuff && **strBuff != cCharToEatPast)
deletechar (*strBuff, 0);
if (**strBuff && **strBuff == cCharToEatPast)
deletechar (*strBuff, 0);
return *strBuff;
}
/* ---------------------------------------------------------------------[<]-
Function: eatchar
Synopsis:
Trims white spaces and eats just past occurrence of cChar. If
contents of cChar is not found then only white spaces are trimmed.
Examples:
char strBuff[] = { "('test', 5)" };
eatchar (&strBuff, '(');
On return here strBuff would be: "'test', 5)".
Returns a pointer to head of the input buffer.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
char *
eatchar (
char **strBuff,
char cChar)
{
ASSERT (strBuff);
ltrim (*strBuff);
if (**strBuff && **strBuff == cChar)
deletechar (*strBuff, 0);
return *strBuff;
}
/* ---------------------------------------------------------------------[<]-
Function: isoneoftokens
Synopsis:
Eats strToEat from strBuff only if it begins with contents of
strToEat, and returns a 0 or 1 to tell what it did. Returns 0
if nothing found, and >= 1 which is an index of the one found.
Examples:
char strBuff[] = { "select * from mytbl;" };
int iWasToken;
isoneoftokens (&strBuff, "INSERT|SELECT|DELETE", "|", &iWasToken);
On return here iWasToken would == 1, and strBuff would be:
" * from mytbl;" and the return value would be 2.
If the token is not found, then strBuff will not be affected, and
a 0 will be returned.
Submitted by Scott Beasley <jscottb@infoave.com>
---------------------------------------------------------------------[>]-*/
int
isoneoftokens (
char **strbuf,
char *strmat,
char *strsep,
int *iWasToken)
{
int nstate = 0, cnt = 0, icmpres;
int iLen;
char *strtemp, cChar;
ASSERT (strbuf);
ASSERT (strmat);
ASSERT (strsep);
ASSERT (iWasToken);
while (1)
{
iLen = getstrfldlen (strmat, cnt, 0, strsep);
strtemp = (char *) malloc (iLen + 1);
getstrfld (strmat, cnt, 0, strsep, strtemp);
if (*strtemp)
{
trim (strtemp);
icmpres = lexncmp (*strbuf, strtemp, strlen (strtemp));
if (!icmpres)
{
cChar = *(*strbuf + strlen (strtemp));
if (!isalpha ((int)cChar)&& cChar != '_')
{
*iWasToken = cnt + 1;
strcpy (*strbuf, (*strbuf + strlen (strtemp)));
nstate = cnt + 1;
}
break;
}
else
{
if (!lexncmp (*strbuf, strtemp, strlen (strtemp)))
{
cChar = *(*strbuf + strlen (strtemp));
if (!isalpha ((int)cChar)&& cChar != '_')
{
*iWasToken = cnt + 1;
strcpy (*strbuf, (*strbuf + strlen (strtemp)));
nstate = cnt + 1;
}
break;
}
}
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -