feisty meow concerns codebase 2.140
string_table.cpp
Go to the documentation of this file.
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
22using namespace basis;
23
24namespace structures {
25
27: symbol_table<astring>(to_copy.estimated_elements()),
28 _add_spaces(false)
29{
30 *this = to_copy;
31}
32
34
35bool string_table::is_comment(const astring &to_check)
36{ return to_check.begins(STRTAB_COMMENT_PREFIX); }
37
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
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
61bool 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
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
84void 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
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
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
void pack(byte_array &target) const
stores this string in the "target". it can later be unpacked again.
Definition astring.cpp:964
bool begins(const astring &maybe_prefix) const
Returns true if "this" string begins with "maybe_prefix".
Definition astring.h:178
int length() const
Returns the current length of the string.
Definition astring.cpp:132
bool unpack(byte_array &source)
retrieves a string (packed with pack()) from "source" into this string.
Definition astring.cpp:967
A very common template for a dynamic array of bytes.
Definition byte_array.h:36
Outcomes describe the state of completion for an operation.
Definition outcome.h:31
Provides a symbol_table that holds strings as the content.
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
basis::astring text_form() const
prints the contents of the table into the returned string.
bool operator==(const string_table &to_compare) const
string_table(int estimated_elements=100)
the "estimated_elements" specifies how many items to prepare to efficiently hold.
static bool is_comment(const basis::astring &to_check)
string_table & operator=(const string_table &to_copy)
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
virtual int packed_size() const
Estimates the space needed for the packed structure.
Maintains a list of names, where each name has a type and some contents.
const basis::astring & name(int index) const
returns the name held at the "index".
basis::astring * find(const basis::astring &name) const
returns the contents held for "name" or NULL_POINTER if it wasn't found.
basis::outcome add(const basis::astring &name, const basis::astring &storage)
Enters a symbol name into the table along with some contents.
basis::astring & operator[](int index)
provides access to the symbol_table's contents at the "index".
int symbols() const
returns the number of symbols listed in the table.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
void attach(byte_array &packed_form, const byte_array &to_attach)
Packs a byte_array "to_attach" into "packed_form".
bool detach(byte_array &packed_form, byte_array &to_detach)
Unpacks a byte_array "to_detach" from "packed_form".
#define STRTAB_COMMENT_PREFIX
anything beginning with this is considered a comment.