📄 propset.cxx
字号:
unsigned int hash = HashString(key, lenKey); Property *pPrev = NULL; for (Property *p = props[hash % hashRoots]; p; p = p->next) { if ((hash == p->hash) && ((strlen(p->key) == static_cast<unsigned int>(lenKey)) && (0 == strncmp(p->key, key, lenKey)))) { if (pPrev) pPrev->next = p->next; else props[hash % hashRoots] = p->next; if (p == enumnext) enumnext = p->next; // Not that anyone should mix enum and Set / Unset. delete [](p->key); delete [](p->val); delete p; return; } else { pPrev = p; } }}void PropSet::SetMultiple(const char *s) { const char *eol = strchr(s, '\n'); while (eol) { Set(s); s = eol + 1; eol = strchr(s, '\n'); } Set(s);}SString PropSet::Get(const char *key) const { unsigned int hash = HashString(key, strlen(key)); for (Property *p = props[hash % hashRoots]; p; p = p->next) { if ((hash == p->hash) && (0 == strcmp(p->key, key))) { return p->val; } } if (superPS) { // Failed here, so try in base property set return superPS->Get(key); } else { return ""; }}// There is some inconsistency between GetExpanded("foo") and Expand("$(foo)").// A solution is to keep a stack of variables that have been expanded, so that// recursive expansions can be skipped. For now I'll just use the C++ stack// for that, through a recursive function and a simple chain of pointers.struct VarChain { VarChain(const char*var_=NULL, const VarChain *link_=NULL): var(var_), link(link_) {} bool contains(const char *testVar) const { return (var && (0 == strcmp(var, testVar))) || (link && link->contains(testVar)); } const char *var; const VarChain *link;};static int ExpandAllInPlace(const PropSet &props, SString &withVars, int maxExpands, const VarChain &blankVars = VarChain()) { int varStart = withVars.search("$("); while ((varStart >= 0) && (maxExpands > 0)) { int varEnd = withVars.search(")", varStart+2); if (varEnd < 0) { break; } // For consistency, when we see '$(ab$(cde))', expand the inner variable first, // regardless whether there is actually a degenerate variable named 'ab$(cde'. int innerVarStart = withVars.search("$(", varStart+2); while ((innerVarStart > varStart) && (innerVarStart < varEnd)) { varStart = innerVarStart; innerVarStart = withVars.search("$(", varStart+2); } SString var(withVars.c_str(), varStart + 2, varEnd); SString val = props.Get(var.c_str()); if (blankVars.contains(var.c_str())) { val.clear(); // treat blankVar as an empty string (e.g. to block self-reference) } if (--maxExpands >= 0) { maxExpands = ExpandAllInPlace(props, val, maxExpands, VarChain(var.c_str(), &blankVars)); } withVars.remove(varStart, varEnd-varStart+1); withVars.insert(varStart, val.c_str(), val.length()); varStart = withVars.search("$("); } return maxExpands;}SString PropSet::GetExpanded(const char *key) const { SString val = Get(key); ExpandAllInPlace(*this, val, 100, VarChain(key)); return val;}SString PropSet::Expand(const char *withVars, int maxExpands) const { SString val = withVars; ExpandAllInPlace(*this, val, maxExpands); return val;}int PropSet::GetInt(const char *key, int defaultValue) const { SString val = GetExpanded(key); if (val.length()) return val.value(); return defaultValue;}bool isprefix(const char *target, const char *prefix) { while (*target && *prefix) { if (*target != *prefix) return false; target++; prefix++; } if (*prefix) return false; else return true;}void PropSet::Clear() { for (int root = 0; root < hashRoots; root++) { Property *p = props[root]; while (p) { Property *pNext = p->next; p->hash = 0; delete []p->key; p->key = 0; delete []p->val; p->val = 0; delete p; p = pNext; } props[root] = 0; }}char *PropSet::ToString() const { size_t len=0; for (int r = 0; r < hashRoots; r++) { for (Property *p = props[r]; p; p = p->next) { len += strlen(p->key) + 1; len += strlen(p->val) + 1; } } if (len == 0) len = 1; // Return as empty string char *ret = new char [len]; if (ret) { char *w = ret; for (int root = 0; root < hashRoots; root++) { for (Property *p = props[root]; p; p = p->next) { strcpy(w, p->key); w += strlen(p->key); *w++ = '='; strcpy(w, p->val); w += strlen(p->val); *w++ = '\n'; } } ret[len-1] = '\0'; } printf("ret = %s\n",ret); return ret;}/** * Creates an array that points into each word in the string and puts \0 terminators * after each word. */static char **ArrayFromWordList(char *wordlist, int *len, bool onlyLineEnds = false) { int prev = '\n'; int words = 0; // For rapid determination of whether a character is a separator, build // a look up table. bool wordSeparator[256]; for (int i=0;i<256; i++) { wordSeparator[i] = false; } wordSeparator['\r'] = true; wordSeparator['\n'] = true; if (!onlyLineEnds) { wordSeparator[' '] = true; wordSeparator['\t'] = true; } for (int j = 0; wordlist[j]; j++) { int curr = static_cast<unsigned char>(wordlist[j]); if (!wordSeparator[curr] && wordSeparator[prev]) words++; prev = curr; } char **keywords = new char *[words + 1]; if (keywords) { words = 0; prev = '\0'; size_t slen = strlen(wordlist); for (size_t k = 0; k < slen; k++) { if (!wordSeparator[static_cast<unsigned char>(wordlist[k])]) { if (!prev) { keywords[words] = &wordlist[k]; words++; } } else { wordlist[k] = '\0'; } prev = wordlist[k]; } keywords[words] = &wordlist[slen]; *len = words; } else { *len = 0; } return keywords;}void WordList::Clear() { if (words) { delete []list; delete []words; } words = 0; list = 0; len = 0; sorted = false;}void WordList::Set(const char *s) { list = StringDup(s); sorted = false; words = ArrayFromWordList(list, &len, onlyLineEnds);}extern "C" int cmpString(const void *a1, const void *a2) { // Can't work out the correct incantation to use modern casts here return strcmp(*(char**)(a1), *(char**)(a2));}static void SortWordList(char **words, unsigned int len) { qsort(reinterpret_cast<void*>(words), len, sizeof(*words), cmpString);}bool WordList::InList(const char *s) { if (0 == words) return false; if (!sorted) { sorted = true; SortWordList(words, len); for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++) starts[k] = -1; for (int l = len - 1; l >= 0; l--) { unsigned char indexChar = words[l][0]; starts[indexChar] = l; } } unsigned char firstChar = s[0]; int j = starts[firstChar]; if (j >= 0) { while ((unsigned char)words[j][0] == firstChar) { if (s[1] == words[j][1]) { const char *a = words[j] + 1; const char *b = s + 1; while (*a && *a == *b) { a++; b++; } if (!*a && !*b) return true; } j++; } } j = starts['^']; if (j >= 0) { while (words[j][0] == '^') { const char *a = words[j] + 1; const char *b = s; while (*a && *a == *b) { a++; b++; } if (!*a) return true; j++; } } return false;}/** similar to InList, but word s can be a substring of keyword. * eg. the keyword define is defined as def~ine. This means the word must start * with def to be a keyword, but also defi, defin and define are valid. * The marker is ~ in this case. */bool WordList::InListAbbreviated(const char *s, const char marker) { if (0 == words) return false; if (!sorted) { sorted = true; SortWordList(words, len); for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++) starts[k] = -1; for (int l = len - 1; l >= 0; l--) { unsigned char indexChar = words[l][0]; starts[indexChar] = l; } } unsigned char firstChar = s[0]; int j = starts[firstChar]; if (j >= 0) { while (words[j][0] == firstChar) { bool isSubword = false; int start = 1; if (words[j][1] == marker) { isSubword = true; start++; } if (s[1] == words[j][start]) { const char *a = words[j] + start; const char *b = s + 1; while (*a && *a == *b) { a++; if (*a == marker) { isSubword = true; a++; } b++; } if ((!*a || isSubword) && !*b) return true; } j++; } } j = starts['^']; if (j >= 0) { while (words[j][0] == '^') { const char *a = words[j] + 1; const char *b = s; while (*a && *a == *b) { a++; b++; } if (!*a) return true; j++; } } return false;}#include <fstream>#include <string>using namespace std;bool PropSet::printToFile(const char *filename){ if(filename == NULL) return false; ofstream outfile; outfile.open(filename,ios_base::out | ios_base::trunc); if(outfile.is_open() == false) return false; Property *proper; std::string data; for(int index = 0;index < hashRoots;index++){ proper = props[index]; while(proper){ if(proper->key) data = proper->key; if(proper->val){ data += '='; data += proper->val; } outfile << data.c_str(); outfile << "\n"; proper = proper->next; } } outfile.close(); return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -