1 /*****************************************************************************\
3 * Name : string_table *
4 * Author : Chris Koeritz *
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 \*****************************************************************************/
15 #include "object_packers.h"
16 #include "string_table.h"
17 #include "symbol_table.h"
19 #include <basis/astring.h>
20 #include <basis/functions.h>
22 using namespace basis;
24 namespace structures {
26 string_table::string_table(const string_table &to_copy)
27 : symbol_table<astring>(to_copy.estimated_elements()),
33 string_table::~string_table() {}
35 bool string_table::is_comment(const astring &to_check)
36 { return to_check.begins(STRTAB_COMMENT_PREFIX); }
38 string_table &string_table::operator = (const string_table &to_copy)
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;
46 astring string_table::text_form() const
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());
55 output += a_sprintf("%s%s=%s%s\n", name(i).s(), space_char,
56 space_char, operator[](i).s());
61 bool string_table::operator ==(const string_table &to_compare) const
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;
74 int string_table::packed_size() const
76 int size = sizeof(int);
77 for (int i = 0; i < symbols(); i++) {
78 size += name(i).length();
79 size += operator[](i).length();
84 void string_table::pack(byte_array &packed_form) const
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);
93 bool string_table::unpack(byte_array &packed_form)
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;