00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "tagmap.h"
00036 #include "outputstream.h"
00037 #include <cstring>
00038 #include <cstdlib>
00039
00040 TagMap *TagMap::instance = NULL;
00041
00042 TagMap *TagMap::getInstance() {
00043 if (instance == NULL) {
00044 instance = new TagMap();
00045 }
00046
00047 return instance;
00048 }
00049
00050 TagMap::TagMap() {
00051 char *root = new char[strlen("ROOT") + 1];
00052
00053 strcpy(root, "ROOT");
00054 tags.push_back(root);
00055 }
00056
00057 TagMap::~TagMap() {
00058 for (unsigned i = 0; i < tags.size(); i++) {
00059 delete[]tags[i];
00060 }
00061 }
00062
00063 unsigned TagMap::insertTag(const char *tag) {
00064 unsigned hash_index = sh.hash(tag);
00065
00066 if (hash_index == tags.size()) {
00067 char *tag_copy = new char[strlen(tag) + 1];
00068
00069 strcpy(tag_copy, tag);
00070 tags.push_back(tag_copy);
00071 }
00072
00073 return hash_index;
00074 }
00075
00076 unsigned TagMap::insertTagWithOffset(const char *tag, unsigned start_offset,
00077 unsigned end_offset) {
00078
00079 unsigned hash_index = sh.hash(tag, start_offset, end_offset);
00080
00081 if (hash_index == tags.size()) {
00082
00083 unsigned total_offset = start_offset + end_offset;
00084 char *tag_copy = new char[strlen(tag) - total_offset + 1];
00085
00086 strncpy(tag_copy, tag + start_offset, strlen(tag) - total_offset);
00087 tag_copy[strlen(tag) - (start_offset + end_offset)] = '\0';
00088 tags.push_back(tag_copy);
00089 }
00090
00091 return hash_index;
00092 }
00093
00094 void TagMap::print(OutputStream & dos) {
00095 for (unsigned i = 0; i < tags.size(); i++) {
00096 dos << i << " -> <" << tags[i] << "></" << tags[i] << ">" << NEWLINE;
00097 }
00098 }