home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / strlist.h < prev    next >
C/C++ Source or Header  |  2001-05-30  |  4KB  |  173 lines

  1. #ifndef _STRLIST_H_
  2. #define _STRLIST_H_
  3.  
  4. class GrowBuf
  5. {
  6.   public:
  7.     GrowBuf() { m_alloc=m_used=0; m_s=NULL; }
  8.     ~GrowBuf() { free(m_s); }
  9.  
  10.     int add(void *data, int len) 
  11.     { 
  12.       if (len<=0) return 0;
  13.       resize(m_used+len); 
  14.       memcpy((char*)m_s+m_used-len,data,len);
  15.       return m_used-len;
  16.     }
  17.  
  18.     void resize(int newlen)
  19.     {
  20.       m_used=newlen;
  21.       if (newlen > m_alloc)
  22.       {
  23.         m_alloc = newlen + 32768;
  24.         m_s = realloc(m_s, m_alloc);
  25.         if (!m_s)
  26.         {
  27.           printf("Internal compiler error #12345: GrowBuf realloc(%d) failed.\n",m_alloc);
  28.           extern void quit(); quit();
  29.         }
  30.       }
  31.       if (!m_used && m_alloc > 65535) // only free if you resize to 0 and we're > 64k
  32.       {
  33.         m_alloc=0;
  34.         free(m_s);
  35.         m_s=NULL;
  36.       }
  37.     }
  38.  
  39.     int getlen() { return m_used; }
  40.     void *get() { return m_s; }
  41.  
  42.   private:
  43.     void *m_s;
  44.     int m_alloc;
  45.     int m_used;
  46.  
  47. };
  48.  
  49. class StringList
  50. {
  51. public:
  52.   StringList() { }
  53.   ~StringList() { }
  54.  
  55.   int add(char *str, int case_sensitive)
  56.   {
  57.     int a=find(str,case_sensitive);
  58.     if (a >= 0 && case_sensitive!=-1) return a;
  59.     return gr.add(str,strlen(str)+1);
  60.   }
  61.  
  62.   // use 2 for case sensitive end-of-string matches too
  63.   int find(char *str, int case_sensitive) // returns -1 if not found
  64.   {
  65.     char *s=(char*)gr.get();
  66.     int ml=gr.getlen();
  67.     int offs=0;
  68.     while (offs < ml)
  69.     {
  70.       if ((case_sensitive && !strcmp(s+offs,str)) ||
  71.           (!case_sensitive && !stricmp(s+offs,str)))
  72.       {
  73.         return offs;
  74.       }
  75.       if (case_sensitive==2 && 
  76.           strlen(str) < strlen(s+offs) &&  // check for end of string
  77.           !strcmp(s+offs+strlen(s+offs)-strlen(str),str))
  78.       {
  79.         return offs+strlen(s+offs)-strlen(str);
  80.       }
  81.       offs+=strlen(s+offs)+1;
  82.     }
  83.     return -1;
  84.   }
  85.  
  86.   char *get() { return (char*)gr.get(); }
  87.   int getlen() { return gr.getlen(); }
  88. private:
  89.   GrowBuf gr;
  90. };
  91.  
  92.  
  93. class MMapBuf
  94. {
  95.   public:
  96.     MMapBuf() 
  97.     { 
  98.       m_hFile = INVALID_HANDLE_VALUE;
  99.       m_hFileMap = 0;
  100.       m_mapping=NULL;
  101.       m_gb_u=0;
  102.       m_alloc=m_used=0;
  103.     }
  104.     ~MMapBuf() 
  105.     { 
  106.       if (m_mapping) UnmapViewOfFile(m_mapping);
  107.       if (m_hFileMap) CloseHandle(m_hFileMap);
  108.       if (m_hFile != INVALID_HANDLE_VALUE) CloseHandle(m_hFile);
  109.     }
  110.  
  111.     int add(void *data, int len) 
  112.     { 
  113.       if (len<=0) return 0;
  114.       resize(getlen()+len); 
  115.       memcpy((char*)get()+getlen()-len,data,len);
  116.       return getlen()-len;
  117.     }
  118.  
  119.     void resize(int newlen)
  120.     {
  121.       if (!m_gb_u && newlen < (16<<20)) // still in db mode
  122.       {
  123.         m_gb.resize(newlen);
  124.         return;
  125.       }
  126.       m_gb_u=1;
  127.       m_used=newlen;
  128.       if (newlen > m_alloc)
  129.       {
  130.         if (m_mapping) UnmapViewOfFile(m_mapping);
  131.         if (m_hFileMap) CloseHandle(m_hFileMap);
  132.         m_hFileMap=0;
  133.         m_mapping=NULL;
  134.         m_alloc = newlen + (16<<20); // add 16mb to top of mapping
  135.         if (m_hFile == INVALID_HANDLE_VALUE)
  136.         {
  137.           char buf[MAX_PATH],buf2[MAX_PATH];
  138.           GetTempPath(MAX_PATH,buf);
  139.           GetTempFileName(buf,"nsd",0,buf2);
  140.           m_hFile=CreateFile(buf2,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_DELETE_ON_CLOSE,NULL);
  141.         }
  142.         if (m_hFile != INVALID_HANDLE_VALUE)
  143.           m_hFileMap=CreateFileMapping(m_hFile,NULL,PAGE_READWRITE,0,m_alloc,NULL);
  144.         if (m_hFileMap) 
  145.           m_mapping=MapViewOfFile(m_hFileMap,FILE_MAP_WRITE,0,0,m_alloc);
  146.         if (!m_mapping)
  147.         {
  148.           printf("Internal compiler error #12345: error mmapping datablock to %d.\n",m_alloc);
  149.           extern void quit(); quit();
  150.         }
  151.         if (m_gb.getlen())
  152.         {
  153.           memcpy(m_mapping,m_gb.get(),m_gb.getlen());
  154.           m_gb.resize(0);
  155.         }
  156.       }
  157.     }
  158.  
  159.     int getlen() { if (m_gb_u) return m_used; return m_gb.getlen(); }
  160.     void *get() { if (m_gb_u) return m_mapping; return m_gb.get(); }
  161.  
  162.   private:
  163.     GrowBuf m_gb;
  164.     int m_gb_u;
  165.  
  166.     HANDLE m_hFile, m_hFileMap;
  167.     void *m_mapping;
  168.     int m_alloc, m_used;
  169. };
  170.  
  171.  
  172. #endif//_STRLIST_H_
  173.