File: embdata.c

package info (click to toggle)
emboss 6.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 571,248 kB
  • ctags: 39,971
  • sloc: ansic: 460,578; java: 29,439; perl: 13,573; sh: 12,740; makefile: 3,275; csh: 706; asm: 351; xml: 239; pascal: 237; modula3: 8
file content (338 lines) | stat: -rw-r--r-- 8,841 bytes parent folder | download | duplicates (7)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* @source embdata ************************************************************
**
** General routines for data files
**
** @author Copyright (c) 1999 Mark Faller
** @version $Revision: 1.22 $
** @modified $Date: 2012/12/07 10:23:28 $ by $Author: rice $
** @@
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301,  USA.
**
******************************************************************************/

#include "embdata.h"
#include "ajlib.h"
#include "ajlist.h"
#include "ajtable.h"
#include "ajfileio.h"




/*
** Routines for getting the data into the data structure. The data structure
** consists of a list of tables. This means the routine can read any amount
** of data from a file. It is up to the developer to know the order of the
** tables in the list and what each refers to
*/




static AjBool dataListNextLine(AjPFile pfile, const char *commentLine,
                               AjPStr * line);




/* @func embDataListDel *******************************************************
**
** Deletes the tables of data list. Calls ajTablestrFree for each table in the
** list, and then calls ajListFree to free the actual list.
**
** @param [w] data [AjPList*] is the list of data tables to delete
** @return [void]
**
**
** @release 1.0.0
** @@
******************************************************************************/

void embDataListDel(AjPList* data)
{
   AjIList iter;
   AjPTable table;

   iter = ajListIterNewread(*data);

   while(!ajListIterDone(iter))
   {
      table = ajListIterGet(iter);
      ajTablestrFree(&table);
   }

   ajListIterDel(&iter);
   ajListFree(data);

   return;
}




/* @funcstatic dataListNextLine ***********************************************
**
** Private function to read in the next line of data from the file. It is
** called from embDataListRead.
**
** @param [u] pfile [AjPFile] file pointer to the data file
** @param [r] commentLine [const char *] the character(s) used to describe the
**        start of a comment line in the data file
** @param [w] line [AjPStr *] Buffer to hold the current line
** @return [AjBool] returns AjTrue if found another line of input otherwise
**         returns AjFalse
**
**
** @release 1.0.0
** @@
******************************************************************************/

static AjBool dataListNextLine(AjPFile pfile, const char *commentLine,
			       AjPStr * line)
{
   ajlong i;
   AjBool test;

   test = ajReadlineTrim(pfile, line);

   while(test)
   {
      i = ajStrFindC(*line, commentLine);

      if(i!=0)
	  break;

      test = ajReadlineTrim(pfile, line);
   }

   if(test)
       return ajTrue;

   return ajFalse;
}




/* @func embDataListRead ******************************************************
**
** General routine for reading in data from a file. The keys and values of
** each table are stored as AjPStr.
**
** @param [w] data [AjPList] is the list of data tables.
** @param [u] pfile [AjPFile] pointer to the data file
** @return [void]
**
**
** @release 2.9.0
** @@
******************************************************************************/

void embDataListRead(AjPList data, AjPFile pfile)
{
    AjPStr line = NULL;
    AjPStrTok tokens = NULL;
    char whiteSpace[]  = " \t\n\r";
    char commentLine[] = "#";
    char endOfData[]   = "//";
    AjPStr key;
    AjPStr copyKey;
    AjPStr value;
    AjPTable table;			/* stored in the list */
    AjIList iter = NULL;
    AjPTable ptable;
    AjPStr tmp;

    tmp  = ajStrNew();
    line = ajStrNew();


    while(dataListNextLine(pfile, commentLine, &line))
    {
	ajStrTokenDel(&tokens);
	tokens = ajStrTokenNewC(line, whiteSpace);

	/* the first token is the key for the row */
	key = ajStrNew();
	ajStrTokenNextParse(tokens, &key);

	if(!ajStrGetLen(key))
	{
	    ajFmtError("Error, did not pick up first key");
	    ajFatal("Error, did not pick up first key");
	}

	while(1)
	{
	    /*
	     ** while there are more tokens generate new table in list and
	     ** add (key,value)
	     */
	    value = NULL;

	    if(ajStrTokenNextParse(tokens, &value))
	    {
		table = ajTablestrNewCase(350);
		copyKey = ajStrNewRef(key);
		ajTablePut(table, copyKey, value);
		ajListPushAppend(data, table);
	    }

	    else break;
	}
	ajStrDel(&value);

	while(dataListNextLine(pfile, commentLine, &line))
	{
	    /*
	     ** for rest of data iterate for each table in list adding
	     ** (key,value) to each
	     */
	    ajStrTokenDel(&tokens);
	    tokens = ajStrTokenNewC(line, whiteSpace);
	    ajStrTokenNextParse(tokens, &key);

	    /* check for end of data block*/
	    if(! ajStrCmpC(key, endOfData))
		break;

	    iter = ajListIterNewread(data);

	    while(!ajListIterDone(iter))
	    {
		ptable = ajListIterGet(iter);
		copyKey = ajStrNewRef(key);

		if(!ajStrTokenNextParse(tokens, &tmp))
                    break;

		value = ajStrNewRef(tmp);
		ajTablePut(ptable, copyKey, value);
	    }

	    ajListIterDel(&iter);
	}
    }

    ajStrDel(&tmp);
    ajStrDel(&line);
    ajStrTokenDel(&tokens);
    ajListIterDel(&iter);
    ajStrDel(&key);

    return;
}




/* @func embDataListGetTables *************************************************
**
** Returns a list of data tables as requested. The data must already have been
** read in and stored as a list of tables. An unsigned integer is used to
** request tables. The first table has a value of 1, the second a value of 2,
** the third a value of 4, the fourth a value of 8 etc. For example a value
** of 10 would request the second and fourth tables from the list in that
** order. Only returns a list of pointers to the data. It does not copy the
** tables.
**
** @param [r] fullList [const AjPList] The list containing all the tables
**                                     of data
** @param [w] returnList [AjPList] The new list containing just the tables
**        requested
** @param [r] required [ajuint] used to request tables. A value of 1
**        requests the first table, a value of 16 requests the fifth table,
**        a value of 14 returns the second third and fourth tables in the
**        original list.
** @return [void]
**
**
** @release 1.0.0
** @@
******************************************************************************/

void embDataListGetTables(const AjPList fullList, AjPList returnList,
			   ajuint required)
{
   AjIList iter;
   AjPTable table;

   iter = ajListIterNewread(fullList);

   while(!ajListIterDone(iter))
   {
      table = ajListIterGet(iter);

      if(required & 1)
          ajListPushAppend(returnList, table);

      required >>= 1;
   }

   ajListIterDel(&iter);

   return;
}




/* @func embDataListGetTable **************************************************
**
** Returns a single table of data from the list of data tables. The data must
** already have been read in and stored as a list of tables. An unsigned
** integer is used to request a table. The first table in the list has a
** value of 1, the second a value of 2, the third a value of 4, the fourth a
** value of 8 etc. For example a value of 64 would request the seventh data
** table in the list. When looking for which table to return the position of
** the lowest set bit in the value determines which table is returned i.e.
** a value of 66 would request the second table (not the seventh)
**
** @param [r] fullList [const AjPList] The list containing all the tables
**                                     of data
** @param [r] required [ajuint] used to request a table. A value of 1
**        requests the first table, a value of 16 requests the fifth table,
**        a value of 14 returns the second table in the original list.
** @return [AjPTable] the data table. Key and value are stored as AjPStrs
**
**
** @release 1.0.0
** @@
******************************************************************************/

AjPTable embDataListGetTable(const AjPList fullList, ajuint required)
{
   AjIList iter;
   AjPTable returnTable = NULL;

   iter = ajListIterNewread(fullList);

   while(!ajListIterDone(iter))
   {
      returnTable = ajListIterGet(iter);

      if(required & 1)
	  break;

      required >>= 1;
   }


   ajListIterDel(&iter);

   return returnTable;
}