📄 stringline.cpp
字号:
}
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 "";
return macrostr;
}
int CStringLines::GetNodeFieldCount(const char* nodesz, const char* buf)
{
int count;
int pos, tpos;
CString tempstr(buf);
count = -1;
pos = tempstr.Find(nodesz, 0);
if(-1 == pos) return count;
tempstr.Delete(0, pos + strlen(nodesz));
tempstr = FilterFrontValidChar(tempstr);
pos = tempstr.Find('{', 0);
if(pos > 1) return count;
tempstr.Delete(0, pos + 1);
tpos = tempstr.Find('}', 0);
if(-1 == tpos) return count;
tempstr.Delete(tpos, tempstr.GetLength() - tpos);
count = 0;
tempstr = FilterFrontValidChar(tempstr);
do
{
pos = tempstr.Find(';', 0);
if(pos != -1)
{
count++;
tempstr.Delete(0, pos + 1);
tempstr = FilterFrontValidChar(tempstr);
}
}
while(pos != -1);
if(!strcmp(nodesz, "/*#tp_node@*/"))
{
m_tp_field_full_count = count;
}
else if(!strcmp(nodesz, "/*#sat_node@*/"))
{
m_sat_field_full_count = count;
}
else if(!strcmp(nodesz, "/*#prog_node@*/"))
{
m_prog_field_full_count = count;
}
return count;
}
CString CStringLines::FilterFrontValidChar(CString linestr)
{
const char *pbuf;
pbuf = (LPCSTR)linestr;
while(pbuf[0] == ' '
|| pbuf[0] == '\t'
|| pbuf[0] == '\n'
|| pbuf[0] == '\r'
|| pbuf[0] == ';'
)
{
pbuf++;
if(!pbuf[0]) return "";
}
CString word(pbuf);
return word;
}
NodeField** CStringLines::ParseNodeField(const char* nodesz, const char* buf)
{
int count, index, pos;
NodeField **pNode = NULL;
NodeField *pNodeInfo = NULL;
count = GetNodeFieldCount(nodesz, buf);
if(count <= 0) return NULL;
pNode = new NodeField*[count + 1];
if(!pNode) return NULL;
memset(pNode, 0, sizeof(NodeField *) * (count + 1));
if(!strcmp(nodesz, "/*#tp_node@*/"))
{
if(m_tp_full_node) FreeNodeFieldInfo(m_tp_full_node);
m_tp_full_node = pNode;
}
else if(!strcmp(nodesz, "/*#sat_node@*/"))
{
if(m_sat_full_node) FreeNodeFieldInfo(m_sat_full_node);
m_sat_full_node = pNode;
}
else if(!strcmp(nodesz, "/*#prog_node@*/"))
{
if(m_prog_full_node) FreeNodeFieldInfo(m_prog_full_node);
m_prog_full_node = pNode;
}
else if(!strcmp(nodesz, "/*#def_prog_node@*/"))
{
if(m_def_prog_full_node) FreeNodeFieldInfo(m_def_prog_full_node);
m_def_prog_full_node = pNode;
m_def_prog_field_full_count = 0;
}
else return NULL;
CString tempstr(buf);
pos = tempstr.Find(nodesz, 0);
if(-1 == pos) return NULL;
tempstr.Delete(0, pos + strlen(nodesz));
tempstr = FilterFrontValidChar(tempstr);
pos = tempstr.Find('{', 0);
if(pos > 1) return NULL;
tempstr.Delete(0, pos + 1);
tempstr = FilterFrontValidChar(tempstr);
pos = tempstr.Find('}', 0);
if(-1 == pos) return NULL;
tempstr = tempstr.Left(pos);
index = 0;
CString linestr;
int bits_offset, bytes_offset;
bits_offset = bytes_offset = 0;
#ifdef _MY_DEBUG
fprintf(debugfd, "\r\nbegin parse include file...\r\n");
fprintf(debugfd, "\tNode type : %s\r\n", nodesz);
#endif
do
{
pos = tempstr.Find(';', 0);
if(pos != -1)
{
linestr = tempstr.Left(pos);
pNodeInfo = ParseNodeFieldLine1(linestr);
if(!pNodeInfo)
{
return NULL;
}
pNodeInfo->bytes_offset = bytes_offset;
pNodeInfo->bits_offset = bits_offset;
#ifdef _MY_DEBUG
fprintf(debugfd, "name : %24s, tag : %d, length : %2d, bits : %2d, bytes: %2d, masked : %1d\r\n",
pNodeInfo->name, pNodeInfo->tag, pNodeInfo->bits_length, pNodeInfo->bits_offset,
pNodeInfo->bytes_offset, pNodeInfo->field_mask);
#endif
bytes_offset = bytes_offset + (bits_offset + pNodeInfo->bits_length) / 32;
bits_offset = (bits_offset + pNodeInfo->bits_length) % 32;
pNode[index] = pNodeInfo;
tempstr.Delete(0, pos + 1);
tempstr = FilterFrontValidChar(tempstr);
index++;
}
}
while(pos != -1);
#ifdef _MY_DEBUG
fprintf(debugfd, "end parse include file...\r\n");
#endif
if(strcmp(nodesz, "/*#def_prog_node@*/"))
{
pNode = AdjustNodeField(pNode);
}
if(!strcmp(nodesz, "/*#tp_node@*/"))
{
if(m_tp_node) FreeNodeFieldInfo(m_tp_node);
m_tp_node = pNode;
}
else if(!strcmp(nodesz, "/*#sat_node@*/"))
{
if(m_sat_node) FreeNodeFieldInfo(m_sat_node);
m_sat_node = pNode;
}
else if(!strcmp(nodesz, "/*#prog_node@*/"))
{
if(m_prog_node) FreeNodeFieldInfo(m_prog_node);
m_prog_node = pNode;
}
else if(!strcmp(nodesz, "/*#def_prog_node@*/"))
{
// if(m_def_prog_full_node) FreeNodeFieldInfo(m_def_prog_full_node);
m_def_prog_full_node = m_def_prog_full_node;
}
else return NULL;
return pNode;
}
#define MAX_PROGRAM_POS 64
NodeField **CStringLines::AdjustNodeField(NodeField **pNode)
{
int i = 0;
int j = 0;
int max_pos = -1;
NodeField **pNewField = NULL;
while(pNode[i])
{
if(pNode[i]->field_pos != -1 && pNode[i]->field_pos != 0)
{
j++;
if(pNode[i]->field_pos > max_pos)
{
if(max_pos > MAX_PROGRAM_POS)
{
ASSERT(0);
}
max_pos = pNode[i]->field_pos;
}
}
i++;
}
if(max_pos == -1) return NULL;
int count = i;
pNewField = new NodeField*[max_pos + 2];
memset(pNewField, 0, sizeof(NodeField*) * (max_pos + 2));
NodeField *pNewNode = NULL;
for(i = 0; i < count; i++)
{
if(pNode[i]->field_pos == -1 || pNode[i]->field_pos == 0) continue;
pNewNode = new NodeField;//i have to create new node to save the same info
memcpy(pNewNode, pNode[i], sizeof(NodeField));
pNewField[pNode[i]->field_pos - 1] = pNewNode;
}
pNode = 0;
return pNewField;
}
int CStringLines::FindSpacePos(CString linestr)
{
int pos, tpos;
pos = linestr.Find(' ', 0);
tpos = linestr.Find('\t', 0);
if(pos > tpos)
{
if(tpos != -1) pos = tpos;
}
if(pos < tpos)
{
if(pos == -1) pos = tpos;
}
return pos;
}
void CStringLines::FreeNodeFieldInfo(NodeField **pNode)
{
int i;
if(!pNode) return;
i = 0;
while(pNode[i])
{
delete pNode[i];
pNode[i] = NULL;
i++;
};
delete[] pNode;
}
NodeField *CStringLines::ParseNodeFieldLine(CString linestr)
{
char name[256];
DataType tag = NOFIND;
int bits_length = 0;
NodeField *pNodeInfo = NULL;
BOOL bArray = FALSE;
BOOL bComment = FALSE;
int CommentPos = 0;
int ArrayNum = 0;
int bFieldMask = 0;
name[0] = 0;
if(linestr.IsEmpty()) return NULL;
linestr = FilterFrontValidChar(linestr);
int pos = linestr.Find("/*#", 0);
if(pos != -1)
{
CommentPos = pos;
pos = linestr.Find("@*/", 0);
}
if(pos != -1 ) bComment = TRUE;
else bComment = FALSE;
pos = linestr.Find(":", 0);
if(pos != -1 && (bComment ? (pos < CommentPos) : 1)) bArray = FALSE;
else bArray = TRUE;
if(bArray)
{
pos = linestr.Find("[", 0);
if(pos != -1 && (bComment ? (pos < CommentPos) : 1)) bArray = TRUE;
else bArray = FALSE;
}
if(bComment)
{
CString commentstr = linestr.Right(linestr.GetLength() - CommentPos);
commentstr.Delete(0, 3);//delete "/*#"
pos = commentstr.Find("@*/", 0);
commentstr = commentstr.Left(pos);//ok, find comment now
CString namestr;
pos = commentstr.Find(":", 0);
if(pos == -1)
{
namestr = commentstr;
tag = NOFIND;
}
else
{
namestr = commentstr.Left(pos);
commentstr = commentstr.Right(commentstr.GetLength() - pos - 1);
namestr = FilterFrontChar(namestr, ' ');
namestr = FilterBackChar(namestr, ' ');
if(!namestr.IsEmpty()) strcpy(name, (LPCSTR)namestr);
commentstr = FilterFrontChar(commentstr, ' ');
commentstr = FilterBackChar(commentstr, ' ');
if(!commentstr.Compare("string"))
{
tag = STRING;
}
}
namestr = FilterFrontChar(namestr, ' ');
namestr = FilterBackChar(namestr, ' ');
strcpy(name, (LPCSTR)namestr);
if(!strcmp(name, "masked")) bFieldMask = 1;
}
int datatype = 0;
pos = linestr.Find("UINT32", 0);
if(!pos)
{
datatype = 32;
}
if(!datatype)
{
pos = linestr.Find("UINT16", 0);
if(!pos)
{
datatype = 16;
}
}
if(!datatype)
{
pos = linestr.Find("UINT8", 0);
if(!pos)
{
datatype = 8;
}
}
if(!datatype)
{
pos = linestr.Find("char", 0);
if(!pos)
{
datatype = 8;
}
}
if(!datatype)
{
pos = linestr.Find("short", 0);
if(!pos)
{
datatype = 16;
}
}
if(!datatype)
{
pos = linestr.Find("int", 0);
if(!pos)
{
datatype = 32;
}
}
if(!datatype)
{
pos = linestr.Find("long", 0);
if(!pos)
{
datatype = 32;
}
}
if(!datatype)
{
pos = linestr.Find("unsigned", 0);
if(!pos)
{
pos = linestr.Find("char", 0);
if(!pos)
{
datatype = 8;
}
if(!datatype)
{
pos = linestr.Find("short", 0);
if(!pos)
{
datatype = 16;
}
}
if(!datatype)
{
pos = linestr.Find("int", 0);
if(!pos)
{
datatype = 32;
}
}
if(!datatype)
{
pos = linestr.Find("long", 0);
if(!pos)
{
datatype = 32;
}
}
}
}
if(!datatype)
{//sorry, can't find data type
return NULL;
}
if(1)
{
pos = FindSpacePos(linestr);
if(pos <= 0) return NULL;//can't find variable
CString namestr = linestr.Right(linestr.GetLength() - pos - 1);
pos = FindSpacePos(namestr);
if(pos > 0) namestr = namestr.Left(pos);
pos = namestr.Find(":", 0);
if(pos == -1)
{
if(bComment)
{
pos = namestr.Find("/*#", 0);
if(pos) namestr = namestr.Left(pos);
else return NULL;//error
}
else
{
;//nothing to do
}
}
else
{
if(bComment)
{
CommentPos = namestr.Find(":", 0);
if(CommentPos > pos || CommentPos == -1) namestr = namestr.Left(pos);
else
{
pos = namestr.Find("/*#", 0);
if(pos > 0) namestr = namestr.Left(pos);
else return NULL;//error
}
}
else
{
namestr = namestr.Left(pos);
}
}
pos = namestr.Find("[", 0);
if(pos != -1)
{
CString arraystr = namestr.Right(namestr.GetLength() - pos - 1);
namestr = namestr.Left(pos);
pos = arraystr.Find(']', 0);
if(pos == -1) return NULL;
arraystr = arraystr.Left(pos);
ArrayNum = GetDigitValue(arraystr);
}
namestr = FilterFrontChar(namestr, ' ');
namestr = FilterBackChar(namestr, ' ');
if(!name[0]) strcpy(name, (LPCSTR)namestr);
}
if(!bits_length)
{
CString namestr;
pos = linestr.Find(":", 0);
if(pos == -1)
{
bits_length = datatype;
}
else
{
if(bComment)
{
CommentPos = linestr.Find("/*#", 0);
if(CommentPos > pos || CommentPos == -1)
{
namestr = linestr.Right(linestr.GetLength() - pos -1);
bits_length = GetDigitValue((LPCSTR)namestr);
}
else
{
bits_length = datatype;
}
}
else
{
namestr = linestr.Right(linestr.GetLength() - pos -1);
bits_length = GetDigitValue((LPCSTR)namestr);
}
}
}
if(!bits_length) return NULL;
if(tag == NOFIND) tag = DIGIT;
if(bArray) bits_length = datatype * ArrayNum;
//well, create new node
pNodeInfo = new NodeField;
memset(pNodeInfo, 0, sizeof(NodeField));
pNodeInfo->tag = tag;
strcpy(pNodeInfo->name, name);
pNodeInfo->bits_length = bits_length;
pNodeInfo->field_mask = bFieldMask;
return pNodeInfo;
}
NodeField *CStringLines::ParseNodeFieldLine1(CString linestr)
{
char name[256];
DataType tag = NOFIND;
int bits_length = 0;
NodeField *pNodeInfo = NULL;
BOOL bArray = FALSE;
BOOL bComment = FALSE;
int CommentPos = 0;
int ArrayNum = 0;
int bFieldMask = 0;
int fieldpos = -1;
name[0] = 0;
if(linestr.IsEmpty()) return NULL;
linestr = FilterFrontValidChar(linestr);
int pos = linestr.Find("/*#", 0);
if(pos != -1)
{
CommentPos = pos;
pos = linestr.Find("@*/", 0);
}
if(pos != -1 ) bComment = TRUE;
else bComment = FALSE;
pos = linestr.Find(":", 0);
if(pos != -1 && (bComment ? (pos < CommentPos) : 1)) bArray = FALSE;
else bArray = TRUE;
if(bArray)
{
pos = linestr.Find("[", 0);
if(pos != -1 && (bComment ? (pos < CommentPos) : 1)) bArray = TRUE;
else bArray = FALSE;
}
if(bComment)
{
CString commentstr = linestr.Right(linestr.GetLength() - CommentPos);
commentstr.Delete(0, 3);//delete "/*#"
pos = commentstr.Find("@*/", 0);
commentstr = commentstr.Left(pos);//ok, find comment now
commentstr = FilterFrontChar(commentstr, ' ');
if(!commentstr.IsEmpty())
{
int rpos, kpos;
CString rule, keywords, keyval;
do
{
rpos = FineNextPostionByKey(commentstr, '|');
if(rpos != -1)
{
rule = commentstr.Left(rpos);
}
else
{
rule = commentstr;
}
kpos = rule.Find(':', 0);
if(kpos == 0 || kpos == -1)
{
CString str;
str.Format("%s error", commentstr);
AfxMessageBox(str);
break;//error
}
keywords = rule.Left(kpos);
keyval = rule.Right(rule.GetLength() - kpos - 1);
keywords = FilterFrontChar(keywords, ' ');
keywords = FilterBackChar(keywords, ' ');
keyval = FilterFrontChar(keyval, ' ');
keyval = FilterBackChar(keyval, ' ');
if(!keywords.Compare("name"))
{
strcpy(name, (LPCSTR)keyval);
}
else if(!keywords.Compare("type"))
{
if(!keyval.Compare("string"))
{
tag = STRING;
}
else if(!keyval.Compare("bool"))
{
// tag = BOOL;
}
}
else if(!keywords.Compare("pos"))
{
fieldpos = GetDigitValue(keyval);
}
else if(!keywords.Compare("masked"))
{
bFieldMask = GetDigitValue(keyval);
}
else
{
;
}
commentstr = commentstr.Right(commentstr.GetLength() - rpos - 1);
}
while(rpos != -1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -