1 /*----------------------------------------------------------------------------
2 buffarray.cc (definition of string array)
3 This file is a part of topaz systems
4 Copyright: Hisao Kawaura, All rights reserved
5 1997 - 98
6
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 ----------------------------------------------------------------------------*/
23
24 #include "buffarray.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <string>
28
buffarray()29 buffarray::buffarray()
30 {
31 int i;
32 arraylength = 0;
33 increment = 10;
34
35 bufflength = (arraylength / increment + 1) * increment;
36
37 strarray = new std::string * [bufflength];
38 memset(strarray, 0, bufflength * sizeof(int));
39 for (i = 0; i < arraylength; i++)
40 strarray[i] = new std::string("");
41
42 }
43
44
~buffarray()45 buffarray::~buffarray()
46 {
47 int i;
48 for (i = 0; i < arraylength; i++)
49 delete strarray[i];
50
51 delete [] strarray;
52 }
53
setscalar(const char * val)54 bool buffarray::setscalar(const char *val)
55 {
56 setbuff(0);
57 return add(val);
58 }
59
setdata(int pos,const char * val)60 bool buffarray::setdata(int pos, const char *val)
61 {
62 if (pos < arraylength)
63 *strarray[pos] = std::string(val);
64 else
65 {
66 changebufflength(pos + 1);
67 for (int i = arraylength; i <= pos; i++)
68 strarray[i] = new std::string("");
69
70 *strarray[pos] = std::string(val);
71 arraylength = pos + 1;
72 }
73 return true;
74 }
75
getdata(int pos)76 std::string buffarray::getdata(int pos)
77 {
78 std::string ret = std::string("");
79 if (pos < arraylength)
80 return *strarray[pos];
81 else
82 return ret;
83 }
84
getarraylength()85 int buffarray::getarraylength()
86 {
87 return arraylength;
88 }
89
setbuff(int length)90 bool buffarray::setbuff(int length)
91 {
92 int i;
93
94 for (i = 0; i < arraylength; i++)
95 delete strarray[i];
96 delete [] strarray;
97
98 arraylength = 0;
99 bufflength = (length / increment + 1) * increment;
100 strarray = new std::string * [bufflength];
101 if (strarray == 0)
102 return false;
103
104 memset(strarray, 0, bufflength * sizeof(int));
105 return true;
106 }
107
setarray(int length)108 bool buffarray::setarray(int length)
109 {
110 int i;
111
112 if (setbuff(length) == false)
113 return false;
114
115 arraylength = length;
116
117 for (i = 0; i < arraylength; i++)
118 strarray[i] = new std::string("");
119
120 return true;
121
122 }
123
124
add(const char * val)125 bool buffarray::add(const char *val)
126 {
127
128 if (arraylength < bufflength)
129 {
130 arraylength++;
131 strarray[arraylength - 1] = new std::string(val);
132 }
133 else
134 {
135 changebufflength(arraylength + 1);
136 arraylength++;
137 strarray[arraylength - 1] = new std::string(val);
138 }
139
140 if (strarray[arraylength - 1] == 0)
141 return false;
142 else
143 return true;
144 }
145
addat(int pos,const char * val)146 bool buffarray::addat(int pos, const char *val)
147 {
148 int i;
149 if (pos < arraylength)
150 {
151 if (arraylength >= bufflength)
152 {
153 if (changebufflength(++arraylength) == false)
154 return false;
155 }
156 else
157 arraylength++;
158
159 for (i = arraylength - 1; i > pos; i--)
160 strarray[i] = strarray[i - 1];
161
162 strarray[pos] = new std::string(val);
163
164 return true;
165 }
166 else
167 return add(val);
168 }
169
remove(int pos)170 bool buffarray::remove(int pos)
171 {
172 int i;
173 for (i = pos; i < arraylength - 1; i++)
174 {
175 if (setdata(i, getdata(i + 1).c_str()) == false)
176 return false;
177 }
178
179 delete strarray[arraylength - 1];
180 arraylength--;
181 return true;
182 }
183
changebufflength(int newlength)184 bool buffarray::changebufflength(int newlength)
185 {
186 int i;
187 std::string **tempstrarray;
188
189 if (newlength <= bufflength)
190 return true;
191
192 bufflength = (newlength / increment + 1) * increment;
193 tempstrarray = new std::string * [bufflength];
194 if (tempstrarray == 0)
195 return false;
196
197 memset(tempstrarray, 0, bufflength * sizeof(int));
198
199 for (i = 0; i < arraylength; i++)
200 tempstrarray[i] = strarray[i];
201
202 delete [] strarray;
203
204 strarray = tempstrarray;
205 return true;
206 }
207
operator =(const buffarray & buff)208 buffarray& buffarray::operator = (const buffarray& buff)
209 {
210 int i;
211 setbuff(buff.bufflength);
212 for (i = 0; i < buff.arraylength; i++)
213 add(buff.strarray[i]->c_str());
214 return *this;
215 }
216
operator [](int pos)217 std::string& buffarray::operator [] (int pos)
218 {
219 return *strarray[pos];
220 }
221
222
223
224
225
226