📄 cssc.cpp
字号:
#include "cssc.h"
int main(int argc, char *argv[])
{
int intFind;
char *strFullPath;
char *strBaseDir;
char *strFile;
int nFiles;
int nDetail;
CountItem clsCISum;
LoadCommand();
nDetail = 0;
nCMDT = 0;
if (argc < 2)
{
ExportHelpInfo();
return 1;
}
if (strcmp(argv[1],"-s") != 0)
nDetail = 0;
else
nDetail = 1;
try
{
ExportCopyRight();
nFiles = 0;
for(int i = 1 + nDetail; i < argc; i++)
{
nFiles++;
strFile = canonicalize_file_name(argv[i]);
SRCCount scCount(strFile,nFiles);
scCount.ShowCountInfo(nDetail);
}
if (nFiles < 1)
{
SetConsoleColor(cscRed);
cout << "File [" << argv[1] << "] Not Found!" << endl;
SetConsoleColor(cscNormal);
return 6;
}
if (nFiles > 1)
{
ShowSumInfo(nDetail);
}
}
catch (ios_base::failure& var)
{
cout << var.what();
}
return 0 ;
}
void ExportHelpInfo()
{
ExportCopyRight();
SetConsoleColor(cscHelp);
cout << "Command Format : CSSC [-s] Target-file-name-group" << endl;
cout << " -s : Show Detail Information" << endl;
cout << " Target-file-name-group : COBOL Source File(s)" << endl;
// SetConsoleColor(7);
}
void ExportCopyRight()
{
SetConsoleColor(cscNormal);
cout << "==================================================================================" << endl;
SetConsoleColor(cscCopy);
cout << "COBOL SOURCE STEP COUNT (ver 0.3)";
cout << " Copyright (c) Sheng Guangli " << endl;
cout << " E-MAIL : bullsgl@gmail.com " << endl;
// cout << " HP-URL : http://www.clily.net " << endl;
SetConsoleColor(cscNormal);
cout << "----------------------------------------------------------------------------------" << endl;
}
void SetConsoleColor(int nColor)
{
cout << "\033[" << nColor << "m";
}
CountItem::CountItem()
{
nALL = 0;
nVALID = 0;
nCOMM = 0;
nCMD =0;
}
SRCCount::SRCCount(char *strFile,int nFiles)
{
for(int i=0; i < COMMAND_NUM; i++)
{
nCMD[i] = 0;
}
clsCI.nCMD = 0;
nFileNo = nFiles;
ifsSRC.open(strFile,ios_base::in);
if (!ifsSRC)
{
SetConsoleColor(cscRed);
cout << "Source File [" << strFile << "] Open Failed!" << endl;
return;
}
if (!ifsSRC.is_open())
{
SetConsoleColor(cscRed);
cout << "Source File [" << strFile << "] Open Failed!" << endl;
return;
}
SetConsoleColor(cscName);
cout << nFileNo << ". Source File : " << strFile << endl;
blDiv = false;
blPGID = false;
strPGID = "";
CountStep();
ifsSRC.close();
}
void SRCCount::CountStep()
{
string strLine;
int nPos;
while (std::getline(ifsSRC,strLine))
{
clsCI.nALL++;
if(!blPGID)
{
nPos = strLine.find(" PROGRAM-ID.",0);
if (nPos == 0)
{
strPGID = strLine.substr(18,strLine.size() - 18);
strPGID = TrimSpace(strPGID,8);
if (strPGID[strPGID.size() - 1] == '.')
{
strPGID.erase(strPGID.size() - 1,1);
}
blPGID = true;
}
}
if(!blDiv)
{
nPos = strLine.find(" PROCEDURE ",0);
if (nPos == 0)
{
strLine = strLine.substr(16,strLine.size() - 16);
strLine = TrimSpace(strLine,0);
nPos = strLine.find("DIVISION",0);
if (nPos == 0)
{
blDiv = true;
}
}
}
if(strLine.size() > 6)
{
if(strLine[6] == '*' || strLine[6] == 'D')
{
clsCI.nCOMM++;
if(blDiv)
{
clsDivCI.nCOMM++;
}
}
else
{
strLine = TrimSpace(strLine,0);
if(strLine.size() == 0)
{
clsCI.nCOMM++;
if(blDiv)
{
clsDivCI.nCOMM++;
}
}
else
{
clsCI.nVALID++;
if(blDiv)
{
clsDivCI.nVALID++;
for(int nIdx = 0; nIdx < COMMAND_NUM; nIdx++)
{
nPos = strLine.find(strCMD[nIdx],0);
if (nPos == 0)
{
if(strLine.size() > strCMD[nIdx].size())
{
if(strLine[strCMD[nIdx].size()] == ' ' || strLine[strCMD[nIdx].size()] == '.')
{
nCMD[nIdx]++;
clsCI.nCMD++;
}
break;
}
else
{
nCMD[nIdx]++;
clsCI.nCMD++;
break;
}
}
}
}
}
}
}
else
{
clsCI.nCOMM++;
if(blDiv)
{
clsDivCI.nCOMM++;
}
}
if(clsCI.nALL >= 65535)
{
SetConsoleColor(cscRed);
cout << "File Lines Too Large!" << endl;
SetConsoleColor(cscNormal);
break;
}
}
}
SRCCount::~SRCCount()
{
SetConsoleColor(cscNormal);
cout << "----------------------------------------------------------------------------------" << endl;
}
string TrimSpace(string strValue,int nWidth)
{
unsigned int nPos, nSize;
for (nPos = 0; nPos < strValue.size(); nPos++)
{
if (strValue[nPos] != ' ')
{
break;
}
}
if (nPos > 0)
{
strValue = strValue.substr(nPos,strValue.size() - nPos);
}
nPos = strValue.size() - 1;
while (nPos > 0 || nPos == 0)
{
if (strValue[nPos] != ' ')
{
break;
}
nPos--;
}
strValue = strValue.substr(0,nPos + 1);
while (strValue.size() < nWidth)
{
strValue.append(" ");
}
return strValue;
}
string FormatNumber(int nNumber,int nWidth)
{
char strBuff[20];
string strValue;
unsigned int nLoop;
nLoop = 1;
sprintf(strBuff,"%d",nNumber);
strValue = string(strBuff);
while (strValue.size() > nLoop * 4 - 1)
{
strValue.insert(strValue.size() - nLoop * 4 + 1,",");
nLoop++;
}
while (strValue.size() < nWidth)
{
strValue.insert(0," ");
}
return strValue;
}
void SRCCount::ShowCountInfo(int nDetail)
{
int nOut;
SetConsoleColor(cscName);
cout << "PG-ID = ";
SetConsoleColor(cscValue);
cout << strPGID;
SetConsoleColor(cscName);
cout << " ALL = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsCI.nALL,6);
SetConsoleColor(cscName);
cout << " VALID = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsCI.nVALID,6);
SetConsoleColor(cscName);
cout << " COMMENT = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsCI.nCOMM,6);
SetConsoleColor(cscName);
cout << " COMMAND = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsCI.nCMD,6) << endl;
clsSum.nALL += clsCI.nALL;
clsSum.nVALID += clsCI.nVALID;
clsSum.nCOMM += clsCI.nCOMM;
clsSum.nCMD += clsCI.nCMD;
if(nDetail > 0)
{
string strSpace = "";
while (strSpace.size() < strPGID.size() + 4)
strSpace.append(" ");
SetConsoleColor(cscName);
cout << strSpace << "PROCEDURE DIVISION";
cout << " (VALID = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsDivCI.nVALID,6);
SetConsoleColor(cscName);
cout << " COMMENT = ";
SetConsoleColor(cscValue);
cout << FormatNumber(clsDivCI.nCOMM,6);
SetConsoleColor(cscName);
cout << ")" << endl;
clsDivCIs.nVALID += clsDivCI.nVALID;
clsDivCIs.nCOMM += clsDivCI.nCOMM;
nOut = 0;
if(clsCI.nCMD > 0)
{
for (int nIdx = 0; nIdx < COMMAND_NUM; nIdx++)
{
if(nCMD[nIdx] > 0)
{
if(nOut % 4 == 0)
{
SetConsoleColor(cscName);
cout << endl << " |";
}
SetConsoleColor(cscName);
cout << setfill(' ') << setw(10) << left;
cout << strCMD[nIdx];
cout << "- ";
SetConsoleColor(cscValue);
cout << FormatNumber(nCMD[nIdx],6);
SetConsoleColor(cscName);
cout << "|";
nOut++;
}
nCMDs[nIdx] += nCMD[nIdx];
}
cout << endl;
}
nCMDT += clsCI.nCMD;
}
}
void ShowSumInfo(int nDetail)
{
int nOut;
SetConsoleColor(cscName + cscRev);
cout << "All Source Sum :";
cout << " ALL = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsSum.nALL,6);
SetConsoleColor(cscName + cscRev);
cout << " VALID = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsSum.nVALID,6);
SetConsoleColor(cscName + cscRev);
cout << " COMMENT = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsSum.nCOMM,6);
SetConsoleColor(cscName + cscRev);
cout << " COMMAND = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsSum.nCMD,6);
cout << " ";
SetConsoleColor(cscNormal);
cout << endl;
SetConsoleColor(cscNormal);
if(nDetail > 0)
{
SetConsoleColor(cscName + cscRev);
cout << " PROCEDURE DIVISION";
cout << " (VALID = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsDivCIs.nVALID,6);
SetConsoleColor(cscName + cscRev);
cout << " COMMENT = ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(clsDivCIs.nCOMM,6);
SetConsoleColor(cscName + cscRev);
cout << ") ";
cout << endl;
SetConsoleColor(cscNormal);
nOut = 0;
if(nCMDT > 0)
{
for (int nIdx = 0; nIdx < COMMAND_NUM; nIdx++)
{
if(nCMDs[nIdx] > 0)
{
if(nOut % 4 == 0)
{
SetConsoleColor(cscName + cscRev);
cout << endl << " |";
}
SetConsoleColor(cscName + cscRev);
cout << setfill(' ') << setw(10) << left;
cout << strCMD[nIdx];
cout << "- ";
SetConsoleColor(cscValue + cscRev);
cout << FormatNumber(nCMDs[nIdx],6);
SetConsoleColor(cscName + cscRev);
cout << "|";
SetConsoleColor(cscNormal);
nOut++;
}
}
cout << endl;
}
}
SetConsoleColor(cscNormal);
cout << "----------------------------------------------------------------------------------" << endl;
}
void LoadCommand()
{
unsigned int nIndex;
nIndex = 0;
strCMD[nIndex] = "ADD";
nIndex++;
strCMD[nIndex] = "ALTER";
nIndex++;
strCMD[nIndex] = "CALL";
nIndex++;
strCMD[nIndex] = "CANCEL";
nIndex++;
strCMD[nIndex] = "CLOSE";
nIndex++;
strCMD[nIndex] = "COMMIT";
nIndex++;
strCMD[nIndex] = "COMPUTE";
nIndex++;
strCMD[nIndex] = "CONTINUE";
nIndex++;
strCMD[nIndex] = "COPY";
nIndex++;
strCMD[nIndex] = "DELETE";
nIndex++;
strCMD[nIndex] = "DISABLE";
nIndex++;
strCMD[nIndex] = "DISPLAY";
nIndex++;
strCMD[nIndex] = "DIVIDE";
nIndex++;
strCMD[nIndex] = "ENABLE";
nIndex++;
strCMD[nIndex] = "ENTER";
nIndex++;
strCMD[nIndex] = "EVALUATE";
nIndex++;
strCMD[nIndex] = "EXEC";
nIndex++;
strCMD[nIndex] = "EXIT";
nIndex++;
strCMD[nIndex] = "GENERATE";
nIndex++;
strCMD[nIndex] = "GO";
nIndex++;
strCMD[nIndex] = "IF";
nIndex++;
strCMD[nIndex] = "INITIATE";
nIndex++;
strCMD[nIndex] = "INITIALIZE";
nIndex++;
strCMD[nIndex] = "INSPECT";
nIndex++;
strCMD[nIndex] = "MERGE";
nIndex++;
strCMD[nIndex] = "MOVE";
nIndex++;
strCMD[nIndex] = "MULTIPLY";
nIndex++;
strCMD[nIndex] = "OPEN";
nIndex++;
strCMD[nIndex] = "PERFORM";
nIndex++;
strCMD[nIndex] = "READ";
nIndex++;
strCMD[nIndex] = "RECEIVE";
nIndex++;
strCMD[nIndex] = "RELEASE";
nIndex++;
strCMD[nIndex] = "REWRITE";
nIndex++;
strCMD[nIndex] = "SEARCH";
nIndex++;
strCMD[nIndex] = "SEND";
nIndex++;
strCMD[nIndex] = "SET";
nIndex++;
strCMD[nIndex] = "SORT";
nIndex++;
strCMD[nIndex] = "START";
nIndex++;
strCMD[nIndex] = "STOP";
nIndex++;
strCMD[nIndex] = "STRING";
nIndex++;
strCMD[nIndex] = "SUBTRACT";
nIndex++;
strCMD[nIndex] = "TERMINATE";
nIndex++;
strCMD[nIndex] = "UNSTRING";
nIndex++;
strCMD[nIndex] = "USE";
nIndex++;
strCMD[nIndex] = "WRITE";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -