string_table.cpp

Go to the documentation of this file.
00001 #ifndef STRING_TABLE_IMPLEMENTATION_FILE
00002 #define STRING_TABLE_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : string_table                                                      *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2000-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "string_table.h"
00019 #include "symbol_table.cpp"
00020 
00021 #include <basis/function.h>
00022 #include <basis/istring.h>
00023 #include <basis/log_base.h>
00024 
00025 string_table::string_table(const string_table &to_copy)
00026 : symbol_table<istring>(to_copy.max_bits()),
00027   _add_spaces(false)
00028 {
00029   *this = to_copy;
00030 }
00031 
00032 string_table::~string_table() {}
00033 
00034 bool string_table::is_comment(const istring &to_check)
00035 { return to_check.begins(STRTAB_COMMENT_PREFIX); }
00036 
00037 string_table &string_table::operator = (const string_table &to_copy)
00038 {
00039   if (this == &to_copy) return *this;
00040   (symbol_table<istring> &)*this = (const symbol_table<istring> &)to_copy;
00041   _add_spaces = to_copy._add_spaces;
00042   return *this;
00043 }
00044 
00045 istring string_table::text_form() const
00046 {
00047   istring output;
00048   const char *space_char = "";
00049   if (_add_spaces) space_char = " ";
00050   for (int i = 0; i < symbols(); i++) {
00051     if (is_comment(name(i)))
00052       output += isprintf("%s%s", operator[](i).s(),
00053           log_base::platform_ending());
00054     else
00055       output += isprintf("%s%s=%s%s%s", name(i).s(), space_char,
00056           space_char, operator[](i).s(), log_base::platform_ending());
00057   }
00058   return output;
00059 }
00060 
00061 bool string_table::operator ==(const string_table &to_compare) const
00062 {
00063   if (to_compare.symbols() != symbols()) return false;
00064   for (int i = 0; i < symbols(); i++) {
00065     const istring &key = name(i);
00066     istring *str1 = find(key);
00067     istring *str2 = to_compare.find(key);
00068     if (!str2) return false;
00069     if (*str1 != *str2) return false;
00070   }
00071   return true;
00072 }
00073 
00074 int string_table::packed_size() const
00075 {
00076   int size = sizeof(int);
00077   for (int i = 0; i < symbols(); i++) {
00078     size += name(i).length();
00079     size += operator[](i).length();
00080   }
00081   return size;
00082 }
00083 
00084 void string_table::pack(byte_array &packed_form) const
00085 {
00086   basis::attach(packed_form, symbols());
00087   for (int i = 0; i < symbols(); i++) {
00088     name(i).pack(packed_form);
00089     operator[](i).pack(packed_form);
00090   }
00091 }
00092 
00093 bool string_table::unpack(byte_array &packed_form)
00094 {
00095   reset();
00096   int syms;
00097   if (!basis::detach(packed_form, syms)) return false;
00098   for (int i = 0; i < syms; i++) {
00099     istring name, content;
00100     if (!name.unpack(packed_form)) return false;
00101     if (!content.unpack(packed_form)) return false;
00102     outcome ret = add(name, content);
00103     if (ret != common::IS_NEW) return false;
00104   }
00105   return true;
00106 }
00107 
00108 #endif //STRING_TABLE_IMPLEMENTATION_FILE
00109 

Generated on Fri Nov 21 04:29:42 2008 for HOOPLE Libraries by  doxygen 1.5.1