Ejemplo n.º 1
0
int CDlgBatteryParam::ReadBatteryParam( CString strKernelPath, STRUCT_BATTERY_PARAM* pBatteryParam )
{
    int nRetCode=0;
    //1.check kernel file
    CFile kernelFile;
    UINT uiKernelSize;
    BOOL bRet;
    bRet = kernelFile.Open(strKernelPath,CFile::modeRead|CFile::typeBinary);
    if (!bRet)
    {
        return -1;
    }
    uiKernelSize = kernelFile.GetLength();
    BYTE *pKernelBuffer=NULL;
    UINT uiRead;
    pKernelBuffer = new BYTE[uiKernelSize];
    if (!pKernelBuffer)
    {
        kernelFile.Close();
        nRetCode = -2;
        goto Exit_ReadBatteryParam;
    }
    uiRead = kernelFile.ReadHuge(pKernelBuffer,uiKernelSize);
    kernelFile.Close();
    if (uiRead!=uiKernelSize)
    {
        nRetCode = -3;
        goto Exit_ReadBatteryParam;
    }
    bRet = CheckKernel(pKernelBuffer);
    if (!bRet)
    {
        nRetCode = -4;
        goto Exit_ReadBatteryParam;
    }
    //2.search param tag
    int iPos;
    iPos = SearchTag(pKernelBuffer);
    if (iPos==-1)
    {
        nRetCode = -5;
        goto Exit_ReadBatteryParam;
    }
    memcpy(&pBatteryParam->dwCell,pKernelBuffer+iPos,sizeof(STRUCT_BATTERY_PARAM)-12);
Exit_ReadBatteryParam:
    if (pKernelBuffer)
    {
        delete pKernelBuffer;
		pKernelBuffer = NULL;
    }
	return nRetCode;
}
Ejemplo n.º 2
0
BOOL CWindowsMetaFile::GetMetaFile(LPCSTR pszFileName, ALDUS_WMF_HEADER* pAldusHeader, METAHEADER* pHeader, HMETAFILE* phMetaFile)
{
	BOOL fSuccess = FALSE;
	
	ASSERT(pszFileName != NULL);
	
	// Create the file we will be using.

	CFile File;

	// Attempt to open the file.

	if (File.Open(pszFileName, CFile::modeRead | CFile::shareDenyWrite))
	{
		ALDUS_WMF_HEADER AldusHeader;

		// Read the Aldus header.

		if (File.Read(&AldusHeader, sizeof(AldusHeader)) == sizeof(AldusHeader))
		{
			// Save the Aldus header if the user has requested it.
					
			if (pAldusHeader != NULL)
			{
				*pAldusHeader = AldusHeader;
			}
						
			// Validate the header.

			if (AldusHeader.key == ALDUS_WMF_KEY)
			{
				// If the user has only requested the Aldus header, stop and return TRUE.
				
				fSuccess = (pHeader == NULL) && (phMetaFile == NULL);
				
				if (!fSuccess)
				{
					// Read the metafile header.
	
					METAHEADER Header;
	
					DWORD dwPosition = File.GetPosition();
	
					if (File.Read(&Header, sizeof(Header)) == sizeof(Header))
					{
						// Save the metafile header if the user has requested it.
							
						if (pHeader != NULL)
						{
							*pHeader = Header;
						}
						
						// If the user does not want a metafile handle, stop and return TRUE.
						
						fSuccess = phMetaFile == NULL;
								
						if (!fSuccess)
						{
							*phMetaFile = NULL;
								
							// Get the size from the metafile header.
							
							DWORD dwSize = Header.mtSize*2;
							
							if (dwSize != 0)
							{
								// Allocate the memory to hold the metafile data.
									
								HGLOBAL hMetaFileData;
									
								hMetaFileData = GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, dwSize);
									
								if (hMetaFileData != NULL)
								{
									LPVOID pMetaFileData;
										
									pMetaFileData = GlobalLock(hMetaFileData);
										
									if (pMetaFileData != NULL)
									{
										// Seek back to the start of the metafile header.
										
										File.Seek(dwPosition, CFile::begin);

										// Read the metafile data.
										
										if (File.ReadHuge(pMetaFileData, dwSize) == dwSize)
										{
											// We have now read the data, make a metafile from it.
	
#ifdef WIN32
											*phMetaFile = ::SetMetaFileBitsEx(dwSize, (LPBYTE)pMetaFileData);
#else
											*phMetaFile = ::SetMetaFileBits(hMetaFileData);
#endif
											fSuccess = *phMetaFile != NULL;
										}

#ifdef WIN32
										GlobalUnlock(hMetaFileData);
										pMetaFileData = NULL;
#else
										if (*phMetaFile == NULL)
										{
											GlobalUnlock(hMetaFileData);
											pMetaFileData = NULL;
										}
#endif
									}
#ifdef WIN32
									GlobalFree(hMetaFileData);
									hMetaFileData = NULL;
#else
									if (*phMetaFile == NULL)
									{
										GlobalFree(hMetaFileData);
										hMetaFileData = NULL;
									}
#endif
								}
							}
						}
					}
				}
			}
		}
	}

	return fSuccess;
}
//bStopWord=true 去掉停用词
//bTag=true      结果文件中包含词性标记
int CWordSegment::SegmentFile(const char *source, const char *target, BOOL bStopWord, BOOL bTag)
{
	CFile fin;
	FILE *fout;
	if(!fin.Open(source,CFile::modeRead))
		return -1;
	if((fout=fopen(target,"w+"))==NULL)
	{
		fin.Close();
		return -1;
	}
	//读入文件的内容
	int num=0;
	unsigned int flen=fin.GetLength();
	char *buffer=new char[flen+1];
	flen=fin.ReadHuge(buffer,flen);
	buffer[flen]='\0';
	fin.Close();


	int i,j,sum;
	char *w;
	//realcnt为文章中去掉停用词后剩下的总共词数
	//nStart为一个句子在buffer中的开始位置
	int nStart=0,nNewStart=0;
	bool flag=true;
	int nSentenceLen=0;
	int realcnt=0;
	while(buffer[nStart]!='\0')
	{
		flag=true;
		nSentenceLen=ParseFile(buffer,nStart,nNewStart);
		nStart=nNewStart;
		if(nSentenceLen==0) continue;
		if(m_pSentence[0]>0) //如果是一个英文单词
		{
			//如果英文单词的长度大于等于2,且不是数字
			if((nSentenceLen>=2)&&((m_pSentence[0]<'0')||(m_pSentence[0]>'9')))
			{
				fprintf(fout,"%s\n",m_pSentence);
				realcnt++;
			}
		}
		else //如果是汉字串
		{
			if(nSentenceLen%2!=0) continue;
			if(nSentenceLen<=6) //如果是单个汉字
			{
				fprintf(fout,"%s\n",m_pSentence);;
				realcnt++;
			}
			else
			{
				g_wordSeg.Segment(m_pSentence);
				for(i=0;i<g_wordSeg.GetSegmentCount();i++)
				{
					sum=0;
					PWORD_RESULT pItem=g_wordSeg.GetWordSeg(i);
					while(pItem[sum].sWord[0]!=0) sum++;
				
					for(j=1;j<sum-1;j++)
					{
						w=pItem[j].sWord;
						if(w[0]=='\0'||(bStopWord&&g_wordSeg.isInStopWords(w))) flag=false;		
						if(flag)
						{
							fprintf(fout,"%s\n",w);
							realcnt++;
						}
					}
				}
			}
		}
	}
	fclose(fout);
	delete[] buffer;
	return realcnt;
}