Source

nsis64 / Source / strlist.h

Full commit
gol...@212acab6-… 205abbe 



























































































































































































































































































































































































































































































































































































  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * strlist.h: Implementation of the StringList class.
 * 
 * This file is a part of NSIS.
 * 
 * Copyright (C) 1999-2009 Nullsoft and Contributors
 * 
 * Licensed under the zlib/libpng license (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * Licence details can be found in the file COPYING.
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.
 *
 * Unicode support and Doxygen comments by Jim Park -- 07/27/2007
 */

#ifndef _STRLIST_H_
#define _STRLIST_H_

#include "Platform.h"
#include <cstdio>
#include <fstream>
#include "tchar.h"
#include "growbuf.h"
#include "util.h"

/**
 * Implements a list of strings mapped into a straight buffer.  It is
 * virtually always O(n) to do any basic operations on this thing.  Very
 * memory efficient but very slow performance.
 */
class StringList
{
private: // don't copy instances
  StringList(const StringList&);
  void operator=(const StringList&);

public:
  StringList() {}
  ~StringList() {}

  /**
   * Adds a string to the StringList.  If the string already exists in the
   * list, then it just returns the index of the found string.
	*
   * @param str String to add.
	*
   * @param case_sensitive If 0, search for whole string case insensitively.
   * If 1, then search for whole string case sensitively.  If 2, then search
   * for just the end of the string match case sensitively.  Basically it's
   * like searching for regexp('str$') in the list of string.  If -1, then it
   * adds the string regardless of whether there is a match or not!
	*
   * @return the index to the string in TCHARs.
   */
  int add(const TCHAR *str, int case_sensitive);

  /**
   * Search the StrinList for a string.  If the string already exists in the
   * list then it returns the index of the found string.  Returns -1 on
   * failure.
   * 
   * @param str String to search for.
	*
   * @param case_sensitive If 0, search for whole string case insensitively.If
   * 1, then search for whole string case <b>sensitively</b>.  If 2, then
   * search for just the <b>end of the string match</b>, case sensitively.
   * Basically it's like searching for regexp('str$') in the list of string.
	*
	* @param idx If not NULL, the *idx is set to the cardinal number of the
	* string.  In other words, it tells you which nth string it is in the
	* list.
	*
   * @return the TCHAR index to the string (not necessarily the byte positional
	* offset).  -1 if not found.
	*/
  int find(const TCHAR *str, int case_sensitive, int *idx=NULL) const; // returns -1 if not found

  /**
	* Delete the string at the positional index.
	*
	* @param pos The number of TCHARS to count from the beginning of the buffer
	* before the start of the string.
	*/
  void delbypos(int pos);

  /**
	* Converts the string index to the positional TCHAR index.  For example,
	* it gives the answer to which TCHAR position is the beginning of the
	* nth string in the list?
	*
	* @param idx The string index.
	*/
  int idx2pos(int idx) const;

  /**
	* Get the count of the number of strings in the list.
	* @return the number of string in the list.
	*/
  int getnum() const;

  /**
	* Get the buffer straight as a const TCHAR pointer.  Very unwise to use.
	* @return m_gr.m_s cast as a TCHAR*.
	*/
  const TCHAR *get() const { return (const TCHAR*) m_gr.get(); }

  /**
	* Get the buffer size (number of TCHARs).
	* @return The buffer size (number of TCHARs).
	*/
  int getcount() const { return m_gr.getlen() / sizeof(TCHAR); }

protected:
  GrowBuf m_gr;
};

/**
 * Similar to StringList with case_sensitive=2, but stores strings as both Unicode AND ANSI (codepaged)
 */
class MLStringList : private StringList
{
private: // don't copy instances
  MLStringList(const MLStringList&);
  void operator=(const MLStringList&);

public:
  MLStringList();
  ~MLStringList() {}

  int add(const TCHAR *str, WORD codepage, bool processed, bool build_unicode);
  int getnum() const                { return StringList::getnum(); }
  int getcount() const              { return StringList::getcount(); }
  const TCHAR *getTchar() const     { return (const TCHAR*) m_gr.get(); }
#ifdef _UNICODE
  const char *getAnsi() const       { return (const char*) m_grAnsi.get(); }
  int findAnsi(const char *str, int case_sensitive) const;
private:
  GrowBuf m_grAnsi;
#endif
};

/**
 * This class maintains a list of T types in a GrowBuf sorted by T.name which
 * is assumed to be a string (TCHAR*).  So it's really sort of a 
 * map<TCHAR*, X> where X is whatever else is defined in T.  But T must define
 * a TCHAR* name.
 */
template <class T>
class SortedStringList
{
  public:
	 /**
	  * Jim Park: Note that SortedStringList actually <b>owns</b> T.name.
	  * Yes, this violates all kinds of encapsulation ideas.
	  */
    virtual ~SortedStringList()
    {
      T *s=(T*) m_gr.get();
      size_t num = m_gr.getlen()/sizeof(T);

      for (size_t i=0; i<num; i++) {
        free(s[i].name);
      }
    }

	 /**
	  * This function adds a new T struct with a copy of TCHAR *name into
	  * T.name.  But adds it into a sorted position.  All calls to
	  * add must be done with the same value for case_sensitive or you
	  * can get random behavior.
	  *
	  * @param name The name which is the "key" to finding the instance of T.
	  * @param case_sensitive 1 means case sensitive, 0 insensitive.
     * @return Returns -1 when name already exists and pos if added.
	  */
    int add(const TCHAR *name, int case_sensitive=0)
    {
      T newstruct={0,};
      int pos=find(name,case_sensitive,1);
      if (pos==-1) return -1;
      newstruct.name=(TCHAR*)malloc((_tcslen(name)+1)*sizeof(TCHAR));
      if (!newstruct.name)
      {
        extern int g_display_errors;
        extern void quit();
        if (g_display_errors)
        {
          PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: GrowBuf realloc/malloc(%lu) failed.\n"),(unsigned long)((_tcslen(name)+1)*sizeof(TCHAR)));
        }
        quit();
      }
      _tcscpy(newstruct.name,name);
      m_gr.add(&newstruct,sizeof(T));

      T *s=(T*) m_gr.get();
      memmove(s+pos+1,s+pos, m_gr.getlen()-((pos+1)*sizeof(T)));
      memcpy(s+pos,&newstruct,sizeof(T));

      return pos;
    }

	 /**
     * This function does a binary search for the T in the buffer that
     * contains str as its T.name.  It then returns the position of the found
     * T, not in its byte position, but its T position (valid offset to T*).
	  *
     * @param str The string to search for in T.name.
	  *
     * @param case_sensitive If 1, do a case sensitive search, otherwise, case insensitive.
	  *
     * @param returnbestpos If 1, then the function changes behavior.  Instead
     * of looking for the string str in the Ts, it tries to find the best
     * sorted position to insert str into the buffer.
	  *
     * @return Returns -1 if not found, position if found.  If returnbestpos=1
     * returns -1 if found, best pos to insert if not found
	  */
    int find(const TCHAR *str, int case_sensitive=0, int returnbestpos=0)
    {
      T *data=(T *) m_gr.get();
      int ul= m_gr.getlen()/sizeof(T);
      int ll=0;
      int nextpos=(ul+ll)/2;

      while (ul > ll)
      {
        int res;
        if (case_sensitive)
          res=_tcscmp(str, data[nextpos].name);
        else
          res=_tcsicmp(str, data[nextpos].name);
        if (res==0) return returnbestpos ? -1 : nextpos;
        if (res<0) ul=nextpos;
        else ll=nextpos+1;
        nextpos=(ul+ll)/2;
      }

      return returnbestpos ? nextpos : -1;
    }

	 /**
	  * This function looks for str in T.name and deletes the T in the
	  * buffer.
	  *
	  * @return Returns 0 on success, 1 on failure.
	  */
    int del(const TCHAR *str, int case_sensitive=0)
    {
      int pos=find(str, case_sensitive);
      if (pos==-1) return 1;

      delbypos(pos);

      return 0;
    }

	 /**
	  * Given a T position, it deletes it from the buffer.  It will
	  * move the rest of the Ts to fill its position.
	  *
	  * @param pos The position of the target for deletion in T* offsets.
	  */
    void delbypos(int pos)
    {
      T *db=(T *) m_gr.get();
      free(db[pos].name);
      memmove(db+pos,db+pos+1, m_gr.getlen()-(pos*sizeof(T))-sizeof(T));
      m_gr.resize(m_gr.getlen()-sizeof(T));
    }

  protected:
    TinyGrowBuf m_gr;
};

#define mymin(x, y) ((x < y) ? x : y)

/**
 * This class maintains a list of T types in a GrowBuf sorted by T.name which
 * is assumed to be an index into m_strings.  So it's really sort of a 
 * map<TCHAR*, X> where X is whatever else is defined in T.  But T must define
 * a int name.
 *
 * This version does not have a delete function, hence the ND designation.
 * Also, because nothing is malloc'ed and free'd, this structure can be
 * placed in a single flat buffer.  (Of course, T itself can be holding
 * pointers to things on the heap, which this structure does not
 * disallow explicitly.)
 */
template <class T>
class SortedStringListND // no delete - can be placed in GrowBuf
{
  public:
    SortedStringListND() { }
    virtual ~SortedStringListND() { }

	 /**
	  * Adds name into the list of sorted strings.
	  *
	  * @param name String to store.
	  * @param case_sensitive Look for string case sensitively.  Default is 0.
	  * @param alwaysreturnpos Always return the position regardless of whether
	  * name was inserted into the list or not.  The default is 0.
	  *
	  * @return Returns -1 when name already exists, otherwise the T offset
	  * into which the struct was stored in m_gr.  If alwaysreturnpos
	  * is true, then it will return the byte offset regardless of whether
	  * the string was found.
	  */
    int add(const TCHAR *name, int case_sensitive=0, int alwaysreturnpos=0)
    {
      int where=0;
      T newstruct={0,};

		// Find the best position to insert.
      int pos=find(name,-1,case_sensitive,1,&where);

      if (pos==-1) return alwaysreturnpos ? where : -1;

      // Note that .name is set with the TCHAR* offset into m_strings.
      newstruct.name=m_strings.add(name,(_tcslen(name)+1)*sizeof(TCHAR))/sizeof(TCHAR);

      m_gr.add(&newstruct,sizeof(T));
      T *s=(T*) m_gr.get();
      memmove(s+pos+1, s+pos, m_gr.getlen()-((pos+1)*sizeof(T)));
      memcpy(s+pos, &newstruct, sizeof(T));

      return pos;
    }

	 /**
	  * This function looks for the string str, in T.name in the buffer m_gr.
	  * If it finds it, it returns the position found.  Otherwise, it returns
	  * -1.  
	  *
	  * This behavior changes when returnbestpos == 1.  In this case,
	  * it will do the reverse.  It will return -1 when it is found, noting
	  * that there is no good place to put this duplicate string.  If it
	  * is <b>not</b> found, it returns the position where it ought to be
	  * placed.
	  *
	  * When case_sensitive == -1 and returnbestpos == 1, then when the string
	  * is found, it returns
	  * the position of the string so that one can overwrite it.  Very strange
	  * special case behavior that I'm not sure if anyone actually uses.
	  *
	  * @param str The key string to search for.
	  *
	  * @param n_chars The number of characters to compare.  Use -1 to match
	  * the entire string.
	  *
	  * @param case_sensitive 1 = case sensitive, 0 = case insensitive,
	  * -1 is a special case where it is case sensitive and overrides the
	  * returnbestpos behavior when the string is found.
	  *
	  * @param returnbestpos If 1, then look for the best position to add the
	  * string.  If found in the list, return -1.
	  *
	  * @param where When str is found, returns the position of the string.
	  *
	  * @return The position of T where T.name == str.  If returnbestpos != 0
	  * then return the best position to add T if not found, otherwise, -1.
	  */
    int find
    (
	    const TCHAR* 	str,					/* key to search for */
		 int 				n_chars=-1,			/* if -1, test the entire string, otherwise just n characters */
		 int				case_sensitive=0,
		 int				returnbestpos=0,	/* if not found, return best pos */
		 int*				where=0				/* */
    )
    {
      T *data=(T *) m_gr.get();
      int ul = m_gr.getlen()/sizeof(T);
      int ll = 0;
      int nextpos = (ul+ll)/2;

		// Do binary search on m_gr which is sorted. m_strings is NOT sorted.
      while (ul > ll)
      {
        int res;
        const TCHAR *pCurr = (TCHAR*) m_strings.get() + data[nextpos].name;
        if (n_chars < 0)
        {
          if (case_sensitive)
            res = _tcscmp(str, pCurr);
          else
            res = _tcsicmp(str, pCurr);
        }
        else
        {
          unsigned int pCurr_len = _tcslen(pCurr);
          if (case_sensitive)
            res = _tcsncmp(str, pCurr, mymin((unsigned int) n_chars, pCurr_len));
          else
            res = _tcsnicmp(str, pCurr, mymin((unsigned int) n_chars, pCurr_len));

          // If there is a match and we are looking for a partial match and
          // n_chars is NOT the length of the current string, then the
          // comparison result is determined by the length comparison.
          if (res == 0 && n_chars != -1 && (unsigned int) n_chars != pCurr_len)
            res = n_chars - pCurr_len;
        }

        // Found!
        if (res==0)
        {
          // Return where we found it in *where.
          if (where) *where = nextpos;

          // If returnbestpos, then we should return -1, otherwise where
          // we found it.  But if (returnbestpos && case_sensitive == -1)
          // returns nextpos.
          return returnbestpos ? (case_sensitive!=-1 ? -1 : nextpos) : nextpos;
        }
        if (res<0) ul=nextpos;
        else ll=nextpos+1;
        nextpos=(ul+ll)/2;
      }

      return returnbestpos ? nextpos : -1;
    }

  protected:
    TinyGrowBuf m_gr;			// Sorted array of Ts
    GrowBuf     m_strings;		// Unsorted array of TCHAR strings
	 									//  (contains the .names)
};

/**
 * Structure stored by DefineList.
 */
struct define {
  TCHAR *name;		// key
  TCHAR *value;	// value stored
};

/**
 * DefineList is a specialized version of a SortedStringList
 * which is like a string to string mapping class.
 */
class DefineList : public SortedStringList<struct define>
{
  private: // don't copy instances
    DefineList(const DefineList&);
    void operator=(const DefineList&);

  public:
	 /* Empty default constructor */
    DefineList() {} // VC6 complains otherwise
    virtual ~DefineList();

	 /**
     * Add a name-value pair, case insensitively.
     * 
     * @param name The name of the variable or key to search by.  In a
     * std::map, it would be the .first of the pair.
     * 
     * @param value The value to store.  In a std::map, it would be the.second
     * of the pair.
     * 
     * @return Returns 0 if successful, 1 if already exists.  Errors cause
	  * general program exit with error logging.
	  */
    int add(const TCHAR *name, const TCHAR *value=_T(""));

	 /**
	  * This function returns the pointer to the .value TCHAR* that corresponds
	  * to the name key.
	  *
	  * @param name The key to search with.
	  * 
	  * @return The TCHAR* to the value portion of the define struct.  If not
	  * found, returns NULL.
	  */
    TCHAR *find(const TCHAR *name);

	 /**
	  * This function deletes the define struct corresponding to the key 'str'.
	  *
	  * @return Returns 0 on success, 1 otherwise
	  */
    int del(const TCHAR *str);

	 /**
	  * This function returns the number of define structs in the sorted array.
	  */
    int getnum();

	 /**
	  * Get the .name string of the (num)th define struct in the sorted array.
	  *
	  * @return Returns 0 if not found, otherwise the pointer to the .name.
	  */
    TCHAR *getname(int num);

	 /**
	  * Get the .value string of the (num)th define struct in the sorted array.
	  *
	  * @return Returns 0 if not found, otherwise the pointer to the .value.
	  */
    TCHAR *getvalue(int num);
};

/**
 * Storage unit for FastStringList.  Contains the byte offset into m_strings.
 */
struct string_t {
  int name;
};

/**
 * This class uses SortedStringListND to implement a "faster" storage of
 * strings.  It sort of implements a std::set<string> that allows you
 * to add to the set and check for existence of the set.
 *
 * It's only faster in the sense that memory moves now only need to occur
 * on the array of string_t structs (or pointers) rather than entire
 * strings.  A truly faster implementation would be using a hash table.
 */
class FastStringList : public SortedStringListND<struct string_t>
{
  private: // don't copy instances
    FastStringList(const FastStringList&);
    void operator=(const FastStringList&);
    
  public:
	 /* Empty constructor */
    FastStringList() {} // VC6 complains otherwise

	 /* Empty virtual destructor */
    virtual ~FastStringList() {}

	 /**
	  * Adds name to sorted array and returns the TCHAR* offset of m_strings
	  * where it is stored.
	  *
	  * @param name The string to store.
	  *
	  * @param case_sensitive Should we store this case sensitively or not?
	  * Setting case_sensitive to -1 will cause it to be case sensitive and
	  * always overwrite.  (Weird bad behavior).
	  *
	  * @return The TCHAR* offset of name in m_string as an int.
	  */
    int add(const TCHAR *name, int case_sensitive=0);

	 /**
	  * Get the buffer that contains the list of the strings in the order
	  * in which they were added.
	  *
	  * @return The pointer to m_strings as a TCHAR*.
	  */
    TCHAR *get() const;

	 /**
	  * The size of the collection of m_strings as a count of TCHARs.
	  *
	  * @return the size of m_strings as count of TCHARs.
	  */
	int getcount() const;

	 /**
	  * The number of strings stored in the sorted array.
	  *
	  * @return The number of strings stored.
	  */
    int getnum() const;
};

#endif//_STRLIST_H_