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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/*
* Copyright (c) 1997, 1998, 1999, 2000, 2004
* Tama Communications Corporation
*
* This file is part of GNU GLOBAL.
*
* GNU GLOBAL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GNU GLOBAL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef DEBUG
#include <stdio.h>
extern int debug;
#endif
#include "locatestring.h"
/*
* strincmp: strncmp with ignoring case.
*
* Interface is same with strncmp.
*/
static int
strincmp(string, pattern, len)
const char *string;
const char *pattern;
size_t len;
{
unsigned char s, p;
while (len--) {
s = tolower(*string++);
p = tolower(*pattern++);
if (s != p)
return s - p;
if (s == 0)
break;
}
return 0;
}
/*
* locatestring: locate pattern from string
*
* i) string string
* i) pattern pattern
* i) flag MATCH_FIRST: match first
* MATCH_AT_FIRST: match only at first column
* MATCH_LAST: match last
* MATCH_AT_LAST: match only at last column
* MATCH_COMPLETE match completely
* IGNORE_CASE: Ignore case
* r) pointer or NULL
* If the flag == MATCH_AT_FIRST then the pointer
* points the following character of the matched
* string, else points at the head of it.
*
* This function is made to avoid compatibility problems.
*/
int (*cmpfunc)(char *, char*, int);
char *
locatestring(string, pattern, flag)
const char *string;
const char *pattern;
int flag;
{
int c = *pattern;
int plen = strlen(pattern);
const char *p = NULL;
int slen;
int (*cmpfunc) ();
#ifdef DEBUG
FILE *dbg = stderr;
const char *pre = string;
#endif
cmpfunc = (flag & IGNORE_CASE) ? strincmp : strncmp;
flag &= ~IGNORE_CASE;
if (flag == MATCH_COMPLETE) {
if (strlen(string) == plen && !(*cmpfunc)(string, pattern, plen))
return (char *)string;
else
return NULL;
}
if (flag == MATCH_AT_LAST && (slen = strlen(string)) > plen)
string += (slen - plen);
for (; *string; string++) {
if (*string == c)
if (!(*cmpfunc)(string, pattern, plen)) {
p = string;
if (flag == MATCH_FIRST)
break;
}
if (flag == MATCH_AT_FIRST || flag == MATCH_AT_LAST)
break;
}
#ifdef DEBUG
if (debug) {
fprintf(dbg, "locatestring: ");
if (p == NULL)
fprintf(dbg, "%s", pre);
else {
const char *pp = p;
const char *post = pp + strlen(pattern);
for (; *pre && pre < pp; pre++)
fputc(*pre, dbg);
fputc('[', dbg);
for (; *pp && pp < post; pp++)
fputc(*pp, dbg);
fputc(']', dbg);
for (; *post; post++)
fputc(*post, dbg);
}
fputc('\n', dbg);
}
#endif
if (p && flag == MATCH_AT_FIRST)
p += plen;
return (char *)p;
}
|