📄 inffile.cpp
字号:
//---------------------------------------------------------------------------
// Pixelworks Inc. Company Confidential Strictly Private
//
// $Archive: /SwTools/FlashUpgrader/InfFile.cpp $
// $Revision: 1.1.1.1 $
// $Author: KevinM $
// $Date: 2003/09/29 18:19:04 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 1997 (c) Pixelworks Inc.
//
// Pixelworks owns the sole copyright to this software. Under international
// copyright laws you (1) may not make a copy of this software except for
// the purposes of maintaining a single archive copy, (2) may not derive
// works herefrom, (3) may not distribute this work to others. These rights
// are provided for information clarification, other restrictions of rights
// may apply as well.
//
// This is an unpublished work.
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>> WARRANTEE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Pixelworks Inc. MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE USE OF
// THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE.
// --------------------------------------------------------------------------
#include "stdafx.h"
#include "InfFile.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
CInfFile::CInfFile()
{
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
CInfFile::~CInfFile()
{
m_LineArray.RemoveAll();
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::OpenInfFile(LPCSTR lpszFilepath)
{
m_LineArray.RemoveAll();
CFile file;
if (FALSE == file.Open(lpszFilepath, CFile::modeRead))
{
return FALSE;
}
int nSize = (int)file.GetLength();
BYTE *pBuffer = new BYTE[nSize];
file.Read(pBuffer, nSize);
file.Close();
int nSrcLine = 1;
int nStart = 0;
for (int n = 0; n < nSize; n++)
{
if (13 == pBuffer[n])
{
pBuffer[n] = 0;
AddLine(nSrcLine, (LPCSTR)&pBuffer[nStart]);
nSrcLine++;
if (10 == pBuffer[n + 1])
{
n++;
}
nStart = n + 1;
}
else if (10 == pBuffer[n])
{
pBuffer[n] = 0;
AddLine(nSrcLine, (LPCSTR)&pBuffer[nStart]);
nSrcLine++;
nStart = n + 1;
}
}
delete pBuffer;
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::FindFirstLine(PLINECONTEXT pContext,
LPCSTR lpszSection,
LPCSTR lpszKey /* = NULL */)
{
pContext->nSection = 0;
pContext->nLine = 0;
if (FALSE == FindSectionStartLine(lpszSection, pContext->nSection))
{
return FALSE;
}
if (lpszKey)
{
return FindNextMatchLine(pContext, lpszKey);
}
else
{
pContext->nLine = 1;
}
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::FindNextLine(PLINECONTEXT pContext)
{
LINECONTEXT lineContext;
lineContext.nSection = pContext->nSection;
lineContext.nLine = pContext->nLine + 1;
CString strSectionName;
if (FALSE == GetStringField(&lineContext, INF_FILE_KEY_FIELD, strSectionName))
{
return FALSE; // start of next section found
}
pContext->nLine = lineContext.nLine;
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::FindNextMatchLine(PLINECONTEXT pContext, LPCSTR lpszKey)
{
BOOL bStatus = FALSE;
LINECONTEXT lineContext;
lineContext.nSection = pContext->nSection;
lineContext.nLine = pContext->nLine + 1;
CString strKeyField;
while(GetStringField(&lineContext, INF_FILE_KEY_FIELD, strKeyField))
{
if (0 == strKeyField.CompareNoCase(lpszKey))
{
pContext->nLine = lineContext.nLine;
bStatus = TRUE;
break;
}
lineContext.nLine++;
}
return bStatus;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::FindNextSection(PLINECONTEXT pContext, CString& ref_strName)
{
BOOL bStatus = FALSE;
LINECONTEXT lineContext;
lineContext.nSection = pContext->nSection;
lineContext.nLine = 1;
CString strLine;
while (GetLineText(&lineContext, strLine))
{
if (ExtractSectionName(strLine, ref_strName))
{
pContext->nSection = lineContext.nLine;
pContext->nLine = 1;
bStatus = TRUE;
break;
}
lineContext.nLine++;
}
return bStatus;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetLineText(PLINECONTEXT pContext, CString& ref_strLine)
{
return GetRawLineText(pContext->nSection + pContext->nLine, ref_strLine);
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetLineText(LPCSTR lpszSection, LPCSTR lpszKey,
CString& ref_strText)
{
LINECONTEXT lineContext;
if (FALSE == FindFirstLine(&lineContext, lpszSection, lpszKey))
{
return FALSE;
}
ref_strText.Empty();
CString strField;
int nFieldIndex = INF_FILE_KEY_FIELD + 1;
while (GetStringField(&lineContext, nFieldIndex, strField))
{
if (FALSE == ref_strText.IsEmpty())
{
ref_strText += ",";
}
ref_strText += strField;
nFieldIndex++;
}
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetStringField(PLINECONTEXT pContext, int nFieldIndex,
CString& ref_strText)
{
if (FALSE == GetRawLineText(pContext->nSection + pContext->nLine, ref_strText))
{
return FALSE;
}
if ('[' == ref_strText.GetAt(0))
{
ref_strText.Empty(); // Section name found (not a key line).
return FALSE;
}
int nIndex = ref_strText.Find('=');
if (-1 == nIndex)
{
ref_strText.Empty();
return FALSE;
}
if (0 == nFieldIndex)
{
ref_strText = ref_strText.Left(nIndex);
}
else
{
for (int i = 0; i < nFieldIndex; i++)
{
if (-1 == nIndex)
{
ref_strText.Empty();
return FALSE;
}
ref_strText = ref_strText.Right(ref_strText.GetLength() - nIndex - 1);
nIndex = ref_strText.Find(',');
}
if (-1 != nIndex)
{
ref_strText = ref_strText.Left(nIndex);
}
}
if (ref_strText.IsEmpty())
{
return TRUE;
}
ref_strText.TrimLeft();
ref_strText.TrimRight();
// Remove any leading and trailing double quotes.
if ('"' == ref_strText.GetAt(0))
{
ref_strText = ref_strText.Right(ref_strText.GetLength() - 1);
}
if ('"' == ref_strText.GetAt(ref_strText.GetLength() - 1))
{
ref_strText = ref_strText.Left(ref_strText.GetLength() - 1);
}
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetIntField(PLINECONTEXT pContext, int nFieldIndex,
int& ref_nValue)
{
CString strValue;
if (FALSE == GetStringField(pContext, nFieldIndex, strValue))
{
return FALSE;
}
// Try to evaluate as a decimal value first.
int nFields = sscanf(strValue, "%d", &ref_nValue);
if (1 != nFields || 0 == ref_nValue)
{
// Try to evaluate as a hexadecimal value (0xXXXXXXXX).
nFields = sscanf(strValue, "%x", &ref_nValue);
}
return (1 == nFields);
}
//****************************************************************************
// Start of private method implementation
//****************************************************************************
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetSourceLineNum(PLINECONTEXT pContext, int& ref_nSrcLine)
{
int nLine = pContext->nSection + pContext->nLine;
if (nLine >= m_LineArray.GetSize())
{
return FALSE;
}
LineItem& lnItem = m_LineArray.ElementAt(nLine);
ref_nSrcLine = lnItem.nSourceLine;
ASSERT(ref_nSrcLine);
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::GetRawLineText(int nLine, CString& ref_strLine)
{
if (nLine >= m_LineArray.GetSize())
{
return FALSE;
}
LineItem& lnItem = m_LineArray.ElementAt(nLine);
ref_strLine = lnItem.strText;
ASSERT(FALSE == ref_strLine.IsEmpty());
return TRUE;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::ExtractSectionName(CString strLine, CString& ref_strName)
{
if (strLine.IsEmpty())
{
return FALSE;
}
BOOL bStatus = FALSE;
if ('[' == strLine.GetAt(0))
{
ref_strName = strLine.Right(strLine.GetLength() - 1);
int nIndex = ref_strName.Find(']');
if (-1 != nIndex)
{
ref_strName = ref_strName.Left(nIndex);
bStatus = TRUE;
}
else
{
ref_strName.Empty();
}
}
return bStatus;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
BOOL CInfFile::FindSectionStartLine(LPCSTR lpszSection, int& ref_nLine)
{
BOOL bStatus = FALSE;
ref_nLine = 0;
int nLines = m_LineArray.GetSize();
CString strLine;
CString strSection;
for (int i = 0; i < nLines; i++)
{
if (GetRawLineText(i, strLine))
{
if (ExtractSectionName(strLine, strSection))
{
if (0 == strSection.CompareNoCase(lpszSection))
{
ref_nLine = i;
bStatus = TRUE;
break;
}
}
}
}
return bStatus;
}
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
void CInfFile::AddLine(int nSrcLine, LPCSTR lpszText)
{
LineItem lnItem;
lnItem.nSourceLine = nSrcLine;
lnItem.strText = lpszText;
lnItem.strText.TrimLeft();
lnItem.strText.TrimRight();
if (lnItem.strText.IsEmpty())
{
return;
}
if (';' == lnItem.strText.GetAt(0))
{
return;
}
m_LineArray.Add(lnItem);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -