]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/PropSet.cxx
701b2a82c75aa9f2c164b935ab6589c49ec7cf0b
1 // SciTE - Scintilla based Text Editor
3 ** A Java style properties file module.
5 // Copyright 1998-2001 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 int CompareCaseInsensitive(const char *a
, const char *b
) {
32 char upperA
= MakeUpperCase(*a
);
33 char upperB
= MakeUpperCase(*b
);
35 return upperA
- upperB
;
40 // Either *a or *b is nul
44 int CompareNCaseInsensitive(const char *a
, const char *b
, int len
) {
45 while (*a
&& *b
&& len
) {
47 char upperA
= MakeUpperCase(*a
);
48 char upperB
= MakeUpperCase(*b
);
50 return upperA
- upperB
;
59 // Either *a or *b is nul
63 bool EqualCaseInsensitive(const char *a
, const char *b
) {
64 return 0 == CompareCaseInsensitive(a
, b
);
67 inline unsigned int HashString(const char *s
, int len
) {
79 for (int root
= 0; root
< hashRoots
; root
++)
88 void PropSet::Set(const char *key
, const char *val
, int lenKey
, int lenVal
) {
89 if (!*key
) // Empty keys are not supported
95 unsigned int hash
= HashString(key
, lenKey
);
96 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
97 if ((hash
== p
->hash
) &&
98 ((strlen(p
->key
) == static_cast<unsigned int>(lenKey
)) &&
99 (0 == strncmp(p
->key
, key
, lenKey
)))) {
100 // Replace current value
102 p
->val
= StringDup(val
, lenVal
);
107 Property
*pNew
= new Property
;
110 pNew
->key
= StringDup(key
, lenKey
);
111 pNew
->val
= StringDup(val
, lenVal
);
112 pNew
->next
= props
[hash
% hashRoots
];
113 props
[hash
% hashRoots
] = pNew
;
117 void PropSet::Set(const char *keyVal
) {
118 while (isspace(*keyVal
))
120 const char *endVal
= keyVal
;
121 while (*endVal
&& (*endVal
!= '\n'))
123 const char *eqAt
= strchr(keyVal
, '=');
125 Set(keyVal
, eqAt
+ 1, eqAt
-keyVal
, endVal
- eqAt
- 1);
126 } else if (*keyVal
) { // No '=' so assume '=1'
127 Set(keyVal
, "1", endVal
-keyVal
, 1);
131 void PropSet::SetMultiple(const char *s
) {
132 const char *eol
= strchr(s
, '\n');
136 eol
= strchr(s
, '\n');
141 SString
PropSet::Get(const char *key
) {
142 unsigned int hash
= HashString(key
, strlen(key
));
143 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
144 if ((hash
== p
->hash
) && (0 == strcmp(p
->key
, key
))) {
149 // Failed here, so try in base property set
150 return superPS
->Get(key
);
156 static bool IncludesVar(const char *value
, const char *key
) {
157 const char *var
= strstr(value
, "$(");
159 if (isprefix(var
+ 2, key
) && (var
[2 + strlen(key
)] == ')')) {
160 // Found $(key) which would lead to an infinite loop so exit
163 var
= strstr(var
+ 2, ")");
165 var
= strstr(var
+ 1, "$(");
170 SString
PropSet::GetExpanded(const char *key
) {
171 SString val
= Get(key
);
172 if (IncludesVar(val
.c_str(), key
))
174 return Expand(val
.c_str());
177 SString
PropSet::Expand(const char *withVars
) {
178 char *base
= StringDup(withVars
);
179 char *cpvar
= strstr(base
, "$(");
181 char *cpendvar
= strchr(cpvar
, ')');
183 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
184 char *var
= StringDup(cpvar
+ 2, lenvar
);
185 SString val
= GetExpanded(var
);
186 int newlenbase
= strlen(base
) + val
.length() - lenvar
;
187 char *newbase
= new char[newlenbase
];
188 strncpy(newbase
, base
, cpvar
- base
);
189 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
190 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
195 cpvar
= strstr(base
, "$(");
202 int PropSet::GetInt(const char *key
, int defaultValue
) {
203 SString val
= GetExpanded(key
);
209 bool isprefix(const char *target
, const char *prefix
) {
210 while (*target
&& *prefix
) {
211 if (*target
!= *prefix
)
222 static bool IsSuffixCaseInsensitive(const char *target
, const char *suffix
) {
223 int lentarget
= strlen(target
);
224 int lensuffix
= strlen(suffix
);
225 if (lensuffix
> lentarget
)
227 for (int i
= lensuffix
- 1; i
>= 0; i
--) {
228 if (MakeUpperCase(target
[i
+ lentarget
- lensuffix
]) !=
229 MakeUpperCase(suffix
[i
]))
235 SString
PropSet::GetWild(const char *keybase
, const char *filename
) {
236 for (int root
= 0; root
< hashRoots
; root
++) {
237 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
238 if (isprefix(p
->key
, keybase
)) {
239 char * orgkeyfile
= p
->key
+ strlen(keybase
);
240 char *keyfile
= NULL
;
242 if (strstr(orgkeyfile
, "$(") == orgkeyfile
) {
243 char *cpendvar
= strchr(orgkeyfile
, ')');
246 SString s
= GetExpanded(orgkeyfile
+ 2);
248 keyfile
= StringDup(s
.c_str());
251 char *keyptr
= keyfile
;
254 keyfile
= orgkeyfile
;
257 char *del
= strchr(keyfile
, ';');
259 del
= keyfile
+ strlen(keyfile
);
262 if (*keyfile
== '*') {
263 if (IsSuffixCaseInsensitive(filename
, keyfile
+ 1)) {
268 } else if (0 == strcmp(keyfile
, filename
)) {
280 if (0 == strcmp(p
->key
, keybase
)) {
287 // Failed here, so try in base property set
288 return superPS
->GetWild(keybase
, filename
);
294 // GetNewExpand does not use Expand as it has to use GetWild with the filename for each
295 // variable reference found.
296 SString
PropSet::GetNewExpand(const char *keybase
, const char *filename
) {
297 char *base
= StringDup(GetWild(keybase
, filename
).c_str());
298 char *cpvar
= strstr(base
, "$(");
300 char *cpendvar
= strchr(cpvar
, ')');
302 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
303 char *var
= StringDup(cpvar
+ 2, lenvar
);
304 SString val
= GetWild(var
, filename
);
305 int newlenbase
= strlen(base
) + val
.length() - lenvar
;
306 char *newbase
= new char[newlenbase
];
307 strncpy(newbase
, base
, cpvar
- base
);
308 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
309 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
314 cpvar
= strstr(base
, "$(");
321 void PropSet::Clear() {
322 for (int root
= 0; root
< hashRoots
; root
++) {
323 Property
*p
= props
[root
];
325 Property
*pNext
= p
->next
;
338 char *PropSet::ToString() {
340 for (int r
= 0; r
< hashRoots
; r
++) {
341 for (Property
*p
= props
[r
]; p
; p
= p
->next
) {
342 len
+= strlen(p
->key
) + 1;
343 len
+= strlen(p
->val
) + 1;
347 len
= 1; // Return as empty string
348 char *ret
= new char [len
];
351 for (int root
= 0; root
< hashRoots
; root
++) {
352 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
367 * Initiate enumeration.
369 bool PropSet::GetFirst(char **key
, char **val
) {
370 for (int i
= 0; i
< hashRoots
; i
++) {
371 for (Property
*p
= props
[i
]; p
; p
= p
->next
) {
375 enumnext
= p
->next
; // GetNext will begin here ...
376 enumhash
= i
; // ... in this block
385 * Continue enumeration.
387 bool PropSet::GetNext(char ** key
, char ** val
) {
388 bool firstloop
= true;
390 // search begins where we left it : in enumhash block
391 for (int i
= enumhash
; i
< hashRoots
; i
++) {
393 enumnext
= props
[i
]; // Begin with first property in block
394 // else : begin where we left
397 for (Property
*p
= enumnext
; p
; p
= p
->next
) {
401 enumnext
= p
->next
; // for GetNext
410 static bool iswordsep(char ch
, bool onlyLineEnds
) {
415 return ch
== '\r' || ch
== '\n';
419 * Creates an array that points into each word in the string and puts \0 terminators
422 static char **ArrayFromWordList(char *wordlist
, int *len
, bool onlyLineEnds
= false) {
425 for (int j
= 0; wordlist
[j
]; j
++) {
426 if (!iswordsep(wordlist
[j
], onlyLineEnds
) && iswordsep(prev
, onlyLineEnds
))
430 char **keywords
= new char * [words
+ 1];
434 int slen
= strlen(wordlist
);
435 for (int k
= 0; k
< slen
; k
++) {
436 if (!iswordsep(wordlist
[k
], onlyLineEnds
)) {
438 keywords
[words
] = &wordlist
[k
];
446 keywords
[words
] = &wordlist
[slen
];
454 void WordList::Clear() {
458 delete []wordsNoCase
;
467 void WordList::Set(const char *s
) {
470 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
471 wordsNoCase
= new char * [len
+ 1];
472 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
475 char *WordList::Allocate(int size
) {
476 list
= new char[size
+ 1];
481 void WordList::SetFromAllocated() {
483 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
484 wordsNoCase
= new char * [len
+ 1];
485 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
488 int cmpString(const void *a1
, const void *a2
) {
489 // Can't work out the correct incantation to use modern casts here
490 return strcmp(*(char**)(a1
), *(char**)(a2
));
493 int cmpStringNoCase(const void *a1
, const void *a2
) {
494 // Can't work out the correct incantation to use modern casts here
495 return CompareCaseInsensitive(*(char**)(a1
), *(char**)(a2
));
498 static void SortWordList(char **words
, char **wordsNoCase
, unsigned int len
) {
499 qsort(reinterpret_cast<void*>(words
), len
, sizeof(*words
),
501 qsort(reinterpret_cast<void*>(wordsNoCase
), len
, sizeof(*wordsNoCase
),
505 bool WordList::InList(const char *s
) {
510 SortWordList(words
, wordsNoCase
, len
);
511 for (unsigned int k
= 0; k
< (sizeof(starts
) / sizeof(starts
[0])); k
++)
513 for (int l
= len
- 1; l
>= 0; l
--) {
514 unsigned char indexChar
= words
[l
][0];
515 starts
[indexChar
] = l
;
518 unsigned char firstChar
= s
[0];
519 int j
= starts
[firstChar
];
521 while (words
[j
][0] == firstChar
) {
522 if (s
[1] == words
[j
][1]) {
523 const char *a
= words
[j
] + 1;
524 const char *b
= s
+ 1;
525 while (*a
&& *a
== *b
) {
537 while (words
[j
][0] == '^') {
538 const char *a
= words
[j
] + 1;
540 while (*a
&& *a
== *b
) {
553 * Returns an element (complete) of the wordlist array which has
554 * the same beginning as the passed string.
555 * The length of the word to compare is passed too.
556 * Letter case can be ignored or preserved (default).
558 const char *WordList::GetNearestWord(const char *wordStart
, int searchLen
/*= -1*/, bool ignoreCase
/*= false*/) {
559 int start
= 0; // lower bound of the api array block to search
560 int end
= len
- 1; // upper bound of the api array block to search
561 int pivot
; // index of api array element just being compared
562 int cond
; // comparison result (in the sense of strcmp() result)
563 const char *word
; // api array element just being compared
569 SortWordList(words
, wordsNoCase
, len
);
572 while (start
<= end
) { // binary searching loop
573 pivot
= (start
+ end
) >> 1;
574 word
= wordsNoCase
[pivot
];
575 cond
= CompareNCaseInsensitive(wordStart
, word
, searchLen
);
576 if (!cond
&& nonFuncChar(word
[searchLen
])) // maybe there should be a "non-word character" test here?
577 return word
; // result must not be freed with free()
583 } else { // preserve the letter case
584 while (start
<= end
) { // binary searching loop
585 pivot
= (start
+ end
) >> 1;
587 cond
= strncmp(wordStart
, word
, searchLen
);
588 if (!cond
&& nonFuncChar(word
[searchLen
])) // maybe there should be a "non-word character" test here?
589 return word
; // result must not be freed with free()
600 * Find the length of a 'word' which is actually an identifier in a string
601 * which looks like "identifier(..." or "identifier:" or "identifier" and where
602 * there may be extra spaces after the identifier that should not be
603 * counted in the length.
605 static unsigned int LengthWord(const char *word
, char otherSeparator
) {
606 // Find a '(', or ':'. If that fails go to the end of the string.
607 const char *endWord
= strchr(word
, '(');
609 endWord
= strchr(word
, ':');
610 if (!endWord
&& otherSeparator
)
611 endWord
= strchr(word
, otherSeparator
);
613 endWord
= word
+ strlen(word
);
614 // Last case always succeeds so endWord != 0
616 // Drop any space characters.
617 if (endWord
> word
) {
618 endWord
--; // Back from the '(', ':', or '\0'
619 // Move backwards over any spaces
620 while ((endWord
> word
) && (isspace(*endWord
))) {
624 return endWord
- word
;
628 * Returns elements (first words of them) of the wordlist array which have
629 * the same beginning as the passed string.
630 * The length of the word to compare is passed too.
631 * Letter case can be ignored or preserved (default).
632 * If there are more words meeting the condition they are returned all of
633 * them in the ascending order separated with spaces.
635 * NOTE: returned buffer has to be freed with delete[].
637 char *WordList::GetNearestWords(
638 const char *wordStart
,
639 int searchLen
/*= -1*/,
640 bool ignoreCase
/*= false*/,
641 char otherSeparator
/*= '\0'*/) {
642 int wordlen
; // length of the word part (before the '(' brace) of the api array element
644 wordsNear
.setsizegrowth(1000);
645 int start
= 0; // lower bound of the api array block to search
646 int end
= len
- 1; // upper bound of the api array block to search
647 int pivot
; // index of api array element just being compared
648 int cond
; // comparison result (in the sense of strcmp() result)
654 SortWordList(words
, wordsNoCase
, len
);
657 while (start
<= end
) { // Binary searching loop
658 pivot
= (start
+ end
) / 2;
659 cond
= CompareNCaseInsensitive(wordStart
, wordsNoCase
[pivot
], searchLen
);
662 while ((pivot
> start
) &&
663 (0 == CompareNCaseInsensitive(wordStart
,
664 wordsNoCase
[pivot
-1], searchLen
))) {
668 while ((pivot
<= end
) &&
669 (0 == CompareNCaseInsensitive(wordStart
,
670 wordsNoCase
[pivot
], searchLen
))) {
671 wordlen
= LengthWord(wordsNoCase
[pivot
], otherSeparator
) + 1;
672 wordsNear
.append(wordsNoCase
[pivot
], wordlen
, ' ');
675 return wordsNear
.detach();
676 } else if (cond
< 0) {
678 } else if (cond
> 0) {
682 } else { // Preserve the letter case
683 while (start
<= end
) { // Binary searching loop
684 pivot
= (start
+ end
) / 2;
685 cond
= strncmp(wordStart
, words
[pivot
], searchLen
);
688 while ((pivot
> start
) &&
689 (0 == strncmp(wordStart
,
690 words
[pivot
-1], searchLen
))) {
694 while ((pivot
<= end
) &&
695 (0 == strncmp(wordStart
,
696 words
[pivot
], searchLen
))) {
697 wordlen
= LengthWord(words
[pivot
], otherSeparator
) + 1;
698 wordsNear
.append(words
[pivot
], wordlen
, ' ');
701 return wordsNear
.detach();
702 } else if (cond
< 0) {
704 } else if (cond
> 0) {