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

  1. #ifndef _LINEPARSE_H_
  2. #define _LINEPARSE_H_
  3.  
  4. class LineParser {
  5.   public:
  6.  
  7.     LineParser()
  8.     {
  9.       m_nt=m_eat=0;
  10.       m_tokens=0;
  11.     }
  12.  
  13.     ~LineParser()
  14.     {
  15.       freetokens();
  16.     }
  17.  
  18.     int parse(char *line) // returns -1 on error
  19.     {
  20.       freetokens();
  21.       int n=doline(line);
  22.       if (n) return n;
  23.       if (m_nt) 
  24.       {
  25.         m_tokens=(char**)malloc(sizeof(char*)*m_nt);
  26.         n=doline(line);
  27.         if (n) 
  28.         {
  29.           freetokens();
  30.           return -1;
  31.         }
  32.       }
  33.       return 0;
  34.     }
  35.  
  36.     int getnumtokens() { return m_nt-m_eat; }
  37.  
  38.     void eattoken() { m_eat++; }
  39.  
  40.     double gettoken_float(int token, int *success=0)
  41.     {
  42.       token+=m_eat;
  43.       if (token < 0 || token >= m_nt) 
  44.       {
  45.         if (success) *success=0;
  46.         return 0.0;
  47.       }
  48.       if (success)
  49.       {
  50.         char *t=m_tokens[token];
  51.         *success=*t?1:0;
  52.         while (*t) 
  53.         {
  54.           if ((*t < '0' || *t > '9')&&*t != '.') *success=0;
  55.           t++;
  56.         }
  57.       }
  58.       return atof(m_tokens[token]);
  59.     }
  60.     int gettoken_int(int token, int *success=0) 
  61.     { 
  62.       token+=m_eat;
  63.       if (token < 0 || token >= m_nt || !m_tokens[token][0]) 
  64.       {
  65.         if (success) *success=0;
  66.         return 0;
  67.       }
  68.       char *tmp;
  69.       int l;
  70.       if (m_tokens[token][0] == '-') l=strtol(m_tokens[token],&tmp,0);
  71.       else l=(int)strtoul(m_tokens[token],&tmp,0);
  72.       if (success) *success=! (int)(*tmp);
  73.       return l;
  74.     }
  75.     char *gettoken_str(int token) 
  76.     { 
  77.       token+=m_eat;
  78.       if (token < 0 || token >= m_nt) return "";
  79.       return m_tokens[token]; 
  80.     }
  81.     int gettoken_enum(int token, char *strlist) // null seperated list
  82.     {
  83.       int x=0;
  84.       char *tt=gettoken_str(token);
  85.       if (tt && *tt) while (*strlist)
  86.       {
  87.         if (!stricmp(tt,strlist)) return x;
  88.         strlist+=strlen(strlist)+1;
  89.         x++;
  90.       }
  91.       return -1;
  92.     }
  93.   private:
  94.     void freetokens()
  95.     {
  96.       if (m_tokens)
  97.       {
  98.         int x;
  99.         for (x = 0; x < m_nt; x ++)
  100.           free(m_tokens[x]);
  101.         free(m_tokens);
  102.       }
  103.       m_tokens=0;
  104.       m_nt=0;
  105.     }
  106.  
  107.     int doline(char *line)
  108.     {
  109.       m_nt=0;
  110.       while (*line == ' ' || *line == '\t') line++;
  111.       while (*line) 
  112.       {
  113.         int lstate=0; // 1=", 2=`, 4='
  114.         if (*line == ';' || *line == '#') break;
  115.         if (*line == '\"') lstate=1;
  116.         else if (*line == '\'') lstate=2;
  117.         else if (*line == '`') lstate=4;
  118.         if (lstate) line++;
  119.         int nc=0;
  120.         while (*line)
  121.         {
  122.           if (lstate==1 && *line =='\"') break;
  123.           if (lstate==2 && *line =='\'') break;
  124.           if (lstate==4 && *line =='`') break;
  125.           if (!lstate && (*line == ' ' || *line == '\t')) break;
  126.           line++;
  127.           nc++;
  128.         }
  129.         if (m_tokens)
  130.         {
  131.           m_tokens[m_nt]=(char*)malloc(nc+1);
  132.           strncpy(m_tokens[m_nt],line-nc,nc);
  133.           m_tokens[m_nt][nc]=0;
  134.         }
  135.         m_nt++;
  136.         if (lstate)
  137.         {
  138.           if (*line) line++;
  139.           else return -2;
  140.         }
  141.         while (*line == ' ' || *line == '\t') line++;
  142.       }
  143.       return 0;
  144.     }
  145.     
  146.     int m_eat;
  147.     int m_nt;
  148.     char **m_tokens;
  149. };
  150. #endif//_LINEPARSE_H_