]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/PropSet.cxx
e97a7f7bef439732c831fab3106c291096b7bb70
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 static inline bool IsLetter(char ch
) {
30 return ((ch
>= 'a' && ch
<= 'z') || (ch
>= 'A' && ch
<= 'Z'));
34 int CompareCaseInsensitive(const char *a
, const char *b
) {
37 char upperA
= MakeUpperCase(*a
);
38 char upperB
= MakeUpperCase(*b
);
40 return upperA
- upperB
;
45 // Either *a or *b is nul
49 int CompareNCaseInsensitive(const char *a
, const char *b
, int len
) {
50 while (*a
&& *b
&& len
) {
52 char upperA
= MakeUpperCase(*a
);
53 char upperB
= MakeUpperCase(*b
);
55 return upperA
- upperB
;
64 // Either *a or *b is nul
69 bool EqualCaseInsensitive(const char *a
, const char *b
) {
70 return 0 == CompareCaseInsensitive(a
, b
);
73 inline unsigned int HashString(const char *s
, int len
) {
85 for (int root
= 0; root
< hashRoots
; root
++)
94 void PropSet::Set(const char *key
, const char *val
, int lenKey
, int lenVal
) {
95 if (!*key
) // Empty keys are not supported
100 lenVal
= strlen(val
);
101 unsigned int hash
= HashString(key
, lenKey
);
102 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
103 if ((hash
== p
->hash
) &&
104 ((strlen(p
->key
) == static_cast<unsigned int>(lenKey
)) &&
105 (0 == strncmp(p
->key
, key
, lenKey
)))) {
106 // Replace current value
108 p
->val
= StringDup(val
, lenVal
);
113 Property
*pNew
= new Property
;
116 pNew
->key
= StringDup(key
, lenKey
);
117 pNew
->val
= StringDup(val
, lenVal
);
118 pNew
->next
= props
[hash
% hashRoots
];
119 props
[hash
% hashRoots
] = pNew
;
123 void PropSet::Set(const char *keyVal
) {
124 while (isspace(*keyVal
))
126 const char *endVal
= keyVal
;
127 while (*endVal
&& (*endVal
!= '\n'))
129 const char *eqAt
= strchr(keyVal
, '=');
131 Set(keyVal
, eqAt
+ 1, eqAt
-keyVal
, endVal
- eqAt
- 1);
132 } else if (*keyVal
) { // No '=' so assume '=1'
133 Set(keyVal
, "1", endVal
-keyVal
, 1);
137 void PropSet::SetMultiple(const char *s
) {
138 const char *eol
= strchr(s
, '\n');
142 eol
= strchr(s
, '\n');
147 SString
PropSet::Get(const char *key
) {
148 unsigned int hash
= HashString(key
, strlen(key
));
149 for (Property
*p
= props
[hash
% hashRoots
]; p
; p
= p
->next
) {
150 if ((hash
== p
->hash
) && (0 == strcmp(p
->key
, key
))) {
155 // Failed here, so try in base property set
156 return superPS
->Get(key
);
162 static bool IncludesVar(const char *value
, const char *key
) {
163 const char *var
= strstr(value
, "$(");
165 if (isprefix(var
+ 2, key
) && (var
[2 + strlen(key
)] == ')')) {
166 // Found $(key) which would lead to an infinite loop so exit
169 var
= strstr(var
+ 2, ")");
171 var
= strstr(var
+ 1, "$(");
176 SString
PropSet::GetExpanded(const char *key
) {
177 SString val
= Get(key
);
178 if (IncludesVar(val
.c_str(), key
))
180 return Expand(val
.c_str());
183 SString
PropSet::Expand(const char *withVars
) {
184 char *base
= StringDup(withVars
);
185 char *cpvar
= strstr(base
, "$(");
187 char *cpendvar
= strchr(cpvar
, ')');
189 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
190 char *var
= StringDup(cpvar
+ 2, lenvar
);
191 SString val
= GetExpanded(var
);
192 int newlenbase
= strlen(base
) + val
.length() - lenvar
;
193 char *newbase
= new char[newlenbase
];
194 strncpy(newbase
, base
, cpvar
- base
);
195 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
196 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
201 cpvar
= strstr(base
, "$(");
208 int PropSet::GetInt(const char *key
, int defaultValue
) {
209 SString val
= GetExpanded(key
);
215 bool isprefix(const char *target
, const char *prefix
) {
216 while (*target
&& *prefix
) {
217 if (*target
!= *prefix
)
228 static bool IsSuffixCaseInsensitive(const char *target
, const char *suffix
) {
229 int lentarget
= strlen(target
);
230 int lensuffix
= strlen(suffix
);
231 if (lensuffix
> lentarget
)
233 for (int i
= lensuffix
- 1; i
>= 0; i
--) {
234 if (MakeUpperCase(target
[i
+ lentarget
- lensuffix
]) !=
235 MakeUpperCase(suffix
[i
]))
241 SString
PropSet::GetWild(const char *keybase
, const char *filename
) {
242 for (int root
= 0; root
< hashRoots
; root
++) {
243 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
244 if (isprefix(p
->key
, keybase
)) {
245 char * orgkeyfile
= p
->key
+ strlen(keybase
);
246 char *keyfile
= NULL
;
248 if (strstr(orgkeyfile
, "$(") == orgkeyfile
) {
249 char *cpendvar
= strchr(orgkeyfile
, ')');
252 SString s
= GetExpanded(orgkeyfile
+ 2);
254 keyfile
= StringDup(s
.c_str());
257 char *keyptr
= keyfile
;
260 keyfile
= orgkeyfile
;
263 char *del
= strchr(keyfile
, ';');
265 del
= keyfile
+ strlen(keyfile
);
268 if (*keyfile
== '*') {
269 if (IsSuffixCaseInsensitive(filename
, keyfile
+ 1)) {
274 } else if (0 == strcmp(keyfile
, filename
)) {
286 if (0 == strcmp(p
->key
, keybase
)) {
293 // Failed here, so try in base property set
294 return superPS
->GetWild(keybase
, filename
);
300 // GetNewExpand does not use Expand as it has to use GetWild with the filename for each
301 // variable reference found.
302 SString
PropSet::GetNewExpand(const char *keybase
, const char *filename
) {
303 char *base
= StringDup(GetWild(keybase
, filename
).c_str());
304 char *cpvar
= strstr(base
, "$(");
306 char *cpendvar
= strchr(cpvar
, ')');
308 int lenvar
= cpendvar
- cpvar
- 2; // Subtract the $()
309 char *var
= StringDup(cpvar
+ 2, lenvar
);
310 SString val
= GetWild(var
, filename
);
311 int newlenbase
= strlen(base
) + val
.length() - lenvar
;
312 char *newbase
= new char[newlenbase
];
313 strncpy(newbase
, base
, cpvar
- base
);
314 strcpy(newbase
+ (cpvar
- base
), val
.c_str());
315 strcpy(newbase
+ (cpvar
- base
) + val
.length(), cpendvar
+ 1);
320 cpvar
= strstr(base
, "$(");
327 void PropSet::Clear() {
328 for (int root
= 0; root
< hashRoots
; root
++) {
329 Property
*p
= props
[root
];
331 Property
*pNext
= p
->next
;
344 char *PropSet::ToString() {
346 for (int r
= 0; r
< hashRoots
; r
++) {
347 for (Property
*p
= props
[r
]; p
; p
= p
->next
) {
348 len
+= strlen(p
->key
) + 1;
349 len
+= strlen(p
->val
) + 1;
353 len
= 1; // Return as empty string
354 char *ret
= new char [len
];
357 for (int root
= 0; root
< hashRoots
; root
++) {
358 for (Property
*p
= props
[root
]; p
; p
= p
->next
) {
373 * Initiate enumeration.
375 bool PropSet::GetFirst(char **key
, char **val
) {
376 for (int i
= 0; i
< hashRoots
; i
++) {
377 for (Property
*p
= props
[i
]; p
; p
= p
->next
) {
381 enumnext
= p
->next
; // GetNext will begin here ...
382 enumhash
= i
; // ... in this block
391 * Continue enumeration.
393 bool PropSet::GetNext(char ** key
, char ** val
) {
394 bool firstloop
= true;
396 // search begins where we left it : in enumhash block
397 for (int i
= enumhash
; i
< hashRoots
; i
++) {
399 enumnext
= props
[i
]; // Begin with first property in block
400 // else : begin where we left
403 for (Property
*p
= enumnext
; p
; p
= p
->next
) {
407 enumnext
= p
->next
; // for GetNext
416 static bool iswordsep(char ch
, bool onlyLineEnds
) {
421 return ch
== '\r' || ch
== '\n';
425 * Creates an array that points into each word in the string and puts \0 terminators
428 static char **ArrayFromWordList(char *wordlist
, int *len
, bool onlyLineEnds
= false) {
431 for (int j
= 0; wordlist
[j
]; j
++) {
432 if (!iswordsep(wordlist
[j
], onlyLineEnds
) && iswordsep(prev
, onlyLineEnds
))
436 char **keywords
= new char * [words
+ 1];
440 int slen
= strlen(wordlist
);
441 for (int k
= 0; k
< slen
; k
++) {
442 if (!iswordsep(wordlist
[k
], onlyLineEnds
)) {
444 keywords
[words
] = &wordlist
[k
];
452 keywords
[words
] = &wordlist
[slen
];
460 void WordList::Clear() {
464 delete []wordsNoCase
;
473 void WordList::Set(const char *s
) {
476 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
477 wordsNoCase
= new char * [len
+ 1];
478 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
481 char *WordList::Allocate(int size
) {
482 list
= new char[size
+ 1];
487 void WordList::SetFromAllocated() {
489 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
490 wordsNoCase
= new char * [len
+ 1];
491 memcpy(wordsNoCase
, words
, (len
+ 1) * sizeof (*words
));
494 int cmpString(const void *a1
, const void *a2
) {
495 // Can't work out the correct incantation to use modern casts here
496 return strcmp(*(char**)(a1
), *(char**)(a2
));
499 int cmpStringNoCase(const void *a1
, const void *a2
) {
500 // Can't work out the correct incantation to use modern casts here
501 return CompareCaseInsensitive(*(char**)(a1
), *(char**)(a2
));
504 static void SortWordList(char **words
, char **wordsNoCase
, unsigned int len
) {
505 qsort(reinterpret_cast<void*>(words
), len
, sizeof(*words
),
507 qsort(reinterpret_cast<void*>(wordsNoCase
), len
, sizeof(*wordsNoCase
),
511 bool WordList::InList(const char *s
) {
516 SortWordList(words
, wordsNoCase
, len
);
517 for (unsigned int k
= 0; k
< (sizeof(starts
) / sizeof(starts
[0])); k
++)
519 for (int l
= len
- 1; l
>= 0; l
--) {
520 unsigned char indexChar
= words
[l
][0];
521 starts
[indexChar
] = l
;
524 unsigned char firstChar
= s
[0];
525 int j
= starts
[firstChar
];
527 while (words
[j
][0] == firstChar
) {
528 if (s
[1] == words
[j
][1]) {
529 const char *a
= words
[j
] + 1;
530 const char *b
= s
+ 1;
531 while (*a
&& *a
== *b
) {
543 while (words
[j
][0] == '^') {
544 const char *a
= words
[j
] + 1;
546 while (*a
&& *a
== *b
) {
559 * Returns an element (complete) of the wordlist array which has
560 * the same beginning as the passed string.
561 * The length of the word to compare is passed too.
562 * Letter case can be ignored or preserved (default).
564 const char *WordList::GetNearestWord(const char *wordStart
, int searchLen
/*= -1*/, bool ignoreCase
/*= false*/) {
565 int start
= 0; // lower bound of the api array block to search
566 int end
= len
- 1; // upper bound of the api array block to search
567 int pivot
; // index of api array element just being compared
568 int cond
; // comparison result (in the sense of strcmp() result)
569 const char *word
; // api array element just being compared
575 SortWordList(words
, wordsNoCase
, len
);
578 while (start
<= end
) { // binary searching loop
579 pivot
= (start
+ end
) >> 1;
580 word
= wordsNoCase
[pivot
];
581 cond
= CompareNCaseInsensitive(wordStart
, word
, searchLen
);
582 if (!cond
&& nonFuncChar(word
[searchLen
])) // maybe there should be a "non-word character" test here?
583 return word
; // result must not be freed with free()
589 } else { // preserve the letter case
590 while (start
<= end
) { // binary searching loop
591 pivot
= (start
+ end
) >> 1;
593 cond
= strncmp(wordStart
, word
, searchLen
);
594 if (!cond
&& nonFuncChar(word
[searchLen
])) // maybe there should be a "non-word character" test here?
595 return word
; // result must not be freed with free()
606 * Find the length of a 'word' which is actually an identifier in a string
607 * which looks like "identifier(..." or "identifier:" or "identifier" and where
608 * there may be extra spaces after the identifier that should not be
609 * counted in the length.
611 static unsigned int LengthWord(const char *word
, char otherSeparator
) {
612 // Find a '(', or ':'. If that fails go to the end of the string.
613 const char *endWord
= strchr(word
, '(');
615 endWord
= strchr(word
, ':');
616 if (!endWord
&& otherSeparator
)
617 endWord
= strchr(word
, otherSeparator
);
619 endWord
= word
+ strlen(word
);
620 // Last case always succeeds so endWord != 0
622 // Drop any space characters.
623 if (endWord
> word
) {
624 endWord
--; // Back from the '(', ':', or '\0'
625 // Move backwards over any spaces
626 while ((endWord
> word
) && (isspace(*endWord
))) {
630 return endWord
- word
;
634 * Returns elements (first words of them) of the wordlist array which have
635 * the same beginning as the passed string.
636 * The length of the word to compare is passed too.
637 * Letter case can be ignored or preserved (default).
638 * If there are more words meeting the condition they are returned all of
639 * them in the ascending order separated with spaces.
641 * NOTE: returned buffer has to be freed with delete[].
643 char *WordList::GetNearestWords(
644 const char *wordStart
,
645 int searchLen
/*= -1*/,
646 bool ignoreCase
/*= false*/,
647 char otherSeparator
/*= '\0'*/) {
648 int wordlen
; // length of the word part (before the '(' brace) of the api array element
650 wordsNear
.setsizegrowth(1000);
651 int start
= 0; // lower bound of the api array block to search
652 int end
= len
- 1; // upper bound of the api array block to search
653 int pivot
; // index of api array element just being compared
654 int cond
; // comparison result (in the sense of strcmp() result)
660 SortWordList(words
, wordsNoCase
, len
);
663 while (start
<= end
) { // Binary searching loop
664 pivot
= (start
+ end
) / 2;
665 cond
= CompareNCaseInsensitive(wordStart
, wordsNoCase
[pivot
], searchLen
);
668 while ((pivot
> start
) &&
669 (0 == CompareNCaseInsensitive(wordStart
,
670 wordsNoCase
[pivot
-1], searchLen
))) {
674 while ((pivot
<= end
) &&
675 (0 == CompareNCaseInsensitive(wordStart
,
676 wordsNoCase
[pivot
], searchLen
))) {
677 wordlen
= LengthWord(wordsNoCase
[pivot
], otherSeparator
) + 1;
678 wordsNear
.append(wordsNoCase
[pivot
], wordlen
, ' ');
681 return wordsNear
.detach();
682 } else if (cond
< 0) {
684 } else if (cond
> 0) {
688 } else { // Preserve the letter case
689 while (start
<= end
) { // Binary searching loop
690 pivot
= (start
+ end
) / 2;
691 cond
= strncmp(wordStart
, words
[pivot
], searchLen
);
694 while ((pivot
> start
) &&
695 (0 == strncmp(wordStart
,
696 words
[pivot
-1], searchLen
))) {
700 while ((pivot
<= end
) &&
701 (0 == strncmp(wordStart
,
702 words
[pivot
], searchLen
))) {
703 wordlen
= LengthWord(words
[pivot
], otherSeparator
) + 1;
704 wordsNear
.append(words
[pivot
], wordlen
, ' ');
707 return wordsNear
.detach();
708 } else if (cond
< 0) {
710 } else if (cond
> 0) {