📄 stringline.cpp
字号:
{
return FineNextPostionByKey(strline, ',');
}
CString CStringLines::Transfer(CString sStringContent)
{
int i = 0;
for(i=0; i<sStringContent.GetLength(); i++){
switch(sStringContent[i]){
case _T('\\'):
case _T('\"'):
case _T('\''):
sStringContent.Insert(i, _T('\\'));
i++;//skip it
break;
default:
break;
}
}
return '\"' + sStringContent + '\"';
}
CString CStringLines::CookDefFiled(CString macroval)
{
BOOL bInQuote;
int bInBracket;
CString cookval, defval, fieldval;
defval.Empty();
cookval.Empty();
fieldval.Empty();
bInQuote = FALSE;
for(int i = 0; i < macroval.GetLength(); i++)
{
if(i == 0)
{
if(macroval.GetAt(i) == '\"')
{
bInQuote = !bInQuote;
}
}
else
{
if((macroval.GetAt(i) == '\"') && (macroval.GetAt(i - 1) != '\\'))
{
bInQuote = !bInQuote;
}
}
if((macroval.GetAt(i) == '{') && (!bInQuote))//find defval
{
cookval = FilterFrontChar(cookval, ' ');
cookval = FilterBackChar(cookval, ' ');
if(!cookval.IsEmpty()) defval = Transfer(cookval);
cookval.Empty();
bInBracket++;
continue;
}
if((macroval.GetAt(i) == '}') && (!bInQuote))
{
cookval = FilterFrontChar(cookval, ' ');
cookval = FilterBackChar(cookval, ' ');
if(!cookval.IsEmpty()) fieldval = fieldval + ", " + Transfer(cookval);
bInBracket--;
cookval.Empty();
continue;
}
if((macroval.GetAt(i) == ',') && (!bInQuote) && bInBracket)//find field
{
cookval = FilterFrontChar(cookval, ' ');
cookval = FilterBackChar(cookval, ' ');
if(!cookval.IsEmpty()) fieldval = fieldval + ", " + Transfer(cookval);
cookval.Empty();
continue;
}
cookval = cookval + macroval.GetAt(i);
}
if(fieldval.IsEmpty()) return Transfer(macroval);
fieldval.Delete(0, 2);
return defval + " {" + fieldval + '}';
}
CString CStringLines::CookMacro(CString linestr)
{
BOOL bNoDef;
CString macro;
bNoDef = FALSE;
if(linestr.IsEmpty())
{
ASSERT(0);
}
// int pos = linestr.Find('=');
int pos = FineNextPostionByKey(linestr, '=');
if(pos == -1)
{
pos = FineNextPostionByKey(linestr, '{');
if(pos == -1) return linestr;
bNoDef = TRUE;
pos--;
}
macro = linestr.Left(pos);
linestr = linestr.Right(linestr.GetLength() - pos - 1);
linestr = FilterFrontChar(linestr, ' ');
linestr = FilterBackChar(linestr, ' ');
if(linestr.IsEmpty()) return macro;
if(!bNoDef) return macro + " = " + CookDefFiled(linestr);
else return macro + CookDefFiled(linestr);
}
LineString *CStringLines::CreateNewLine(LineString *pCdlLine, int lines, enum TagType tag, CString linestr)
{
CString macrostr;
LineString *pLine, *pNext;
pNext = GetExpressionByLine(pCdlLine, lines);
BOOL bCreateNext = FALSE;
if(tag == MACRO)
{
int pos = FineNewMacroPos(linestr);
if(pos != -1)
{
macrostr = linestr.Right(linestr.GetLength() - pos - 1);
linestr = linestr.Left(pos);
bCreateNext = TRUE;
}
linestr = FilterFrontChar(linestr, ' ');
if(linestr.IsEmpty())
{
ASSERT(0);
}
linestr = "\tmacro " + CookMacro(linestr) + ";";
}
pLine = new LineString;
pLine->lineseq = lines;
pLine->tag = tag;
pLine->plinestr = new CString(linestr);
queue_insert(&(pNext->lineque), &(pLine->lineque));
if(pLine->lineseq == 0) pCdlLine = pLine;
while(pNext && pNext != pCdlLine)
{
pNext->lineseq++;
pNext = NextLineString(pNext);
};
if(bCreateNext) pCdlLine = CreateNewLine(pCdlLine, lines + 1, tag, macrostr);
return pCdlLine;
}
int CStringLines::FindNewPosition(LineString *pCdlLine, enum TagType tag)
{
LineString *pPrev;
pPrev = pCdlLine;
while((pPrev = PrevLineString(pPrev)) != pCdlLine)
{
if(pPrev->plinestr->Find("}") != -1) return pPrev->lineseq;
};
return -1;
}
//add new express after line of tag, if exist
LineString * CStringLines::ReplaceExpressionByTag(LineString *pCdlLine, CString keybuf, enum TagType tag)
{
int lines;
CString linestr;
BOOL bDelCur, bNewLine;
LineString *pLine = NULL;
bDelCur = TRUE;
if((tag != MODULE) && (keybuf.IsEmpty())) bNewLine = FALSE;
else bNewLine = TRUE;
lines = GetLineByTag(pCdlLine, tag);
if(lines == -1)
{
lines = FindNewPosition(pCdlLine, tag);
// if(lines == -1) AfxMessageBox("FindNewPosition BUG");
bDelCur = FALSE;
};
switch(tag)
{
case EMPTY:
linestr = "";
break;
case COMMENT:
linestr = "//" + keybuf;
break;
case SCRIPTVER:
linestr = "ScriptVersion = " + keybuf + ";// version of generator of this file";
break;
case MODULE:
linestr = "module " + keybuf;
break;
case LBRACKET:
linestr = "{";
break;
case RBRACKET:
linestr = "}";
break;
case TYPE:
linestr = "\ttype = " + keybuf + ";//{non_leaf|leaf};";
break;
case VERSION:
linestr = "\tversion = " + keybuf + ";";
break;
case DESCRIPTION:
linestr = "\tdescription = " + keybuf + ";";
break;
case SIGNATURE:
linestr = "\tsignature = " + keybuf + ";";
break;
case SUBMODELS:
linestr = "\tsubmodules = {" + keybuf + "};";
break;
case CODEFILES:
linestr = "\tcodefiles = {" + keybuf + "};";
break;
case DOCFILES:
linestr = "\tdocfiles = {" + keybuf + "};";
break;
case MACRO:
linestr = keybuf;//"\tmacro MACRO = " + keybuf + ";";
break;
case RULES:
linestr = "\trules = {" + keybuf + "};";
break;
default:
break;
}
int first_line = lines;
do
{
if(bDelCur) pCdlLine = DeleteExpressionByTag(pCdlLine, lines, tag);
}
while((lines = GetLineByTag(pCdlLine, tag)) != -1);
if(bNewLine) pCdlLine = CreateNewLine(pCdlLine, first_line, tag, linestr);
return pCdlLine;
}
CString CStringLines::PaserMacroFromFile(CString pathname, BOOL bMaskDef)
{
BOOL bFineEnd;
BOOL bReadLine;
int count;
int pos, tpos, nInIf;
CString linestr;
CString macrostr, ifmacrostr;
CString macro, value;
CStdioFile stdfd(pathname, CFile::modeRead);
enum CONDITION{BEGIN, IF,IFNDEF, DEFINE, DEFCON, IFDEF, ELSE, ELIF, ENDIF, END} type;
count = 0;
nInIf = 0;
type = BEGIN;
macrostr = "";
ifmacrostr = "";
bFineEnd = FALSE;
bReadLine = TRUE;
while(1)
{
if(bReadLine)
{
if(!stdfd.ReadString(linestr)) break;
}
count++;
bReadLine = TRUE;
if(linestr.IsEmpty()) continue;
linestr = FilterFrontChar(linestr, ' ');
switch(type)
{
case BEGIN://fisrt try match ifndef or define
nInIf = 0;
pos = linestr.Find("#ifndef");
if(!pos)
{
type = IFNDEF;
bFineEnd = TRUE;
break;
}
pos = linestr.Find("#define");
if(!pos)
{
bReadLine = FALSE;
bFineEnd = FALSE;
type = DEFINE;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IF;
nInIf++;
break;
}
break;
case DEFINE:
ASSERT(!nInIf);
if(bMaskDef)
{
pos = linestr.Find("//#define");
if(!pos) linestr.Delete(0, 2);
}
pos = linestr.Find("#define");
if(!pos)//if #define, ok, generate macro
{
linestr.Replace("#define", "");
if(linestr.Find("TABLE_INFO_SPACE_SIZE") != -1)
{
linestr = linestr;
}
linestr = FilterFrontChar(linestr, ' ');
linestr = FilterBackChar(linestr, ' ');
if(linestr.IsEmpty())
{
CString str;
str.Format("%d line error!", count + 1);
// AfxMessageBox(str);
ASSERT(0);//bug
}
macro = "";
value = "";
pos = linestr.Find(' ');
tpos = linestr.Find(9);//match tab key
if(pos == -1 && tpos == -1)
{
macrostr = macrostr + ", " + linestr;
break;
}
if(pos != -1 && tpos != -1)
{
pos = (pos > tpos) ? tpos : pos;
}
else
{
pos = (pos == -1) ? tpos : pos;
}
macro = linestr.Left(pos);
linestr = linestr.Right(linestr.GetLength() - pos - 1);
linestr = FilterFrontChar(linestr, ' ');
if(!linestr.IsEmpty()) value = linestr;
macrostr = macrostr + ", " + macro + " = " + value;
break;
}
pos = linestr.Find("#ifndef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFNDEF;
nInIf++;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IF;
nInIf++;
break;
}
if(bFineEnd)
{
pos = linestr.Find("#endif");
if(!pos)
{
type = END;
break;
}
}
break;
case DEFCON:
break;
case IF:
case IFDEF:
case IFNDEF://try match #define, if ok then goto define state
if(!nInIf)
{
pos = linestr.Find("#define");
if((!pos) && (type == IFNDEF))
{
type = DEFINE;
break;
}
break;
}
ifmacrostr = ifmacrostr + linestr + '\n';
pos = linestr.Find("#endif");
if(!pos)
{
type = ENDIF;
nInIf--;
if(!nInIf) ifmacrostr += '\n';
break;
}
pos = linestr.Find("#else");
if(!pos)
{
type = ELSE;
break;
}
pos = linestr.Find("#elif");
if(!pos)
{
type = ELIF;
break;
}
pos = linestr.Find("#ifndef");
if(!pos)
{
type = IFNDEF;
nInIf++;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
type = IF;
nInIf++;
break;
}
break;
case ELSE:
case ELIF:
ifmacrostr = ifmacrostr + linestr + '\n';
pos = linestr.Find("#endif");
if(!pos)
{
type = ENDIF;
nInIf--;
if(!nInIf) ifmacrostr += '\n';
break;
}
pos = linestr.Find("#ifndef");
if(!pos)
{
type = IFNDEF;
nInIf++;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
type = IF;
nInIf++;
break;
}
break;
case ENDIF:
bReadLine = FALSE;
if(nInIf)
{
type = IF;
break;
}
type = DEFINE;
break;
case END:
bReadLine = FALSE;
bFineEnd = FALSE;
type = BEGIN;
break;
default:
ASSERT(0);
break;
}
};
if(macrostr.IsEmpty()) return "";
macrostr.Delete(0, 2);
FILE *pfd = fopen("sysconfig_ifdef.h", "wb");
fwrite((LPCSTR)ifmacrostr, 1, ifmacrostr.GetLength(), pfd);
fclose(pfd);
return macrostr;
}
CString CStringLines::GetMacroFromFile(CString pathname, BOOL bMaskDef)
{
BOOL bFineEnd;
BOOL bReadLine;
int count;
int pos, nInIf;
CString linestr;
CString macrostr, ifmacrostr;
CStdioFile stdfd(pathname, CFile::modeRead);
enum CONDITION{BEGIN, IF,IFNDEF, DEFINE, DEFCON, IFDEF, ELSE, ELIF, ENDIF, END} type;
count = 0;
nInIf = 0;
type = BEGIN;
macrostr = "";
ifmacrostr = "";
bFineEnd = FALSE;
bReadLine = TRUE;
while(1)
{
if(bReadLine)
{
if(!stdfd.ReadString(linestr)) break;
}
count++;
bReadLine = TRUE;
if(linestr.IsEmpty()) continue;
linestr = FilterFrontChar(linestr, ' ');
switch(type)
{
case BEGIN://fisrt try match ifndef or define
nInIf = 0;
pos = linestr.Find("#ifndef");
if(!pos)
{
type = IFNDEF;
bFineEnd = TRUE;
break;
}
pos = linestr.Find("#define");
if(!pos)
{
bReadLine = FALSE;
bFineEnd = FALSE;
type = DEFINE;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IF;
nInIf++;
break;
}
break;
case DEFINE:
ASSERT(!nInIf);
pos = linestr.Find("#define");
if(!pos)//if #define, ok, generate macro
{
macrostr = macrostr + linestr + "\n";
break;
}
pos = linestr.Find("#ifndef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFNDEF;
nInIf++;
break;
}
pos = linestr.Find("#ifdef");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IFDEF;
nInIf++;
break;
}
pos = linestr.Find("#if");
if(!pos)
{
ifmacrostr = ifmacrostr + linestr + '\n';
type = IF;
nInIf++;
break;
}
if(bFineEnd)
{
pos = linestr.Find("#endif");
if(!pos)
{
type = END;
break;
}
}
break;
case DEFCON:
break;
case IF:
case IFDEF:
case IFNDEF://try match #define, if ok then goto define state
if(!nInIf)
{
pos = linestr.Find("#define");
if((!pos) && (type == IFNDEF))
{
type = DEFINE;
break;
}
break;
}
ifmacrostr = ifmacrostr + linestr + '\n';
pos = linestr.Find("#endif");
if(!pos)
{
type = ENDIF;
nInIf--;
if(!nInIf) ifmacrostr += '\n';
break;
}
pos = linestr.Find("#else");
if(!pos)
{
type = ELSE;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -