first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / structures / string_table.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : string_table                                                      *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "object_packers.h"
16 #include "string_table.h"
17 #include "symbol_table.h"
18
19 #include <basis/astring.h>
20 #include <basis/functions.h>
21
22 using namespace basis;
23
24 namespace structures {
25
26 string_table::string_table(const string_table &to_copy)
27 : symbol_table<astring>(to_copy.estimated_elements()),
28   _add_spaces(false)
29 {
30   *this = to_copy;
31 }
32
33 string_table::~string_table() {}
34
35 bool string_table::is_comment(const astring &to_check)
36 { return to_check.begins(STRTAB_COMMENT_PREFIX); }
37
38 string_table &string_table::operator = (const string_table &to_copy)
39 {
40   if (this == &to_copy) return *this;
41   (symbol_table<astring> &)*this = (const symbol_table<astring> &)to_copy;
42   _add_spaces = to_copy._add_spaces;
43   return *this;
44 }
45
46 astring string_table::text_form() const
47 {
48   astring output;
49   const char *space_char = "";
50   if (_add_spaces) space_char = " ";
51   for (int i = 0; i < symbols(); i++) {
52     if (is_comment(name(i)))
53       output += a_sprintf("%s\n", operator[](i).s());
54     else
55       output += a_sprintf("%s%s=%s%s\n", name(i).s(), space_char,
56           space_char, operator[](i).s());
57   }
58   return output;
59 }
60
61 bool string_table::operator ==(const string_table &to_compare) const
62 {
63   if (to_compare.symbols() != symbols()) return false;
64   for (int i = 0; i < symbols(); i++) {
65     const astring &key = name(i);
66     astring *str1 = find(key);
67     astring *str2 = to_compare.find(key);
68     if (!str2) return false;
69     if (*str1 != *str2) return false;
70   }
71   return true;
72 }
73
74 int string_table::packed_size() const
75 {
76   int size = sizeof(int);
77   for (int i = 0; i < symbols(); i++) {
78     size += name(i).length();
79     size += operator[](i).length();
80   }
81   return size;
82 }
83
84 void string_table::pack(byte_array &packed_form) const
85 {
86   structures::attach(packed_form, symbols());
87   for (int i = 0; i < symbols(); i++) {
88     name(i).pack(packed_form);
89     operator[](i).pack(packed_form);
90   }
91 }
92
93 bool string_table::unpack(byte_array &packed_form)
94 {
95   reset();
96   int syms;
97   if (!structures::detach(packed_form, syms)) return false;
98   for (int i = 0; i < syms; i++) {
99     astring name, content;
100     if (!name.unpack(packed_form)) return false;
101     if (!content.unpack(packed_form)) return false;
102     outcome ret = add(name, content);
103     if (ret != common::IS_NEW) return false;
104   }
105   return true;
106 }
107
108 } //namespace.
109
110