]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/PropSet.cxx
b527c385ce15f54beff0d2fb63a006dc2e0b2896
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
415 * Creates an array that points into each word in the string and puts \0 terminators
418 static char **ArrayFromWordList(char *wordlist
, int *len
, bool onlyLineEnds
= false) {
421 // For rapid determination of whether a character is a separator, build
423 bool wordSeparator
[256];
424 for (int i
=0;i
<256; i
++) {
425 wordSeparator
[i
] = false;
427 wordSeparator
['\r'] = true;
428 wordSeparator
['\n'] = true;
430 wordSeparator
[' '] = true;
431 wordSeparator
['\t'] = true;
433 for (int j
= 0; wordlist
[j
]; j
++) {
434 int curr
= static_cast<unsigned char>(wordlist
[j
]);
435 if (!wordSeparator
[curr
] && wordSeparator
[prev
])
439 char **keywords
= new char *[words
+ 1];
443 size_t slen
= strlen(wordlist
);
444 for (size_t k
= 0; k
< slen
; k
++) {
445 if (!wordSeparator
[static_cast<unsigned char>(wordlist
[k
])]) {
447 keywords
[words
] = &wordlist
[k
];
455 keywords
[words
] = &wordlist
[slen
];
463 void WordList::Clear() {
467 delete []wordsNoCase
;
476 void WordList::Set(const char *s
) {
479 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
480 wordsNoCase
= new char * [len
+ 1];
481 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
484 char *WordList::Allocate(int size
) {
485 list
= new char[size
+ 1];
490 void WordList::SetFromAllocated() {
492 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
493 wordsNoCase
= new char * [len
+ 1];
494 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
497 int cmpString(const void *a1
, const void *a2
) {
498 // Can't work out the correct incantation to use modern casts here
499 return strcmp(*(char**)(a1
), *(char**)(a2
));
502 int cmpStringNoCase(const void *a1
, const void *a2
) {
503 // Can't work out the correct incantation to use modern casts here
504 return CompareCaseInsensitive(*(char**)(a1
), *(char**)(a2
));
507 static void SortWordList(char **words
, char **wordsNoCase
, unsigned int len
) {
508 qsort(reinterpret_cast<void*>(words
), len
, sizeof(*words
),
510 qsort(reinterpret_cast<void*>(wordsNoCase
), len
, sizeof(*wordsNoCase
),
514 bool WordList::InList(const char *s
) {
519 SortWordList(words
, wordsNoCase
, len
);
520 for (unsigned int k
= 0; k
< (sizeof(starts
) / sizeof(starts
[0])); k
++)
522 for (int l
= len
- 1; l
>= 0; l
--) {
523 unsigned char indexChar
= words
[l
][0];
524 starts
[indexChar
] = l
;
527 unsigned char firstChar
= s
[0];
528 int j
= starts
[firstChar
];
530 while (words
[j
][0] == firstChar
) {
531 if (s
[1] == words
[j
][1]) {
532 const char *a
= words
[j
] + 1;
533 const char *b
= s
+ 1;
534 while (*a
&& *a
== *b
) {
546 while (words
[j
][0] == '^') {
547 const char *a
= words
[j
] + 1;
549 while (*a
&& *a
== *b
) {
562 * Returns an element (complete) of the wordlist array which has
563 * the same beginning as the passed string.
564 * The length of the word to compare is passed too.
565 * Letter case can be ignored or preserved (default).
567 const char *WordList::GetNearestWord(const char *wordStart
, int searchLen
/*= -1*/, bool ignoreCase
/*= false*/, SString wordCharacters
/*='/0' */) {
568 int start
= 0; // lower bound of the api array block to search
569 int end
= len
- 1; // upper bound of the api array block to search
570 int pivot
; // index of api array element just being compared
571 int cond
; // comparison result (in the sense of strcmp() result)
572 const char *word
; // api array element just being compared
578 SortWordList(words
, wordsNoCase
, len
);
581 while (start
<= end
) { // binary searching loop
582 pivot
= (start
+ end
) >> 1;
583 word
= wordsNoCase
[pivot
];
584 cond
= CompareNCaseInsensitive(wordStart
, word
, searchLen
);
585 if (!cond
&& (!wordCharacters
.contains(word
[searchLen
])))
586 return word
; // result must not be freed with free()
592 } else { // preserve the letter case
593 while (start
<= end
) { // binary searching loop
594 pivot
= (start
+ end
) >> 1;
596 cond
= strncmp(wordStart
, word
, searchLen
);
597 if (!cond
&& (!wordCharacters
.contains(word
[searchLen
])))
598 return word
; // result must not be freed with free()
609 * Find the length of a 'word' which is actually an identifier in a string
610 * which looks like "identifier(..." or "identifier:" or "identifier" and where
611 * there may be extra spaces after the identifier that should not be
612 * counted in the length.
614 static unsigned int LengthWord(const char *word
, char otherSeparator
) {
615 // Find a '(', or ':'. If that fails go to the end of the string.
616 const char *endWord
= strchr(word
, '(');
618 endWord
= strchr(word
, ':');
619 if (!endWord
&& otherSeparator
)
620 endWord
= strchr(word
, otherSeparator
);
622 endWord
= word
+ strlen(word
);
623 // Last case always succeeds so endWord != 0
625 // Drop any space characters.
626 if (endWord
> word
) {
627 endWord
--; // Back from the '(', ':', or '\0'
628 // Move backwards over any spaces
629 while ((endWord
> word
) && (isspace(*endWord
))) {
633 return endWord
- word
;
637 * Returns elements (first words of them) of the wordlist array which have
638 * the same beginning as the passed string.
639 * The length of the word to compare is passed too.
640 * Letter case can be ignored or preserved (default).
641 * If there are more words meeting the condition they are returned all of
642 * them in the ascending order separated with spaces.
644 * NOTE: returned buffer has to be freed with delete[].
646 char *WordList::GetNearestWords(
647 const char *wordStart
,
648 int searchLen
/*= -1*/,
649 bool ignoreCase
/*= false*/,
650 char otherSeparator
/*= '\0'*/) {
651 int wordlen
; // length of the word part (before the '(' brace) of the api array element
653 wordsNear
.setsizegrowth(1000);
654 int start
= 0; // lower bound of the api array block to search
655 int end
= len
- 1; // upper bound of the api array block to search
656 int pivot
; // index of api array element just being compared
657 int cond
; // comparison result (in the sense of strcmp() result)
663 SortWordList(words
, wordsNoCase
, len
);
666 while (start
<= end
) { // Binary searching loop
667 pivot
= (start
+ end
) / 2;
668 cond
= CompareNCaseInsensitive(wordStart
, wordsNoCase
[pivot
], searchLen
);
671 while ((pivot
> start
) &&
672 (0 == CompareNCaseInsensitive(wordStart
,
673 wordsNoCase
[pivot
-1], searchLen
))) {
677 while ((pivot
<= end
) &&
678 (0 == CompareNCaseInsensitive(wordStart
,
679 wordsNoCase
[pivot
], searchLen
))) {
680 wordlen
= LengthWord(wordsNoCase
[pivot
], otherSeparator
) + 1;
681 wordsNear
.append(wordsNoCase
[pivot
], wordlen
, ' ');
684 return wordsNear
.detach();
685 } else if (cond
< 0) {
687 } else if (cond
> 0) {
691 } else { // Preserve the letter case
692 while (start
<= end
) { // Binary searching loop
693 pivot
= (start
+ end
) / 2;
694 cond
= strncmp(wordStart
, words
[pivot
], searchLen
);
697 while ((pivot
> start
) &&
698 (0 == strncmp(wordStart
,
699 words
[pivot
-1], searchLen
))) {
703 while ((pivot
<= end
) &&
704 (0 == strncmp(wordStart
,
705 words
[pivot
], searchLen
))) {
706 wordlen
= LengthWord(words
[pivot
], otherSeparator
) + 1;
707 wordsNear
.append(words
[pivot
], wordlen
, ' ');
710 return wordsNear
.detach();
711 } else if (cond
< 0) {
713 } else if (cond
> 0) {