pstrlist.cxx (3496B)
1 /* 2 * 3 * C++ Portable Types Library (PTypes) 4 * Version 2.1.1 Released 27-Jun-2007 5 * 6 * Copyright (C) 2001-2007 Hovik Melikyan 7 * 8 * http://www.melikyan.com/ptypes/ 9 * 10 */ 11 12 #include "ptypes.h" 13 14 15 namespace ptypes { 16 17 18 typedef _stritem* pstritem; 19 20 21 void _strlist::sortederror() 22 { 23 fatal(CRIT_FIRST + 32, "Operation not allowed on sorted string lists"); 24 } 25 26 27 void _strlist::notsortederror() 28 { 29 fatal(CRIT_FIRST + 33, "Search only allowed on sorted string lists"); 30 } 31 32 33 void _strlist::duperror() 34 { 35 fatal(CRIT_FIRST + 34, "Duplicate items not allowed in this string list"); 36 } 37 38 39 _strlist::_strlist(int flags) 40 : tobjlist<_stritem>(true) 41 { 42 if ((flags & SL_SORTED) != 0) 43 config.sorted = 1; 44 if ((flags & SL_DUPLICATES) != 0) 45 config.duplicates = 1; 46 if ((flags & SL_CASESENS) != 0) 47 config.casesens = 1; 48 if ((flags & SL_OWNOBJECTS) != 0) 49 config.ownslobjects = 1; 50 } 51 52 53 _strlist::~_strlist() 54 { 55 } 56 57 58 void _strlist::dofree(void* item) 59 { 60 if (config.ownslobjects) 61 dofreeobj(pstritem(item)->obj); 62 delete pstritem(item); 63 } 64 65 66 void _strlist::dofreeobj(void*) 67 { 68 fatal(CRIT_FIRST + 38, "strlist::dofree() not defined"); 69 } 70 71 72 int _strlist::compare(const void* key, const void* item) const 73 { 74 if (config.casesens) 75 return strcmp(pconst(key), pstritem(item)->key); 76 else 77 return strcasecmp(pconst(key), pstritem(item)->key); 78 } 79 80 81 void _strlist::doins(int index, const string& key, void* obj) 82 { 83 tobjlist<_stritem>::ins(index, new _stritem(key, obj)); 84 } 85 86 87 void _strlist::doput(int index, const string& key, void* obj) 88 { 89 if (config.sorted) 90 sortederror(); 91 _stritem* p = doget(index); 92 if (config.ownslobjects) 93 dofreeobj(p->obj); 94 p->key = key; 95 p->obj = obj; 96 } 97 98 99 void _strlist::doput(int index, void* obj) 100 { 101 _stritem* p = doget(index); 102 if (config.ownslobjects) 103 dofreeobj(p->obj); 104 p->obj = obj; 105 } 106 107 108 int _strlist::put(const string& key, void* obj) 109 { 110 if (!config.sorted) 111 notsortederror(); 112 if (config.duplicates) 113 duperror(); 114 int index; 115 if (search(key, index)) 116 { 117 if (obj == nil) 118 dodel(index); 119 else 120 doput(index, obj); 121 } 122 else if (obj != nil) 123 doins(index, key, obj); 124 return index; 125 } 126 127 128 int _strlist::add(const string& key, void* obj) 129 { 130 int index; 131 if (config.sorted) 132 { 133 if (search(key, index) && !config.duplicates) 134 duperror(); 135 } 136 else 137 index = count; 138 doins(index, key, obj); 139 return index; 140 } 141 142 143 void* _strlist::operator [](const char* key) const 144 { 145 if (!config.sorted) 146 notsortederror(); 147 int index; 148 if (search(key, index)) 149 return dogetobj(index); 150 else 151 return nil; 152 } 153 154 155 int _strlist::indexof(const char* key) const 156 { 157 if (config.sorted) 158 { 159 int index; 160 if (search(key, index)) 161 return index; 162 } 163 else 164 { 165 for (int i = 0; i < count; i++) 166 if (compare(key, doget(i)) == 0) 167 return i; 168 } 169 return -1; 170 } 171 172 173 int _strlist::indexof(void* obj) const 174 { 175 for (int i = 0; i < count; i++) 176 if (pstritem(doget(i))->obj == obj) 177 return i; 178 return -1; 179 } 180 181 182 // 183 // strmap 184 // 185 186 #ifdef PTYPES19_COMPAT 187 188 strlist::strlist(int flags): tstrlist<unknown>(flags) {} 189 190 strlist::~strlist() {} 191 192 strmap::strmap(int flags) 193 : tstrlist<unknown>((flags | SL_SORTED) & ~SL_DUPLICATES) 194 { 195 } 196 197 strmap::~strmap() 198 { 199 } 200 201 #endif 202 203 204 }