feisty meow concerns codebase  2.140
string_array.h
Go to the documentation of this file.
1 #ifndef STRING_ARRAY_CLASS
2 #define STRING_ARRAY_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : string_array *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1993-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "object_packers.h"
19 
20 #include <basis/astring.h>
21 #include <basis/byte_array.h>
22 #include <basis/functions.h>
23 
24 namespace structures {
25 
27 
29 : public basis::array<basis::astring>,
30  public virtual basis::packable,
31  public virtual basis::equalizable
32 {
33 public:
34  string_array(int number = 0, const basis::astring *initial_contents = NULL_POINTER)
35  : basis::array<basis::astring>(number, initial_contents,
38 
43 
45  string_array(int number, const char *initial_contents[])
46  : basis::array<basis::astring>(number, NULL_POINTER, EXPONE | FLUSH_INVISIBLE) {
47  for (int i = 0; i < number; i++) {
48  put(i, basis::astring(initial_contents[i]));
49  }
50  }
51 
53  : basis::array<basis::astring>(to_copy) {}
55 
56  DEFINE_CLASS_NAME("string_array");
57 
59  basis::astring text_format(const basis::astring &separator = ",",
60  const basis::astring &delimiter = "\"") const {
61  basis::astring to_return;
62  for (int i = 0; i < length(); i++) {
63  to_return += delimiter;
64  to_return += get(i);
65  to_return += delimiter;
66  if (i < last())
67  to_return += separator;
68  }
69  return to_return;
70  }
71 
72  basis::astring text_form() const { return text_format(); }
74 
75 //hmmm: extract as re-usable equality operation.
76 
78  bool equal_to(const equalizable &to_compare) const {
79  const string_array *cast = cast_or_throw(to_compare, *this);
80  if (length() != cast->length())
81  return false;
82  for (int i = 0; i < length(); i++)
83  if (cast->get(i) != get(i))
84  return false;
85  return true;
86  }
87 
89  int find(const basis::astring &to_find) const {
90  for (int i = 0; i < length(); i++) {
91  if (to_find.equal_to(get(i))) return i;
92  }
93  return basis::common::NOT_FOUND;
94  }
95 
97 
99  bool prefix_compare(const string_array &second) const {
100  if (second.length() < length()) return false;
101  if (!length()) return false;
102  for (int i = 0; i < length(); i++)
103  if ((*this)[i] != second[i]) return false;
104  return true;
105  }
106 
108  virtual void pack(basis::byte_array &packed_form) const
109  { pack_array(packed_form, *this); }
110 
112  virtual bool unpack(basis::byte_array &packed_form)
113  { return unpack_array(packed_form, *this); }
114 
116  virtual int packed_size() const {
117  int to_return = sizeof(int) * 2; // length packed in, using obscure.
118  for (int i = 0; i < length(); i++)
119  to_return += get(i).length() + 1;
120  return to_return;
121  }
122 };
123 
124 } //namespace.
125 
126 #endif
127 
Represents a sequential, ordered, contiguous collection of objects.
Definition: array.h:54
@ EXPONE
synonym for EXPONENTIAL_GROWTH.
Definition: array.h:61
@ FLUSH_INVISIBLE
blanks out allocated but inaccessible elements.
Definition: array.h:62
outcome put(int index, const basis::astring &to_put)
Stores an object at the index "index" in the array.
Definition: array.h:834
const basis::astring & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
Definition: array.h:372
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
array(int number=0, const basis::astring *init=NULL_POINTER, int flags=EXPONENTIAL_GROWTH|FLUSH_INVISIBLE)
Constructs an array with room for "number" objects.
Definition: array.h:315
int last() const
Returns the last valid element in the array.
Definition: array.h:118
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
bool equal_to(const char *that) const
returns true if "that" is equal to this.
Definition: astring.cpp:159
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
Base class for object that can tell itself apart from other instances.
Definition: contracts.h:44
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
An array of strings with some additional helpful methods.
Definition: string_array.h:32
DEFINE_CLASS_NAME("string_array")
virtual void pack(basis::byte_array &packed_form) const
Packs this string array into the "packed_form" byte array.
Definition: string_array.h:107
bool equal_to(const equalizable &to_compare) const
Compares this string array for equality with "to_compare".
Definition: string_array.h:77
virtual bool unpack(basis::byte_array &packed_form)
Unpacks a string array from the "packed_form" byte array.
Definition: string_array.h:111
int find(const basis::astring &to_find) const
locates string specified and returns its index, or negative if missing.
Definition: string_array.h:88
bool prefix_compare(const string_array &second) const
Returns true if all of the elements in this are the same in "second".
Definition: string_array.h:98
basis::astring text_format(const basis::astring &separator=",", const basis::astring &delimiter="\"") const
Prints out a formatted view of the contained strings and returns it.
Definition: string_array.h:58
virtual int packed_size() const
Returns the number of bytes this string array would consume if packed.
Definition: string_array.h:115
basis::astring text_form() const
A synonym for the text_format() method.
Definition: string_array.h:71
string_array(int number=0, const basis::astring *initial_contents=NULL_POINTER)
Constructs an array of "number" strings.
Definition: string_array.h:34
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
target_type * cast_or_throw(source_type &to_cast, const target_type &ignored)
dynamically converts a type to a target type, or throws an exception if it cannot.
Definition: functions.h:68
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
bool unpack_array(basis::byte_array &packed_form, basis::array< contents > &to_unpack)
provides a way to unpack any array that stores packable objects.
void pack_array(basis::byte_array &packed_form, const basis::array< contents > &to_pack)
provides a way to pack any array that stores packable objects.