1 : /*
2 : * This file is part of MIN Test Framework. Copyright © 2008 Nokia Corporation
3 : * and/or its subsidiary(-ies).
4 : * Contact: Konrad Marek Zapalowicz
5 : * Contact e-mail: DG.MIN-Support@nokia.com
6 : *
7 : * This program is free software: you can redistribute it and/or modify it
8 : * under the terms of the GNU General Public License as published by the Free
9 : * Software Foundation, version 2 of the License.
10 : *
11 : * This program is distributed in the hope that it will be useful, but WITHOUT
12 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 : * more details. You should have received a copy of the GNU General Public
15 : * License along with this program. If not, see
16 : * <http://www.gnu.org/licenses/>.
17 : */
18 :
19 :
20 : /**
21 : * @file text.c
22 : * @version 0.1
23 : * @brief This file contains implementation of Text interface.
24 : */
25 :
26 : /* ------------------------------------------------------------------------- */
27 : /* INCLUDE FILES */
28 : #include <stdio.h>
29 : #include <min_text.h>
30 :
31 : /* ------------------------------------------------------------------------- */
32 : /* EXTERNAL DATA STRUCTURES */
33 : /* None */
34 :
35 : /* ------------------------------------------------------------------------- */
36 : /* EXTERNAL GLOBAL VARIABLES */
37 : /* None */
38 :
39 : /* ------------------------------------------------------------------------- */
40 : /* EXTERNAL FUNCTION PROTOTYPES */
41 : /* None */
42 :
43 : /* ------------------------------------------------------------------------- */
44 : /* GLOBAL VARIABLES */
45 : /* None */
46 :
47 : /* ------------------------------------------------------------------------- */
48 : /* CONSTANTS */
49 : /* None */
50 :
51 : /* ------------------------------------------------------------------------- */
52 : /* MACROS */
53 :
54 : /* ------------------------------------------------------------------------- */
55 : /* LOCAL GLOBAL VARIABLES */
56 : /* None */
57 :
58 : /* ------------------------------------------------------------------------- */
59 : /* LOCAL CONSTANTS AND MACROS */
60 : /* None */
61 :
62 : /* ------------------------------------------------------------------------- */
63 : /* MODULE DATA STRUCTURES */
64 : /* None */
65 :
66 : /* ------------------------------------------------------------------------- */
67 : /* LOCAL FUNCTION PROTOTYPES */
68 : /* None */
69 :
70 : /* ------------------------------------------------------------------------- */
71 : /* FORWARD DECLARATIONS */
72 : /* None */
73 :
74 : /* ------------------------------------------------------------------------- */
75 : /* ==================== LOCAL FUNCTIONS ==================================== */
76 : /* None */
77 :
78 : /* ------------------------------------------------------------------------- */
79 : /* ======================== FUNCTIONS ====================================== */
80 : /* ------------------------------------------------------------------------- */
81 : Text *tx_create (const char *txt)
82 141865 : {
83 141865 : Text *retval = NEW (Text);
84 141865 : unsigned int tlen = 0;
85 141865 : unsigned int tmp = 0;
86 :
87 141865 : retval->max_size_ = MaxTextBufferSize;
88 141865 : retval->buf_ = NEW2 (char, MaxTextBufferSize);
89 141865 : memset (retval->buf_, '\0', MaxTextBufferSize);
90 141865 : retval->size_ = 0;
91 :
92 141865 : if (txt == INITPTR || txt == NULL) {
93 : goto EXIT;
94 : }
95 :
96 141759 : tlen = strlen (txt);
97 :
98 141759 : if (tlen == 0) {
99 125747 : goto EXIT;
100 : }
101 :
102 16012 : tmp = ((int)((float)tlen / (float)MaxTextBufferSize)) + 1;
103 :
104 16012 : if (tmp > 1) {
105 3 : DELETE (retval->buf_);
106 3 : retval->max_size_ = tmp * MaxTextBufferSize;
107 3 : retval->buf_ = NEW2 (char, retval->max_size_);
108 3 : memset (retval->buf_, '\0', retval->max_size_);
109 : }
110 :
111 16012 : STRCPY (retval->buf_, txt, tlen);
112 16012 : retval->size_ = tlen;
113 141865 : EXIT:
114 141865 : return retval;
115 : }
116 :
117 : /* ------------------------------------------------------------------------- */
118 : void tx_destroy (Text ** txt)
119 141626 : {
120 141626 : if (txt != INITPTR) {
121 141625 : if (*txt != INITPTR) {
122 141549 : DELETE ((*txt)->buf_)
123 141549 : DELETE (*txt);
124 141549 : *txt = INITPTR;
125 : }
126 : }
127 141626 : }
128 :
129 : /* ------------------------------------------------------------------------- */
130 : void tx_append (Text * dest, const Text * src)
131 118 : {
132 118 : unsigned int space = 0;
133 118 : unsigned int totalsize = 0;
134 118 : unsigned int tmp = 0;
135 118 : char *tmpbuf = INITPTR;
136 :
137 118 : if (dest == INITPTR) {
138 2 : goto EXIT;
139 : }
140 116 : if (src == INITPTR) {
141 1 : goto EXIT;
142 : }
143 :
144 115 : space = dest->max_size_ - dest->size_;
145 :
146 115 : if (src->size_ > space) {
147 3 : totalsize = dest->size_ + src->size_;
148 3 : tmp =
149 : ((int)((float)totalsize / (float)MaxTextBufferSize)) + 1;
150 3 : dest->max_size_ = tmp * MaxTextBufferSize;
151 3 : tmpbuf = NEW2 (char, dest->max_size_);
152 3 : memset (tmpbuf, '\0', dest->max_size_);
153 3 : STRCPY (tmpbuf, dest->buf_, dest->size_);
154 3 : STRCPY (&tmpbuf[dest->size_], src->buf_, src->size_);
155 3 : dest->size_ = totalsize;
156 3 : DELETE (dest->buf_);
157 3 : dest->buf_ = tmpbuf;
158 : } else {
159 112 : STRCPY (&dest->buf_[dest->size_], src->buf_, src->size_);
160 112 : dest->size_ += src->size_;
161 : }
162 :
163 118 : EXIT:
164 : return;
165 : }
166 :
167 : /* ------------------------------------------------------------------------- */
168 : void tx_copy (Text * dest, const Text * src)
169 118 : {
170 118 : if (dest == INITPTR) {
171 2 : goto EXIT;
172 : }
173 116 : if (src == INITPTR) {
174 1 : goto EXIT;
175 : }
176 115 : if (dest == src) {
177 1 : goto EXIT;
178 : }
179 :
180 114 : DELETE (dest->buf_);
181 :
182 114 : dest->max_size_ = src->max_size_;
183 114 : dest->buf_ = NEW2 (char, dest->max_size_);
184 114 : memset (dest->buf_, '\0', dest->max_size_);
185 114 : dest->size_ = src->size_;
186 :
187 114 : STRCPY (dest->buf_, src->buf_, dest->size_);
188 118 : EXIT:
189 : return;
190 : }
191 :
192 : /* ------------------------------------------------------------------------- */
193 : void tx_c_append (Text * dest, const char *src)
194 1807686 : {
195 1807686 : unsigned int space = 0;
196 1807686 : unsigned int totalsize = 0;
197 1807686 : unsigned int tmpsize = 0;
198 1807686 : unsigned int slen = 0;
199 1807686 : char *tmpbuf = INITPTR;
200 :
201 1807686 : if (dest == INITPTR) {
202 2 : goto EXIT;
203 : }
204 1807684 : if (src == INITPTR) {
205 1 : goto EXIT;
206 : }
207 :
208 1807683 : space = dest->max_size_ - dest->size_;
209 1807683 : slen = strlen (src);
210 :
211 1807683 : if (slen >= space) {
212 125 : totalsize = dest->size_ + slen;
213 125 : tmpsize =
214 : ((int)((float)totalsize / (float)MaxTextBufferSize)) + 1;
215 125 : dest->max_size_ = tmpsize * MaxTextBufferSize;
216 125 : tmpbuf = NEW2 (char, dest->max_size_);
217 125 : memset (tmpbuf, '\0', dest->max_size_);
218 125 : STRCPY (tmpbuf, dest->buf_, dest->size_);
219 125 : STRCPY (&tmpbuf[dest->size_], src, slen);
220 125 : dest->size_ = totalsize;
221 125 : DELETE (dest->buf_);
222 125 : dest->buf_ = tmpbuf;
223 : } else {
224 1807558 : STRCPY (&dest->buf_[dest->size_], src, slen);
225 1807558 : dest->size_ += slen;
226 : }
227 :
228 1807686 : EXIT:
229 : return;
230 : }
231 :
232 : /* ------------------------------------------------------------------------- */
233 : void tx_prepend (Text * src, Text * dest)
234 0 : {
235 0 : Text *tmp=INITPTR;
236 0 : if( src==INITPTR || dest == INITPTR ) return;
237 0 : tmp=src;
238 0 : src=dest;
239 0 : dest=tmp;
240 0 : tx_append(src,dest);
241 : }
242 :
243 : /* ------------------------------------------------------------------------- */
244 : void tx_c_prepend (Text * src, const char * dest)
245 108 : {
246 108 : Text *tmp=INITPTR;
247 108 : if( src==INITPTR || dest == INITPTR ) return;
248 108 : tmp = tx_create(dest);
249 108 : tx_append(tmp,src);
250 108 : tx_copy(src,tmp);
251 108 : tx_destroy (&tmp);
252 : }
253 :
254 : /* ------------------------------------------------------------------------- */
255 : void tx_c_copy (Text * dest, const char *src)
256 136 : {
257 136 : unsigned int slen = 0;
258 136 : unsigned int tmp = 0;
259 :
260 136 : if (dest == INITPTR) {
261 2 : goto EXIT;
262 : }
263 134 : if (src == INITPTR) {
264 1 : goto EXIT;
265 : }
266 133 : if (dest->buf_ == src) {
267 0 : goto EXIT;
268 : }
269 :
270 133 : slen = strlen (src);
271 133 : tmp = ((int)((float)slen / (float)MaxTextBufferSize)) + 1;
272 133 : DELETE (dest->buf_);
273 :
274 133 : dest->max_size_ = tmp * MaxTextBufferSize;
275 133 : dest->buf_ = NEW2 (char, dest->max_size_);
276 133 : memset (dest->buf_, '\0', dest->max_size_);
277 133 : dest->size_ = slen;
278 :
279 133 : STRCPY (dest->buf_, src, slen);
280 136 : EXIT:
281 : return;
282 : }
283 :
284 : /* ------------------------------------------------------------------------- */
285 : char *tx_get_buf (Text * txt)
286 122370 : {
287 : char *retval;
288 122370 : if (txt == INITPTR) {
289 0 : return INITPTR;
290 : } else {
291 122370 : retval = NEW2 (char, txt->size_ + 1);
292 122370 : STRCPY (retval, txt->buf_, txt->size_ + 1);
293 122370 : return retval;
294 : }
295 : }
296 :
297 : /* ------------------------------------------------------------------------- */
298 : char *tx_share_buf (Text * txt)
299 38483 : {
300 38483 : if (txt == INITPTR) {
301 0 : return INITPTR;
302 : } else {
303 38483 : return txt->buf_;
304 : }
305 : }
306 :
307 : /* ------------------------------------------------------------------------- */
308 : void tx_back_trim (Text * txt, const char *chars)
309 122351 : {
310 122351 : char *c = INITPTR;
311 122351 : int tmp = 0;
312 122351 : int tmp2 = 0;
313 122351 : if (txt == INITPTR) {
314 0 : return;
315 : }
316 122351 : if (chars == INITPTR) {
317 0 : return;
318 : }
319 122351 : if (txt->size_ == 0) {
320 14266 : return;
321 : }
322 108085 : tmp = ((int)((float)txt->size_ / (float)MaxTextBufferSize)) + 1;
323 108085 : c = &txt->buf_[txt->size_ - 1];
324 219612 : while (strchr (chars, *c) != NULL && c != &txt->buf_[0]) {
325 3442 : c--;
326 : }
327 108085 : *(c + 1) = '\0';
328 108085 : txt->size_ = strlen (txt->buf_);
329 108085 : tmp2 = ((int)((float)txt->size_ / (float)MaxTextBufferSize)) + 1;
330 108085 : if (tmp != tmp2) {
331 0 : txt->max_size_ = tmp2 * MaxTextBufferSize;
332 0 : c = NEW2 (char, txt->max_size_);
333 0 : memset (c, '\0', txt->max_size_);
334 0 : STRCPY (c, txt->buf_, txt->size_);
335 0 : DELETE (txt->buf_);
336 0 : txt->buf_ = c;
337 : }
338 : }
339 :
340 : /* ------------------------------------------------------------------------- */
341 : char tx_at (Text * txt, unsigned int index)
342 0 : {
343 0 : if(txt==INITPTR || index>=txt->size_) return '\0';
344 0 : else return txt->buf_[index];
345 : }
346 :
347 : /* ------------------------------------------------------------------------- */
348 : void tx_int_append(Text * dest,const char *flags, int src)
349 28993 : {
350 : /* assumed that there will be no number longer than 63 digits */
351 : char tmp[64];
352 : char format[64];
353 28993 : if (dest==INITPTR) { return; }
354 28993 : memset(tmp,'\0',64);
355 28993 : memset(format,'\0',64);
356 28993 : sprintf(format,"%%%sd",flags);
357 28993 : sprintf(tmp,format,src);
358 28993 : tx_c_append(dest,tmp);
359 : }
360 :
361 : /* ------------------------------------------------------------------------- */
362 : /* ================= OTHER EXPORTED FUNCTIONS ============================== */
363 : /* None */
364 :
365 : /* ------------------------------------------------------------------------- */
366 : /* ================= TESTS FOR LOCAL FUNCTIONS ============================= */
367 : #ifdef MIN_UNIT_TEST
368 : #include "text.tests"
369 : #endif /* MIN_UNIT_TEST */
370 : /* ------------------------------------------------------------------------- */
371 : /* End of file */
|