Selaa lähdekoodia

读取文件和读取指定行。

JeffWang 3 vuotta sitten
vanhempi
commit
286b5bcaa6

+ 47 - 0
Source/OGCAssistTool/OGCAssistTool/Global.cpp

@@ -96,6 +96,53 @@ again:
 		return TRUE;
 	}
 
+	std::string GetFileData(LPCTSTR lpFile)
+	{
+		if ( !lpFile || lpFile[0] == '\0' || !PathFileExists(lpFile) )
+			return std::string();
+
+		CFile file;
+		CFileException exp;
+		if ( file.Open(lpFile, CFile::modeRead, &exp) )
+		{
+			DWORD dwRead = 0;
+			DWORD dwCount = 0;
+			std::string strData;
+			char szRead[MAX_PATH] = {0};
+			DWORD dwLen = file.GetLength();
+			while ( dwCount < dwLen )
+			{
+				dwRead = file.Read(szRead, MAX_PATH);
+				strData.append(szRead, dwRead);
+				dwCount += dwRead;
+			}
+
+			file.Close();
+
+			return strData;
+		}
+
+		return std::string();
+	}
+
+	std::string GetFileLine(std::string &strFileData, int nLine)
+	{
+		int nPos=0;
+		int nIndex = 0;
+		std::string strTemp = strFileData;
+		while ( (nPos=strTemp.find('\r\n')) != std::string::npos )
+		{
+			if ( nIndex++ == nLine )
+			{
+				strTemp = strTemp.substr(0, nPos);
+				break;
+			}
+			strTemp = strTemp.substr(nPos+2);				
+		}
+
+		return  nIndex < nLine ? std::string() : strTemp;
+	}
+
 	DWORD FindProcess(LPCTSTR lpProName)
 	{
 		ASSERT(lpProName!=NULL);

+ 2 - 0
Source/OGCAssistTool/OGCAssistTool/Global.h

@@ -160,6 +160,8 @@ namespace GLOBAL {
 	HANDLE FindModuleEx(LPCTSTR lpModuleName, DWORD dwProcId);
 	BOOL GetDebugPriv();
 
+	std::string GetFileData(LPCTSTR lpFile);
+	std::string GetFileLine(std::string &strFileData, int nLine);
 	BOOL GetFileVersion( IN HMODULE hModule, IN DWORD (&dwArray)[4]);
 	BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] );
 	BOOL GetProductVersion( IN HMODULE hModule, IN DWORD (&dwArray)[4]);