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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
** SRCHFILE.C - Functions for searching files
**
** public domain by Bob Stout
**
** Note: Although this snippet demonstrates some useful techniques, even
** the fast text searching algorithm used can't provide particularly
** good performance. Left as an exercise for the user is to perform
** explicit buffering using fread() rather than fgets() as is used
** here. See CHBYTES.C in SNIPPETS for how to perform searches in
** user-managed buffers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SUCCESS 0
/*
** Allocate a big buffer, use it to buffer a specified stream
*/
static size_t fsetup(FILE *fp, size_t minbuf)
{
register size_t bufsize;
register char *buffer;
/* Allocate the largest buffer we can */
for (bufsize = 0x4000; bufsize >= minbuf; bufsize >>= 1)
{
if (NULL != (buffer = (char *) malloc(bufsize)))
break;
}
if (NULL == buffer)
return 0;
/* Use the buffer to buffer the file */
if (SUCCESS == setvbuf(fp, buffer, _IOFBF, bufsize))
return bufsize;
else return 0;
}
/*
** Search a file for a pattern match (forward)
**
** Arguments: FILE pointer
** pattern to search for
** size of pattern
** find Nth occurance
**
** Returns: -1L if pattern not found
** -2L in case of error
*/
long ffsearch(FILE *fp, const char *pattern, const size_t size, int N)
{
long pos = -2L, tempos = 0L;
char *sbuf, *p;
size_t i, skip;
int ch = 0;
/* Allocate a search buffer */
if (NULL == (sbuf = (char *)malloc(size - 1)))
goto FDONE;
/* Buffer the file and position us within it */
if (0 == fsetup(fp, size))
goto FDONE;
pos = -1L;
fseek(fp, 0L, SEEK_SET);
/* Set up for smart searching */
if (1 < strlen(pattern) && NULL != (p = strchr(pattern + 1, *pattern)))
skip = p - (char *)pattern;
else skip = strlen(pattern);
/* Look for the pattern */
while (EOF != ch)
{
if (EOF == (ch = fgetc(fp)))
break;
if ((int)*pattern == ch)
{
tempos = ftell(fp);
if (size - 1 > fread(sbuf, sizeof(char), size - 1, fp))
goto FDONE;
if (SUCCESS == memcmp(sbuf, &pattern[1], size - 1))
{
if (0 == --N)
{
pos = tempos - 1L;
goto FDONE;
}
}
fseek(fp, tempos + skip, SEEK_SET);
}
}
/* Clean up and leave */
FDONE:
free(sbuf);
return pos;
}
/*
** Search a file for a pattern match (backwards)
**
** Arguments: FILE pointer
** pattern to search for
** size of pattern
** find Nth occurance
**
** Returns: -1L if pattern not found
** -2L in case of error
*/
long rfsearch(FILE *fp, const char *pattern, const size_t size, int N)
{
long pos = -2L, tempos;
char *sbuf, *p;
size_t i, skip;
int ch = 0;
/* Allocate a search buffer */
if (NULL == (sbuf = (char *)malloc(size - 1)))
goto RDONE;
/* Buffer the file and position us within it */
if (0 == fsetup(fp, size))
goto RDONE;
pos = -1L;
fseek(fp, -1L, SEEK_END);
tempos = ftell(fp) - strlen(pattern);
/* Set up for smart searching */
if (1 < strlen(pattern) && NULL != (p = strrchr(pattern + 1, *pattern)))
skip = strlen(pattern) - (p - (char *)pattern);
else skip = strlen(pattern);
/* Look for the pattern */
while (0L <= tempos)
{
fseek(fp, tempos, SEEK_SET);
if (EOF == (ch = fgetc(fp)))
break;
if ((int)*pattern == ch)
{
if (size - 1 <= fread(sbuf, sizeof(char), size - 1, fp))
{
if (SUCCESS == memcmp(sbuf, &pattern[1], size - 1))
{
if (0 == --N)
{
pos = tempos;
goto RDONE;
}
}
}
tempos -= skip;
}
else --tempos;
}
/* Clean up and leave */
RDONE:
free(sbuf);
return pos;
}
#ifdef TEST
int main(int argc, char *argv[])
{
long pos;
int N = 1;
size_t size = strlen(argv[1]);
char buf[256], *fname = "SRCHFILE.C";
FILE *fp;
if (2 > argc)
{
puts("Usage: SRCHFILE string [N] [file]");
puts("where: N = find Nth occurance");
puts(" If file is specified, N must be given");
return EXIT_FAILURE;
}
if (2 < argc)
N = atoi(argv[2]);
if (3 < argc)
fname = strupr(argv[3]);
fp = fopen(fname, "r");
printf("ffsearch(%s, %s) returned %ld\n", fname, argv[1],
pos = ffsearch(fp, argv[1], size, N));
fseek(fp, pos, SEEK_SET);
fgets(buf, 256, fp);
printf("...which contains \"%s\"\n\n", buf);
fclose(fp);
fp = fopen(fname, "rb");
printf("rfsearch(%s, %s) returned %ld\n", fname, argv[1],
pos = rfsearch(fp, argv[1], size, N));
fseek(fp, pos, SEEK_SET);
fgets(buf, 256, fp);
printf("...which contains \"%s\"\n\n", buf);
fclose(fp);
return EXIT_SUCCESS;
}
#endif /* TEST */
|