~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/lib/std/shl/obj/Strvec.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ---------------------------------------------------------------------------
2
 
// - Strvec.cpp                                                              -
3
 
// - standard object library - string vector class implementation            -
4
 
// ---------------------------------------------------------------------------
5
 
// - This program is free software;  you can redistribute it  and/or  modify -
6
 
// - it provided that this copyright notice is kept intact.                  -
7
 
// -                                                                         -
8
 
// - This program  is  distributed in  the hope  that it will be useful, but -
9
 
// - without  any  warranty;  without  even   the   implied    warranty   of -
10
 
// - merchantability or fitness for a particular purpose.  In no event shall -
11
 
// - the copyright holder be liable for any  direct, indirect, incidental or -
12
 
// - special damages arising in any way out of the use of this software.     -
13
 
// ---------------------------------------------------------------------------
14
 
// - copyright (c) 1999-2007 amaury darsch                                   -
15
 
// ---------------------------------------------------------------------------
16
 
 
17
 
#include "Stdsid.hxx"
18
 
#include "Strvec.hpp"
19
 
#include "Buffer.hpp"
20
 
#include "Vector.hpp"
21
 
#include "Unicode.hpp"
22
 
#include "Integer.hpp"
23
 
#include "Exception.hpp"
24
 
 
25
 
namespace afnix {
26
 
 
27
 
  // -------------------------------------------------------------------------
28
 
  // - private section                                                       -
29
 
  // -------------------------------------------------------------------------
30
 
 
31
 
  // check that a character is in the c-string. 
32
 
  static bool match_break_sequence (const t_quad c, const t_quad* str) {
33
 
    long size = Unicode::strlen (str);
34
 
    // loop and compare
35
 
    for (long i = 0; i < size; i++)
36
 
      if (c == str[i]) return true;
37
 
    return false;
38
 
  }
39
 
 
40
 
  // -------------------------------------------------------------------------
41
 
  // - public section                                                        -
42
 
  // -------------------------------------------------------------------------
43
 
 
44
 
  // split this string with a sequence of characters
45
 
 
46
 
  Strvec Strvec::split (const String& name, const String& sbrk) {
47
 
    Strvec result;
48
 
    Buffer buffer;
49
 
    
50
 
    // first thing first - do we have a nil string
51
 
    if (name.length () == 0) return result;
52
 
    
53
 
    // get a unicode string representation
54
 
    t_quad* data = name.toquad ();
55
 
    t_quad* cptr = data;
56
 
    // fix the break sequence in case it is nil
57
 
    const t_quad* cbrk = (sbrk.length () == 0) ? Unicode::strdup (" \t\n") : 
58
 
                                                 sbrk.toquad ();
59
 
    // loop and accumulate - if a character match the break sequence
60
 
    // the buffer is inserted into the vector
61
 
    t_quad c = nilq;
62
 
    buffer.reset ();
63
 
    while ((c = *data++) != nilp) {
64
 
      if (match_break_sequence (c, cbrk) == true) {
65
 
        result.add (buffer.tostring());
66
 
        buffer.reset ();
67
 
        continue;
68
 
      }
69
 
      buffer.add (c);
70
 
    }
71
 
    // check if the buffer is not empty
72
 
    if (buffer.length () != 0) result.add (buffer.tostring());
73
 
    // clean the break sequence and return
74
 
    delete [] cbrk;
75
 
    delete [] cptr;
76
 
    return result;
77
 
  }
78
 
 
79
 
  // split this string with a default break sequence
80
 
 
81
 
  Strvec Strvec::split (const String& name) {
82
 
    return Strvec::split (name, "");
83
 
  }
84
 
 
85
 
  // -------------------------------------------------------------------------
86
 
  // - class section                                                         -
87
 
  // -------------------------------------------------------------------------
88
 
 
89
 
  // create an empty string vector
90
 
 
91
 
  Strvec::Strvec (void) {
92
 
    d_size   = 0;
93
 
    d_length = 0;
94
 
    p_vector = nilp;
95
 
  }
96
 
 
97
 
  // create a string vector with an original size
98
 
 
99
 
  Strvec::Strvec (const long size) {
100
 
    if (size < 0) throw Exception ("size-error","negative strvec size");
101
 
    d_size   = size;
102
 
    d_length = 0;
103
 
    p_vector = new String[d_size];
104
 
  }
105
 
 
106
 
  // copy constructor for this string vector
107
 
 
108
 
  Strvec::Strvec (const Strvec& that) {
109
 
    that.rdlock ();
110
 
    // copy arguments
111
 
    d_size   = that.d_length;
112
 
    d_length = that.d_length;
113
 
    p_vector = nilp;
114
 
    // create a new vector of strings and copy them
115
 
    if ((d_length > 0) && (that.p_vector != nilp)) {
116
 
      p_vector = new String[d_length];
117
 
      for (long i = 0; i < d_length; i++) p_vector[i] = that.p_vector[i];
118
 
    }
119
 
    that.unlock ();
120
 
  }
121
 
 
122
 
  // destroy this string vector
123
 
  
124
 
  Strvec::~Strvec (void) {
125
 
    delete [] p_vector;
126
 
  }
127
 
 
128
 
  // return the class name
129
 
 
130
 
  String Strvec::repr (void) const {
131
 
    return "Strvec";
132
 
  }
133
 
 
134
 
  // return the string vector serial code
135
 
 
136
 
  t_byte Strvec::serialid (void) const {
137
 
    return SERIAL_STRV_ID;
138
 
  }
139
 
 
140
 
  // serialize this string vector
141
 
 
142
 
  void Strvec::wrstream (Output& os) const {
143
 
    rdlock ();
144
 
    try {
145
 
      // write the vector length
146
 
      Integer vlen (d_length);
147
 
      vlen.wrstream (os);
148
 
      // write the strings
149
 
      for (long i = 0; i < d_length; i++) {
150
 
        p_vector[i].wrstream (os);
151
 
      }
152
 
      unlock ();
153
 
    } catch (...) {
154
 
      unlock ();
155
 
      throw;
156
 
    }
157
 
  }
158
 
 
159
 
  // deserialize this string vector
160
 
 
161
 
  void Strvec::rdstream (Input& is) {
162
 
    wrlock ();
163
 
    try {
164
 
      // reset the string vector
165
 
      reset  ();
166
 
      // get the vector length
167
 
      Integer vlen;
168
 
      vlen.rdstream (is);
169
 
      long len = vlen.tointeger ();
170
 
      // read in each object
171
 
      for (long i = 0; i < len; i++) {
172
 
        String data;
173
 
        data.rdstream (is);
174
 
        add (data);
175
 
      }
176
 
      unlock ();
177
 
    } catch (...) {
178
 
      reset  ();
179
 
      unlock ();
180
 
      throw;
181
 
    }
182
 
  }
183
 
 
184
 
  // assign a string vector to this one
185
 
  
186
 
  Strvec& Strvec::operator = (const Strvec& that) {
187
 
    // check againt equal equal
188
 
    if (this == &that) return *this;
189
 
    // lock everything
190
 
    wrlock ();
191
 
    that.rdlock ();
192
 
    // delete old value
193
 
    delete [] p_vector;
194
 
    // copy arguments
195
 
    d_size   = that.d_length;
196
 
    d_length = that.d_length;
197
 
    p_vector = nilp;
198
 
    // create a new string vector of strings and copy them
199
 
    if ((d_length > 0) && (that.p_vector != nilp)) {
200
 
      p_vector = new String[d_size];
201
 
      for (long i = 0; i < d_length; i++) p_vector[i] = that.p_vector[i];
202
 
    }
203
 
    that.unlock ();
204
 
    unlock ();
205
 
    return *this;
206
 
  }
207
 
 
208
 
  // reset this vector
209
 
 
210
 
  void Strvec::reset (void) {
211
 
    wrlock ();
212
 
    delete [] p_vector;
213
 
    d_size   = 0;
214
 
    d_length = 0;
215
 
    p_vector = nilp;
216
 
    unlock ();
217
 
  }
218
 
  
219
 
  // return true if the vector is empty
220
 
 
221
 
  bool Strvec::empty (void) const {
222
 
    rdlock ();
223
 
    bool result = (d_length == 0);
224
 
    unlock ();
225
 
    return result;
226
 
  }
227
 
 
228
 
  // check that a string exists in this vector
229
 
 
230
 
  bool Strvec::exists (const String& name) const {
231
 
    rdlock ();
232
 
    try {
233
 
      if (d_length == 0) {
234
 
        unlock ();
235
 
        return false;
236
 
      }
237
 
      for (long i = 0; i < d_length; i++) {
238
 
        if (p_vector[i] == name) {
239
 
          unlock ();
240
 
          return true;
241
 
        }
242
 
      }
243
 
      unlock ();
244
 
      return false;
245
 
    } catch (...) {
246
 
      unlock ();
247
 
      throw;
248
 
    }
249
 
  }
250
 
 
251
 
  // get the number of element in this string vector
252
 
 
253
 
  long Strvec::length (void) const {
254
 
    rdlock ();
255
 
    long result = d_length;
256
 
    unlock ();
257
 
    return result;
258
 
  }
259
 
 
260
 
  // add a new element in this string vector
261
 
  
262
 
  void Strvec::add (const String& str) {
263
 
    wrlock ();
264
 
    try {
265
 
      // check if we have to resize the Strvec
266
 
      if (d_length + 1 >= d_size) {
267
 
        long size = (d_size <= 0) ? 1 : d_size * 2;
268
 
        String* vector = new String[size];
269
 
        for (long i = 0; i < d_length; i++) vector[i] = p_vector[i];
270
 
        delete [] p_vector;
271
 
        d_size   = size;
272
 
        p_vector = vector;
273
 
      }
274
 
      // set the string in this Strvec
275
 
      p_vector[d_length++] = str;
276
 
      unlock ();
277
 
    } catch (...) {
278
 
      unlock ();
279
 
      throw;
280
 
    }
281
 
  }
282
 
 
283
 
  // set an string at a certain position in this vector
284
 
  // the old string is destroyed
285
 
 
286
 
  void Strvec::set (const long index, const String& str) {
287
 
    wrlock ();
288
 
    try {
289
 
      // check that we are bounded
290
 
      if (index >= d_length) 
291
 
        throw Exception ("index-error","in string vector set");
292
 
      p_vector[index] = str;
293
 
      unlock ();
294
 
    } catch (...) {
295
 
      unlock ();
296
 
      throw;
297
 
    }
298
 
  }
299
 
 
300
 
  // pop the first string from this vector
301
 
 
302
 
  String Strvec::pop (void) {
303
 
    wrlock ();
304
 
    try {
305
 
      // check that the vector is not empty
306
 
      if (d_length == 0) {
307
 
        throw Exception ("pop-error", "pop request with empty vector");
308
 
      }
309
 
      // save first string
310
 
      String result = p_vector[0];
311
 
      // compress the vector
312
 
      for (long i = 1; i < d_length; i++) {
313
 
        p_vector[i-1] = p_vector[i];
314
 
      }
315
 
      // clear last element
316
 
      p_vector[--d_length] = "";
317
 
      // unlock and return
318
 
      unlock ();
319
 
      return result;
320
 
    } catch (...) {
321
 
      unlock ();
322
 
      throw;
323
 
    }
324
 
  }
325
 
 
326
 
  // pop the last string from this vector
327
 
 
328
 
  String Strvec::rml (void) {
329
 
    wrlock ();
330
 
    try {
331
 
      // check that the vector is not empty
332
 
      if (d_length == 0) {
333
 
        throw Exception ("rml-error", "rml request with empty vector");
334
 
      }
335
 
      // save last string
336
 
      String result = p_vector[d_length-1];
337
 
      // clear last element
338
 
      p_vector[--d_length] = "";
339
 
      // unlock and return
340
 
      unlock ();
341
 
      return result;
342
 
    } catch (...) {
343
 
      unlock ();
344
 
      throw;
345
 
    }
346
 
  }
347
 
 
348
 
  // get an string at a certain position
349
 
 
350
 
  String Strvec::get (const long index) const {
351
 
    rdlock ();
352
 
    try {
353
 
      // check that we are bounded
354
 
      if (index >= d_length) 
355
 
        throw Exception ("index-error","in string vector set");
356
 
      const String& result = p_vector[index];
357
 
      unlock ();
358
 
      return result;
359
 
    } catch (...) {
360
 
      unlock ();
361
 
      throw;
362
 
    }
363
 
  }
364
 
 
365
 
  // return the first string in this vector
366
 
 
367
 
  String Strvec::first (void) const {
368
 
    rdlock ();
369
 
    try {
370
 
      String result = get (0);
371
 
      unlock ();
372
 
      return result;
373
 
    } catch (...) {
374
 
      unlock ();
375
 
      throw;
376
 
    }
377
 
  }
378
 
 
379
 
  // return the last string in this vector
380
 
 
381
 
  String Strvec::last (void) const {
382
 
    rdlock ();
383
 
    try {
384
 
      String result = get (d_length-1);
385
 
      unlock ();
386
 
      return result;
387
 
    } catch (...) {
388
 
      unlock ();
389
 
      throw;
390
 
    }
391
 
  }
392
 
 
393
 
  // return the index of a key or -1
394
 
 
395
 
  long Strvec::find (const String& key) const {
396
 
    rdlock ();
397
 
    try {
398
 
      for (long i = 0; i < d_length; i++) {
399
 
        if (p_vector[i] == key) {
400
 
          unlock ();
401
 
          return i;
402
 
        }
403
 
      }
404
 
      unlock ();
405
 
      return -1;
406
 
    } catch (...) {
407
 
      unlock () ;
408
 
      throw;
409
 
    }
410
 
  }
411
 
 
412
 
  // return the index of a key in this string vector
413
 
 
414
 
  long Strvec::lookup (const String& key) const {
415
 
    rdlock ();
416
 
    try {
417
 
      for (long i = 0; i < d_length; i++) {
418
 
        if (p_vector[i] == key) {
419
 
          unlock ();
420
 
          return i;
421
 
        }
422
 
      }
423
 
      throw Exception ("key-error", "key not found", key);
424
 
    } catch (...) {
425
 
      unlock () ;
426
 
      throw;
427
 
    }
428
 
  }
429
 
  
430
 
  // remove an entry by index and repack the vector
431
 
 
432
 
  void Strvec::remove (const long index) {
433
 
    wrlock ();
434
 
    try {
435
 
      if ((index < 0) || (index >= d_length)) {
436
 
        throw Exception ("index-error","index is out of range");
437
 
      }
438
 
      // repack the vector
439
 
      long mark = d_length - 1;
440
 
      for (long i = index; i < mark; i++) {
441
 
        p_vector[i] = p_vector[i+1];
442
 
      }
443
 
      p_vector[mark] = "";
444
 
      d_length = mark;
445
 
      unlock ();
446
 
    } catch (...) {
447
 
      unlock ();
448
 
      throw;
449
 
    }
450
 
  }
451
 
 
452
 
  // remove an entry by value if it exists
453
 
 
454
 
  void Strvec::remove (const String& key) {
455
 
    wrlock ();
456
 
    try {
457
 
      // get the key index
458
 
      long index = find (key);
459
 
      // remove the entry
460
 
      if (index != -1) remove (index);
461
 
      unlock ();
462
 
    } catch (...) {
463
 
      unlock ();
464
 
      throw;
465
 
    }
466
 
  }
467
 
  // return the maximum string length in this vector
468
 
 
469
 
  long Strvec::maxlen (void) const {
470
 
    rdlock ();
471
 
    try {
472
 
      long result = 0;
473
 
      for (long i = 0; i < d_length; i++) {
474
 
        long len = p_vector[i].length ();
475
 
        if (len > result) result = len;
476
 
      }
477
 
      unlock ();
478
 
      return result;
479
 
    } catch (...) {
480
 
      unlock () ;
481
 
      throw;
482
 
    }
483
 
  }
484
 
 
485
 
  // return the minimum string length in this vector
486
 
 
487
 
  long Strvec::minlen (void) const {
488
 
    rdlock ();
489
 
    try {
490
 
      long result = 0;
491
 
      for (long i = 0; i < d_length; i++) {
492
 
        long len = p_vector[i].length ();
493
 
        if (len < result) result = len;
494
 
      }
495
 
      unlock ();
496
 
      return result;
497
 
    } catch (...) {
498
 
      unlock () ;
499
 
      throw;
500
 
    }
501
 
  }
502
 
 
503
 
  // return an array of quarks for this vector
504
 
 
505
 
  long* Strvec::toquarks (void) const {
506
 
    rdlock ();
507
 
    try {
508
 
      if (d_length == 0) {
509
 
        unlock ();
510
 
        return nilp;
511
 
      }
512
 
      long* result = new long[d_length];
513
 
      for (long i = 0; i < d_length; i++) result[i] = p_vector[i].toquark ();
514
 
      unlock ();
515
 
      return result;
516
 
    } catch (...) {
517
 
      unlock () ;
518
 
      throw;
519
 
    }
520
 
  }
521
 
 
522
 
  // retrun a vector of strings
523
 
 
524
 
  Vector* Strvec::tovector (void) const {
525
 
    rdlock ();
526
 
    Vector* result = new Vector;
527
 
    try {
528
 
      if (d_length == 0) {
529
 
        unlock ();
530
 
        return nilp;
531
 
      }
532
 
      for (long i = 0; i < d_length; i++) {
533
 
        result->append (new String (p_vector[i]));
534
 
      }
535
 
      unlock ();
536
 
      return result;
537
 
    } catch (...) {
538
 
      unlock () ;
539
 
      throw;
540
 
    } 
541
 
  }
542
 
}