// SciTE - Scintilla based Text Editor /** @file PropSet.cxx ** A Java style properties file module. **/ // Copyright 1998-2002 by Neil Hodgson // The License.txt file describes the conditions under which this software may be distributed. // Maintain a dictionary of properties #include #include #include #include #include "Platform.h" #include "PropSet.h" // The comparison and case changing functions here assume ASCII // or extended ASCII such as the normal Windows code page. static inline char MakeUpperCase(char ch) { if (ch < 'a' || ch > 'z') return ch; else return static_cast(ch - 'a' + 'A'); } static inline bool IsLetter(char ch) { return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')); } int CompareCaseInsensitive(const char *a, const char *b) { while (*a && *b) { if (*a != *b) { char upperA = MakeUpperCase(*a); char upperB = MakeUpperCase(*b); if (upperA != upperB) return upperA - upperB; } a++; b++; } // Either *a or *b is nul return *a - *b; } int CompareNCaseInsensitive(const char *a, const char *b, int len) { while (*a && *b && len) { if (*a != *b) { char upperA = MakeUpperCase(*a); char upperB = MakeUpperCase(*b); if (upperA != upperB) return upperA - upperB; } a++; b++; len--; } if (len == 0) return 0; else // Either *a or *b is nul return *a - *b; } bool EqualCaseInsensitive(const char *a, const char *b) { return 0 == CompareCaseInsensitive(a, b); } inline unsigned int HashString(const char *s, int len) { unsigned int ret = 0; while (len--) { ret <<= 4; ret ^= *s; s++; } return ret; } PropSet::PropSet() { superPS = 0; for (int root = 0; root < hashRoots; root++) props[root] = 0; } PropSet::~PropSet() { superPS = 0; Clear(); } void PropSet::Set(const char *key, const char *val, int lenKey, int lenVal) { if (!*key) // Empty keys are not supported return; if (lenKey == -1) lenKey = strlen(key); if (lenVal == -1) lenVal = strlen(val); unsigned int hash = HashString(key, lenKey); for (Property *p = props[hash % hashRoots]; p; p = p->next) { if ((hash == p->hash) && ((strlen(p->key) == static_cast(lenKey)) && (0 == strncmp(p->key, key, lenKey)))) { // Replace current value delete [](p->val); p->val = StringDup(val, lenVal); return ; } } // Not found Property *pNew = new Property; if (pNew) { pNew->hash = hash; pNew->key = StringDup(key, lenKey); pNew->val = StringDup(val, lenVal); pNew->next = props[hash % hashRoots]; props[hash % hashRoots] = pNew; } } void PropSet::Set(const char *keyVal) { while (isspace(*keyVal)) keyVal++; const char *endVal = keyVal; while (*endVal && (*endVal != '\n')) endVal++; const char *eqAt = strchr(keyVal, '='); if (eqAt) { Set(keyVal, eqAt + 1, eqAt-keyVal, endVal - eqAt - 1); } else if (*keyVal) { // No '=' so assume '=1' Set(keyVal, "1", endVal-keyVal, 1); } } 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) { 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 ""; } } static bool IncludesVar(const char *value, const char *key) { const char *var = strstr(value, "$("); while (var) { if (isprefix(var + 2, key) && (var[2 + strlen(key)] == ')')) { // Found $(key) which would lead to an infinite loop so exit return true; } var = strstr(var + 2, ")"); if (var) var = strstr(var + 1, "$("); } return false; } SString PropSet::GetExpanded(const char *key) { SString val = Get(key); if (IncludesVar(val.c_str(), key)) return val; return Expand(val.c_str()); } SString PropSet::Expand(const char *withVars) { char *base = StringDup(withVars); char *cpvar = strstr(base, "$("); while (cpvar) { char *cpendvar = strchr(cpvar, ')'); if (cpendvar) { int lenvar = cpendvar - cpvar - 2; // Subtract the $() char *var = StringDup(cpvar + 2, lenvar); SString val = GetExpanded(var); int newlenbase = strlen(base) + val.length() - lenvar; char *newbase = new char[newlenbase]; strncpy(newbase, base, cpvar - base); strcpy(newbase + (cpvar - base), val.c_str()); strcpy(newbase + (cpvar - base) + val.length(), cpendvar + 1); delete []var; delete []base; base = newbase; } cpvar = strstr(base, "$("); } SString sret = base; delete []base; return sret; } int PropSet::GetInt(const char *key, int defaultValue) { 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; } static bool IsSuffixCaseInsensitive(const char *target, const char *suffix) { int lentarget = strlen(target); int lensuffix = strlen(suffix); if (lensuffix > lentarget) return false; for (int i = lensuffix - 1; i >= 0; i--) { if (MakeUpperCase(target[i + lentarget - lensuffix]) != MakeUpperCase(suffix[i])) return false; } return true; } SString PropSet::GetWild(const char *keybase, const char *filename) { for (int root = 0; root < hashRoots; root++) { for (Property *p = props[root]; p; p = p->next) { if (isprefix(p->key, keybase)) { char * orgkeyfile = p->key + strlen(keybase); char *keyfile = NULL; if (strstr(orgkeyfile, "$(") == orgkeyfile) { char *cpendvar = strchr(orgkeyfile, ')'); if (cpendvar) { *cpendvar = '\0'; SString s = GetExpanded(orgkeyfile + 2); *cpendvar = ')'; keyfile = StringDup(s.c_str()); } } char *keyptr = keyfile; if (keyfile == NULL) keyfile = orgkeyfile; for (; ; ) { char *del = strchr(keyfile, ';'); if (del == NULL) del = keyfile + strlen(keyfile); char delchr = *del; *del = '\0'; if (*keyfile == '*') { if (IsSuffixCaseInsensitive(filename, keyfile + 1)) { *del = delchr; delete []keyptr; return p->val; } } else if (0 == strcmp(keyfile, filename)) { *del = delchr; delete []keyptr; return p->val; } if (delchr == '\0') break; *del = delchr; keyfile = del + 1; } delete []keyptr; if (0 == strcmp(p->key, keybase)) { return p->val; } } } } if (superPS) { // Failed here, so try in base property set return superPS->GetWild(keybase, filename); } else { return ""; } } // GetNewExpand does not use Expand as it has to use GetWild with the filename for each // variable reference found. SString PropSet::GetNewExpand(const char *keybase, const char *filename) { char *base = StringDup(GetWild(keybase, filename).c_str()); char *cpvar = strstr(base, "$("); while (cpvar) { char *cpendvar = strchr(cpvar, ')'); if (cpendvar) { int lenvar = cpendvar - cpvar - 2; // Subtract the $() char *var = StringDup(cpvar + 2, lenvar); SString val = GetWild(var, filename); int newlenbase = strlen(base) + val.length() - lenvar; char *newbase = new char[newlenbase]; strncpy(newbase, base, cpvar - base); strcpy(newbase + (cpvar - base), val.c_str()); strcpy(newbase + (cpvar - base) + val.length(), cpendvar + 1); delete []var; delete []base; base = newbase; } cpvar = strstr(base, "$("); } SString sret = base; delete []base; return sret; } 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() { unsigned int len=0; for (int r = 0; r < hashRoots; r++) { for (Property HTTP/1.1 200 OK Connection: keep-alive Connection: keep-alive Content-Disposition: inline; filename="PropSet.cxx" Content-Disposition: inline; filename="PropSet.cxx" Content-Length: 17925 Content-Length: 17925 Content-Security-Policy: default-src 'none' Content-Security-Policy: default-src 'none' Content-Type: text/plain; charset=UTF-8 Content-Type: text/plain; charset=UTF-8 Date: Mon, 20 Oct 2025 07:35:56 UTC ETag: "ba938947f59db0d9751214375126fc4a4af13da0" ETag: "ba938947f59db0d9751214375126fc4a4af13da0" Expires: Thu, 18 Oct 2035 07:35:56 GMT Expires: Thu, 18 Oct 2035 07:35:56 GMT Last-Modified: Mon, 20 Oct 2025 07:35:56 GMT Last-Modified: Mon, 20 Oct 2025 07:35:56 GMT Server: OpenBSD httpd Server: OpenBSD httpd X-Content-Type-Options: nosniff X-Content-Type-Options: nosniff // SciTE - Scintilla based Text Editor /** @file PropSet.cxx ** A Java style properties file module. **/ // Copyright 1998-2002 by Neil Hodgson // The License.txt file describes the conditions under which this software may be distributed. // Maintain a dictionary of properties #include #include #include #include #include "Platform.h" #include "PropSet.h" // The comparison and case changing functions here assume ASCII // or extended ASCII such as the normal Windows code page. static inline char MakeUpperCase(char ch) { if (ch < 'a' || ch > 'z') return ch; else return static_cast(ch - 'a' + 'A'); } static inline bool IsLetter(char ch) { return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')); } int CompareCaseInsensitive(const char *a, const char *b) { while (*a && *b) { if (*a != *b) { char upperA = MakeUpperCase(*a); char upperB = MakeUpperCase(*b); if (upperA != upperB) return upperA - upperB; } a++; b++; } // Either *a or *b is nul return *a - *b; } int CompareNCaseInsensitive(const char *a, const char *b, int len) { while (*a && *b && len) { if (*a != *b) { char upperA = MakeUpperCase(*a); char upperB = MakeUpperCase(*b); if (upperA != upperB) return upperA - upperB; } a++; b++; len--; } if (len == 0) return 0; else // Either *a or *b is nul return *a - *b; } bool EqualCaseInsensitive(const char *a, const char *b) { return 0 == CompareCaseInsensitive(a, b); } inline unsigned int HashString(const char *s, int len) { unsigned int ret = 0; while (len--) { ret <<= 4; ret ^= *s; s++; } return ret; } PropSet::PropSet() { superPS = 0; for (int root = 0; root < hashRoots; root++) props[root] = 0; } PropSet::~PropSet() { superPS = 0; Clear(); } void PropSet::Set(const char *key, const char *val, int lenKey, int lenVal) { if (!*key) // Empty keys are not supported return; if (lenKey == -1) lenKey = strlen(key); if (lenVal == -1) lenVal = strlen(val); unsigned int hash = HashString(key, lenKey); for (Property *p = props[hash % hashRoots]; p; p = p->next) { if ((hash == p->hash) && ((strlen(p->key) == static_cast(lenKey)) && (0 == strncmp(p->key, key, lenKey)))) { // Replace current value delete [](p->val); p->val = StringDup(val, lenVal); return ; } } // Not found Property *pNew = new Property; if (pNew) { pNew->hash = hash; pNew->key = StringDup(key, lenKey); pNew->val = StringDup(val, lenVal); pNew->next = props[hash % hashRoots]; props[hash % hashRoots] = pNew; } } void PropSet::Set(const char *keyVal) { while (isspace(*keyVal)) keyVal++; const char *endVal = keyVal; while (*endVal && (*endVal != '\n')) endVal++; const char *eqAt = strchr(keyVal, '='); if (eqAt) { Set(keyVal, eqAt + 1, eqAt-keyVal, endVal - eqAt - 1); } else if (*keyVal) { // No '=' so assume '=1' Set(keyVal, "1", endVal-keyVal, 1); } } 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) { 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 ""; } } static bool IncludesVar(const char *value, const char *key) { const char *var = strstr(value, "$("); while (var) { if (isprefix(var + 2, key) && (var[2 + strlen(key)] == ')')) { // Found $(key) which would lead to an infinite loop so exit return true; } var = strstr(var + 2, ")"); if (var) var = strstr(var + 1, "$("); } return false; } SString PropSet::GetExpanded(const char *key) { SString val = Get(key); if (IncludesVar(val.c_str(), key)) return val; return Expand(val.c_str()); } SString PropSet::Expand(const char *withVars) { char *base = StringDup(withVars); char *cpvar = strstr(base, "$("); while (cpvar) { char *cpendvar = strchr(cpvar, ')'); if (cpendvar) { int lenvar = cpendvar - cpvar - 2; // Subtract the $() char *var = StringDup(cpvar + 2, lenvar); SString val = GetExpanded(var); int newlenbase = strlen(base) + val.length() - lenvar; char *newbase = new char[newlenbase]; strncpy(newbase, base, cpvar - base); strcpy(newbase + (cpvar - base), val.c_str()); strcpy(newbase + (cpvar - base) + val.length(), cpendvar + 1); delete []var; delete []base; base = newbase; } cpvar = strstr(base, "$("); } SString sret = base; delete []base; return sret; } int PropSet::GetInt(const char *key, int defaultValue) { 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; } static bool IsSuffixCaseInsensitive(const char *target, const char *suffix) { int lentarget = strlen(target); int lensuffix = strlen(suffix); if (lensuffix > lentarget) return false; for (int i = lensuffix - 1; i >= 0; i--) { if (MakeUpperCase(target[i + lentarget - lensuffix]) != MakeUpperCase(suffix[i])) return false; } return true; } SString PropSet::GetWild(const char *keybase, const char *filename) { for (int root = 0; root < hashRoots; root++) { for (Property *p = props[root]; p; p = p->next) { if (isprefix(p->key, keybase)) { char * orgkeyfile = p->key + strlen(keybase); char *keyfile = NULL; if (strstr(orgkeyfile, "$(") == orgkeyfile) { char *cpendvar = strchr(orgkeyfile, ')'); if (cpendvar) { *cpendvar = '\0'; SString s = GetExpanded(orgkeyfile + 2); *cpendvar = ')'; keyfile = StringDup(s.c_str()); } } char *keyptr = keyfile; if (keyfile == NULL) keyfile = orgkeyfile; for (; ; ) { char *del = strchr(keyfile, ';'); if (del == NULL) del = keyfile + strlen(keyfile); char delchr = *del; *del = '\0'; if (*keyfile == '*') { if (IsSuffixCaseInsensitive(filename, keyfile + 1)) { *del = delchr; delete []keyptr; return p->val; } } else if (0 == strcmp(keyfile, filename)) { *del = delchr; delete []keyptr; return p->val; } if (delchr == '\0') break; *del = delchr; keyfile = del + 1; } delete []keyptr; if (0 == strcmp(p->key, keybase)) { return p->val; } } } } if (superPS) { // Failed here, so try in base property set return superPS->GetWild(keybase, filename); } else { return ""; } } // GetNewExpand does not use Expand as it has to use GetWild with the filename for each // variable reference found. SString PropSet::GetNewExpand(const char *keybase, const char *filename) { char *base = StringDup(GetWild(keybase, filename).c_str()); char *cpvar = strstr(base, "$("); while (cpvar) { char *cpendvar = strchr(cpvar, ')'); if (cpendvar) { int lenvar = cpendvar - cpvar - 2; // Subtract the $() char *var = StringDup(cpvar + 2, lenvar); SString val = GetWild(var, filename); int newlenbase = strlen(base) + val.length() - lenvar; char *newbase = new char[newlenbase]; strncpy(newbase, base, cpvar - base); strcpy(newbase + (cpvar - base), val.c_str()); strcpy(newbase + (cpvar - base) + val.length(), cpendvar + 1); delete []var; delete []base; base = newbase; } cpvar = strstr(base, "$("); } SString sret = base; delete []base; return sret; } 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() { unsigned int 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'; } return ret; } /** * Initiate enumeration. */ bool PropSet::GetFirst(char **key, char **val) { for (int i = 0; i < hashRoots; i++) { for (Property *p = props[i]; p; p = p->next) { if (p) { *key = p->key; *val = p->val; enumnext = p->n