]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/PropSet.cxx
29679969132f0f3eeabcf9da2c084875303df0a0
1 // SciTE - Scintilla based Text Editor
3 ** A Java style properties file module.
5 // Copyright 1998-2002 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
19 // The comparison and case changing functions here assume ASCII
20 // or extended ASCII such as the normal Windows code page.
22 static inline char MakeUpperCase(char ch
) {
23 if (ch
< 'a' || ch
> 'z')
26 return static_cast<char>(ch
- 'a' + 'A');
29 static inline bool IsLetter(char ch
) {
30 return ((ch
>= 'a' && ch
<= 'z') || (ch
>= 'A' && ch
<= 'Z'));
33 int CompareCaseInsensitive(const char *a
, const char *b
) {
36 char upperA
= MakeUpperCase(*a
);
37 char upperB
= MakeUpperCase(*b
);
39 return upperA
- upperB
;
44 // Either *a or *b is nul
48 int CompareNCaseInsensitive(const char *a
, const char *b
, size_t len
) {
49 while (*a
&& *b
&& len
) {
51 char upperA
= MakeUpperCase(*a
);
52 char upperB
= MakeUpperCase(*b
);
54 return upperA
- upperB
;
63 // Either *a or *b is nul
67 bool EqualCaseInsensitive(const char *a
, const char *b
) {
68 return 0 == CompareCaseInsensitive(a
, b
);
71 inline unsigned int HashString(const char *s
, size_t len
) {
83 for (int root
= 0; root
< hashRoots
; root
++)
92 void PropSet::Set(const char *key
, const char *val
, int lenKey
, int lenVal
) {
93 if (!*key
) // Empty keys are not supported
96 lenKey
= static_cast<int>(strlen(key
));
98 lenVal
= static_cast<int>(strlen(val
));
99 unsigned int hash
= HashString(key
, lenKey
);
100 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
101 if ((hash
== p
->hash
) &&
102 ((strlen(p
->key
) == static_cast<unsigned int>(lenKey
)) &&
103 (0 == strncmp(p
->key
, key
, lenKey
)))) {
104 // Replace current value
106 p
->val
= StringDup(val
, lenVal
);
111 Property
*pNew
= new Property
;
114 pNew
->key
= StringDup(key
, lenKey
);
115 pNew
->val
= StringDup(val
, lenVal
);
116 pNew
->next
= props
[hash
% hashRoots
];
117 props
[hash
% hashRoots
] = pNew
;
121 void PropSet::Set(const char *keyVal
) {
122 while (isspace(*keyVal
))
124 const char *endVal
= keyVal
;
125 while (*endVal
&& (*endVal
!= '\n'))
127 const char *eqAt
= strchr(keyVal
, '=');
129 Set(keyVal
, eqAt
+ 1, eqAt
-keyVal
, endVal
- eqAt
- 1);
130 } else if (*keyVal
) { // No '=' so assume '=1'
131 Set(keyVal
, "1", endVal
-keyVal
, 1);
135 void PropSet::SetMultiple(const char *s
) {
136 const char *eol
= strchr(s
, '\n');
140 eol
= strchr(s
, '\n');
145 SString
PropSet::Get(const char *key
) {
146 unsigned int hash
= HashString(key
, strlen(key
));
147 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
148 if ((hash
== p
->hash
) && (0 == strcmp(p
->key
, key
))) {
153 // Failed here, so try in base property set
154 return superPS
->Get(key
);
160 static bool IncludesVar(const char *value
, const char *key
) {
161 const char *var
= strstr(value
, "$(");
163 if (isprefix(var
+ 2, key
) && (var
[2 + strlen(key
)] == ')')) {
164 // Found $(key) which would lead to an infinite loop so exit
167 var
= strstr(var
+ 2, ")");
169 var
= strstr(var
+ 1, "$(");
174 SString
PropSet::GetExpanded(const char *key
) {
175 SString val
= Get(key
);
176 if (IncludesVar(val
.c_str(), key
))
178 return Expand(val
.c_str());
181 SString
PropSet::Expand(const char *withVars
) {
182 char *base
= StringDup(withVars
);
183 char *cpvar
= strstr(base
, "$(");
185 char *cpendvar
= strchr(cpvar
, ')');
187 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
188 char *var
= StringDup(cpvar
+ 2, lenvar
);
189 SString val
= GetExpanded(var
);
190 size_t newlenbase
= strlen(base
) + val
.length() - lenvar
;
191 char *newbase
= new char[newlenbase
];
192 strncpy(newbase
, base
, cpvar
- base
);
193 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
194 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
199 cpvar
= strstr(base
, "$(");
206 int PropSet::GetInt(const char *key
, int defaultValue
) {
207 SString val
= GetExpanded(key
);
213 bool isprefix(const char *target
, const char *prefix
) {
214 while (*target
&& *prefix
) {
215 if (*target
!= *prefix
)
226 static bool IsSuffixCaseInsensitive(const char *target
, const char *suffix
) {
227 size_t lentarget
= strlen(target
);
228 size_t lensuffix
= strlen(suffix
);
229 if (lensuffix
> lentarget
)
231 for (int i
= static_cast<int>(lensuffix
) - 1; i
>= 0; i
--) {
232 if (MakeUpperCase(target
[i
+ lentarget
- lensuffix
]) !=
233 MakeUpperCase(suffix
[i
]))
239 SString
PropSet::GetWild(const char *keybase
, const char *filename
) {
240 for (int root
= 0; root
< hashRoots
; root
++) {
241 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
242 if (isprefix(p
->key
, keybase
)) {
243 char * orgkeyfile
= p
->key
+ strlen(keybase
);
244 char *keyfile
= NULL
;
246 if (strstr(orgkeyfile
, "$(") == orgkeyfile
) {
247 char *cpendvar
= strchr(orgkeyfile
, ')');
250 SString s
= GetExpanded(orgkeyfile
+ 2);
252 keyfile
= StringDup(s
.c_str());
255 char *keyptr
= keyfile
;
258 keyfile
= orgkeyfile
;
261 char *del
= strchr(keyfile
, ';');
263 del
= keyfile
+ strlen(keyfile
);
266 if (*keyfile
== '*') {
267 if (IsSuffixCaseInsensitive(filename
, keyfile
+ 1)) {
272 } else if (0 == strcmp(keyfile
, filename
)) {
284 if (0 == strcmp(p
->key
, keybase
)) {
291 // Failed here, so try in base property set
292 return superPS
->GetWild(keybase
, filename
);
298 // GetNewExpand does not use Expand as it has to use GetWild with the filename for each
299 // variable reference found.
300 SString
PropSet::GetNewExpand(const char *keybase
, const char *filename
) {
301 char *base
= StringDup(GetWild(keybase
, filename
).c_str());
302 char *cpvar
= strstr(base
, "$(");
304 char *cpendvar
= strchr(cpvar
, ')');
306 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
307 char *var
= StringDup(cpvar
+ 2, lenvar
);
308 SString val
= GetWild(var
, filename
);
309 size_t newlenbase
= strlen(base
) + val
.length() - lenvar
;
310 char *newbase
= new char[newlenbase
];
311 strncpy(newbase
, base
, cpvar
- base
);
312 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
313 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
318 cpvar
= strstr(base
, "$(");
325 void PropSet::Clear() {
326 for (int root
= 0; root
< hashRoots
; root
++) {
327 Property
*p
= props
[root
];
329 Property
*pNext
= p
->next
;
342 char *PropSet::ToString() {
344 for (int r
= 0; r
< hashRoots
; r
++) {
345 for (Property
*p
= props
[r
]; p
; p
= p
->next
) {
346 len
+= strlen(p
->key
) + 1;
347 len
+= strlen(p
->val
) + 1;
351 len
= 1; // Return as empty string
352 char *ret
= new char [len
];
355 for (int root
= 0; root
< hashRoots
; root
++) {
356 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
371 * Initiate enumeration.
373 bool PropSet::GetFirst(char **key
, char **val
) {
374 for (int i
= 0; i
< hashRoots
; i
++) {
375 for (Property
*p
= props
[i
]; p
; p
= p
->next
) {
379 enumnext
= p
->next
; // GetNext will begin here ...
380 enumhash
= i
; // ... in this block
389 * Continue enumeration.
391 bool PropSet::GetNext(char ** key
, char ** val
) {
392 bool firstloop
= true;
394 // search begins where we left it : in enumhash block
395 for (int i
= enumhash
; i
< hashRoots
; i
++) {
397 enumnext
= props
[i
]; // Begin with first property in block
398 // else : begin where we left
401 for (Property
*p
= enumnext
; p
; p
= p
->next
) {
405 enumnext
= p
->next
; // for GetNext
414 static bool iswordsep(char ch
, bool onlyLineEnds
) {
419 return ch
== '\r' || ch
== '\n';
423 * Creates an array that points into each word in the string and puts \0 terminators
426 static char **ArrayFromWordList(char *wordlist
, int *len
, bool onlyLineEnds
= false) {
429 for (int j
= 0; wordlist
[j
]; j
++) {
430 if (!iswordsep(wordlist
[j
], onlyLineEnds
) && iswordsep(prev
, onlyLineEnds
))
434 char **keywords
= new char * [words
+ 1];
438 size_t slen
= strlen(wordlist
);
439 for (size_t k
= 0; k
< slen
; k
++) {
440 if (!iswordsep(wordlist
[k
], onlyLineEnds
)) {
442 keywords
[words
] = &wordlist
[k
];
450 keywords
[words
] = &wordlist
[slen
];
458 void WordList::Clear() {
462 delete []wordsNoCase
;
471 void WordList::Set(const char *s
) {
474 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
475 wordsNoCase
= new char * [len
+ 1];
476 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
479 char *WordList::Allocate(int size
) {
480 list
= new char[size
+ 1];
485 void WordList::SetFromAllocated() {
487 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
488 wordsNoCase
= new char * [len
+ 1];
489 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
492 int cmpString(const void *a1
, const void *a2
) {
493 // Can't work out the correct incantation to use modern casts here
494 return strcmp(*(char**)(a1
), *(char**)(a2
));
497 int cmpStringNoCase(const void *a1
, const void *a2
) {
498 // Can't work out the correct incantation to use modern casts here
499 return CompareCaseInsensitive(*(char**)(a1
), *(char**)(a2
));
502 static void SortWordList(char **words
, char **wordsNoCase
, unsigned int len
) {
503 qsort(reinterpret_cast<void*>(words
), len
, sizeof(*words
),
505 qsort(reinterpret_cast<void*>(wordsNoCase
), len
, sizeof(*wordsNoCase
),
509 bool WordList::InList(const char *s
) {
514 SortWordList(words
, wordsNoCase
, len
);
515 for (unsigned int k
= 0; k
< (sizeof(starts
) / sizeof(starts
[0])); k
++)
517 for (int l
= len
- 1; l
>= 0; l
--) {
518 unsigned char indexChar
= words
[l
][0];
519 starts
[indexChar
] = l
;
522 unsigned char firstChar
= s
[0];
523 int j
= starts
[firstChar
];
525 while (words
[j
][0] == firstChar
) {
526 if (s
[1] == words
[j
][1]) {
527 const char *a
= words
[j
] + 1;
528 const char *b
= s
+ 1;
529 while (*a
&& *a
== *b
) {
541 while (words
[j
][0] == '^') {
542 const char *a
= words
[j
] + 1;
544 while (*a
&& *a
== *b
) {
557 * Returns an element (complete) of the wordlist array which has
558 * the same beginning as the passed string.
559 * The length of the word to compare is passed too.
560 * Letter case can be ignored or preserved (default).
562 const char *WordList::GetNearestWord(const char *wordStart
, int searchLen
/*= -1*/, bool ignoreCase
/*= false*/, SString wordCharacters
/*='/0' */) {
563 int start
= 0; // lower bound of the api array block to search
564 int end
= len
- 1; // upper bound of the api array block to search
565 int pivot
; // index of api array element just being compared
566 int cond
; // comparison result (in the sense of strcmp() result)
567 const char *word
; // api array element just being compared
573 SortWordList(words
, wordsNoCase
, len
);
576 while (start
<= end
) { // binary searching loop
577 pivot
= (start
+ end
) >> 1;
578 word
= wordsNoCase
[pivot
];
579 cond
= CompareNCaseInsensitive(wordStart
, word
, searchLen
);
580 if (!cond
&& (!wordCharacters
.contains(word
[searchLen
])))
581 return word
; // result must not be freed with free()
587 } else { // preserve the letter case
588 while (start
<= end
) { // binary searching loop
589 pivot
= (start
+ end
) >> 1;
591 cond
= strncmp(wordStart
, word
, searchLen
);
592 if (!cond
&& (!wordCharacters
.contains(word
[searchLen
])))
593 return word
; // result must not be freed with free()
604 * Find the length of a 'word' which is actually an identifier in a string
605 * which looks like "identifier(..." or "identifier:" or "identifier" and where
606 * there may be extra spaces after the identifier that should not be
607 * counted in the length.
609 static unsigned int LengthWord(const char *word
, char otherSeparator
) {
610 // Find a '(', or ':'. If that fails go to the end of the string.
611 const char *endWord
= strchr(word
, '(');
613 endWord
= strchr(word
, ':');
614 if (!endWord
&& otherSeparator
)
615 endWord
= strchr(word
, otherSeparator
);
617 endWord
= word
+ strlen(word
);
618 // Last case always succeeds so endWord != 0
620 // Drop any space characters.
621 if (endWord
> word
) {
622 endWord
--; // Back from the '(', ':', or '\0'
623 // Move backwards over any spaces
624 while ((endWord
> word
) && (isspace(*endWord
))) {
628 return endWord
- word
;
632 * Returns elements (first words of them) of the wordlist array which have
633 * the same beginning as the passed string.
634 * The length of the word to compare is passed too.
635 * Letter case can be ignored or preserved (default).
636 * If there are more words meeting the condition they are returned all of
637 * them in the ascending order separated with spaces.
639 * NOTE: returned buffer has to be freed with delete[].
641 char *WordList::GetNearestWords(
642 const char *wordStart
,
643 int searchLen
/*= -1*/,
644 bool ignoreCase
/*= false*/,
645 char otherSeparator
/*= '\0'*/) {
646 int wordlen
; // length of the word part (before the '(' brace) of the api array element
648 wordsNear
.setsizegrowth(1000);
649 int start
= 0; // lower bound of the api array block to search
650 int end
= len
- 1; // upper bound of the api array block to search
651 int pivot
; // index of api array element just being compared
652 int cond
; // comparison result (in the sense of strcmp() result)
658 SortWordList(words
, wordsNoCase
, len
);
661 while (start
<= end
) { // Binary searching loop
662 pivot
= (start
+ end
) / 2;
663 cond
= CompareNCaseInsensitive(wordStart
, wordsNoCase
[pivot
], searchLen
);
666 while ((pivot
> start
) &&
667 (0 == CompareNCaseInsensitive(wordStart
,
668 wordsNoCase
[pivot
-1], searchLen
))) {
672 while ((pivot
<= end
) &&
673 (0 == CompareNCaseInsensitive(wordStart
,
674 wordsNoCase
[pivot
], searchLen
))) {
675 wordlen
= LengthWord(wordsNoCase
[pivot
], otherSeparator
) + 1;
676 wordsNear
.append(wordsNoCase
[pivot
], wordlen
, ' ');
679 return wordsNear
.detach();
680 } else if (cond
< 0) {
682 } else if (cond
> 0) {
686 } else { // Preserve the letter case
687 while (start
<= end
) { // Binary searching loop
688 pivot
= (start
+ end
) / 2;
689 cond
= strncmp(wordStart
, words
[pivot
], searchLen
);
692 while ((pivot
> start
) &&
693 (0 == strncmp(wordStart
,
694 words
[pivot
-1], searchLen
))) {
698 while ((pivot
<= end
) &&
699 (0 == strncmp(wordStart
,
700 words
[pivot
], searchLen
))) {
701 wordlen
= LengthWord(words
[pivot
], otherSeparator
) + 1;
702 wordsNear
.append(words
[pivot
], wordlen
, ' ');
705 return wordsNear
.detach();
706 } else if (cond
< 0) {
708 } else if (cond
> 0) {