Debian Codegraph @

wordlist.c

raw symbols SCIP
1 /*** wordlist.c ***************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **
25 **-----------------------------------------------------------------------------
26 ** Description:
27 ** This module contains functions which deal with lists of
28 ** words. Those words are in fact simple strings. Thus this
29 ** module provides a very general functionality, namely a list of
30 ** strings and the associated methods.
31 **
32 ******************************************************************************/
33
34 #include <bibtool/general.h>
35 #include <bibtool/error.h>
36 #include <bibtool/symbols.h>
37 #include <bibtool/wordlist.h>
38
39 /*****************************************************************************/
40 /* Internal Programs */
41 /*===========================================================================*/
42
43 /*****************************************************************************/
44 /* External Programs */
45 /*===========================================================================*/
46
47 /*---------------------------------------------------------------------------*/
48
49 /*-----------------------------------------------------------------------------
50 ** Function: add_word()
51 ** Purpose: Put a string into a word list. The string itself is
52 ** \emph{not} copied. Thus it is highly recommended to
53 ** use symbols as words nevertheless this is not
54 ** required as long as the string |s| persists as long as
55 ** the word list exists.
56 **
57 ** The second argument is a pointer to a |WordList|. This
58 ** destination is modified by adding a new node. The use
59 ** of a pointer allows a uniform treatment of empty and
60 ** not empty word lists.
61 **
62 ** If no memory is left then an error is raised and the program
63 ** is terminated.
64 ** Arguments:
65 ** s String to add to the wordlist.
66 ** wlp Pointer to a wordlist.
67 ** Returns: nothing
68 **___________________________________________________ */
69 void add_word(sym, wlp) /* */
70 register Symbol sym; /* */
71 register WordList *wlp; /* */
72 { register WordList wl; /* */
73 register int cmp = 1; /* */
74 /* */
75 while (*wlp != WordNULL /* */
76 && (cmp=symcmp(ThisWord(*wlp), sym)) < 0) /* */
77 { wlp = & NextWord(*wlp); } /* */
78 /* */
79 if ( cmp == 0 ) return; /* */
80 /* */
81 if ( (wl=(WordList)malloc(sizeof(SWordList))) == WordNULL )/* */
82 { OUT_OF_MEMORY("WordList"); } /* */
83 /* */
84 LinkSymbol(sym); /* */
85 ThisWord(wl) = sym; /* */
86 NextWord(wl) = *wlp; /* */
87 *wlp = wl; /* */
88 } /*------------------------*/
89
90 /*-----------------------------------------------------------------------------
91 ** Function: delete_word()
92 ** Purpose: Remove a word from a |WordList|. Only the first
93 ** appearance of such a word is removed. I a word is
94 ** found which contains the same string as |s| then the
95 ** associated node is removed from the list and the
96 ** function |fct| is called to free the memory of the
97 ** string in the |WordList| node if the function is not
98 ** |NULL|. In this case the function returns
99 ** |0|. Otherwise |1| is returned.
100 ** Arguments:
101 ** s Word to remove.
102 ** wlp Pointer to the word list to modify.
103 ** fct Function to call to free the memory occupied by the word.
104 ** Returns: |0| if the word was not found. |1| otherwise.
105 **___________________________________________________ */
106 int delete_word(sym, wlp, fct) /* */
107 Symbol sym; /* */
108 WordList *wlp; /* */
109 void (*fct)_ARG((String)); /* */
110 { WordList wl; /* */
111 int cmp = 1; /* */
112 while ( *wlp != WordNULL /* */
113 && (cmp=symcmp(ThisWord(*wlp), sym)) < 0 )/* */
114 { wlp = & NextWord(*wlp); } /* */
115 /* */
116 if ( cmp == 0 ) return 0; /* */
117 /* */
118 wl = *wlp; /* */
119 *wlp = NextWord(wl); /* */
120 if ( fct != NULL ) { (*fct)(SymbolValue(ThisWord(wl))); }/* */
121 free(wl); /* */
122 return 1; /* */
123 } /*------------------------*/
124
125 /*-----------------------------------------------------------------------------
126 ** Function: free_words()
127 ** Purpose: Release the memory allocated for a list of words.
128 ** All nodes in the list are freed. The function |fct| is
129 ** called to free the memory occupied by the string
130 ** component if it is not |NULL|.
131 ** Arguments:
132 ** wlp Pointer to the |WordList|.
133 ** fct Function to be called to free the memory of the word itself.
134 ** If it is |NULL| then no function is called.
135 ** Returns: nothing
136 **___________________________________________________ */
137 void free_words(wlp, fct) /* */
138 WordList *wlp; /* */
139 void (*fct)_ARG((Symbol)); /* */
140 { WordList wl, next; /* */
141 /* */
142 if (wlp == NULL) return; /* */
143 /* */
144 if (fct != NULL) /* */
145 { for ( wl = *wlp; wl; wl = next) /* */
146 { next = NextWord(wl); /* */
147 (*fct)(ThisWord(wl)); /* */
148 free(wl); /* */
149 } /* */
150 } /* */
151 else /* */
152 { for ( wl = *wlp; wl; wl = next) /* */
153 { next = NextWord(wl); /* */
154 free(wl); /* */
155 } /* */
156 } /* */
157 *wlp = WordNULL; /* */
158 } /*------------------------*/
159
160 /*-----------------------------------------------------------------------------
161 ** Function: foreach_word()
162 ** Purpose: Applies the given function |fct| to all elements in
163 ** the |WordList| as long as the function does not return
164 ** 0. Thus it can be used to search for a specified word
165 ** -- e.g. determined by matching against a
166 ** template. Another application the the processing of
167 ** all elements in the |WordList|. In this case |fct|
168 ** must always return |true|.
169 ** Arguments:
170 ** wl WordList to traverse.
171 ** fct function to apply.
172 ** Returns: return value of last function or |true|.
173 **___________________________________________________ */
174 bool foreach_word(wl, fct) /* */
175 WordList wl; /* */
176 bool (*fct)_ARG((Symbol)); /* */
177 { bool ret = true; /* */
178 while ( wl && (ret=(fct)(ThisWord(wl))) ) /* */
179 { wl = NextWord(wl); } /* */
180 return ret; /* */
181 } /*------------------------*/
182
183 /*-----------------------------------------------------------------------------
184 ** Function: find_word()
185 ** Purpose: Look up a word in a word list. The comparison is done
186 ** case insensitive.
187 ** Arguments:
188 ** s String to find.
189 ** wl Word list to search in.
190 ** Returns: |false| iff the word does not occur in the word list.
191 **___________________________________________________ */
192 bool find_word(s, wl) /* */
193 register String s; /* */
194 register WordList wl; /* */
195 { /* */
196 while ( wl != WordNULL ) /* */
197 { if (case_eq(SymbolValue(ThisWord(wl)), s)) /* */
198 { return true; } /* */
199 wl = NextWord(wl); /* */
200 } /* */
201 /* */
202 return false; /* */
203 } /*------------------------*/