1 // SciTE - Scintilla based Text Editor
2 /** @file PropSetSimple.cxx
3 ** A Java style properties file module.
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
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)
22 #include "PropSetSimple.h"
25 using namespace Scintilla
;
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
);
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
46 lenKey
= static_cast<int>(strlen(key
));
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
))
59 const char *endVal
= keyVal
;
60 while (*endVal
&& (*endVal
!= '\n'))
62 const char *eqAt
= strchr(keyVal
, '=');
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');
75 eol
= strchr(s
, '\n');
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();
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.
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
));
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
) {
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("$(");
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());
151 int PropSetSimple::GetExpanded(const char *key
, char *result
) const {
152 char *val
= Expanded(key
);
153 const int n
= strlen(val
);
158 return n
; // Not including NUL
161 int PropSetSimple::GetInt(const char *key
, int defaultValue
) const {
162 char *val
= Expanded(key
);
164 int retVal
= val
[0] ? atoi(val
) : defaultValue
;