📄 pascalfile.cpp
字号:
// PascalFile.cpp: implementation of the PascalFile class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyPascal.h"
#include "PascalFile.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
StringTable PascalFile::s_flagTable("Pascle Flags table");
PascalFile::PascalFile()
{
if(s_flagTable.GetLength() == 0)
{
s_flagTable.CreateTable(MAX_NUM);
s_flagTable.SetEnlargeLength(MAX_NUM);
LoadFlagsTable();
}
stirngTable = StringTable("string records table", false);
i_line = 0;
i_row = 0;
}
PascalFile::~PascalFile()
{
}
void PascalFile::open(const char* filename,
int iostype/* = ios::in*/, int filebuffer/* = filebuf::openprot*/)
// open a file
{
this->i_line = 1;
this->i_row = 1;
ifstream::open(filename, iostype, filebuffer);
}
int PascalFile::peek()
// get next char but don't move read header
{
int i = ifstream::peek();
if(i == -1)
{
i = 0;
}
return i;
}
void PascalFile::LoadFlagsTable()
// load flags table
{
ifstream file("PASCAL.txt");
char s[100];
int i = 1;
while(!file.eof() && s_flagTable.GetLength() < MAX_NUM)
{
file>>s;
s_flagTable.InsertElement(i, s);
i++;
}
}
CString PascalFile::FlagsTableToString()
// get flags table' string
{
return s_flagTable.ToString();
}
CString PascalFile::StringTableToString()
// get string table's string
{
return this->stirngTable.ToString();
}
CString PascalFile::SymbleTableToString()
// get symble table's string
{
return this->symbleTable.ToString();
}
char PascalFile::GetChar()
// get one char from file
{
char ch = this->peek();
this->get();
if(ch == '\n')
{
this->i_row = 1;
this->i_line++;
}
else
{
this->i_row++;
}
return ch;
}
CString PascalFile::GetSYM()
// get word
{
if(!this->is_open())
{
return "file not opened!";
}
CString re = "";
char ch;
int type;
ch = this->peek();
// skip ' ' like characters
while(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\b')
{
this->GetChar();
ch = this->peek();
}
if(ch == 0)
{
this->GetChar();
return "";
}
// get letter character first
if((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z'))
{
int n = 0;
CString temp = "";
while((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))
{
if(n < 20) // first 20 characters is available
{
temp += ch;
n++;
}
this->GetChar();
ch = this->peek();
}
re = temp;
re.MakeUpper();
type = this->s_flagTable.MatchType(re);
if(type == -1)
{
re = temp;
type = 34;
this->symbleTable.InsertElement(type, temp);
}
}
else if(ch >= '0' && ch <= '9')
{
UINT val = 0;
while(ch >= '0' && ch <= '9')
{
val = val * 10 + ch - '0';
this->GetChar();
ch = this->peek();
}
re.Format("%u", val);
type = 33;
}
else
{
int t1, t2;
char s1[3] = {0};
s1[0] = ch;
t1 = s_flagTable.MatchType(s1);
if(t1 != -1)
{
re = s1;
type = t1;
this->GetChar();
s1[1] = this->peek();
t2 = s_flagTable.MatchType(s1);
if(t2 != -1)
{
re = s1;
type = t2;
this->GetChar();
}
}
else
{
re.Format("在%d行%d列开始发现错误字符串:", this->i_line, this->i_row);
CString s = ch;
GetChar();
while(ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r' && ch != 0 && ch != '\b')
{
ch = GetChar();
s += ch;
}
re += "\"" + s + "\"";
type = -1;
}
}
this->stirngTable.InsertElement(type, re);
// put record in
return re;
}
/*int PascalFile::GetSYSFlag(char *word, int type)
// get system flag
{
int flag = 1;
return flag;
}*/
void PascalFile::WordsTranslate()
// translate all words in the file, close file
{
this->symbleTable.DeleteTable();
this->stirngTable.DeleteTable();
for(int i = 0; i < 32; i++)
{
this->symbleTable.InsertElement(this->s_flagTable[i]);
}
this->symbleTable.InsertElement(35, "#");
this->symbleTable.SetName("symble table");
while(!this->eof())
{
this->GetSYM();
}
this->close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -