00001 /*****************************************************************************\ 00002 * * 00003 * Name : insert_before * 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 finding the second argument and inserting"); 00031 out.log("the third argument just before it. This is only done once."); 00032 out.log("For example:"); 00033 out.log(" replace_text c:/turnips.txt turnip rutabega"); 00034 out.log("This will insert the word 'rutabega' just before the word"); 00035 out.log("'turnip' in a file named 'c:/turnips.txt'."); 00036 return 3; 00037 } 00038 00039 istring editing_filename(argv[1]); 00040 00041 byte_filer edit_file(editing_filename, "rb"); 00042 if (!edit_file.good()) { 00043 out.log(istring("Could not find the logfile called ") + editing_filename); 00044 return 4; 00045 } 00046 int file_len = int(edit_file.length()); 00047 istring total_file; 00048 edit_file.read(total_file, file_len); 00049 edit_file.close(); 00050 00051 const istring to_seek = argv[2]; 00052 istring insertion_chunk = argv[3]; 00053 insertion_chunk += log_base::platform_ending(); 00054 00055 int last_indy = 0; 00056 00057 bool found = false; 00058 while (!found) { 00059 // look for the item that needs to be whacked. 00060 int indy = total_file.find(to_seek, last_indy); 00061 if (negative(indy)) break; // not found. 00063 found = true; 00064 total_file.insert(indy, insertion_chunk); 00065 } 00066 if (!found) { 00067 out.log(istring("Could not find the text \"") + to_seek + "\""); 00068 return 34; 00069 } 00070 00071 byte_filer new_file(editing_filename, "wb"); 00072 if (!new_file.good()) { 00073 out.log(istring("Could not re-open the file called ") + editing_filename); 00074 return 4; 00075 } 00076 new_file.write(total_file); 00077 new_file.close(); 00078 00079 return 0; 00080 } 00081 00082 #ifdef __BUILD_STATIC_APPLICATION__ 00083 // static dependencies found by buildor_gen_deps.sh: 00084 #include <basis/array.cpp> 00085 #include <basis/byte_array.cpp> 00086 #include <basis/callstack_tracker.cpp> 00087 #include <basis/chaos.cpp> 00088 #include <basis/convert_utf.cpp> 00089 #include <basis/definitions.cpp> 00090 #include <basis/earth_time.cpp> 00091 #include <basis/guards.cpp> 00092 #include <basis/istring.cpp> 00093 #include <basis/log_base.cpp> 00094 #include <basis/memory_checker.cpp> 00095 #include <basis/mutex.cpp> 00096 #include <basis/object_base.cpp> 00097 #include <basis/outcome.cpp> 00098 #include <basis/packable.cpp> 00099 #include <basis/portable.cpp> 00100 #include <basis/sequence.cpp> 00101 #include <basis/set.cpp> 00102 #include <basis/utility.cpp> 00103 #include <basis/version_record.cpp> 00104 #include <data_struct/amorph.cpp> 00105 #include <data_struct/bit_vector.cpp> 00106 #include <data_struct/byte_hasher.cpp> 00107 #include <data_struct/configurator.cpp> 00108 #include <data_struct/hash_table.cpp> 00109 #include <data_struct/pointer_hash.cpp> 00110 #include <data_struct/stack.cpp> 00111 #include <data_struct/static_memory_gremlin.cpp> 00112 #include <data_struct/string_hash.cpp> 00113 #include <data_struct/string_hasher.cpp> 00114 #include <data_struct/string_table.cpp> 00115 #include <data_struct/symbol_table.cpp> 00116 #include <data_struct/table_configurator.cpp> 00117 #include <loggers/console_logger.cpp> 00118 #include <loggers/file_logger.cpp> 00119 #include <loggers/locked_logger.cpp> 00120 #include <loggers/null_logger.cpp> 00121 #include <loggers/program_wide_logger.cpp> 00122 #include <opsystem/byte_filer.cpp> 00123 #include <opsystem/command_line.cpp> 00124 #include <opsystem/critical_events.cpp> 00125 #include <opsystem/directory.cpp> 00126 #include <opsystem/filename.cpp> 00127 #include <opsystem/ini_config.cpp> 00128 #include <opsystem/ini_parser.cpp> 00129 #include <opsystem/path_configuration.cpp> 00130 #include <opsystem/rendezvous.cpp> 00131 #include <textual/byte_format.cpp> 00132 #include <textual/parser_bits.cpp> 00133 #include <textual/string_manipulation.cpp> 00134 #include <textual/tokenizer.cpp> 00135 #endif // __BUILD_STATIC_APPLICATION__ 00136
1.5.1