pastebin

Paste #8nB -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

  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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *compileglob(const char *pattern)
{
	char *compiled, *ptr;
	compiled = malloc(strlen(pattern)+1);
	if (!compiled) {
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	ptr = compiled;
	/* Coalesce runs of star operators */
	while (*pattern) {
		if (*pattern != '*' || ptr == compiled || ptr[-1] != '*')
			*ptr++ = *pattern;
		++pattern;
	}
	return compiled;
}

int checkglob(const char *pattern, const char *text)
{
	size_t n, statescount, nextstatescount, i, prev;
	size_t *states, *nextstates;
	int accepting;

	n = strlen(pattern) + 1;

	statescount = nextstatescount = 0;

	states = malloc(2 * n * sizeof(size_t));
	if(states == 0) {
		perror("malloc");
		exit(EXIT_FAILURE);
	}

	nextstates = states + n;

	/* Set initial states */
	states[0] = 0;
	statescount = 1;
	if(pattern[0] == '*') {
		states[1] = 1;
		statescount = 2;
	}

	/* Simulate the NFA (each state = index into pattern) */
	for (;;) {
		nextstatescount = 0;
		prev = (size_t) -1;

		for (i = 0; i < statescount; ++i) {
			size_t state = states[i];
			if (pattern[state] == '*') {
				/* Star - expect arbitrary number of chars */
				if (prev != state) {
					nextstates[nextstatescount++] = state;
				}
				nextstates[nextstatescount++] = state + 1;
				prev = state + 1;
			} else if (pattern[state] == '?') {
				/* Question mark - expect single char */
				if (*text) {
					nextstates[nextstatescount++] = state + 1;
					prev = state + 1;
				}
			} else if (pattern[state]) {
				/* Normal state - expect specific char */
				if (*text == pattern[state]) {
					nextstates[nextstatescount++] = state + 1;
					prev = state + 1;
				}
			} else {
				/* Accepting (final) state */
				if (!*text && prev != state) {
					nextstates[nextstatescount++] = state;
					prev = state;
				}
			}
		}

		if (nextstatescount == 0) {
			free(states);
			return 0;
		}
		if (!*text) {
			accepting = nextstates[nextstatescount - 1] == n - 1;
			free(states);
			return accepting;
		}

		memcpy(states, nextstates, nextstatescount * sizeof(size_t));
		statescount = nextstatescount;
		++text;
	}

	/* not reached */
	return 0;
}

int main(int argc, char **argv)
{
	int i;
	char *pattern;
	FILE *f;
	char buf[1024];

	if (argc<2)
		return EXIT_FAILURE;

	pattern = compileglob(argv[1]);
	for (i = 2; i < argc; ++i) {
		f = fopen(argv[i], "r");
		if (!f) {
			perror(argv[i]);
			return 1;
		}
		while (fgets(buf, sizeof buf, f)) {
			if (*buf && buf[strlen(buf)-1] == '\n')
				buf[strlen(buf)-1] = 0;
			if (checkglob(pattern, buf))
				puts(buf);
		}
		fclose(f);
	}
	free(pattern);
	return 0;
}