first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / structures / string_table.h
1 #ifndef STRING_TABLE_CLASS
2 #define STRING_TABLE_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : string_table                                                      *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
9 *******************************************************************************
10 * Copyright (c) 2000-$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 "symbol_table.h"
19
20 #include <basis/astring.h>
21 #include <basis/contracts.h>
22
23 namespace structures {
24
25 //! Provides a symbol_table that holds strings as the content.
26 /*! This is essentially a table of named strings. */
27
28 class string_table
29 : public symbol_table<basis::astring>,
30   public virtual basis::packable,
31   public virtual basis::hoople_standard
32 {
33 public:
34   string_table(int estimated_elements = 100) : symbol_table<basis::astring>(estimated_elements),
35         _add_spaces(false) {}
36     //!< the "estimated_elements" specifies how many items to prepare to efficiently hold.
37   string_table(const string_table &to_copy);
38   virtual ~string_table();
39
40   DEFINE_CLASS_NAME("string_table");
41
42   string_table &operator = (const string_table &to_copy);
43
44   bool operator ==(const string_table &to_compare) const;
45
46   virtual bool equal_to(const equalizable &to_compare) const {
47     const string_table *cast = dynamic_cast<const string_table *>(&to_compare);
48     if (!cast) return false;
49     return operator ==(*cast);
50   }
51
52   #define STRTAB_COMMENT_PREFIX "#comment#"
53     //!< anything beginning with this is considered a comment.
54     /*!< a numerical uniquifier should be appended to the string to ensure that
55     multiple comments can be handled per table. */
56
57   static bool is_comment(const basis::astring &to_check);
58
59   basis::astring text_form() const;
60     //!< prints the contents of the table into the returned string.
61     /*!< if names in the table start with the comment prefix (see above), then
62     they will not be printed as "X=Y" but instead as just "Y". */
63
64   virtual void text_form(basis::base_string &fill) const { fill = text_form(); }
65
66   // dictates whether the output will have spaces between the assignment
67   // character and the key name and value.  default is to not add them.
68   bool add_spaces() const { return _add_spaces; }
69   void add_spaces(bool add_them) { _add_spaces = add_them; }
70
71   virtual int packed_size() const;
72   virtual void pack(basis::byte_array &packed_form) const;
73   virtual bool unpack(basis::byte_array &packed_form);
74
75 private:
76   bool _add_spaces;  // records whether we add spaces around the assignment.
77 };
78
79 } //namespace.
80
81 #endif
82