⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileex.cpp

📁 Visual C++编写的工程解析器源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
							int eIndex = strLine.Find(';');
							
							if(nIndex == -1 && eIndex == -1)
							{
								while(!file.IsEof())
								{
									char ch = 0;
									file.Read(&ch, 1);
									if(ch != '{')
									{
										if(ch != ';')
										{
											strClassDefine += ch;
											continue;
										}
										else
											//反之就说明只有类声明,而无类定义实体
											bHaveClassBody = FALSE;
									}
									break;
								}
							}
							else
							{
								if(nIndex != -1)
									strClassDefine = strClassDefine.Left(nIndex);

								if(eIndex != -1)
									strClassDefine = strClassDefine.Left(eIndex);
							}
							
							strClassDefine.Replace('\n', ' ');
							
							CString strClassName = "";
							nIndex = strClassDefine.Find(':');
							
							//如果该类是从其他类继承来的
							if(nIndex != -1)
							{
								strClassName = strClassDefine.Left(nIndex);
								strClassName.TrimRight();
								
								int nSize = strClassName.GetLength(), j = 0;
								for(j = nSize - 1; j >=0; --j)
								{
									if(strClassName.GetAt(j) == ' ')	break;
								}
									
								strClassName = strClassName.Right(strClassName.GetLength() - j - 1);
							}
							else
							{
								//如果该类无父类
								strClassDefine.TrimRight();
								strClassName = strClassDefine;
								
								int nSize = strClassName.GetLength(), j = 0;
								for(j = nSize - 1; j >=0; --j)
								{
									if(strClassName.GetAt(j) == ' ')	break;
								}
								
								strClassName = strClassName.Right(strClassName.GetLength() - j - 1);
							}
							lstrcpy(lpitem->strClassName, strClassName.GetBuffer(0));
														
							m_cilClassNames.AddTail(lpitem);
						}
					}
				}
			}			
		}
		else
		{
			//打开无注释/无字符串内容文件失败
			::DeleteFile((LPCTSTR)m_strNoNotesFilename);
			return FALSE;
		}
	}
	else
		return FALSE;
	
	::DeleteFile((LPCTSTR)m_strNoNotesFilename);
	return TRUE;
}

/*******************CParseDelphiFile文件解析类******************/
CParseDelphiFile::CParseDelphiFile()
{
	m_strNoNotesFilename = "";
}

CParseDelphiFile::~CParseDelphiFile()
{
	this->m_sltHeadFiles.RemoveAll();
}

//清除Delphi[dpr/pas文件]所有的注释信息,然后把它保存到新文件中去[保持文件后缀名不变]
BOOL CParseDelphiFile::RemoveProjFileNoteInfo()
{
	m_strNoNotesFilename = GetNoNotesFilePath(this->GetFileName());
	
	CFileEx lpfile;	//保存没有注释的文件内容
	try
	{
		CFileException e;
		lpfile.Open((LPCTSTR)m_strNoNotesFilename, CFile::modeCreate | CFile::modeWrite, &e);
	}
	catch(...)
	{
		return FALSE;
	}
	
	
	/******************************************************************/	
	char bufLine[MAX_LINE_SIZE];		//存放行无注释的行内容
	memset(bufLine, 0x0, sizeof(bufLine));
	
	while(!this->IsEof())				//循环遍历文件内容
	{		
		char buf[3];					//用于判断是否为注释符号
		memset(buf, 0x0, sizeof(buf));	
		
		//读2个连续的字节[即:同一行的两个字节]
		//如果到行尾则有多少读多少(可能读不够两个字节)
		int i = 0; 
		while(!this->IsEof() && i < 2)		
		{	
			char ch = 0;	
			this->Read(&ch, 1);
			buf[i++] = ch; 
			if(ch == '\n')	break;
		}
		
		//如果其中有字符串标志符则一直读到该字符串的结尾[字符串的优先级最高]
		char* p = strchr(buf, GLOBAL_PASSTR_SPECCHAR);
		if(p != NULL)
		{			
			/********************先保存Buf缓存内容*******************/
			if( strlen(bufLine) + strlen(buf) < MAX_LINE_SIZE )	//如果缓存未满
			{
				unsigned int nIndex = 0;
				for(char ch = 0; nIndex < (unsigned)strlen(buf); ++nIndex)
					if((ch = buf[nIndex]) != '\n')
						bufLine[strlen(bufLine)] = ch;	//则连到行内容字符串中
					else
					{
						lpfile.Write(bufLine, strlen(bufLine));						
						memset(bufLine, 0x0, sizeof(bufLine));
						break;			//至此一行内容解析完毕[不包含注释部分]				
					}
			}
			
			char ch = 0;		
			if(p == buf)				//如果单引号是在开头字符[即第一个字符]
			{				
				if(*(p + 1) == GLOBAL_PASSTR_SPECCHAR)		//如果Buf的内容刚好构成一对单引号,则无须遍历文件寻找其配对的单引号
					continue;
			}
			/*********************读该字符串到结尾********************/
			while(!this->IsEof())
			{				
				this->Read(&ch, 1);
				
				bufLine[strlen(bufLine)] = ch;	
				if(ch == GLOBAL_PASSTR_SPECCHAR)	break;		//配对成功则结束该字符串
			}
			continue;
		}
		
		/*****************************如果读到'//'注释符号时***************************/
		if(buf[0] != '{' && strchr(buf, '/'))	//!!!注意即包含'/'又包含'{'的情况!!!
		{
			if(strcmp(buf, "//") == 0)	//!!!如果单行注释符号一次性读入Buf!!!
			{
				char ch = 0;
				while(!this->IsEof())	//读到行结尾
				{	
					this->Read(&ch, 1);
					if(ch == '\n') 				
					{
						//至此一行内容解析完毕
						lpfile.Write(bufLine, strlen(bufLine));
						memset(bufLine, 0x0, sizeof(bufLine));
						break;	
					}
				}
			}
			else						//!!![注意当'//'分两次才读完时的情况]!!!
			{
				bufLine[strlen(bufLine)] = buf[0];
				char ch = 0;
				if(!this->IsEof())
				{
					this->Read(&ch, 1);
					
					if(ch == '/' && !this->IsEof())
					{
						while(!this->IsEof())		//读到行结尾
						{	
							this->Read(&ch, 1);
							if(ch == '\n') 				
							{
								//至此一行内容解析完毕
								lpfile.Write(bufLine, strlen(bufLine));
								memset(bufLine, 0x0, sizeof(bufLine));
								break;	
							}
						}						
					}
					else if(ch != '/')	
						bufLine[strlen(bufLine)] = ch;
				}
			}
		}
		else if(strchr(buf, '{'))		//如果找到注释符号'{'
		{
			char ch = 0;		char stack[1];	int stackPoint = -1;	//栈顶指针
			
			while(!this->IsEof())		//读到注释结束符号'}'[!!!注意大括号嵌套情况!!!]
			{	
				this->Read(&ch, 1);
				if(ch == '{'){	stack[++stackPoint] = ch; continue;	}
				if(ch == '}')
				{	
					if(stackPoint >= 0 && stack[stackPoint] == '{') 
						stack[stackPoint--] = 0;	
					else if(stackPoint < 0)
						break;	 
				}
				else if(ch == '\n') 			
					memset(bufLine, 0x0, sizeof(bufLine));
			}
		}
		else							//普通字符
		{
			if( strlen(bufLine) + strlen(buf) < MAX_LINE_SIZE )	//如果缓存未满
			{
				unsigned int nIndex = 0;
				for(char ch = 0 ; nIndex < (unsigned)strlen(buf); ++nIndex)
					if((ch = buf[nIndex]) != '\n')
						bufLine[strlen(bufLine)] = ch;	//则连到行内容字符串中
					else
					{
						lpfile.Write(bufLine, strlen(bufLine));
						memset(bufLine, 0x0, sizeof(bufLine));
						break;			//至此一行内容解析完毕[不包含注释部分]				
					}
			}
		}	
	}
	lpfile.Close();
	return TRUE;
}

//专门针对Delphi的pas源代码文件进行解析
BOOL CParseDelphiFile::RemovePasFileNoteInfo(char strPasFilename[MAX_PATH])
{
	m_strNoNotesFilename = GetNoNotesFilePath(strPasFilename);
	
	FILE* lpfile;
	lpfile = fopen(m_strNoNotesFilename.GetBuffer(0), "w");
	if(lpfile == NULL)	return FALSE;
	
	//由于效率问题所以采用C语言文件操作
	FILE* file;
	file = fopen(strPasFilename, "r");	//以只读方式打开PAS文件
	
	if(file == NULL)
	{
		fclose(lpfile);
		return FALSE;					//打开文件失败
	}
	
	char bufLine[MAX_LINE_SIZE];		//存放行内容
	memset(bufLine, 0x0, sizeof(bufLine));
	
	while(!feof(file))					//循环遍历文件内容
	{		
		char buf[3];					//用于判断是否为注释符号
		memset(buf, 0x0, sizeof(buf));	
		
		//读2个连续的字节[即:同一行的两个字节]
		//如果到行尾则有多少读多少(可能读不够两个字节)
		int i = 0; 
		while(!feof(file) && i < 2)		
		{	
			char ch = 0;	
			fread(&ch, sizeof(char), 1, file);	
			buf[i++] = ch; 
			if(ch == ';')	break;
		}
		
		//如果其中有字符串标志符则一直读到该字符串的结尾[字符串的优先级最高]
		char* p = strchr(buf, GLOBAL_PASSTR_SPECCHAR);
		if(p != NULL)
		{						
			char ch = 0;		
			if(p == buf)								//如果单引号是在开头字符[即第一个字符]
			{				
				if(*(p + 1) == GLOBAL_PASSTR_SPECCHAR)		//如果Buf的内容刚好构成一对单引号,则无须遍历文件寻找其配对的单引号
					continue;
			}
			/*********************反之则读该字符串到结尾********************/
			while(!feof(file))
			{				
				fread(&ch, sizeof(char), 1, file);								
				if(ch == GLOBAL_PASSTR_SPECCHAR)	break;	//配对成功则结束该字符串
			}
			continue;
		}
		
		/*****************************如果读到'//'注释符号时***************************/
		if(buf[0] != '{' && strchr(buf, '/'))	//!!!注意即包含'/'又包含'{'的情况!!!
		{
			if(strcmp(buf, "//") == 0)	//!!!如果单行注释符号一次性读入Buf!!!
			{
				char ch = 0;
				while(!feof(file))		//读到句尾
				{	
					fread(&ch, sizeof(char), 1, file);	 
					if(ch == '\n')
					{						
						if(strcmp(bufLine, "") != 0)
						{
							fprintf(lpfile, "%s\n", bufLine);
							memset(bufLine, 0x0, sizeof(bufLine));
						}
						break;			//至此一行内容解析完毕[不包含注释部分]				
					}
				}
			}
			else						//!!![注意当'//'分两次才读完时的情况]!!!
			{
				bufLine[strlen(bufLine)] = buf[0];
				char ch = 0;
				if(!feof(file))
				{
					fread(&ch, sizeof(char), 1, file);
					
					if(ch == '/' && !feof(file))
					{
						while(!feof(file))		//读到句尾
						{	
							fread(&ch, sizeof(char), 1, file);	 
							if(ch == '\n')
							{
								if(strcmp(bufLine, "") != 0)
								{
									fprintf(lpfile, "%s\n", bufLine);
									memset(bufLine, 0x0, sizeof(bufLine));
								}
								break;			//至此一行内容解析完毕[不包含注释部分]						
							}
						}						
					}
					else if(ch != '/')	
						bufLine[strlen(bufLine)] = ch;
				}
			}
		}
		else if(strchr(buf, '{'))		//如果找到注释符号'{'
		{
			char ch = 0;		char stack[1];	int stackPoint = -1;	//栈顶指针
			
			while(!feof(file))			//读到注释结束符号'}'[!!!注意大括号嵌套情况!!!]
			{	
				fread(&ch, sizeof(char), 1, file);	 
				if(ch == '{'){	stack[++stackPoint] = ch; continue;	}
				if(ch == '}')
				{	
					if(stackPoint >= 0 && stack[stackPoint] == '{') 
						stack[stackPoint--] = 0;	
					else if(stackPoint < 0)
						break;	 
				}
				else if(ch == '\n')
				{
					if(strcmp(bufLine, "") != 0)
					{
						fprintf(lpfile, "%s", bufLine);
						memset(bufLine, 0x0, sizeof(bufLine));
					}
				}
			}
		}
		else if(strchr(buf, '('))
		{
			char* p = strchr(buf, '(');
			//先保存缓存内容
			for(int i = 0; (unsigned)i < strlen(buf); ++i)
				bufLine[strlen(bufLine)] = buf[i];			
			
			if(p == buf && buf[1] != ')' || p != buf)
			{
				char ch = 0;
				while(!feof(file))
				{
					fread(&ch, sizeof(char), 1, file);
					bufLine[strlen(bufLine)] = ch;					
					if(ch == ')') break;
				}
			}
		}
		else							//普通字符
		{
			if( strlen(bufLine) + strlen(buf) < MAX_LINE_SIZE )	//如果缓存未满
			{
				char ch = 0;
				for(int nIndex = 0; (unsigned)nIndex < strlen(buf); ++nIndex)
				{
					if((ch = buf[nIndex]) != ';')
						bufLine[strlen(bufLine)] = ch;	//则连到行内容字符串中
					else
					{	
						bufLine[strlen(bufLine)] = ch;	//注意分号不能丢掉
						
						CString str = bufLine;						
						str.TrimLeft();	str.TrimRight();
						str.MakeLower();	
						
						int nIndex = str.Find("var");
						
						if(nIndex == 0)
						{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -