00001 /*****************************************************************************\ 00002 * * 00003 * Name : replace_text * 00004 * Author : Chris Koeritz * 00005 * * 00006 ******************************************************************************* 00007 * Copyright (c) 2004-$now By Author. This program is free software; you can * 00008 * redistribute it and/or modify it under the terms of the GNU General Public * 00009 * License as published by the Free Software Foundation; either version 2 of * 00010 * the License or (at your option) any later version. This is online at: * 00011 * http://www.fsf.org/copyleft/gpl.html * 00012 * Please send any updates to: fred@gruntose.com * 00013 \*****************************************************************************/ 00014 00015 #include <basis/function.h> 00016 #include <basis/istring.h> 00017 #include <opsystem/byte_filer.h> 00018 #include <loggers/console_logger.h> 00019 #include <data_struct/static_memory_gremlin.h> 00020 #include <textual/parser_bits.h> 00021 00022 HOOPLE_STARTUP_CODE; 00023 00024 console_logger out; 00025 00026 int main(int argc, char *argv[]) 00027 { 00028 if (argc < 4) { 00029 out.log("This program takes three arguments. The first is a file name that will be"); 00030 out.log("edited. The edit will consist of taking any occurrences of the second"); 00031 out.log("argument and replacing each one with the third argument."); 00032 out.log("For example:"); 00033 out.log(" replace_text c:/turnips.txt turnip rutabega"); 00034 out.log("This will replace all occurrences of the word 'turnip' with the word"); 00035 out.log("'rutabega' in a file named 'c:/turnips.txt'."); 00036 out.log("There is also a special trick for replacing with the quote character (\")."); 00037 out.log("Use the keyword @@QUOTE@@ and that will become the quote character."); 00038 return 3; 00039 } 00040 00041 istring replace_filename(argv[1]); 00042 00043 byte_filer edit_file(replace_filename, "rb"); 00044 if (!edit_file.good()) { 00045 out.log(istring("Could not find the logfile called ") + replace_filename); 00046 return 4; 00047 } 00048 int file_len = int(edit_file.length()); 00049 istring total_file; 00050 edit_file.read(total_file, file_len); 00051 edit_file.close(); 00052 00053 const istring to_replace = argv[2]; 00054 istring replacement_chunk = argv[3]; 00055 if (replacement_chunk.contains("@@QUOTE@@")) { 00056 const istring quote = "\""; 00057 while (replacement_chunk.replace("@@QUOTE@@", quote)); 00058 // loop until all occurrences get replaced. 00059 } 00060 00061 int last_indy = 0; 00062 00063 bool found_anything = false; 00064 while (true) { 00065 // look for the item that needs to be whacked. 00066 int indy = total_file.find(to_replace, last_indy); 00067 last_indy = indy; // update our position. 00068 if (negative(indy)) break; // not found. 00069 found_anything = true; 00070 // now whack the thing to be zapped. 00071 total_file.zap(indy, indy + to_replace.length() - 1); 00072 // now insert the replacement bit. 00073 total_file.insert(indy, replacement_chunk); 00074 last_indy = indy + replacement_chunk.length(); 00075 // add to the starting index to avoid replacing inside the replacement 00076 // text that we're plugging in. otherwise, we could have infinite 00077 // looping. 00078 } 00079 00080 if (found_anything) { 00081 byte_filer new_file(replace_filename, "wb"); 00082 if (!new_file.good()) { 00083 out.log(istring("Could not re-open the file called ") + replace_filename); 00084 return 4; 00085 } 00086 new_file.write(total_file); 00087 new_file.close(); 00088 } 00089 00090 return 0; 00091 } 00092 00093 #ifdef __BUILD_STATIC_APPLICATION__ 00094 // static dependencies found by buildor_gen_deps.sh: 00095 #include <basis/array.cpp> 00096 #include <basis/byte_array.cpp> 00097 #include <basis/callstack_tracker.cpp> 00098 #include <basis/chaos.cpp> 00099 #include <basis/convert_utf.cpp> 00100 #include <basis/definitions.cpp> 00101 #include <basis/earth_time.cpp> 00102 #include <basis/guards.cpp> 00103 #include <basis/istring.cpp> 00104 #include <basis/log_base.cpp> 00105 #include <basis/memory_checker.cpp> 00106 #include <basis/mutex.cpp> 00107 #include <basis/object_base.cpp> 00108 #include <basis/outcome.cpp> 00109 #include <basis/packable.cpp> 00110 #include <basis/portable.cpp> 00111 #include <basis/sequence.cpp> 00112 #include <basis/set.cpp> 00113 #include <basis/utility.cpp> 00114 #include <basis/version_record.cpp> 00115 #include <data_struct/amorph.cpp> 00116 #include <data_struct/bit_vector.cpp> 00117 #include <data_struct/byte_hasher.cpp> 00118 #include <data_struct/configurator.cpp> 00119 #include <data_struct/hash_table.cpp> 00120 #include <data_struct/pointer_hash.cpp> 00121 #include <data_struct/stack.cpp> 00122 #include <data_struct/static_memory_gremlin.cpp> 00123 #include <data_struct/string_hash.cpp> 00124 #include <data_struct/string_hasher.cpp> 00125 #include <data_struct/string_table.cpp> 00126 #include <data_struct/symbol_table.cpp> 00127 #include <data_struct/table_configurator.cpp> 00128 #include <loggers/console_logger.cpp> 00129 #include <loggers/file_logger.cpp> 00130 #include <loggers/locked_logger.cpp> 00131 #include <loggers/null_logger.cpp> 00132 #include <loggers/program_wide_logger.cpp> 00133 #include <opsystem/byte_filer.cpp> 00134 #include <opsystem/command_line.cpp> 00135 #include <opsystem/critical_events.cpp> 00136 #include <opsystem/directory.cpp> 00137 #include <opsystem/filename.cpp> 00138 #include <opsystem/ini_config.cpp> 00139 #include <opsystem/ini_parser.cpp> 00140 #include <opsystem/path_configuration.cpp> 00141 #include <opsystem/rendezvous.cpp> 00142 #include <textual/byte_format.cpp> 00143 #include <textual/parser_bits.cpp> 00144 #include <textual/string_manipulation.cpp> 00145 #include <textual/tokenizer.cpp> 00146 #endif // __BUILD_STATIC_APPLICATION__ 00147
1.5.1