libicl_depr.c
来自「SRI international 发布的OAA框架软件」· C语言 代码 · 共 1,035 行 · 第 1/3 页
C
1,035 行
}
break;
case '\'':
inquotes = !inquotes;
break;
case ',' :
if ((parens == 0) && (inquotes == 0))
done = TRUE;
break;
case ' ':
case '\n':
case '\t':
if ((parens == 0) && (inquotes == 0) && seen_something)
done = TRUE;
break;
default:
if (icl_stIsSeparator(&terms[i])) {
if ((parens == 0) && (inquotes == 0))
done = TRUE;
}
else
seen_something = TRUE;
} /* End switch */
} /* End while */
if ((i >= 0) && !parens && !inquotes) {
if (icl_stIsSeparator(&terms[i])) {
n = i;
last_separator = terms[i];
}
else
n = i+1;
*aterm = malloc(n+1);
strncpy(*aterm, terms, n);
(*aterm)[n] = '\0';
i++;
while ((terms[i] != 0) && ((terms[i] == ' ') ||
(icl_stIsSeparator(&terms[i]))))
i++;
*restterms = &terms[i];
return TRUE;
}
else {
*aterm = strdup("");
*restterms = strdup("");
return FALSE;
}
}
/****************************************************************************
* name: icl_stFunctorArgs
* purpose: Splits a string containing a prolog-style structure into a functor
* and arguments
* inputs:
* - char *structure: In form "a(arg1, arg2, ...)"
* outputs:
* - char *func: returns "a"
* - char *args: returns "arg1, arg2, ..."
* remarks:
* - func and args will return new copies of the data, which should
* be free'd once they are no longer needed
* - If no arguments exist, the empty string is returned in args
* (1 byte allocated)
****************************************************************************/
EXPORT_MSCPP
void EXPORT_BORLAND
icl_stFunctorArgs(char *structure,
char **func,
char **args)
{
char *p;
int i;
/* Make sure structure is not NULL! */
if (!structure)
structure = strdup("");
/* If structure is just a string, list or other compound term, */
/* or an atom, just return the term, don't search for "(" */
if (((*structure) && (strchr("{[(\"", *structure) != NULL))
|| icl_stIsStr(structure)) {
icl_stHeadTailPtr(structure, func, args);
*args = strdup("");
}
/* Otherwise, assume structure in form aaa(bbb,ccc): pluck of */
/* functor by searching for first "(" char. */
else {
p = strstr(structure, "(");
if (!p) { /* no arguments in term */
*func = strdup(structure);
/* Chop off trailing '.' */
i = strlen(*func) - 1;
if ((i > 0) && (*func)[i] == '.')
(*func)[i] = '\0';
*args = strdup("");
}
else {
*func = malloc(p - structure + 1);
strncpy(*func, structure, p - structure);
(*func)[p - structure] = '\0';
*args = strdup(p + 1);
/* Chop off everything up to trailing ")" */
i = strlen(*args) - 1;
while ((i >= 0) && ((*args)[i] != ')')) {
(*args)[i] = '\0';
i--;
}
if (i >= 0) {
(*args)[i] = '\0';
}
}
}
}
/****************************************************************************
* name: icl_stOperationArgs
* purpose: Returns true if the term is an operation
* inputs:
* - char *term: a string containing a prolog term (or variable)
* returns:
* True if the term is a variable
* Note : An operation is defined by a binary operator and two operands.
* Operators currently are :
* +, - : of priority 2
* *, / : of priority 3
* :,:- : of priority 1
****************************************************************************/
EXPORT_MSCPP
int EXPORT_BORLAND
icl_stOperationArgs(char *t, char** op, char** left, char** right)
{
/* First we look for the and operator */
int index = 0;
while (operators[index].mPriority>=0){
int operator_index=0;
if ((operator_index = find_first_operator(t, operators[index].mName))>0) {
// Here we have found an suitable operator
if (left) {
*left = (char*)malloc(operator_index+1);
strncpy(*left, t, operator_index);
(*left)[operator_index]=0;
}
if(op)
*op = strdup(operators[index].mName);
if (right) {
*right = strdup(t+operator_index+strlen(operators[index].mName));
//*right = (char*)malloc(strlen(t)-operator_index-strlen(operators[index].mName));
//strncpy(*right, t+operator_index+strlen(operators[index].mName), strlen(t)-operator_index-strlen(operators[index].mName));
}
/* DEBUG */
if (0 && op && left && right)
printf("Expression Operator : %s Left : %s Right : %s\n", *op, *left, *right);
return 1;
}
index++;
}
return 0;
}
/****************************************************************************
* name: icl_stRemoveQuotes
* purpose: Removes leading and trailing ' and " marks
* inputs:
* - char *s: a string containing prolog terms
* outputs:
* - char *s: the string minus leading and trailing quotes
****************************************************************************/
EXPORT_MSCPP
void EXPORT_BORLAND
icl_stRemoveQuotes(char *s)
{
size_t len = strlen(s);
if ((*s == '\'') || (*s == '"')) {
memmove(s, &s[1], (len - 1) * sizeof(char));
--len;
}
/* One beyond length of data should be null char */
s[len] = '\0';
if ((s[len-1] == '\'') || (s[len-1] == '"'))
s[len-1] = '\0';
}
/****************************************************************************
* name: icl_stIsGroup
* purpose: Returns true if the term is a group (list of terms delimited by
* {} or ().
* A list (delimited by []) is not considered a group.
* inputs:
* - char *s: a string containing a prolog term
* returns:
* True if the term is a group
****************************************************************************/
EXPORT_MSCPP
int EXPORT_BORLAND
icl_stIsGroup(char *s)
{
int result;
result = (s && (
((*s == '{') && (s[strlen(s)-1] == '}')) ||
((*s == '(') && (s[strlen(s)-1] == ')'))));
return result;
}
/****************************************************************************
* name: icl_stIsStruct
* purpose: Returns true if the term is a struct (in form "a(...)")
* inputs:
* - char *s: a string containing an icl term
* returns:
* True if the term is a struct
****************************************************************************/
EXPORT_MSCPP
int EXPORT_BORLAND
icl_stIsStruct(char *s)
{
char *p;
if (!s || !*s) return FALSE;
p = strstr(s, "(");
if (p && (s[strlen(s)-1] == ')')) {
char *func = malloc(p - s + 2);
int res;
strncpy(func, s, p - s);
func[p-s] = '\0';
res = icl_stIsStr(func);
icl_stFree(func);
return (res);
}
else return FALSE;
}
/****************************************************************************
* name: icl_stIsList
* purpose: Returns true if the term is a list (in form [1,2,3])
* inputs:
* - char *s: a string containing a prolog term
* returns:
* True if the term is a list
****************************************************************************/
EXPORT_MSCPP
int EXPORT_BORLAND
icl_stIsList(char *s)
{
int result;
result = (s && (*s == '[') && (s[strlen(s)-1] == ']'));
return result;
}
/****************************************************************************
* name: icl_stIsStr
* purpose: Returns true if the term is an atom
* inputs:
* - char *s: a string containing a prolog term
* returns:
* True if the term is an atom
* Note: this assumes no leading or trailing whitespace in s.
* This is defined in accordance with QP 3.1 manual, section G.1-1-3.
****************************************************************************/
EXPORT_MSCPP
int EXPORT_BORLAND
icl_stIsStr(char *s)
{
char *p;
int len;
if ((s == 0) || (*s == 0))
return FALSE;
/* 4 special cases */
if (strcmp(s, "!") == 0 || strcmp(s, ";") == 0)
return TRUE;
/* Why ???? DG
if (strcmp(s, "[]") == 0 || strcmp(s, "{}") == 0)
return TRUE;
*/
/* Any sequence of chars delimited by single quotes */
len = strlen(s);
if ((*s == '\'') && (s[len-1] == '\''))
return TRUE;
/* Any sequence of these chars: +-*^<>=`-:.?@#$&/\ */
if (strchr("+-*/\\^<>=`-:.?@#$&", *s) != NULL) {
for (p = s + 1 ; *p ; p++) {
if (strchr("+-*/\\^<>=`-:.?@#$&", *p) == NULL)
return FALSE;
}
return TRUE;
}
/* Any sequence of alphanumeric or '_' starting with a lowercase letter */
if (islower(*s)) {
for (p = s + 1 ; *p ; p++) {
if ((isalnum(*p) == FALSE) && (*p != '_'))
return FALSE;
}
return TRUE;
}
return FALSE;
}
/****************************************************************************
* name: icl_stGroupToTerms
* purpose: Converts an incoming ICL group into
* a list of terms, on which we can use list_len, NthElt, etc.
* i.e. a comma-separated (or otherwise separated) list
* "elt1, elt2, etl3".
* Basically, just removes the outer delimiters
* inputs/outputs:
* - char *s: a list in prolog canonical syntax
* remarks:
* icl_stGroupToTerms overwrites old value with the new because the new value
* is guaranteed to be shorter.
****************************************************************************/
EXPORT_MSCPP
void EXPORT_BORLAND
icl_stGroupToTerms(char *s)
{
int err = 0;
if (s && (*s) && ((*s == '{') || (*s == '(')))
strcpy(s, &s[1]);
else err = 1;
if (s && (*s) && ((s[strlen(s)-1] == '}') || (s[strlen(s)-1] == ')')))
s[strlen(s)-1] = '\0';
else err = 1;
/*
if (err) printf("Error in icl_stListToTerms() -- invalid list:\n '%s'\n",s);
*/
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?