Debian Codegraph @

Source/strlist.h

raw
1 /*
2 * strlist.h: Implementation of the StringList class.
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support and Doxygen comments by Jim Park -- 07/27/2007
17 */
18
19 #ifndef _STRLIST_H_
20 #define _STRLIST_H_
21
22 #include "Platform.h"
23 #include <cstdio>
24 #include <fstream>
25 #include "tchar.h"
26 #include "growbuf.h"
27 #include "util.h"
28
29 /**
30 * Implements a list of strings mapped into a straight buffer. It is
31 * virtually always O(n) to do any basic operations on this thing. Very
32 * memory efficient but very slow performance.
33 */
34 class StringList
35 {
36 private: // don't copy instances
37 StringList(const StringList&);
38 void operator=(const StringList&);
39
40 public:
41 StringList() {}
42 ~StringList() {}
43
44 /**
45 * Adds a string to the StringList. If the string already exists in the
46 * list, then it just returns the index of the found string.
47 *
48 * @param str String to add.
49 *
50 * @param case_sensitive If 0, search for whole string case insensitively.
51 * If 1, then search for whole string case sensitively. If 2, then search
52 * for just the end of the string match case sensitively. Basically it's
53 * like searching for regexp('str$') in the list of string. If -1, then it
54 * adds the string regardless of whether there is a match or not!
55 *
56 * @return the index to the string in TCHARs.
57 */
58 int add(const TCHAR *str, int case_sensitive);
59
60 /**
61 * Search the StringList for a string. If the string already exists in the
62 * list then it returns the index of the found string. Returns -1 on
63 * failure.
64 *
65 * @param str String to search for.
66 *
67 * @param case_sensitive If 0, search for whole string case insensitively.If
68 * 1, then search for whole string case <b>sensitively</b>. If 2, then
69 * search for just the <b>end of the string match</b>, case sensitively.
70 * Basically it's like searching for regexp('str$') in the list of string.
71 *
72 * @param idx If not NULL, the *idx is set to the cardinal number of the
73 * string. In other words, it tells you which nth string it is in the
74 * list.
75 *
76 * @return the TCHAR index to the string (not necessarily the byte positional
77 * offset). -1 if not found.
78 */
79 int find(const TCHAR *str, int case_sensitive, int *idx=NULL) const; // returns -1 if not found
80
81 /**
82 * Delete the string at the positional index.
83 *
84 * @param pos The number of TCHARS to count from the beginning of the buffer
85 * before the start of the string.
86 */
87 void delbypos(int pos);
88 void deleteall() { m_gr.resize(0); }
89
90 /**
91 * Converts the string index to the positional TCHAR index. For example,
92 * it gives the answer to which TCHAR position is the beginning of the
93 * nth string in the list?
94 *
95 * @param idx The string index.
96 */
97 int idx2pos(int idx) const;
98
99 /**
100 * Get the count of the number of strings in the list.
101 * @return the number of string in the list.
102 */
103 int getnum() const;
104
105 /**
106 * Get the buffer straight as a const TCHAR pointer. Very unwise to use.
107 * @return m_gr.m_s cast as a TCHAR*.
108 */
109 const TCHAR *get() const { return (const TCHAR*) m_gr.get(); }
110
111 /**
112 * Get the buffer size (number of TCHARs).
113 * @return The buffer size (number of TCHARs).
114 */
115 int getcount() const { return m_gr.getlen() / sizeof(TCHAR); }
116
117 protected:
118 GrowBuf m_gr;
119 };
120
121
122 #include "tstring.h"
123 /**
124 * Stores a list of strings as UTF16LE or raw bytes (multibyte char*).
125 */
126 class ExeHeadStringList
127 {
128 private: // don't copy instances
129 ExeHeadStringList(const ExeHeadStringList&);
130 void operator=(const ExeHeadStringList&);
131
132 public:
133 ExeHeadStringList()
134 {
135 m_wide = false;
136 m_gr.set_zeroing(true);
137 }
138
139 void setunicode(bool unicode) { m_wide = unicode; }
140 bool addemptystring() { return true; } // Added by add() when the first real string is added
141 int add(const TCHAR *str, WORD codepage, bool processed);
142 unsigned int find(const TCHAR *str, WORD codepage, bool processed, char**ppBufMB) const;
143 bool get(unsigned int offset, tstring&str) const;
144 unsigned int getnum() const;
145 unsigned int gettotalsize() const { return m_gr.get() ? m_gr.getlen() : (m_wide ? 2 : 1); }
146 const void* getstorageptr() const { return m_gr.get() ? m_gr.get() : (void*)L""; }
147
148 protected:
149 unsigned int find(const void *str, unsigned int cchF, WORD codepage, bool processed, char**ppBufMB) const;
150
151 GrowBuf m_gr;
152 bool m_wide; // Are we storing wide or narrow strings
153 enum {WIDEDIV=2}; // ExeHead expects us to provide offsets this way, also helps UTF16 offsets for shell constants to fit in < 0xFF
154 };
155
156
157 /**
158 * This class maintains a list of T types in a GrowBuf sorted by T.name which
159 * is assumed to be a string (TCHAR*). So it's really sort of a
160 * map<TCHAR*, X> where X is whatever else is defined in T. But T must define
161 * a TCHAR* name.
162 */
163 template <class T>
164 class SortedStringList
165 {
166 public:
167 /**
168 * Jim Park: Note that SortedStringList actually <b>owns</b> T.name.
169 * Yes, this violates all kinds of encapsulation ideas.
170 */
171 virtual ~SortedStringList()
172 {
173 T *s=(T*) m_gr.get();
174 size_t num = m_gr.getlen()/sizeof(T);
175
176 for (size_t i=0; i<num; i++) {
177 free(s[i].name);
178 }
179 }
180
181 /**
182 * This function adds a new T struct with a copy of TCHAR *name into
183 * T.name. But adds it into a sorted position. All calls to
184 * add must be done with the same value for case_sensitive or you
185 * can get random behavior.
186 *
187 * @param name The name which is the "key" to finding the instance of T.
188 * @param case_sensitive 1 means case sensitive, 0 insensitive.
189 * @return Returns -1 when name already exists and pos if added.
190 */
191 int add(const TCHAR *name, int case_sensitive=0)
192 {
193 T newstruct={0,};
194 int pos=find(name,case_sensitive,1);
195 if (pos==-1) return -1;
196 const unsigned long cbName=(unsigned long) (_tcslen(name)+1)*sizeof(TCHAR);
197 newstruct.name=(TCHAR*)malloc(cbName);
198 if (!newstruct.name)
199 {
200 extern int g_display_errors;
201 extern void quit();
202 if (g_display_errors)
203 {
204 PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: SortedStringList malloc(%lu) failed.\n"),(unsigned long)cbName);
205 }
206 quit();
207 }
208 _tcscpy(newstruct.name,name);
209 m_gr.add(&newstruct,sizeof(T));
210
211 T *s=(T*) m_gr.get();
212 memmove(s+pos+1,s+pos, m_gr.getlen()-((pos+1)*sizeof(T)));
213 memcpy(s+pos,&newstruct,sizeof(T));
214
215 return pos;
216 }
217
218 /**
219 * This function does a binary search for the T in the buffer that
220 * contains str as its T.name. It then returns the position of the found
221 * T, not in its byte position, but its T position (valid offset to T*).
222 *
223 * @param str The string to search for in T.name.
224 *
225 * @param case_sensitive If 1, do a case sensitive search, otherwise, case insensitive.
226 *
227 * @param returnbestpos If 1, then the function changes behavior. Instead
228 * of looking for the string str in the Ts, it tries to find the best
229 * sorted position to insert str into the buffer.
230 *
231 * @return Returns -1 if not found, position if found. If returnbestpos=1
232 * returns -1 if found, best pos to insert if not found
233 */
234 int find(const TCHAR *str, int case_sensitive=0, int returnbestpos=0)
235 {
236 T *data=(T *) m_gr.get();
237 int ul= m_gr.getlen()/sizeof(T);
238 int ll=0;
239 int nextpos=(ul+ll)/2;
240
241 while (ul > ll)
242 {
243 int res;
244 if (case_sensitive)
245 res=_tcscmp(str, data[nextpos].name);
246 else
247 res=_tcsicmp(str, data[nextpos].name);
248 if (res==0) return returnbestpos ? -1 : nextpos;
249 if (res<0) ul=nextpos;
250 else ll=nextpos+1;
251 nextpos=(ul+ll)/2;
252 }
253
254 return returnbestpos ? nextpos : -1;
255 }
256
257 /**
258 * This function looks for str in T.name and deletes the T in the
259 * buffer.
260 *
261 * @return Returns 0 on success, 1 on failure.
262 */
263 int del(const TCHAR *str, int case_sensitive=0)
264 {
265 int pos=find(str, case_sensitive);
266 if (pos==-1) return 1;
267
268 delbypos(pos);
269
270 return 0;
271 }
272
273 /**
274 * Given a T position, it deletes it from the buffer. It will
275 * move the rest of the Ts to fill its position.
276 *
277 * @param pos The position of the target for deletion in T* offsets.
278 */
279 void delbypos(int pos)
280 {
281 T *db=(T *) m_gr.get();
282 free(db[pos].name);
283 memmove(db+pos,db+pos+1, m_gr.getlen()-(pos*sizeof(T))-sizeof(T));
284 m_gr.resize(m_gr.getlen()-sizeof(T));
285 }
286
287 protected:
288 TinyGrowBuf m_gr;
289 };
290
291
292 /**
293 * This class maintains a list of T types in a GrowBuf sorted by T.name which
294 * is assumed to be an index into m_strings. So it's really sort of a
295 * map<TCHAR*, X> where X is whatever else is defined in T. But T must define
296 * a int name.
297 *
298 * This version does not have a delete function, hence the ND designation.
299 * Also, because nothing is malloc'ed and free'd, this structure can be
300 * placed in a single flat buffer. (Of course, T itself can be holding
301 * pointers to things on the heap, which this structure does not
302 * disallow explicitly.)
303 */
304 template <class T>
305 class SortedStringListND // no delete - can be placed in GrowBuf
306 {
307 public:
308 SortedStringListND() { }
309 virtual ~SortedStringListND() { }
310
311 /**
312 * Adds name into the list of sorted strings.
313 *
314 * @param name String to store.
315 * @param case_sensitive Look for string case sensitively. Default is 0.
316 * @param alwaysreturnpos Always return the position regardless of whether
317 * name was inserted into the list or not. The default is 0.
318 *
319 * @return Returns -1 when name already exists, otherwise the T offset
320 * into which the struct was stored in m_gr. If alwaysreturnpos
321 * is true, then it will return the byte offset regardless of whether
322 * the string was found.
323 */
324 int add(const TCHAR *name, int case_sensitive=0, int alwaysreturnpos=0)
325 {
326 int where=0;
327 T newstruct={0,};
328
329 // Find the best position to insert.
330 int pos=find(name,-1,case_sensitive,1,&where);
331
332 if (pos==-1) return alwaysreturnpos ? where : -1;
333
334 // Note that .name is set with the TCHAR* offset into m_strings.
335 newstruct.name=m_strings.add(name,((unsigned int)_tcslen(name)+1)*sizeof(TCHAR))/sizeof(TCHAR);
336
337 m_gr.add(&newstruct,sizeof(T));
338 T *s=(T*) m_gr.get();
339 memmove(s+pos+1, s+pos, m_gr.getlen()-((pos+1)*sizeof(T)));
340 memcpy(s+pos, &newstruct, sizeof(T));
341
342 return pos;
343 }
344
345 /**
346 * This function looks for the string str, in T.name in the buffer m_gr.
347 * If it finds it, it returns the position found. Otherwise, it returns
348 * -1.
349 *
350 * This behavior changes when returnbestpos == 1. In this case,
351 * it will do the reverse. It will return -1 when it is found, noting
352 * that there is no good place to put this duplicate string. If it
353 * is <b>not</b> found, it returns the position where it ought to be
354 * placed.
355 *
356 * When case_sensitive == -1 and returnbestpos == 1, then when the string
357 * is found, it returns
358 * the position of the string so that one can overwrite it. Very strange
359 * special case behavior that I'm not sure if anyone actually uses.
360 *
361 * @param str The key string to search for.
362 *
363 * @param n_chars The number of characters to compare. Use -1 to match
364 * the entire string.
365 *
366 * @param case_sensitive 1 = case sensitive, 0 = case insensitive,
367 * -1 is a special case where it is case sensitive and overrides the
368 * returnbestpos behavior when the string is found.
369 *
370 * @param returnbestpos If 1, then look for the best position to add the
371 * string. If found in the list, return -1.
372 *
373 * @param where When str is found, returns the position of the string.
374 *
375 * @return The position of T where T.name == str. If returnbestpos != 0
376 * then return the best position to add T if not found, otherwise, -1.
377 */
378 int find
379 (
380 const TCHAR* str, /* key to search for */
381 int n_chars=-1, /* if -1, test the entire string, otherwise just n characters */
382 int case_sensitive=0,
383 int returnbestpos=0, /* if not found, return best pos */
384 int*where=0
385 ) const
386 {
387 T *data=(T *) m_gr.get();
388 int ul = m_gr.getlen()/sizeof(T);
389 int ll = 0;
390 int nextpos = (ul+ll)/2;
391
392 // Do binary search on m_gr which is sorted. m_strings is NOT sorted.
393 while (ul > ll)
394 {
395 int res;
396 const TCHAR *pCurr = (TCHAR*) m_strings.get() + data[nextpos].name;
397 if (n_chars < 0)
398 {
399 if (case_sensitive)
400 res = _tcscmp(str, pCurr);
401 else
402 res = _tcsicmp(str, pCurr);
403 }
404 else
405 {
406 unsigned int pCurr_len = (unsigned int) _tcslen(pCurr);
407 if (case_sensitive)
408 res = _tcsncmp(str, pCurr, STD_MIN((unsigned int) n_chars, pCurr_len));
409 else
410 res = _tcsnicmp(str, pCurr, STD_MIN((unsigned int) n_chars, pCurr_len));
411
412 // If there is a match and we are looking for a partial match and
413 // n_chars is NOT the length of the current string, then the
414 // comparison result is determined by the length comparison.
415 if (res == 0 && n_chars != -1 && (unsigned int) n_chars != pCurr_len)
416 res = n_chars - pCurr_len;
417 }
418
419 // Found!
420 if (res==0)
421 {
422 // Return where we found it in *where.
423 if (where) *where = nextpos;
424
425 // If returnbestpos, then we should return -1, otherwise where
426 // we found it. But if (returnbestpos && case_sensitive == -1)
427 // returns nextpos.
428 return returnbestpos ? (case_sensitive!=-1 ? -1 : nextpos) : nextpos;
429 }
430 if (res<0) ul=nextpos;
431 else ll=nextpos+1;
432 nextpos=(ul+ll)/2;
433 }
434
435 return returnbestpos ? nextpos : -1;
436 }
437
438 protected:
439 TinyGrowBuf m_gr; // Sorted array of Ts
440 GrowBuf m_strings; // Unsorted array of TCHAR strings (contains the .names)
441 };
442
443 /**
444 * Structure stored by DefineList.
445 */
446 struct define {
447 TCHAR *name; // key
448 TCHAR *value; // value stored
449 };
450
451 /**
452 * DefineList is a specialized version of a SortedStringList
453 * which is like a string to string mapping class.
454 */
455 class DefineList : public SortedStringList<struct define>
456 {
457 private: // don't copy instances
458 DefineList(const DefineList&);
459 void operator=(const DefineList&);
460
461 public:
462 /* Empty default constructor */
463 DefineList() {} // VC6 complains otherwise
464 virtual ~DefineList();
465
466 /**
467 * Add a name-value pair, case insensitively.
468 *
469 * @param name The name of the variable or key to search by. In a
470 * std::map, it would be the .first of the pair.
471 *
472 * @param value The value to store. In a std::map, it would be the.second
473 * of the pair.
474 *
475 * @return Returns 0 if successful, 1 if already exists. Errors cause
476 * general program exit with error logging.
477 */
478 int add(const TCHAR *name, const TCHAR *value=_T(""));
479 int addn(const TCHAR *name, size_t maxvallen, const TCHAR *value); // maxvallen does not include \0
480 int set(const TCHAR *name, const TCHAR *value=_T(""));
481 int set_si32(const TCHAR *name, long value);
482 int set_ui32(const TCHAR *name, unsigned long value);
483
484 /**
485 * This function returns the pointer to the .value TCHAR* that corresponds
486 * to the name key.
487 *
488 * @param name The key to search with.
489 *
490 * @return The TCHAR* to the value portion of the define struct. If not
491 * found, returns NULL.
492 */
493 TCHAR *find(const TCHAR *name);
494
495 /**
496 * This function deletes the define struct corresponding to the key 'str'.
497 *
498 * @return Returns 0 on success, 1 otherwise
499 */
500 int del(const TCHAR *str);
501
502 /**
503 * This function returns the number of define structs in the sorted array.
504 */
505 int getnum();
506
507 /**
508 * Get the .name string of the (num)th define struct in the sorted array.
509 *
510 * @return Returns 0 if not found, otherwise the pointer to the .name.
511 */
512 TCHAR *getname(int num);
513
514 /**
515 * Get the .value string of the (num)th define struct in the sorted array.
516 *
517 * @return Returns 0 if not found, otherwise the pointer to the .value.
518 */
519 TCHAR *getvalue(int num);
520 };
521
522 /**
523 * Storage unit for FastStringList. Contains the byte offset into m_strings.
524 */
525 struct string_t {
526 int name;
527 };
528
529 /**
530 * This class uses SortedStringListND to implement a "faster" storage of
531 * strings. It sort of implements a std::set<string> that allows you
532 * to add to the set and check for existence of the set.
533 *
534 * It's only faster in the sense that memory moves now only need to occur
535 * on the array of string_t structs (or pointers) rather than entire
536 * strings. A truly faster implementation would be using a hash table.
537 */
538 class FastStringList : public SortedStringListND<struct string_t>
539 {
540 private: // don't copy instances
541 FastStringList(const FastStringList&);
542 void operator=(const FastStringList&);
543
544 public:
545 /* Empty constructor */
546 FastStringList() {} // VC6 complains otherwise
547
548 /* Empty virtual destructor */
549 virtual ~FastStringList() {}
550
551 /**
552 * Adds name to sorted array and returns the TCHAR* offset of m_strings
553 * where it is stored.
554 *
555 * @param name The string to store.
556 *
557 * @param case_sensitive Should we store this case sensitively or not?
558 * Setting case_sensitive to -1 will cause it to be case sensitive and
559 * always overwrite. (Weird bad behavior).
560 *
561 * @return The TCHAR* offset of name in m_string as an int.
562 */
563 int add(const TCHAR *name, int case_sensitive=0);
564
565 /**
566 * Get the buffer that contains the list of the strings in the order
567 * in which they were added.
568 *
569 * @return The pointer to m_strings as a TCHAR*.
570 */
571 TCHAR *get() const;
572
573 /**
574 * The size of the collection of m_strings as a count of TCHARs.
575 *
576 * @return the size of m_strings as count of TCHARs.
577 */
578 int getcount() const;
579
580 /**
581 * The number of strings stored in the sorted array.
582 *
583 * @return The number of strings stored.
584 */
585 int getnum() const;
586 };
587
588 #endif//_STRLIST_H_