|
/************************************************************************
*
* Resource ID Organiser Core Library
*
* (c) Copyright 2000-2004 by Anna-Jayne Metcalfe (resorg@annasplace.me.uk)
* All rights reserved.
*
************************************************************************
*
* Filename : ResourceSymbolFileBuffer.cpp
*
* Description : CResourceSymbolFileBuffer - string buffer class for parsing
* manipulating and writing a resource symbol file
*
* Compiler : Microsoft Visual C++ 6.0, Service Pack 3 or later
* Microsoft Visual C++ .NET 2002
*
* Target
* Environment : Windows 98/NT/2000/XP
*
* NOTE:
*
* This software is provided "as is" free for personal use. All
* title and copyrights in and to the software, including but not
* limited to any images, text, etc. incorporated into it, are
* owned by Anna-Jayne Metcalfe, except where acknowledged otherwise.
*
* Your may freely to use this code in your own products, PROVIDED
* this notice is not removed or modified.
*
*
* Visit http://www.annasplace.me.uk/resorg for latest updates
*
************************************************************************
*
* MODIFICATION HISTORY:
*
* This is a controlled document. See project configuration
* control tool for latest version and full version history.
*
* $Archive: /Projects/AddIns/ResOrg/ResOrgCore/ResourceSymbolFileBuffer.cpp $
* $Revision: 7 $
* $Date: 23/08/04 21:38 $
* $Author: Anna $
*
* $History: ResourceSymbolFileBuffer.cpp $
*
* ***************** Version 7 *****************
* User: Anna Date: 23/08/04 Time: 21:38
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Added support for the "_APS_NO_MFC" define sometimes found in non-MFC
* projects
*
* ***************** Version 6 *****************
* User: Anna Date: 15/04/03 Time: 20:37
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* 1. Removed unnecessary file guards (#pragma once works well enough)
* 2. Updated file banners
*
* ***************** Version 5 *****************
* User: Anna Date: 25/11/02 Time: 15:20
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Changed website address in banner
*
* ***************** Version 4 *****************
* User: Anna Date: 22/10/02 Time: 13:24
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Changed name/mail address (at last!)
*
* ***************** Version 3 *****************
* User: Andy Date: 12/06/02 Time: 22:53
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* CResourceSymbolFileBuffer::Build() now writes VC7 style resource symbol
* files (OK, so it just says "Visual C++" instead of "Developer
* Studio"!!)
*
* ***************** Version 2 *****************
* User: Andy Date: 7/06/02 Time: 17:04
* Updated in $/Projects/AddIns/ResOrg/ResOrgCore
* Renamed the ResOrgUtils module to ResOrgCore. Updated file banners
* accordingly
*
* ***************** Version 1 *****************
* User: Andy Date: 27/05/02 Time: 13:28
* Created in $/Projects/AddIns/ResOrg/ResOrgUtils
*
*
* $Nokeywords: $
*
************************************************************************/
#include "StdAfx.h"
#include <math.h>
#include "ResOrgCore_Priv.h"
#include "ResourceSymbol.h"
#include "ResourceSymbolFileBuffer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CResourceSymbolFileBuffer
#define BUFFER_GROWBY 100 // Grow file buffer by 100 lines at a time
#define BUF_SIZE 1024
#define WHITE_SPACE _T(" \t")
CResourceSymbolFileBuffer::CResourceSymbolFileBuffer(CResourceSymbolList* plistSymbols)
{
m_plistSymbols = plistSymbols;
m_sResourceFileName = _T("");
m_bNoMFC = false;
m_b3dControls = true;
m_uNextResourceValue = 100;
m_uNextControlValue = 1000;
m_uNextSymedValue = 200;
m_uNextCommandValue = 32771;
}
CResourceSymbolFileBuffer::~CResourceSymbolFileBuffer(void)
{
}
/////////////////////////////////////////////////////////////////////////////
// CResourceSymbolFileBuffer operators
/////////////////////////////////////////////////////////////////////////////
// CResourceSymbolFileBuffer Virtual Overrides
void CResourceSymbolFileBuffer::Serialize(CArchive& ar)
{
ASSERT_VALID(this);
if (ar.IsLoading())
{
// Before loading reset the value of _APS_3D_CONTROLS in case it
// isn't specified in the resource symbol file
Set3dControlsValue(FALSE);
}
CResourceSymbolFileBuffer_BASE::Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CResourceSymbolFileBuffer Operations
CResourceSymbol* CResourceSymbolFileBuffer::Add(const CString& sSymbolName,
UINT uSymbolValue)
{
if (sSymbolName == _T("_APS_NO_MFC") )
{
SetNoMFCValue(uSymbolValue);
}
if (sSymbolName == _T("_APS_3D_CONTROLS") )
{
Set3dControlsValue(uSymbolValue);
}
else if (sSymbolName == _T("_APS_NEXT_RESOURCE_VALUE") )
{
SetNextResourceValue(uSymbolValue);
}
else if (sSymbolName == _T("_APS_NEXT_CONTROL_VALUE") )
{
SetNextControlValue(uSymbolValue);
}
else if (sSymbolName == _T("_APS_NEXT_SYMED_VALUE") )
{
SetNextSymedValue(uSymbolValue);
}
else if (sSymbolName == _T("_APS_NEXT_COMMAND_VALUE") )
{
SetNextCommandValue(uSymbolValue);
}
else
{
CResourceSymbol* pSymbol = new CResourceSymbol;
pSymbol->SetName(sSymbolName);
pSymbol->SetValue(uSymbolValue);
pSymbol->SetFileName(m_sFileName);
pSymbol->SetModifiedFlag(FALSE);
if (Add(pSymbol) )
{
return pSymbol;
}
delete pSymbol;
}
return NULL;
}
BOOL CResourceSymbolFileBuffer::Add(CResourceSymbol* pSymbol)
{
m_plistSymbols->AddTail(pSymbol);
return TRUE;
}
BOOL CResourceSymbolFileBuffer::Remove(CResourceSymbol* pSymbol)
{
POSITION pos = m_plistSymbols->Find(pSymbol);
if (pos != NULL)
{
m_plistSymbols->RemoveAt(pos);
return TRUE;
}
return FALSE;
}
void CResourceSymbolFileBuffer::RemoveAll(void)
{
POSITION pos = m_plistSymbols->GetHeadPosition();
while (pos != NULL)
{
CResourceSymbol* pSymbol = m_plistSymbols->GetNext(pos);
if (pSymbol != NULL)
{
delete pSymbol;
}
}
m_plistSymbols->RemoveAll();
}
BOOL CResourceSymbolFileBuffer::SetNoMFCValue(BOOL bNoMFC)
{
if (m_bNoMFC != bNoMFC)
{
m_bNoMFC = bNoMFC;
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::Set3dControlsValue(BOOL b3dControls)
{
if (m_b3dControls != b3dControls)
{
m_b3dControls = b3dControls;
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::SetNextResourceValue(UINT uValue)
{
if ( (uValue > 0) && (uValue != IDC_STATIC) && (uValue != m_uNextResourceValue) )
{
m_uNextResourceValue = uValue;
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::SetNextCommandValue(UINT uValue)
{
if ( (uValue > 0) && (uValue != IDC_STATIC) && (uValue != m_uNextCommandValue) )
{
m_uNextCommandValue = uValue;
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::SetNextControlValue(UINT uValue)
{
if ( (uValue > 0) && (uValue != IDC_STATIC) && (uValue != m_uNextControlValue) )
{
m_uNextControlValue = uValue;
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::SetNextSymedValue(UINT uValue)
{
if ( (uValue > 0) && (uValue != IDC_STATIC) && (uValue != m_uNextSymedValue) )
{
m_uNextSymedValue = uValue;
return TRUE;
}
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// Implementation
/////////////////////////////////////////////////////////////////////////////
// File Read Implementation
/******************************************************************************
* Read from an archive
*
******************************************************************************/
BOOL CResourceSymbolFileBuffer::Read(CArchive& ar)
{
return CResourceSymbolFileBuffer_BASE::Read(ar);
}
/******************************************************************************
* Parse the given line
*
******************************************************************************/
BOOL CResourceSymbolFileBuffer::Parse(const CString& rsLine)
{
if (!rsLine.IsEmpty())
{
CString sLine = rsLine;
CString sToken = GetToken(sLine);
if (IsComment(sToken))
{
CString sUsedBy = ::After(sLine, _T("Used by") );
sUsedBy.TrimLeft();
sUsedBy.TrimRight();
if (!sUsedBy.IsEmpty())
{
m_sResourceFileName = sUsedBy;
}
}
else
{
if (IsDefine(sToken))
{
CString sSymbolName = GetToken(sLine);
CString sSymbolValue = GetToken(sLine);
UINT uSymbolValue = 0;
BOOL bReadOnly = FALSE;
// Hex symbols are assumed to be read only...
if (sSymbolValue.FindOneOf( _T("Xx") ) >= 0)
{
uSymbolValue =_tcstoul( sSymbolValue,
NULL,
16);
bReadOnly = TRUE;
}
else
{
uSymbolValue = _ttoi(sSymbolValue);
}
TRACE2(" Symbol: %s, Value: %d\n", sSymbolName, uSymbolValue);
// Create a CResourceSymbol object to hold the symbol data
if ( (!sSymbolName.IsEmpty()) && (uSymbolValue > 0) )
{
CResourceSymbol* pSymbol = Add(sSymbolName,
uSymbolValue);
if ( (NULL != pSymbol) && bReadOnly)
{
pSymbol->SetReadOnly();
}
}
}
}
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::IsComment(const CString& sToken) const
{
if (sToken.Left(2) == _T("//"))
{
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::IsDefine(const CString& sToken) const
{
if (sToken.Find( _T("#define") ) == 0)
{
return TRUE;
}
return FALSE;
}
BOOL CResourceSymbolFileBuffer::IsInteger(const CString& sToken) const
{
CString s = sToken.SpanExcluding( _T("0123456789") );
return s.IsEmpty();
}
CString CResourceSymbolFileBuffer::GetToken(CString& rsLine)
{
rsLine.TrimLeft(WHITE_SPACE);
CString sToken;
int nPos = rsLine.FindOneOf(WHITE_SPACE);
if (nPos > 0)
{
sToken = rsLine.Left(nPos);
rsLine = rsLine.Mid(nPos);
}
else
{
sToken = rsLine;
rsLine = _T("");
}
sToken.TrimLeft(WHITE_SPACE);
sToken.TrimRight(WHITE_SPACE);
return sToken;
}
/////////////////////////////////////////////////////////////////////////////
// File Write Implementation
/******************************************************************************
* Write the buffer to an archive
*
******************************************************************************/
BOOL CResourceSymbolFileBuffer::Write(CArchive& ar)
{
// Build a buffer from our data
Build();
return CResourceSymbolFileBuffer_BASE::Write(ar);
}
void CResourceSymbolFileBuffer::Add(CString sText)
{
sText.TrimLeft(_T(": ") );
sText.TrimRight(_T(":; ") );
m_arrayLines.Add(sText);
}
void CResourceSymbolFileBuffer::AddComment(const CString& sComment)
{
m_arrayLines.Add( _T("//") + sComment);
}
void CResourceSymbolFileBuffer::AddNewline(void)
{
m_arrayLines.Add( _T("") );
}
BOOL CResourceSymbolFileBuffer::Build(void)
{
m_plistSymbols->SortByValue();
m_arrayLines.RemoveAll();
AddComment( _T("{{NO_DEPENDENCIES}}") );
AddComment( _T(" Microsoft Visual C++ generated include file.") );
AddComment( _T(" Used by ") + m_sResourceFileName );
AddComment( _T("") );
AddSymbols();
AddNewline();
AddComment( _T(" Next default values for new objects") );
AddComment( _T("") );
Add( _T("#ifdef APSTUDIO_INVOKED") );
Add( _T("#ifndef APSTUDIO_READONLY_SYMBOLS") );
if (GetNoMFCValue() )
{
Add( FormatSymbolDefine(_T("_APS_NO_MFC"), GetNoMFCValue() ) );
}
if (Get3dControlsValue() )
{
Add( FormatSymbolDefine(_T("_APS_3D_CONTROLS"), Get3dControlsValue() ) );
}
Add( FormatSymbolDefine(_T("_APS_NEXT_RESOURCE_VALUE"), GetNextResourceValue() ) );
Add( FormatSymbolDefine(_T("_APS_NEXT_COMMAND_VALUE"), GetNextCommandValue() ) );
Add( FormatSymbolDefine(_T("_APS_NEXT_CONTROL_VALUE"), GetNextControlValue() ) );
Add( FormatSymbolDefine(_T("_APS_NEXT_SYMED_VALUE"), GetNextSymedValue() ) );
Add( _T("#endif") );
Add( _T("#endif") );
return TRUE;
}
BOOL CResourceSymbolFileBuffer::AddSymbols(void)
{
POSITION pos = m_plistSymbols->GetHeadPosition();
while (NULL != pos)
{
CResourceSymbol* pSymbol = m_plistSymbols->GetNext(pos);
if (NULL != pSymbol)
{
if (pSymbol->IsReadOnly())
{
Add( FormatHexSymbolDefine( pSymbol->GetName(),
pSymbol->GetValue()) );
}
else
{
Add( FormatSymbolDefine( pSymbol->GetName(),
pSymbol->GetValue()) );
}
}
}
return TRUE;
}
CString CResourceSymbolFileBuffer::FormatHexSymbolDefine( const CString& sName,
UINT uValue)
{
CString sDefine;
sDefine.Format( _T("#define %-31s0x%X"),
sName + _T(" "),
uValue);
return sDefine;
}
CString CResourceSymbolFileBuffer::FormatSymbolDefine( const CString& sName,
UINT uValue)
{
CString sDefine;
sDefine.Format( _T("#define %-32s%u"),
sName + _T(" "),
uValue);
return sDefine;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing
Darth Vader's Codpiece * for the UK Army in 1990).
* Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for
Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company,
Riverblade Ltd.
One of my personal specialities is IDE plug-in development.
ResOrg was my first attempt at a plug-in, but my day to day work is with
Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.
I love lots of things, but particularly music, photography and anything connected with history or engineering. I
despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...
I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!