File: txtutil.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (461 lines) | stat: -rw-r--r-- 10,546 bytes parent folder | download | duplicates (5)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (c) 1993-1995 Vectaport Inc.
 * Copyright (c) 1989 Triple Vision, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of the copyright holders not be used in 
 * advertising or publicity pertaining to distribution of the software without 
 * specific, written prior permission.  The copyright holders make no 
 * representations about the suitability of this software for any purpose.  
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
txtutil.c        TeXT storage UTILities

Externals:       unsigned int txtstore()
                 unsigned int txtread()
                 unsigned int txtopenclose()
                 unsigned int txtkwsrch()
                 unsigned int txtprint()

Summary:         Routines to manage the text storage table.

Author:          Rob Graber   7-89
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "comutil.ci"

#define TXT_STORE_PATH "$comtxt$.$$$"

/* Local globals */
static int Currid = -1;
static int Lastid = -1;
static FILE *fd   = NULL;
static char tmpstr[256];


/*!

txtstore          STORE TeXT in text table


Summary:

*/

unsigned int txtstore(int new_entry,char * txtstr)


/*!
Return Value:  -1 = Error, # = Text ID


Parameters:

Type      Name               IO    Description
----      ----               --    -----------                 */
#ifdef DOC
int       new_entry     ;/*  I     New entry flag.
                                   1 = Start new entry
                                   0 = Continue previous entry */
char *    txtstr        ;/*  I     String to write.            */
#endif


/*!
Description:

`txtstore` stores a text string in the text table and returns an
ID for it.  If the `new_entry` flag is set a new ID is created and
returned, otherwise `txtstr` is appended to the previous entry.

!*/

#undef     TITLE
#define    TITLE  "txtstore"

{
    int index;

    /* Check for file open */
    if (fd == NULL)
    {
        /* Text storage file not open */
        COMERR_SET(ERR_TXTUT_FNOTOPEN);
        return(-1);
    }

    /* Check for string beginning with internal delimiter */
    if ( !strncmp(txtstr, ".!#ID#",6))
    {
        /* String begins with internal delimiter string */
        COMERR_SET(ERR_TXTUT_DELIM);
        return(-1);
    }

    /* Check for new entry */
    if (new_entry)
    {
        Lastid++;
        fprintf(fd,".!#ID#%d\n",Lastid);
    }

    /* Check for multiple \n's */
    index = strlen(txtstr) - 1;
    if (txtstr[index] == '\n') txtstr[index] = '\0';

    /* Write text string */
    fprintf(fd,"%s\n",txtstr);

    /* Return */
    return(Lastid);
}

/*!

txtread          READ TeXT from text table


Summary:

*/

unsigned int txtread (


/*!
Return Value:  0 = OK, -1 = ERROR


Parameters:

Type       Name               IO    Description
----       ----               --    -----------*/
unsigned   id            ,/*  I     Text ID           */
char *     txtstr         /*  I     String to write.  */

)


/*!
Description:
`txtread` reads a text string from the text table.
If `id` is the same as the previous `id` no seek is
done and the next line from the file is returned (unless it
is the end of the text segment).  A -1 value for ID will cause
the current file position to be reset.

!*/

#undef     TITLE
#define    TITLE  "txtread"

{
    char *tst, cmpstr[15];

    /* Check for file open */
    if (fd == NULL)
    {
        /* Text storage file not open */
        COMERR_SET(ERR_TXTUT_FNOTOPEN);
        return(-1);
    }

    /* Check for file reset */
    if (id == -1)
    {
        Currid = -1;
        return(0);
    }

    /* Check for new id */
    if (id != Currid)
    {
        /* Reset current id pointer and seek to beginning of file */
        Currid = id;
        fseek(fd,0L,SEEK_SET);

        /* Search for .!#ID#id delimiter */
        sprintf( cmpstr, ".!#ID#%d\n",id);
        while( (tst = fgets(txtstr, 256, fd)) != NULL )
            if (!strcmp(txtstr, cmpstr)) break;

        /* Check for id not found */
        if (tst == NULL)
        {
            Currid = -1;
            return(-1);
        }
    }

    /* Read next line of file */
    tst = fgets(txtstr, 256, fd);

    /* Check for end of text entry (or EOF) */
    if ( (!strncmp(txtstr, ".!#ID#",6)) || (tst == NULL) )
    {
        Currid = -1;
        txtstr[0] = '\0';
        return(-1);
    }

    /* Return */
    return(0);
}


/*!

txtopenclose          TeXT table file OPEN and CLOSE


Summary:

*/

unsigned int txtopenclose(int openclose)


/*!
Return Value:  0 = OK, -1 = ERROR


Parameters:

Type      Name               IO    Description
----      ----               --    -----------               */
#ifdef DOC
int       openclose     ;/*  I     Txt file open close flag.
                                   0 = open
                                   1 = close                 */
#endif


/*!
Description:

`txtopenclose` opens or closes the txt file based on the `openclose` flag.

!*/

#undef     TITLE
#define    TITLE  "txtopenclose"

{
    /* Check for open/close */
    if (openclose == 0)
    {
        /* If file is not already open - open it */
        if (fd == NULL)
        {
            fd = fopen(TXT_STORE_PATH,"w+");
            if (fd == NULL)
            {
                /* Error opening text storage file */
                COMERR_SET(ERR_TXTUT_FOPEN);
                return(-1);
            }
        }
    }
    else
        /* Close file and remove */
        if (fd != NULL) 
        {
            fclose(fd);
            unlink(TXT_STORE_PATH);
        }

   return(0);
}



/*!

txtkwsrch          TeXT Key Word SeaRCH


Summary:

*/

unsigned int txtkwsrch(char * keyword,int bol,char * rdstr)


/*!
Return Value:  -1 = ERROR, # = text ID


Parameters:

Type      Name             IO    Description
----      ----             --    -----------                         */
#ifdef DOC
char *    keyword     ;/*  I     String to search table for          */
int       bol         ;/*  I     Search flag
                                    0 = whole line 
                                    1 = beginning only               */
char *    rdstr       ;/*  IO    String to return value in. 
                                 Must be alloced in calling program.
                                 If rdstr is NULL an internal string
                                 is used.                            */
#endif

/*!
Description:

`txtkwsrch` searches the text table and returns the first next ID
containing `keyword`. Use a NULL keyword to reset the file.

NOTE: The file position can be changed by reading or writing using txtstore
and txtread.  The file needs to be opened using txtopenclose prior to calling
kwsearch.

!*/

#undef     TITLE
#define    TITLE  "txtkwsrch"

{
    char *tst, *rdptr;
    int id, index;

    /* Check for file open */
    if (fd == NULL)
    {
        /* Text storage file not open */
        COMERR_SET(ERR_TXTUT_FNOTOPEN);
        return(-1);
    }

    /* Check for file reset */
    if (keyword == NULL)
    {
        fseek(fd,0L,SEEK_SET);
        return(0);
    }

    /* Set which read buffer to use */
    if (rdstr == NULL)
       rdptr = tmpstr;
    else
       rdptr = rdstr;

    id = -1;
    while( (tst = fgets(rdptr, 256, fd)) != NULL )
    {

        /* Check for .!#ID#id delimiter */
        if (!strncmp(rdptr, ".!#ID#",6)) 
            id = atoi(&rdptr[6]);
        else
        {
            if (bol)
            {
                /* One check only */  
                if (!strncmp(rdptr, keyword, strlen(keyword)))
                if (id != -1) return(id);
            }
            else
            {
                /* Search line for string */
                for (index = 0; index < strlen(rdptr); index++)
                    if (!strncmp(&rdptr[index], keyword, strlen(keyword)))
                        if (id != -1) return(id);
            }
        }
    }
    return(-1);
}

#if 0
/*!

txtprint          TeXT PRINT


Summary:

*/

unsigned int txtprint(unsigned id,char * ignorestr,unsigned pause,
		      unsigned * nlines)


/*!
Return Value:  -1 if not found, 0 = OK


Parameters:

Type      Name           IO    Description
----      ----           --    -----------                        */
#ifdef DOC
unsigned  id        ;/*  I     ID to print                        */
char *    ignorestr ;/*  I     Strings beginning with ignorestr 
                               are not printed                    */
unsigned  pause     ;/*  I     Pause afeter 22 lines flag. 
                               0 = no pause                       */
unsigned  *nlines   ;/*  I/0   Number of lines printed prior to
                               this call of txtrpint. Updated on
                               output                             */
#endif

/*!
Description:

`txtprint` searchs the text table and prints out entry `id`.
Any string beginning with `ignorestr` are NOT printed.  This is
useful for user-defined delimiters within a block of text, specifically
the .!#KW# identifiers for COMTERP.
!*/

#undef     TITLE
#define    TITLE  "txtprint"

{
    int i, xsize, ysize;

    /* Illegal ID value */
    if (id == -1) return(-1);

    /* Constants */
    crt_size(&xsize, &ysize);

    /* Find first line */
    txtread(-1,tmpstr);
    if (txtread(id, tmpstr) == -1) return(-1);
    txtread(-1,tmpstr);

    /* Print until end of id */
    for (i = *nlines; txtread(id, tmpstr) != -1; i++)
    {
        if (strncmp(ignorestr, tmpstr, strlen(ignorestr)))
        {
            printf("%s",tmpstr);
            *nlines = *nlines+1;
            if (pause)
                if ( (i%ysize == 0) && (i != 0) )
                    key_to_continue();
        }
    }
    return(0);
}

#endif