splitter.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : splitter                                                          *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Takes text as input and splits the lines so that they will fit on a      *
00009 *  standard 80 column terminal.                                               *
00010 *                                                                             *
00011 *******************************************************************************
00012 * Copyright (c) 1993-$now By Author.  This program is free software; you can  *
00013 * redistribute it and/or modify it under the terms of the GNU General Public  *
00014 * License as published by the Free Software Foundation; either version 2 of   *
00015 * the License or (at your option) any later version.  This is online at:      *
00016 *     http://www.fsf.org/copyleft/gpl.html                                    *
00017 * Please send any updates to: fred@gruntose.com                               *
00018 \*****************************************************************************/
00019 
00020 #include <basis/istring.h>
00021 #include <basis/set.cpp>
00022 #include <opsystem/application_shell.h>
00023 #include <opsystem/byte_filer.h>
00024 #include <opsystem/command_line.h>
00025 #include <loggers/console_logger.h>
00026 #include <opsystem/filename.h>
00027 #include <loggers/file_logger.h>
00028 #include <data_struct/static_memory_gremlin.h>
00029 #include <textual/string_manipulation.h>
00030 
00031 #include <stdio.h>
00032 
00033 const int MAX_BUFFER = 1024;
00034 
00035 class splitter_app : public application_shell
00036 {
00037 public:
00038   splitter_app() : application_shell(static_class_name()) {}
00039 
00040   IMPLEMENT_CLASS_NAME("splitter_app");
00041 
00042   virtual int execute();
00043 
00044   int print_instructions();
00045 
00046 private:
00047 };
00048 
00050 
00051 int splitter_app::print_instructions()
00052 {
00053   istring name = filename(__argv[0]).basename().raw();
00054   log(isprintf("%s usage:", name.s()));
00055   log("");
00056   log(isprintf("\
00057 This program splits long lines in input files into a more reasonable size.\n\
00058 Any filenames on the command line are split and sent to standard output.\n\
00059 The following options change how the splitting is performed:\n\
00060    --help or -?\tShow this help information.\n\
00061    --mincol N\tMinimum column to use for output.\n\
00062    --maxcol N\tMinimum column to use for output.\n\
00063 "));
00064   return -3;
00065 }
00066 
00067 int splitter_app::execute()
00068 {
00069   command_line cmds(__argc, __argv);  // parse the command line up.
00070 
00071   // retrieve any specific flags first.
00072   istring temp;
00073   int min_col = 0;
00074   if (cmds.get_value("mincol", temp))
00075     min_col = temp.convert(min_col);
00076   int max_col = 77;
00077   if (cmds.get_value("maxcol", temp))
00078     max_col = temp.convert(max_col);
00079   // look for help command.
00080   int junk_index = 0;
00081   if (cmds.find("help", junk_index, false)
00082       || cmds.find('h', junk_index, false)
00083       || cmds.find("?", junk_index, false)
00084       || cmds.find('?', junk_index, false) ) {
00085     print_instructions();
00086     return 0;
00087   }
00088 
00089   // gather extra input files.
00090   string_set input_files;
00091   for (int i = 0; i < cmds.entries(); i++) {
00092     const command_parameter &curr = cmds.get(i);
00093     if (curr.type() == command_parameter::VALUE) {
00094 //log(istring("adding input file:") + curr.text());
00095       input_files += curr.text();
00096     }
00097   }
00098 
00099   istring accumulator;
00100   for (int q = 0; q < input_files.length(); q++) {
00101     byte_filer current(input_files[q], "r");
00102     if (!current.good()) continue;
00103     while (!current.eof()) {
00104       istring line_read;
00105       int num_chars = current.getline(line_read, MAX_BUFFER);
00106       if (!num_chars) continue;
00107 //printf("line len=%d, cont=%s\n", line_read.length(), line_read.s());
00108       accumulator += line_read;
00110     }
00111   }
00112 
00113   // now get from standard input if there weren't any files specified.
00114   if (!input_files.length()) {
00115     char input_line[MAX_BUFFER + 2];
00116     while (!feof(stdin)) {
00117       char *got = fgets(input_line, MAX_BUFFER, stdin);
00118       if (!got) break;
00119 //printf("line=%s\n", got);
00120       accumulator += got;
00122     }
00123   }
00124 //printf("splitting accum with %d chars...\n", accumulator.length());
00125   istring chewed;
00126   string_manipulation::split_lines(accumulator, chewed, min_col, max_col);
00127 //printf("chewed string now has %d chars...\n", chewed.length());
00128   printf(chewed.s());
00129   return 0;
00130 }
00131 
00133 
00134 HOOPLE_MAIN(splitter_app, )
00135 
00136 #ifdef __BUILD_STATIC_APPLICATION__
00137   // static dependencies found by buildor_gen_deps.sh:
00138   #include <basis/array.cpp>
00139   #include <basis/byte_array.cpp>
00140   #include <basis/callstack_tracker.cpp>
00141   #include <basis/chaos.cpp>
00142   #include <basis/convert_utf.cpp>
00143   #include <basis/definitions.cpp>
00144   #include <basis/earth_time.cpp>
00145   #include <basis/guards.cpp>
00146   #include <basis/istring.cpp>
00147   #include <basis/log_base.cpp>
00148   #include <basis/memory_checker.cpp>
00149   #include <basis/mutex.cpp>
00150   #include <basis/object_base.cpp>
00151   #include <basis/outcome.cpp>
00152   #include <basis/packable.cpp>
00153   #include <basis/portable.cpp>
00154   #include <basis/sequence.cpp>
00155   #include <basis/set.cpp>
00156   #include <basis/utility.cpp>
00157   #include <basis/version_record.cpp>
00158   #include <data_struct/amorph.cpp>
00159   #include <data_struct/bit_vector.cpp>
00160   #include <data_struct/byte_hasher.cpp>
00161   #include <data_struct/configurator.cpp>
00162   #include <data_struct/hash_table.cpp>
00163   #include <data_struct/pointer_hash.cpp>
00164   #include <data_struct/stack.cpp>
00165   #include <data_struct/static_memory_gremlin.cpp>
00166   #include <data_struct/string_hash.cpp>
00167   #include <data_struct/string_hasher.cpp>
00168   #include <data_struct/string_table.cpp>
00169   #include <data_struct/symbol_table.cpp>
00170   #include <data_struct/table_configurator.cpp>
00171   #include <loggers/console_logger.cpp>
00172   #include <loggers/file_logger.cpp>
00173   #include <loggers/locked_logger.cpp>
00174   #include <loggers/null_logger.cpp>
00175   #include <loggers/program_wide_logger.cpp>
00176   #include <opsystem/application_base.cpp>
00177   #include <opsystem/application_shell.cpp>
00178   #include <opsystem/byte_filer.cpp>
00179   #include <opsystem/command_line.cpp>
00180   #include <opsystem/critical_events.cpp>
00181   #include <opsystem/directory.cpp>
00182   #include <opsystem/filename.cpp>
00183   #include <opsystem/ini_config.cpp>
00184   #include <opsystem/ini_parser.cpp>
00185   #include <opsystem/path_configuration.cpp>
00186   #include <opsystem/rendezvous.cpp>
00187   #include <textual/byte_format.cpp>
00188   #include <textual/parser_bits.cpp>
00189   #include <textual/string_manipulation.cpp>
00190   #include <textual/tokenizer.cpp>
00191 #endif // __BUILD_STATIC_APPLICATION__
00192 

Generated on Fri Nov 21 04:29:06 2008 for HOOPLE Libraries by  doxygen 1.5.1