~ubuntu-branches/ubuntu/edgy/agrep/edgy

« back to all changes in this revision

Viewing changes to utilities.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2005-12-27 17:01:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051227170100-nk2hnq0bnlkbk3q3
Tags: 4.17-2
Added patch to fix FTBS on amd64 (Closes: #344909).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
1
2
/* this file contains various utility functions for accessing
2
3
   and manipulating regular expression syntax trees.    */
3
4
 
4
5
#include <stdio.h>
 
6
#include <ctype.h>
5
7
#include "re.h"
6
8
 
7
9
/************************************************************************/
14
16
Stack *s;
15
17
Re_node v;
16
18
{
17
 
    Stack node;
 
19
        Stack node;
18
20
 
19
 
    node = (Stack) new_node(node);
20
 
    if (s == NULL || node == NULL) return NULL;     /* can't allocate */
21
 
    node->next = *s;
22
 
    node->val = v;
23
 
    if (*s == NULL) node->size = 1;
24
 
    else node->size = (*s)->size + 1;
25
 
    *s = node;
26
 
    return *s;
 
21
        new_node(Stack, node, node);
 
22
        if (s == NULL || node == NULL) return NULL;         /* can't allocate */
 
23
        node->next = *s;
 
24
        node->val = v;
 
25
        if (*s == NULL) node->size = 1;
 
26
        else node->size = (*s)->size + 1;
 
27
        *s = node;
 
28
        return *s;
27
29
}
28
30
 
29
31
Re_node Pop(s)
30
32
Stack *s;
31
33
{
32
 
    Re_node node;
33
 
    Stack temp;
 
34
        Re_node node;
 
35
        Stack temp;
34
36
 
35
 
    if (s == NULL || *s == NULL) return NULL;
36
 
    else {
37
 
        temp = *s;
38
 
        node = (*s)->val;
39
 
        *s = (*s)->next;
40
 
        free(temp);
41
 
        return node;
42
 
    }
 
37
        if (s == NULL || *s == NULL) return NULL;
 
38
        else {
 
39
                temp = *s;
 
40
                node = (*s)->val;
 
41
                *s = (*s)->next;
 
42
                free(temp);
 
43
                return node;
 
44
        }
43
45
}
44
46
 
45
47
Re_node Top(s)
46
48
Stack s;
47
49
{
48
 
    if (s == NULL) return NULL;
49
 
    else return s->val;
 
50
        if (s == NULL) return NULL;
 
51
        else return s->val;
50
52
}
51
53
 
52
54
int Size(s)
53
55
Stack s;
54
56
{
55
 
    if (s == NULL) return 0;
56
 
    else return s->size;
 
57
        if (s == NULL) return 0;
 
58
        else return s->size;
57
59
}
58
60
 
59
61
/************************************************************************/
66
68
int n;
67
69
Pset p;
68
70
{
69
 
    while (p != NULL)
70
 
        if (n == p->posnum) return 1;
71
 
        else p = p->nextpos;
72
 
    return 0;
 
71
        while (p != NULL)
 
72
                if (n == p->posnum) return 1;
 
73
                else p = p->nextpos;
 
74
        return 0;
73
75
}
74
76
 
75
77
/* pset_union() takes two position-sets and returns their union.    */
76
78
 
77
 
Pset pset_union(s1, s2)
 
79
Pset pset_union(s1, s2, dontreplicate)
78
80
Pset s1, s2;
 
81
int dontreplicate;
79
82
{
80
 
    Pset hd, curr, new;
81
 
    int occ;
82
 
 
83
 
    hd = NULL; curr = NULL;
84
 
    while (s1 != NULL) {
85
 
        if (!occurs_in(s1->posnum, s2)) {
86
 
            new = (Pset) new_node(new);
87
 
            if (new == NULL) return NULL;
88
 
            new->posnum = s1->posnum;
89
 
            if (hd == NULL) hd = new;
90
 
            else curr->nextpos = new;
91
 
        }
92
 
        curr = new;
93
 
        s1 = s1->nextpos;
94
 
    }
95
 
    if (hd == NULL) hd = s2;
96
 
    else curr->nextpos = s2;
97
 
    return hd;
 
83
        Pset hd, curr, new = NULL;
 
84
        Pset replicas2 = NULL, temps2 = s2;     /* code added: 26/Aug/96 */
 
85
 
 
86
        /* Code added on 26/Aug/96 */
 
87
        if (dontreplicate) replicas2 = s2;
 
88
        else while (temps2 != NULL) {
 
89
                new_node(Pset, new, new);
 
90
                if (new == NULL) return NULL;
 
91
                new->posnum = temps2->posnum;
 
92
                if (replicas2 == NULL) replicas2 = new;
 
93
                else curr->nextpos = new;
 
94
                curr = new;
 
95
                temps2 = temps2->nextpos;
 
96
        }
 
97
 
 
98
        hd = NULL; 
 
99
        curr = NULL;
 
100
        while (s1 != NULL) {
 
101
                if (!occurs_in(s1->posnum, s2)) {
 
102
                        new_node(Pset, new, new);
 
103
                        if (new == NULL) return NULL;
 
104
                        new->posnum = s1->posnum;
 
105
                        if (hd == NULL) hd = new;
 
106
                        else curr->nextpos = new;
 
107
                }
 
108
                curr = new;
 
109
                s1 = s1->nextpos;
 
110
        }
 
111
        if (hd == NULL) hd = replicas2; /* changed from s2: 26/Aug/96 */
 
112
        else curr->nextpos = replicas2; /* changed from s2: 26/Aug/96 */
 
113
        return hd;
98
114
}
99
115
 
100
116
/* create_pos() creates a position node with the position value given,
103
119
Pset create_pos(n)
104
120
int n;
105
121
{
106
 
    Pset x;
 
122
        Pset x;
107
123
 
108
 
    x = (Pset) new_node(x);
109
 
    if (x == NULL) return NULL;
110
 
    x->posnum = n;
111
 
    x->nextpos = NULL;
112
 
    return x;
 
124
        new_node(Pset, x, x);
 
125
        if (x == NULL) return NULL;
 
126
        x->posnum = n;
 
127
        x->nextpos = NULL;
 
128
        return x;
113
129
}
114
130
 
115
131
/* eq_pset() takes two position sets and checks to see if they are
116
132
   equal.  It returns 1 if the sets are equal, 0 if they are not.       */
117
 
 
 
133
int
118
134
subset_pset(s1, s2)
119
135
Pset s1, s2;
120
136
{
121
 
    int subs = 1;
 
137
        int subs = 1;
122
138
 
123
 
    while (s1 != NULL && subs != 0) {
124
 
        subs = 0;
125
 
        while (s2 != NULL && subs != 1)
126
 
            if (s1->posnum == s2->posnum) subs = 1;
127
 
            else s2 = s2->nextpos;
128
 
        s1 = s1->nextpos;
129
 
    }
130
 
    return subs;
 
139
        while (s1 != NULL && subs != 0) {
 
140
                subs = 0;
 
141
                while (s2 != NULL && subs != 1)
 
142
                        if (s1->posnum == s2->posnum) subs = 1;
 
143
                        else s2 = s2->nextpos;
 
144
                s1 = s1->nextpos;
 
145
        }
 
146
        return subs;
131
147
}       
132
148
 
133
149
int eq_pset(s1, s2)
134
150
Pset s1, s2;
135
151
{
136
 
    return subset_pset(s1, s2) && subset_pset(s2, s1);
137
 
}
138
 
 
 
152
        return subset_pset(s1, s2) && subset_pset(s2, s1);
 
153
}
 
154
 
 
155
int
 
156
word_exists(word, wordlen, line, linelen)
 
157
unsigned char *word, *line;
 
158
int wordlen, linelen;
 
159
{
 
160
        unsigned char oldchar, *lineend = line+linelen;
 
161
        int i;
 
162
 
 
163
        i = 0;
 
164
        while(line<lineend) {
 
165
                if (!isalnum(line[i])) {
 
166
                        oldchar = line[i];
 
167
                        line[i] = '\0';
 
168
                        if (!strncmp(line, word, wordlen)) {
 
169
                                line[i] = oldchar;
 
170
                                return 1;
 
171
                        }
 
172
                        line[i] = oldchar;
 
173
                        line += i + 1;
 
174
                        i = 0;
 
175
                } else i++;
 
176
        }
 
177
        return 0;
 
178
}