diff options
Diffstat (limited to 'tools/SearchReplace')
-rw-r--r-- | tools/SearchReplace/SearchReplace.cpp | 336 | ||||
-rw-r--r-- | tools/SearchReplace/SearchReplace.dsp | 97 | ||||
-rw-r--r-- | tools/SearchReplace/SearchReplace.dsw | 32 |
3 files changed, 465 insertions, 0 deletions
diff --git a/tools/SearchReplace/SearchReplace.cpp b/tools/SearchReplace/SearchReplace.cpp new file mode 100644 index 0000000..f2fe3a9 --- /dev/null +++ b/tools/SearchReplace/SearchReplace.cpp @@ -0,0 +1,336 @@ +// SearchReplace.cpp : Defines the entry point for the console application. +// + +#include <assert.h> +#include <vector> + +class ReplacementTableEntry; + +std::vector<ReplacementTableEntry*> gReplacementTable; + +bool BuildReplacementTable( FILE* rulesFile ); +void RemoveNewLine( char* string ); +bool IsWhiteSpace( char* string ); +void ProcessSourceFile( FILE* sourceFile, FILE* targetFile ); + + + +class ReplacementTableEntry +{ + public: + + ReplacementTableEntry( const char* findString, const char* replaceString ); + ~ReplacementTableEntry(); + + char* mFindString; + char* mReplaceString; +}; + + +ReplacementTableEntry::ReplacementTableEntry +( + const char* findString, + const char* replaceString +) +: +mFindString( 0 ), +mReplaceString( 0 ) +{ + assert( findString ); + assert( replaceString ); + + mFindString = new char[strlen(findString)+1]; + strcpy( mFindString, findString ); + + mReplaceString = new char[strlen(replaceString)+1]; + strcpy( mReplaceString, replaceString ); +} + +ReplacementTableEntry::~ReplacementTableEntry() +{ + delete [] mFindString; + delete [] mReplaceString; +} + + + +//============================================================================== +// int main +//============================================================================== +// +// Description: +// +// Parameters: +// +// Return: +// +//============================================================================== +int main(int argc, char* argv[]) +{ + // Validate command line arguments + assert( argc == 4 ); + + const char* rulesName = argv[1]; + const char* sourceName = argv[2]; + const char* targetName = argv[3]; + + // Open rules file + FILE* rulesFile = fopen( rulesName, "rt" ); + + if( rulesFile == NULL ) + { + printf( "Failed to open rules file: %s\n", rulesName ); + return( 0 ); + } + + // Build search and replace table + bool result = BuildReplacementTable( rulesFile ); + + // Close rules file + fclose( rulesFile ); + + + // Open source file + FILE* sourceFile = fopen( sourceName, "rt" ); + + if( sourceFile == NULL ) + { + fclose( rulesFile ); + printf( "Failed to open source file: %s\n", sourceName ); + return( 0 ); + } + + // Open target file + FILE* targetFile = fopen( targetName, "wt" ); + + if( targetFile == NULL ) + { + fclose( rulesFile ); + fclose( sourceFile ); + printf( "Failed to open target file: %s\n", targetName ); + return( 0 ); + } + + // Process source file + + ProcessSourceFile( sourceFile, targetFile ); + + fclose( sourceFile ); + fclose( targetFile ); + + + // Clean up + std::vector<ReplacementTableEntry*>::const_iterator iter = gReplacementTable.begin(); + + for( ; iter != gReplacementTable.end(); ++iter ) + { + ReplacementTableEntry* entry = (*iter); + + delete entry; + } + + gReplacementTable.clear(); + + return 0; +} + + +//============================================================================== +// bool BuildReplacementTable +//============================================================================== +// +// Description: +// +// Parameters: +// +// Return: +// +//============================================================================== +bool BuildReplacementTable( FILE* rulesFile ) +{ + // Get the next line from the file + + int lineCount = 0; + + while( 1 ) + { + const int BUFFER_SIZE = 512; + char* fgetsResult; + char lineBuffer[BUFFER_SIZE]; + + do + { if( feof( rulesFile ) ) + { + return( true ); + } + + fgetsResult = fgets( lineBuffer, BUFFER_SIZE, rulesFile ); + ++lineCount; + } + while( IsWhiteSpace( lineBuffer ) == true ); + + char findString[BUFFER_SIZE]; + strcpy( findString, lineBuffer ); + RemoveNewLine( findString ); + + + do + { + if( feof( rulesFile ) ) + { + printf( "Error in rules file, missing replace string (line: %d)\n", lineCount ); + return( false ); + } + + fgetsResult = fgets( lineBuffer, BUFFER_SIZE, rulesFile ); + ++lineCount; + } + while( IsWhiteSpace( lineBuffer ) == true ); + + char replaceString[BUFFER_SIZE]; + strcpy( replaceString, lineBuffer ); + RemoveNewLine( replaceString ); + + // Create Entry + // + ReplacementTableEntry* entry = new ReplacementTableEntry( findString, replaceString ); + gReplacementTable.push_back( entry ); + } +} + + +//============================================================================== +// void RemoveNewLine +//============================================================================== +// +// Description: +// +// Parameters: +// +// Return: +// +//============================================================================== +void RemoveNewLine( char* string ) +{ + assert( string ); + + int length = strlen( string ); + + int i; + for( i = 0; i < length; ++i ) + { + if( '\n' == string[i] ) + { + string[i] = '\0'; + } + } +} + + +//============================================================================== +// bool IsWhiteSpace +//============================================================================== +// +// Description: +// +// Parameters: +// +// Return: +// +//============================================================================== +bool IsWhiteSpace( char* string ) +{ + assert( string ); + + int length = strlen( string ); + + int i; + for( i = 0; i < length; ++i ) + { + if( (string[i] != '\n') && (string[i] != ' ') ) + { + return( false ); + } + } + + return( true ); +} + + +//============================================================================== +// void ProcessSourceFile +//============================================================================== +// +// Description: +// +// Parameters: +// +// Return: +// +//============================================================================== +void ProcessSourceFile( FILE* sourceFile, FILE* targetFile ) +{ + int lineNumber = 0; + while( !feof( sourceFile ) ) + { + // Read in a line from the source + const int BUFFER_SIZE = 1024; + char* fgetsResult; + char sourceBuffer[BUFFER_SIZE]; + char targetBuffer[BUFFER_SIZE]; + + fgetsResult = fgets( sourceBuffer, BUFFER_SIZE, sourceFile ); + strcpy( targetBuffer, sourceBuffer ); + ++lineNumber; + + // Iterate through the find strings for a match + std::vector<ReplacementTableEntry*>::const_iterator iter = gReplacementTable.begin(); + + for( ; iter != gReplacementTable.end(); ++iter ) + { + ReplacementTableEntry* entry = (*iter); + + char* start = sourceBuffer; + char* targetPos = targetBuffer; + char* found = NULL; + + do + { + found = strstr( start, entry->mFindString ); + + // Replace if found + if( found != NULL ) + { + // Copy over the prefix + memcpy(targetPos, start, found - start ); + targetPos += found - start; + + // Copy over the replacement string + memcpy(targetPos, entry->mReplaceString, strlen( entry->mReplaceString ) ); + targetPos += strlen( entry->mReplaceString ); + start = found + strlen( entry->mFindString ); + + // Copy over the postfix + memcpy( targetPos, start, strlen(start) ); + + // NULL terminate + char* term = targetPos + strlen(start); + *term = '\0'; + } + } + while( found != NULL ); + + // The target now becomes the source to preserve any replacements. + strcpy( sourceBuffer, targetBuffer ); + } + + // Write out line to target + fputs( targetBuffer, targetFile ); + } + + + + + + +}
\ No newline at end of file diff --git a/tools/SearchReplace/SearchReplace.dsp b/tools/SearchReplace/SearchReplace.dsp new file mode 100644 index 0000000..6e15a4e --- /dev/null +++ b/tools/SearchReplace/SearchReplace.dsp @@ -0,0 +1,97 @@ +# Microsoft Developer Studio Project File - Name="SearchReplace" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 60000 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=SearchReplace - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "SearchReplace.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "SearchReplace.mak" CFG="SearchReplace - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "SearchReplace - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "SearchReplace - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "SearchReplace - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "RAD_WIN32" /D "RAD_RELEASE" /Yu"stdafx.h" /FD /c +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "SearchReplace - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\..\game\libs\radcore\inc" /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "RAD_WIN32" /D "RAD_DEBUG" /FD /GZ /c +# SUBTRACT CPP /YX /Yc /Yu +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "SearchReplace - Win32 Release" +# Name "SearchReplace - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\SearchReplace.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# End Group +# End Target +# End Project diff --git a/tools/SearchReplace/SearchReplace.dsw b/tools/SearchReplace/SearchReplace.dsw new file mode 100644 index 0000000..d28da6e --- /dev/null +++ b/tools/SearchReplace/SearchReplace.dsw @@ -0,0 +1,32 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "SearchReplace"=.\SearchReplace.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ + Begin Project Dependency + Project_Dep_Name radcore + End Project Dependency +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + |