moved implementation of LineParser into lineparse.cpp
git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@3706 212acab6-be3b-0410-9dea-997c60f758d6
This commit is contained in:
parent
3ed6d7ea22
commit
a0e67e3e8a
4 changed files with 220 additions and 186 deletions
|
@ -5,8 +5,8 @@
|
|||
#
|
||||
|
||||
# -- Objects and source files --
|
||||
SRCS = zlib/deflate.c zlib/trees.c bzip2/blocksort.c bzip2/bzlib.c bzip2/compress.c bzip2/huffman.c 7zip/7zGuids.cpp 7zip/Common/CRC.cpp 7zip/7zip/Compress/LZ/LZInWindow.cpp 7zip/7zip/Compress/LZMA/LZMAEncoder.cpp 7zip/7zip/Common/OutBuffer.cpp 7zip/7zip/Compress/RangeCoder/RangeCoderBit.cpp 7zip/Common/Alloc.cpp build.cpp crc32.c DialogTemplate.cpp exedata.cpp lang.cpp makenssi.cpp Plugins.cpp ResourceEditor.cpp ResourceVersionInfo.cpp script.cpp tokens.cpp util.cpp strlist.cpp growbuf.cpp mmap.cpp clzma.cpp
|
||||
OBJS = 7zGuids.o blocksort.o build.o bzlib.o compress.o CRC.o crc32.o deflate.o DialogTemplate.o exedata.o huffman.o lang.o LZInWindow.o LZMAEncoder.o Alloc.o makenssi.o OutBuffer.o Plugins.o RangeCoderBit.o ResourceEditor.o ResourceVersionInfo.o script.o tokens.o trees.o util.o strlist.o growbuf.o mmap.o clzma.o
|
||||
SRCS = zlib/deflate.c zlib/trees.c bzip2/blocksort.c bzip2/bzlib.c bzip2/compress.c bzip2/huffman.c 7zip/7zGuids.cpp 7zip/Common/CRC.cpp 7zip/7zip/Compress/LZ/LZInWindow.cpp 7zip/7zip/Compress/LZMA/LZMAEncoder.cpp 7zip/7zip/Common/OutBuffer.cpp 7zip/7zip/Compress/RangeCoder/RangeCoderBit.cpp 7zip/Common/Alloc.cpp build.cpp crc32.c DialogTemplate.cpp exedata.cpp lang.cpp makenssi.cpp Plugins.cpp ResourceEditor.cpp ResourceVersionInfo.cpp script.cpp tokens.cpp util.cpp strlist.cpp growbuf.cpp mmap.cpp clzma.cpp lineparse.cpp
|
||||
OBJS = 7zGuids.o blocksort.o build.o bzlib.o compress.o CRC.o crc32.o deflate.o DialogTemplate.o exedata.o huffman.o lang.o LZInWindow.o LZMAEncoder.o Alloc.o makenssi.o OutBuffer.o Plugins.o RangeCoderBit.o ResourceEditor.o ResourceVersionInfo.o script.o tokens.o trees.o util.o strlist.o growbuf.o mmap.o clzma.o lineparse.o
|
||||
ifeq "$(strip $(findstring i386pe,$(shell ld -V)))" ""
|
||||
LIBS = -lstdc++ -lpthread
|
||||
EXESUFF =
|
||||
|
|
201
Source/lineparse.cpp
Normal file
201
Source/lineparse.cpp
Normal file
|
@ -0,0 +1,201 @@
|
|||
#include "lineparse.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
LineParser::LineParser(bool bCommentBlock)
|
||||
{
|
||||
m_bCommentBlock=bCommentBlock;
|
||||
m_nt=m_eat=0;
|
||||
m_tokens=0;
|
||||
}
|
||||
|
||||
LineParser::~LineParser()
|
||||
{
|
||||
freetokens();
|
||||
}
|
||||
|
||||
bool LineParser::InCommentBlock()
|
||||
{
|
||||
return m_bCommentBlock;
|
||||
}
|
||||
|
||||
int LineParser::parse(char *line, int ignore_escaping/*=0*/) // returns -1 on error
|
||||
{
|
||||
freetokens();
|
||||
bool bPrevCB=m_bCommentBlock;
|
||||
int n=doline(line, ignore_escaping);
|
||||
if (n) return n;
|
||||
if (m_nt)
|
||||
{
|
||||
m_bCommentBlock=bPrevCB;
|
||||
m_tokens=(char**)malloc(sizeof(char*)*m_nt);
|
||||
n=doline(line, ignore_escaping);
|
||||
if (n)
|
||||
{
|
||||
freetokens();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LineParser::getnumtokens()
|
||||
{
|
||||
return m_nt-m_eat;
|
||||
}
|
||||
|
||||
void LineParser::eattoken()
|
||||
{
|
||||
m_eat++;
|
||||
}
|
||||
|
||||
double LineParser::gettoken_float(int token, int *success/*=0*/)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt)
|
||||
{
|
||||
if (success) *success=0;
|
||||
return 0.0;
|
||||
}
|
||||
if (success)
|
||||
{
|
||||
char *t=m_tokens[token];
|
||||
*success=*t?1:0;
|
||||
while (*t)
|
||||
{
|
||||
if ((*t < '0' || *t > '9')&&*t != '.') *success=0;
|
||||
t++;
|
||||
}
|
||||
}
|
||||
return atof(m_tokens[token]);
|
||||
}
|
||||
|
||||
int LineParser::gettoken_int(int token, int *success/*=0*/)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt || !m_tokens[token][0])
|
||||
{
|
||||
if (success) *success=0;
|
||||
return 0;
|
||||
}
|
||||
char *tmp;
|
||||
int l;
|
||||
if (m_tokens[token][0] == '-') l=strtol(m_tokens[token],&tmp,0);
|
||||
else l=(int)strtoul(m_tokens[token],&tmp,0);
|
||||
if (success) *success=! (int)(*tmp);
|
||||
return l;
|
||||
}
|
||||
|
||||
char* LineParser::gettoken_str(int token)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt) return "";
|
||||
return m_tokens[token];
|
||||
}
|
||||
|
||||
int LineParser::gettoken_enum(int token, const char *strlist) // null seperated list
|
||||
{
|
||||
int x=0;
|
||||
char *tt=gettoken_str(token);
|
||||
if (tt && *tt) while (*strlist)
|
||||
{
|
||||
if (!stricmp(tt,strlist)) return x;
|
||||
strlist+=strlen(strlist)+1;
|
||||
x++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void LineParser::freetokens()
|
||||
{
|
||||
if (m_tokens)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < m_nt; x ++)
|
||||
free(m_tokens[x]);
|
||||
free(m_tokens);
|
||||
}
|
||||
m_tokens=0;
|
||||
m_nt=0;
|
||||
}
|
||||
|
||||
int LineParser::doline(char *line, int ignore_escaping/*=0*/)
|
||||
{
|
||||
m_nt=0;
|
||||
if ( m_bCommentBlock )
|
||||
{
|
||||
while ( *line )
|
||||
{
|
||||
if ( *line == '*' && *(line+1) == '/' )
|
||||
{
|
||||
m_bCommentBlock=false; // Found end of comment block
|
||||
line+=2;
|
||||
break;
|
||||
}
|
||||
line++;
|
||||
}
|
||||
}
|
||||
while (*line == ' ' || *line == '\t') line++;
|
||||
while (*line)
|
||||
{
|
||||
int lstate=0; // 1=", 2=`, 4='
|
||||
if (*line == ';' || *line == '#') break;
|
||||
if (*line == '/' && *(line+1) == '*')
|
||||
{
|
||||
m_bCommentBlock = true;
|
||||
line+=2;
|
||||
return doline(line, ignore_escaping);
|
||||
}
|
||||
if (*line == '\"') lstate=1;
|
||||
else if (*line == '\'') lstate=2;
|
||||
else if (*line == '`') lstate=4;
|
||||
if (lstate) line++;
|
||||
int nc=0;
|
||||
char *p = line;
|
||||
while (*line)
|
||||
{
|
||||
if (line[0] == '$' && line[1] == '\\') {
|
||||
switch (line[2]) {
|
||||
case '"':
|
||||
case '\'':
|
||||
case '`':
|
||||
nc += ignore_escaping ? 3 : 1;
|
||||
line += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (lstate==1 && *line =='\"') break;
|
||||
if (lstate==2 && *line =='\'') break;
|
||||
if (lstate==4 && *line =='`') break;
|
||||
if (!lstate && (*line == ' ' || *line == '\t')) break;
|
||||
line++;
|
||||
nc++;
|
||||
}
|
||||
if (m_tokens)
|
||||
{
|
||||
int i;
|
||||
m_tokens[m_nt]=(char*)malloc(nc+1);
|
||||
for (i = 0; p < line; i++, p++) {
|
||||
if (!ignore_escaping && p[0] == '$' && p[1] == '\\') {
|
||||
switch (p[2]) {
|
||||
case '"':
|
||||
case '\'':
|
||||
case '`':
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
m_tokens[m_nt][i] = *p;
|
||||
}
|
||||
m_tokens[m_nt][nc]=0;
|
||||
}
|
||||
m_nt++;
|
||||
if (lstate)
|
||||
{
|
||||
if (*line) line++;
|
||||
else return -2;
|
||||
}
|
||||
while (*line == ' ' || *line == '\t') line++;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -4,194 +4,23 @@
|
|||
class LineParser {
|
||||
public:
|
||||
|
||||
LineParser(bool bCommentBlock)
|
||||
{
|
||||
m_bCommentBlock=bCommentBlock;
|
||||
m_nt=m_eat=0;
|
||||
m_tokens=0;
|
||||
}
|
||||
|
||||
~LineParser()
|
||||
{
|
||||
freetokens();
|
||||
}
|
||||
|
||||
bool InCommentBlock()
|
||||
{
|
||||
return m_bCommentBlock;
|
||||
}
|
||||
|
||||
int parse(char *line, int ignore_escaping=0) // returns -1 on error
|
||||
{
|
||||
freetokens();
|
||||
bool bPrevCB=m_bCommentBlock;
|
||||
int n=doline(line, ignore_escaping);
|
||||
if (n) return n;
|
||||
if (m_nt)
|
||||
{
|
||||
m_bCommentBlock=bPrevCB;
|
||||
m_tokens=(char**)malloc(sizeof(char*)*m_nt);
|
||||
n=doline(line, ignore_escaping);
|
||||
if (n)
|
||||
{
|
||||
freetokens();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
LineParser(bool bCommentBlock);
|
||||
virtual ~LineParser();
|
||||
|
||||
int getnumtokens() { return m_nt-m_eat; }
|
||||
bool InCommentBlock();
|
||||
int parse(char *line, int ignore_escaping=0); // returns -1 on error
|
||||
int getnumtokens();
|
||||
void eattoken();
|
||||
double gettoken_float(int token, int *success=0);
|
||||
int gettoken_int(int token, int *success=0);
|
||||
char *gettoken_str(int token);
|
||||
int gettoken_enum(int token, const char *strlist); // null seperated list
|
||||
|
||||
void eattoken() { m_eat++; }
|
||||
|
||||
double gettoken_float(int token, int *success=0)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt)
|
||||
{
|
||||
if (success) *success=0;
|
||||
return 0.0;
|
||||
}
|
||||
if (success)
|
||||
{
|
||||
char *t=m_tokens[token];
|
||||
*success=*t?1:0;
|
||||
while (*t)
|
||||
{
|
||||
if ((*t < '0' || *t > '9')&&*t != '.') *success=0;
|
||||
t++;
|
||||
}
|
||||
}
|
||||
return atof(m_tokens[token]);
|
||||
}
|
||||
int gettoken_int(int token, int *success=0)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt || !m_tokens[token][0])
|
||||
{
|
||||
if (success) *success=0;
|
||||
return 0;
|
||||
}
|
||||
char *tmp;
|
||||
int l;
|
||||
if (m_tokens[token][0] == '-') l=strtol(m_tokens[token],&tmp,0);
|
||||
else l=(int)strtoul(m_tokens[token],&tmp,0);
|
||||
if (success) *success=! (int)(*tmp);
|
||||
return l;
|
||||
}
|
||||
char *gettoken_str(int token)
|
||||
{
|
||||
token+=m_eat;
|
||||
if (token < 0 || token >= m_nt) return "";
|
||||
return m_tokens[token];
|
||||
}
|
||||
int gettoken_enum(int token, const char *strlist) // null seperated list
|
||||
{
|
||||
int x=0;
|
||||
char *tt=gettoken_str(token);
|
||||
if (tt && *tt) while (*strlist)
|
||||
{
|
||||
if (!stricmp(tt,strlist)) return x;
|
||||
strlist+=strlen(strlist)+1;
|
||||
x++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
private:
|
||||
void freetokens()
|
||||
{
|
||||
if (m_tokens)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < m_nt; x ++)
|
||||
free(m_tokens[x]);
|
||||
free(m_tokens);
|
||||
}
|
||||
m_tokens=0;
|
||||
m_nt=0;
|
||||
}
|
||||
|
||||
int doline(char *line, int ignore_escaping=0)
|
||||
{
|
||||
m_nt=0;
|
||||
if ( m_bCommentBlock )
|
||||
{
|
||||
while ( *line )
|
||||
{
|
||||
if ( *line == '*' && *(line+1) == '/' )
|
||||
{
|
||||
m_bCommentBlock=false; // Found end of comment block
|
||||
line+=2;
|
||||
break;
|
||||
}
|
||||
line++;
|
||||
}
|
||||
}
|
||||
while (*line == ' ' || *line == '\t') line++;
|
||||
while (*line)
|
||||
{
|
||||
int lstate=0; // 1=", 2=`, 4='
|
||||
if (*line == ';' || *line == '#') break;
|
||||
if (*line == '/' && *(line+1) == '*')
|
||||
{
|
||||
m_bCommentBlock = true;
|
||||
line+=2;
|
||||
return doline(line, ignore_escaping);
|
||||
}
|
||||
if (*line == '\"') lstate=1;
|
||||
else if (*line == '\'') lstate=2;
|
||||
else if (*line == '`') lstate=4;
|
||||
if (lstate) line++;
|
||||
int nc=0;
|
||||
char *p = line;
|
||||
while (*line)
|
||||
{
|
||||
if (line[0] == '$' && line[1] == '\\') {
|
||||
switch (line[2]) {
|
||||
case '"':
|
||||
case '\'':
|
||||
case '`':
|
||||
nc += ignore_escaping ? 3 : 1;
|
||||
line += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (lstate==1 && *line =='\"') break;
|
||||
if (lstate==2 && *line =='\'') break;
|
||||
if (lstate==4 && *line =='`') break;
|
||||
if (!lstate && (*line == ' ' || *line == '\t')) break;
|
||||
line++;
|
||||
nc++;
|
||||
}
|
||||
if (m_tokens)
|
||||
{
|
||||
int i;
|
||||
m_tokens[m_nt]=(char*)malloc(nc+1);
|
||||
for (i = 0; p < line; i++, p++) {
|
||||
if (!ignore_escaping && p[0] == '$' && p[1] == '\\') {
|
||||
switch (p[2]) {
|
||||
case '"':
|
||||
case '\'':
|
||||
case '`':
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
m_tokens[m_nt][i] = *p;
|
||||
}
|
||||
m_tokens[m_nt][nc]=0;
|
||||
}
|
||||
m_nt++;
|
||||
if (lstate)
|
||||
{
|
||||
if (*line) line++;
|
||||
else return -2;
|
||||
}
|
||||
while (*line == ' ' || *line == '\t') line++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void freetokens();
|
||||
int doline(char *line, int ignore_escaping=0);
|
||||
|
||||
int m_eat;
|
||||
int m_nt;
|
||||
bool m_bCommentBlock;
|
||||
|
|
|
@ -217,6 +217,10 @@ SOURCE=.\lang.cpp
|
|||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\lineparse.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\makenssi.cpp
|
||||
# ADD CPP /G6
|
||||
# End Source File
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue