Commit 44b782c4 authored by rswindell's avatar rswindell
Browse files

Created findstr_in_string() function (this is the function that needs better

regular expression suppor).
Reverted findstr() and trashcan() to read the file a line at a time rather than
load the entire file (potentially very large) into a string list.
parent 11b40bb1
......@@ -848,6 +848,7 @@ extern "C" {
DLLEXPORT char * DLLCALL truncstr(char* str, const char* set);
DLLEXPORT char * DLLCALL ascii_str(uchar* str);
DLLEXPORT BOOL DLLCALL findstr(const char *insearch, const char *fname);
DLLEXPORT BOOL DLLCALL findstr_in_string(const char* insearchof, char* string);
DLLEXPORT BOOL DLLCALL findstr_in_list(const char* insearchof, str_list_t list);
DLLEXPORT BOOL DLLCALL trashcan(scfg_t* cfg, const char *insearch, const char *name);
DLLEXPORT char * DLLCALL trashcan_fname(scfg_t* cfg, const char *name, char* fname, size_t);
......
......@@ -112,71 +112,79 @@ char* DLLCALL prep_file_desc(char *str)
}
/****************************************************************************/
/* Pattern matching string search of 'insearchof' in 'fname'. */
/* Pattern matching string search of 'insearchof' in 'string'. */
/****************************************************************************/
BOOL DLLCALL findstr_in_list(const char* insearchof, str_list_t list)
BOOL DLLCALL findstr_in_string(const char* insearchof, char* string)
{
char* p;
char str[256];
char search[81];
int c;
int i;
size_t index;
BOOL found;
BOOL found=FALSE;
if(list==NULL || insearchof==NULL)
if(string==NULL || insearchof==NULL)
return(FALSE);
SAFECOPY(search,insearchof);
strupr(search);
SAFECOPY(str,string);
found=FALSE;
p=str;
SKIP_WHITESPACE(p);
for(index=0;list[index]!=NULL && !found; index++) {
SAFECOPY(str,list[index]);
found=FALSE;
if(*p==';') /* comment */
return(FALSE);
p=str;
while(*p && *p<=' ') p++; /* Skip white-space */
if(*p=='!') { /* !match */
found=TRUE;
p++;
}
if(*p==';') /* comment */
continue;
truncsp(p);
c=strlen(p);
if(c) {
c--;
strupr(p);
if(p[c]=='~') {
p[c]=0;
if(strstr(search,p))
found=!found;
}
if(*p=='!') { /* !match */
found=TRUE;
p++;
else if(p[c]=='^' || p[c]=='*') {
p[c]=0;
if(!strncmp(p,search,c))
found=!found;
}
truncsp(p);
c=strlen(p);
if(c) {
c--;
strupr(p);
if(p[c]=='~') {
p[c]=0;
if(strstr(search,p))
found=!found;
}
else if(p[0]=='*') {
i=strlen(search);
if(i<c)
return(found);
if(!strncmp(p+1,search+(i-c),c))
found=!found;
}
else if(p[c]=='^' || p[c]=='*') {
p[c]=0;
if(!strncmp(p,search,c))
found=!found;
}
else if(!strcmp(p,search))
found=!found;
}
return(found);
}
else if(p[0]=='*') {
i=strlen(search);
if(i<c)
continue;
if(!strncmp(p+1,search+(i-c),c))
found=!found;
}
/****************************************************************************/
/* Pattern matching string search of 'insearchof' in 'fname'. */
/****************************************************************************/
BOOL DLLCALL findstr_in_list(const char* insearchof, str_list_t list)
{
size_t index;
BOOL found=FALSE;
else if(!strcmp(p,search))
found=!found;
}
}
if(list==NULL || insearchof==NULL)
return(FALSE);
for(index=0;list[index]!=NULL && !found; index++)
found=findstr_in_string(insearchof, list[index]);
return(found);
}
......@@ -185,23 +193,23 @@ BOOL DLLCALL findstr_in_list(const char* insearchof, str_list_t list)
/****************************************************************************/
BOOL DLLCALL findstr(const char* insearchof, const char* fname)
{
str_list_t list;
BOOL found;
char str[256];
BOOL found=FALSE;
FILE* fp;
if(insearchof==NULL)
if(insearchof==NULL || fname==NULL)
return(FALSE);
if((fp=fopen(fname,"r"))==NULL)
return(FALSE);
list=strListReadFile(fp,NULL,255);
fclose(fp);
found=findstr_in_list(insearchof,list);
strListFree(&list);
while(!feof(fp) && !ferror(fp) && !found) {
if(!fgets(str,sizeof(str),fp))
break;
found=findstr_in_string(insearchof,str);
}
fclose(fp);
return(found);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment