X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Flibrary%2Fstructures%2Fstring_array.h;fp=nucleus%2Flibrary%2Fstructures%2Fstring_array.h;h=22d5c3f4fecdb77ab3d18457dcaf563ac21b7869;hb=457b128b77b5b4a0b7dd3094de543de2ce1477ad;hp=0000000000000000000000000000000000000000;hpb=32d7caf45d886d0d24e69eea00511c7815ac15d0;p=feisty_meow.git diff --git a/nucleus/library/structures/string_array.h b/nucleus/library/structures/string_array.h new file mode 100644 index 00000000..22d5c3f4 --- /dev/null +++ b/nucleus/library/structures/string_array.h @@ -0,0 +1,127 @@ +#ifndef STRING_ARRAY_CLASS +#define STRING_ARRAY_CLASS + +/*****************************************************************************\ +* * +* Name : string_array * +* Author : Chris Koeritz * +* * +******************************************************************************* +* Copyright (c) 1993-$now By Author. This program is free software; you can * +* redistribute it and/or modify it under the terms of the GNU General Public * +* License as published by the Free Software Foundation; either version 2 of * +* the License or (at your option) any later version. This is online at: * +* http://www.fsf.org/copyleft/gpl.html * +* Please send any updates to: fred@gruntose.com * +\*****************************************************************************/ + +#include "object_packers.h" + +#include +#include +#include + +namespace structures { + +//! An array of strings with some additional helpful methods. + +class string_array +: public basis::array, + public virtual basis::packable, + public virtual basis::equalizable +{ +public: + string_array(int number = 0, const basis::astring *initial_contents = NIL) + : basis::array(number, initial_contents, + EXPONE | FLUSH_INVISIBLE) {} + //!< Constructs an array of "number" strings. + /*!< creates a list of strings based on an initial "number" of entries and + some "initial_contents", which should be a regular C array of astrings + with at least as many entries as "number". */ + + //! a constructor that operates on an array of char pointers. + /*! be very careful with the array to ensure that the right number of + elements is provided. */ + string_array(int number, const char *initial_contents[]) + : basis::array(number, NIL, EXPONE | FLUSH_INVISIBLE) { + for (int i = 0; i < number; i++) { + put(i, basis::astring(initial_contents[i])); + } + } + + string_array(const basis::array &to_copy) + : basis::array(to_copy) {} + //!< copy constructor that takes a templated array of astring. + + DEFINE_CLASS_NAME("string_array"); + + //! Prints out a formatted view of the contained strings and returns it. + basis::astring text_format(const basis::astring &separator = ",", + const basis::astring &delimiter = "\"") const { + basis::astring to_return; + for (int i = 0; i < length(); i++) { + to_return += delimiter; + to_return += get(i); + to_return += delimiter; + if (i < last()) + to_return += separator; + } + return to_return; + } + + basis::astring text_form() const { return text_format(); } + //!< A synonym for the text_format() method. + +//hmmm: extract as re-usable equality operation. + + //! Compares this string array for equality with "to_compare". + bool equal_to(const equalizable &to_compare) const { + const string_array *cast = cast_or_throw(to_compare, *this); + if (length() != cast->length()) + return false; + for (int i = 0; i < length(); i++) + if (cast->get(i) != get(i)) + return false; + return true; + } + + //! locates string specified and returns its index, or negative if missing. + int find(const basis::astring &to_find) const { + for (int i = 0; i < length(); i++) { + if (to_find.equal_to(get(i))) return i; + } + return basis::common::NOT_FOUND; + } + + //! Returns true if all of the elements in this are the same in "second". + /*! The array "second" can have more elements, but must have all of the + items listed in this string array. */ + bool prefix_compare(const string_array &second) const { + if (second.length() < length()) return false; + if (!length()) return false; + for (int i = 0; i < length(); i++) + if ((*this)[i] != second[i]) return false; + return true; + } + + //! Packs this string array into the "packed_form" byte array. + virtual void pack(basis::byte_array &packed_form) const + { pack_array(packed_form, *this); } + + //! Unpacks a string array from the "packed_form" byte array. + virtual bool unpack(basis::byte_array &packed_form) + { return unpack_array(packed_form, *this); } + + //! Returns the number of bytes this string array would consume if packed. + virtual int packed_size() const { + int to_return = sizeof(int) * 2; // length packed in, using obscure. + for (int i = 0; i < length(); i++) + to_return += get(i).length() + 1; + return to_return; + } +}; + +} //namespace. + +#endif +