diff --git a/zsnes/src/Makefile.in b/zsnes/src/Makefile.in index 0f4d381b..27c0a521 100644 --- a/zsnes/src/Makefile.in +++ b/zsnes/src/Makefile.in @@ -110,7 +110,7 @@ ${PSR}: parsegen.cpp ALL: rm -f version.o -tools: minwhite extraext sec-test srccount +tools: minwhite extraext sec-test srccount nreplace minwhite: ${TOOLSOBJ} @CXX@ @CFLAGS@ -o ${TOOLSDIR}/minwhite ${TOOLSDIR}/minwhite.cpp ${TOOLSDIR}/fileutil.o @@ -124,6 +124,9 @@ sec-test: ${TOOLSOBJ} srccount: ${TOOLSOBJ} @CXX@ @CFLAGS@ -o ${TOOLSDIR}/srccount ${TOOLSDIR}/srccount.cpp ${TOOLSDIR}/fileutil.o +nreplace: ${TOOLSOBJ} + @CXX@ @CFLAGS@ -o ${TOOLSDIR}/nreplace ${TOOLSDIR}/nreplace.cpp ${TOOLSDIR}/fileutil.o + cfgload.o: cfgload.c macros.mac cfgparse.o: cfgparse.psr endmem.o: endmem.asm macros.mac diff --git a/zsnes/src/tools/compile.txt b/zsnes/src/tools/compile.txt index b03b652b..fbbafed0 100644 --- a/zsnes/src/tools/compile.txt +++ b/zsnes/src/tools/compile.txt @@ -19,8 +19,14 @@ Source Counter: g++ -Wall -O3 -o srccount.exe srccount.cpp fileutil.o +Nach's Replacer: +g++ -Wall -O3 -o nreplace.exe nreplace.cpp fileutil.o + All the tools scan every compatible file they find from the directory they are in. It also scans all sub directories recursively. Extra EXTSYMs accepts command line arguments. You can specify filenames -to put on the can't be opened ignore list. \ No newline at end of file +to put on the can't be opened ignore list. + +Nach's Replacer accept parameters for various files, and -r for +recursive directory support. diff --git a/zsnes/src/tools/nreplace.cpp b/zsnes/src/tools/nreplace.cpp new file mode 100644 index 00000000..966e1d44 --- /dev/null +++ b/zsnes/src/tools/nreplace.cpp @@ -0,0 +1,163 @@ +/* +Copyright (C) 2005 Nach, grinvader ( http://www.zsnes.com ) + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* +This is part of a toolkit used to assist in ZSNES development + +This program is able to replace one list of data with another in multiple files. +Multiline replace is of course supported. +*/ + +#include +#include +#include +using namespace std; + +#include "fileutil.h" + +string SearchText, ReplaceText; + +void help() +{ + cout << "Usage: nreplace [-r] ...\n" << endl; + exit(1); +} + +void readin_file(istream& stream, string& buffer) +{ + char byte; + for (;;) + { + stream.get(byte); + if (!stream.eof()) + { + buffer += byte; + } + else + { + break; + } + } +} + +void handle_file(const char *filename, struct stat& stat_buffer) +{ + fstream ModifyFile(filename, ios::in | ios::out); + if (ModifyFile) + { + string ModifyText; + readin_file(ModifyFile, ModifyText); + + bool changed = false; + + for (size_t start_pos = 0;;) + { + size_t match_point = ModifyText.find(SearchText, start_pos); + if (match_point != string::npos) + { + ModifyText.replace(match_point, SearchText.size(), ReplaceText); + start_pos += ReplaceText.size(); + changed = true; + } + else + { + break; + } + } + + if (changed) + { + ModifyFile.clear(); + ModifyFile.seekp(0, ios::beg); + ModifyFile.write(ModifyText.data(), ModifyText.size()); + truncate(filename, ModifyText.size()); + } + + ModifyFile.close(); + } + else + { + cout << "Could not open " << filename << endl; + } +} + +int main(size_t argc, const char **argv) +{ + bool subdir_scan = false; + const char **argp = argv+1; + + if (*argp && !strcmp(*argp, "-r")) + { + if (argc < 5) { help(); } + + subdir_scan = true; + argp++; + } + else if (argc < 4) + { + help(); + } + + + ifstream SearchFile(*argp, ios::in); + if (SearchFile) + { + argp++; + } + else + { + cout << "Could not open " << *argp << endl; + return(2); + } + + ifstream ReplaceFile(*argp, ios::in); + if (ReplaceFile) + { + argp++; + } + else + { + cout << "Could not open " << *argp << endl; + return(2); + } + + readin_file(SearchFile, SearchText); SearchFile.close(); + readin_file(ReplaceFile, ReplaceText); ReplaceFile.close(); + + if (subdir_scan) + { + for (; **argp; argp++) + { + if (!parse_path(*argp, handle_file)) + { + cout << "Could not open " << *argp << endl; + } + } + } + else + { + struct stat stat_buffer; //Not used + for (; *argp; argp++) + { + handle_file(*argp, stat_buffer); + } + } + + return(0); +}