scintilla: Update scintilla with changeset 3662:1d1c06df8a2f using gtk+3
[anjuta-extras.git] / plugins / scintilla / scintilla / PropSetSimple.cxx
blob6942f6e71fa103809cba9f9f984fc92dee0cfddb
1 // SciTE - Scintilla based Text Editor
2 /** @file PropSetSimple.cxx
3 ** A Java style properties file module.
4 **/
5 // Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 // Maintain a dictionary of properties
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
14 #ifdef _MSC_VER
15 // Visual C++ doesn't like unreachable code or long decorated names in its own headers.
16 #pragma warning(disable: 4018 4100 4245 4511 4512 4663 4702 4786)
17 #endif
19 #include <string>
20 #include <map>
22 #include "PropSetSimple.h"
24 #ifdef SCI_NAMESPACE
25 using namespace Scintilla;
26 #endif
28 typedef std::map<std::string, std::string> mapss;
30 PropSetSimple::PropSetSimple() {
31 mapss *props = new mapss;
32 impl = static_cast<void *>(props);
35 PropSetSimple::~PropSetSimple() {
36 mapss *props = static_cast<mapss *>(impl);
37 delete props;
38 impl = 0;
41 void PropSetSimple::Set(const char *key, const char *val, int lenKey, int lenVal) {
42 mapss *props = static_cast<mapss *>(impl);
43 if (!*key) // Empty keys are not supported
44 return;
45 if (lenKey == -1)
46 lenKey = static_cast<int>(strlen(key));
47 if (lenVal == -1)
48 lenVal = static_cast<int>(strlen(val));
49 (*props)[std::string(key, lenKey)] = std::string(val, lenVal);
52 static bool IsASpaceCharacter(unsigned int ch) {
53 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
56 void PropSetSimple::Set(const char *keyVal) {
57 while (IsASpaceCharacter(*keyVal))
58 keyVal++;
59 const char *endVal = keyVal;
60 while (*endVal && (*endVal != '\n'))
61 endVal++;
62 const char *eqAt = strchr(keyVal, '=');
63 if (eqAt) {
64 Set(keyVal, eqAt + 1, eqAt-keyVal, endVal - eqAt - 1);
65 } else if (*keyVal) { // No '=' so assume '=1'
66 Set(keyVal, "1", endVal-keyVal, 1);
70 void PropSetSimple::SetMultiple(const char *s) {
71 const char *eol = strchr(s, '\n');
72 while (eol) {
73 Set(s);
74 s = eol + 1;
75 eol = strchr(s, '\n');
77 Set(s);
80 const char *PropSetSimple::Get(const char *key) const {
81 mapss *props = static_cast<mapss *>(impl);
82 mapss::const_iterator keyPos = props->find(std::string(key));
83 if (keyPos != props->end()) {
84 return keyPos->second.c_str();
85 } else {
86 return "";
90 // There is some inconsistency between GetExpanded("foo") and Expand("$(foo)").
91 // A solution is to keep a stack of variables that have been expanded, so that
92 // recursive expansions can be skipped. For now I'll just use the C++ stack
93 // for that, through a recursive function and a simple chain of pointers.
95 struct VarChain {
96 VarChain(const char *var_=NULL, const VarChain *link_=NULL): var(var_), link(link_) {}
98 bool contains(const char *testVar) const {
99 return (var && (0 == strcmp(var, testVar)))
100 || (link && link->contains(testVar));
103 const char *var;
104 const VarChain *link;
107 static int ExpandAllInPlace(const PropSetSimple &props, std::string &withVars, int maxExpands, const VarChain &blankVars) {
108 size_t varStart = withVars.find("$(");
109 while ((varStart != std::string::npos) && (maxExpands > 0)) {
110 size_t varEnd = withVars.find(")", varStart+2);
111 if (varEnd == std::string::npos) {
112 break;
115 // For consistency, when we see '$(ab$(cde))', expand the inner variable first,
116 // regardless whether there is actually a degenerate variable named 'ab$(cde'.
117 size_t innerVarStart = withVars.find("$(", varStart+2);
118 while ((innerVarStart != std::string::npos) && (innerVarStart > varStart) && (innerVarStart < varEnd)) {
119 varStart = innerVarStart;
120 innerVarStart = withVars.find("$(", varStart+2);
123 std::string var(withVars.c_str(), varStart + 2, varEnd - varStart - 2);
124 std::string val = props.Get(var.c_str());
126 if (blankVars.contains(var.c_str())) {
127 val = ""; // treat blankVar as an empty string (e.g. to block self-reference)
130 if (--maxExpands >= 0) {
131 maxExpands = ExpandAllInPlace(props, val, maxExpands, VarChain(var.c_str(), &blankVars));
134 withVars.erase(varStart, varEnd-varStart+1);
135 withVars.insert(varStart, val.c_str(), val.length());
137 varStart = withVars.find("$(");
140 return maxExpands;
143 char *PropSetSimple::Expanded(const char *key) const {
144 std::string val = Get(key);
145 ExpandAllInPlace(*this, val, 100, VarChain(key));
146 char *ret = new char [val.size() + 1];
147 strcpy(ret, val.c_str());
148 return ret;
151 int PropSetSimple::GetExpanded(const char *key, char *result) const {
152 char *val = Expanded(key);
153 const int n = strlen(val);
154 if (result) {
155 strcpy(result, val);
157 delete []val;
158 return n; // Not including NUL
161 int PropSetSimple::GetInt(const char *key, int defaultValue) const {
162 char *val = Expanded(key);
163 if (val) {
164 int retVal = val[0] ? atoi(val) : defaultValue;
165 delete []val;
166 return retVal;
168 return defaultValue;