1 /*****************************************************************************/
2 /*                                                                           */
3 /*                 (C) Copyright 1994-1996  Alberto Pasquale                 */
4 /*                 Portions (C) Copyright 1999 Per Lundberg                  */
5 /*                                                                           */
6 /*                   A L L   R I G H T S   R E S E R V E D                   */
7 /*                                                                           */
8 /*****************************************************************************/
9 /*                                                                           */
10 /*   How to contact the author:  Alberto Pasquale of 2:332/504@fidonet       */
11 /*                               Viale Verdi 106                             */
12 /*                               41100 Modena                                */
13 /*                               Italy                                       */
14 /*                                                                           */
15 /*****************************************************************************/
16 
17 #include "apgenlib.hpp"
18 #include <string.h>
19 
20 
Init()21 void WordStore::Init ()
22 {
23     usedsize = strlen (storebuf);
24     nextp = storebuf;
25     lastp = NULL;
26 }
27 
28 
WordStore(char * buf,int size,int flags)29 WordStore::WordStore (char *buf, int size, int flags)
30 {
31     storebuf = buf;
32     sflags = flags;
33 
34     if (size) {
35         storesize = size - 1;
36         if (!(flags & WST_NoClear))
37             Clear ();
38         else
39             Init ();
40     } else {
41         storesize = 0;
42         Init ();
43     }
44 }
45 
46 
Clear()47 void WordStore::Clear ()
48 {
49     storebuf[0] = '\0';
50     Init ();
51 }
52 
53 
Store(const char * tok)54 int WordStore::Store (const char *tok)
55 {
56     if (!storesize)
57         return -1;
58 
59     bool first = (usedsize == 0);
60 
61     int toklen = strlen (tok);
62     int totlen = toklen + first ? 0 : 1;
63 
64     if (totlen > (storesize - usedsize))
65         return -1;
66 
67     if (!first)
68         storebuf[usedsize++] = ' ';
69 
70     strcpy (storebuf+usedsize, tok);
71     usedsize += toklen;
72 
73     return 0;
74 }
75 
76 
GetFirst()77 char *WordStore::GetFirst ()
78 {
79     nextp = storebuf;
80     return GetNext ();
81 }
82 
83 
GetNext()84 char *WordStore::GetNext ()
85 {
86     lastp = nextp;
87     if (GetName (nextp, retbuf, PATH_MAX) <= 0)
88         return NULL;
89     return retbuf;
90 }
91 
92 
RemoveLast()93 void WordStore::RemoveLast ()
94 {
95     if (!lastp)
96         return;
97     if (*lastp == '\0')
98         return;
99 
100     if (*nextp == '\0') {
101         while (lastp > storebuf)        // remove useless space
102             if (*(lastp-1) == ' ')
103                 lastp --;
104     }
105 
106     int movelen = strlen (nextp) + 1;   // includes terminating NULL
107 
108     memmove ((char *)lastp, nextp, movelen);
109 
110     if (storesize)
111         usedsize -= (nextp - lastp);
112 
113     nextp = lastp;
114     lastp = NULL;
115 }
116 
117 
AddWord(const char * src,char * dest,size_t totsize)118 int AddWord (const char *src, char *dest, size_t totsize)
119 {
120     if (*src == '\0')           // no word to add
121         return 1;
122 
123     size_t curlen = strlen (dest);
124     size_t srclen = strlen (src);
125     if (curlen != 0)        // separating space
126         srclen ++;
127 
128     if ((curlen + srclen) >= totsize)
129         return -1;
130 
131     if (curlen != 0)
132         dest[curlen++] = ' ';
133     strcpy (dest+curlen, src);
134 
135     return 0;
136 }
137 
138 
139 
140