1 /* @source ajtext *************************************************************
2 **
3 ** AJAX TEXT functions
4 **
5 ** These functions control all aspects of AJAX text
6 ** parsing and include simple utilities.
7 **
8 ** @author Copyright (C) 2010 Peter Rice
9 ** @version $Revision: 1.8 $
10 ** @modified Oct 5 pmr First version
11 ** @modified $Date: 2011/10/18 14:23:41 $ by $Author: rice $
12 ** @@
13 **
14 ** This library is free software; you can redistribute it and/or
15 ** modify it under the terms of the GNU Lesser General Public
16 ** License as published by the Free Software Foundation; either
17 ** version 2.1 of the License, or (at your option) any later version.
18 **
19 ** This library is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ** Lesser General Public License for more details.
23 **
24 ** You should have received a copy of the GNU Lesser General Public
25 ** License along with this library; if not, write to the Free Software
26 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 ** MA 02110-1301, USA.
28 **
29 ******************************************************************************/
30
31 #include "ajlib.h"
32
33 #include "ajtext.h"
34 #include "ajtextread.h"
35 #include "ajtextwrite.h"
36
37
38
39
40 static AjPStr textTempQry = NULL;
41
42 static void textMakeQry(const AjPText thys, AjPStr* qry);
43
44
45
46
47 /* @filesection ajtext ********************************************************
48 **
49 ** @nam1rule aj Function belongs to the AJAX library.
50 **
51 ******************************************************************************/
52
53
54
55
56 /* @datasection [AjPText] Text data ********************************************
57 **
58 ** Function is for manipulating text data objects
59 **
60 ** @nam2rule Text Text data objects
61 **
62 ******************************************************************************/
63
64
65
66
67 /* @section constructors ******************************************************
68 **
69 ** Constructors
70 **
71 ** @fdata [AjPText]
72 **
73 ** @nam3rule New Constructor
74 **
75 ** @valrule * [AjPText] Text data object
76 **
77 ** @fcategory new
78 **
79 ******************************************************************************/
80
81
82
83
84 /* @func ajTextNew ************************************************************
85 **
86 ** Text data constructor
87 **
88 ** @return [AjPText] New object
89 **
90 ** @release 6.4.0
91 ** @@
92 ******************************************************************************/
93
ajTextNew(void)94 AjPText ajTextNew(void)
95 {
96 AjPText ret;
97
98 AJNEW0(ret);
99
100 ret->Lines = ajListstrNew();
101
102 return ret;
103 }
104
105
106
107
108 /* @section Text data destructors *********************************************
109 **
110 ** Destruction destroys all internal data structures and frees the
111 ** memory allocated for the text data object.
112 **
113 ** @fdata [AjPText]
114 **
115 ** @nam3rule Del Destructor
116 **
117 ** @argrule Del Ptext [AjPText*] Text data
118 **
119 ** @valrule * [void]
120 **
121 ** @fcategory delete
122 **
123 ******************************************************************************/
124
125
126
127
128 /* @func ajTextDel ************************************************************
129 **
130 ** Text data destructor
131 **
132 ** @param [d] Ptext [AjPText*] Text data object to delete
133 ** @return [void]
134 **
135 ** @release 6.4.0
136 ** @@
137 ******************************************************************************/
138
ajTextDel(AjPText * Ptext)139 void ajTextDel(AjPText *Ptext)
140 {
141 AjPText text;
142 AjPStr tmpstr = NULL;
143
144 if(!Ptext) return;
145 if(!(*Ptext)) return;
146
147 text = *Ptext;
148
149 ajStrDel(&text->Id);
150 ajStrDel(&text->Db);
151 ajStrDel(&text->Setdb);
152 ajStrDel(&text->Full);
153 ajStrDel(&text->Qry);
154 ajStrDel(&text->Formatstr);
155 ajStrDel(&text->Filename);
156
157 while(ajListstrPop(text->Lines, &tmpstr))
158 ajStrDel(&tmpstr);
159 ajListFree(&text->Lines);
160
161 AJFREE(*Ptext);
162 *Ptext = NULL;
163
164 return;
165 }
166
167
168
169
170 /* @section Casts *************************************************************
171 **
172 ** Return values from a text data object
173 **
174 ** @fdata [AjPText]
175 **
176 ** @nam3rule Get Return a value
177 ** @nam4rule Qry Return a query field
178 ** @suffix C Character string result
179 ** @suffix S String object result
180 **
181 ** @argrule * text [const AjPText] Text data object.
182 **
183 ** @valrule *C [const char*] Query as a character string.
184 ** @valrule *S [const AjPStr] Query as a string object.
185 **
186 ** @fcategory cast
187 **
188 ******************************************************************************/
189
190
191
192
193 /* @func ajTextGetQryC ********************************************************
194 **
195 ** Returns the query string of a text data object.
196 ** Because this is a pointer to the real internal string
197 ** the caller must take care not to change the character string in any way.
198 ** If the string is to be changed (case for example) then it must first
199 ** be copied.
200 **
201 ** @param [r] text [const AjPText] Text data object.
202 ** @return [const char*] Query as a character string.
203 **
204 ** @release 6.4.0
205 ** @@
206 ******************************************************************************/
207
ajTextGetQryC(const AjPText text)208 const char* ajTextGetQryC(const AjPText text)
209 {
210 return MAJSTRGETPTR(ajTextGetQryS(text));
211 }
212
213
214
215
216 /* @func ajTextGetQryS ********************************************************
217 **
218 ** Returns the query string of a text data object.
219 ** Because this is a pointer to the real internal string
220 ** the caller must take care not to change the character string in any way.
221 ** If the string is to be changed (case for example) then it must first
222 ** be copied.
223 **
224 ** @param [r] text [const AjPText] Text data object.
225 ** @return [const AjPStr] Query as a string.
226 **
227 ** @release 6.4.0
228 ** @@
229 ******************************************************************************/
230
ajTextGetQryS(const AjPText text)231 const AjPStr ajTextGetQryS(const AjPText text)
232 {
233 ajDebug("ajTextGetQryS '%S'\n", text->Qry);
234
235 if(ajStrGetLen(text->Qry))
236 return text->Qry;
237
238 textMakeQry(text, &textTempQry);
239
240 return textTempQry;
241 }
242
243
244
245
246 /* @funcstatic textMakeQry ****************************************************
247 **
248 ** Sets the query for a text data object.
249 **
250 ** @param [r] thys [const AjPText] Text data object
251 ** @param [w] qry [AjPStr*] Query string in full
252 ** @return [void]
253 **
254 ** @release 6.4.0
255 ** @@
256 ******************************************************************************/
257
textMakeQry(const AjPText thys,AjPStr * qry)258 static void textMakeQry(const AjPText thys, AjPStr* qry)
259 {
260 ajDebug("textMakeQry (Id <%S> Formatstr <%S> Db <%S> "
261 "Filename <%S>)\n",
262 thys->Id, thys->Formatstr, thys->Db,
263 thys->Filename);
264
265 /* ajTextTrace(thys); */
266
267 if(ajStrGetLen(thys->Db))
268 ajFmtPrintS(qry, "%S-id:%S", thys->Db, thys->Id);
269 else
270 {
271 ajFmtPrintS(qry, "%S::%S:%S", thys->Formatstr,
272 thys->Filename,thys->Id);
273 }
274
275 ajDebug(" result: <%S>\n",
276 *qry);
277
278 return;
279 }
280
281
282
283
284 /* @section text data modifiers ***********************************************
285 **
286 ** Text data modifiers
287 **
288 ** @fdata [AjPText]
289 **
290 ** @nam3rule Clear clear internal values
291 **
292 ** @argrule * text [AjPText] Text data object
293 **
294 ** @valrule * [void]
295 **
296 ** @fcategory modify
297 **
298 ******************************************************************************/
299
300
301
302
303 /* @func ajTextClear **********************************************************
304 **
305 ** Resets all data for a text data object so that it can be reused.
306 **
307 ** @param [u] text [AjPText] text data
308 ** @return [void]
309 **
310 ** @release 6.4.0
311 ** @@
312 ******************************************************************************/
313
ajTextClear(AjPText text)314 void ajTextClear(AjPText text)
315 {
316 AjPStr tmpstr = NULL;
317
318 if(MAJSTRGETLEN(text->Id))
319 ajStrSetClear(&text->Id);
320
321 if(MAJSTRGETLEN(text->Db))
322 ajStrSetClear(&text->Db);
323
324 if(MAJSTRGETLEN(text->Setdb))
325 ajStrSetClear(&text->Setdb);
326
327 if(MAJSTRGETLEN(text->Full))
328 ajStrSetClear(&text->Full);
329
330 if(MAJSTRGETLEN(text->Qry))
331 ajStrSetClear(&text->Qry);
332
333 if(MAJSTRGETLEN(text->Formatstr))
334 ajStrSetClear(&text->Formatstr);
335
336 if(MAJSTRGETLEN(text->Filename))
337 ajStrSetClear(&text->Filename);
338
339 while(ajListstrPop(text->Lines, &tmpstr))
340 ajStrDel(&tmpstr);
341
342 text->Count = 0;
343 text->Fpos = 0L;
344 text->Format = 0;
345
346 return;
347 }
348
349
350
351
352 /* @datasection [none] Miscellaneous functions ********************************
353 **
354 ** Functions to initialise and clean up internals
355 **
356 ** @nam2rule Text Text internals
357 **
358 ******************************************************************************/
359
360
361
362
363 /* @section exit **************************************************************
364 **
365 ** Functions called on exit from the program by ajExit to do
366 ** any necessary cleanup and to report internal statistics to the debug file
367 **
368 ** @fdata [none]
369 ** @fnote general exit functions, no arguments
370 **
371 ** @nam3rule Exit Cleanup and report on exit
372 **
373 ** @valrule * [void]
374 **
375 ** @fcategory misc
376 ******************************************************************************/
377
378
379
380
381 /* @func ajTextExit ***********************************************************
382 **
383 ** Cleans up text processing internal memory
384 **
385 ** @return [void]
386 **
387 ** @release 6.4.0
388 ** @@
389 ******************************************************************************/
390
ajTextExit(void)391 void ajTextExit(void)
392 {
393 ajTextinExit();
394 ajTextoutExit();
395
396 return;
397 }
398