1 /*****************************************************************************\
4 * Author : Chris Koeritz *
8 * Takes text as input and splits the lines so that they will fit on a *
9 * standard 80 column terminal. *
11 *******************************************************************************
12 * Copyright (c) 1993-$now By Author. This program is free software; you can *
13 * redistribute it and/or modify it under the terms of the GNU General Public *
14 * License as published by the Free Software Foundation; either version 2 of *
15 * the License or (at your option) any later version. This is online at: *
16 * http://www.fsf.org/copyleft/gpl.html *
17 * Please send any updates to: fred@gruntose.com *
18 \*****************************************************************************/
20 #include <application/hoople_main.h>
21 #include <basis/astring.h>
22 #include <filesystem/byte_filer.h>
23 #include <filesystem/filename.h>
24 #include <loggers/console_logger.h>
25 #include <loggers/file_logger.h>
26 #include <structures/static_memory_gremlin.h>
27 #include <structures/set.h>
28 #include <textual/string_manipulation.h>
32 using namespace application;
33 using namespace basis;
34 using namespace filesystem;
35 using namespace loggers;
36 using namespace structures;
37 using namespace textual;
39 const int MAX_BUFFER = 1024;
41 class splitter_app : public application_shell
44 splitter_app() : application_shell() {}
46 DEFINE_CLASS_NAME("splitter_app");
48 virtual int execute();
50 int print_instructions();
57 int splitter_app::print_instructions()
59 astring name = filename(_global_argv[0]).basename().raw();
60 log(a_sprintf("%s usage:", name.s()));
61 log(astring::empty_string());
63 This program splits long lines in input files into a more reasonable size.\n\
64 Any filenames on the command line are split and sent to standard output.\n\
65 The following options change how the splitting is performed:\n\
66 --help or -?\tShow this help information.\n\
67 --mincol N\tMinimum column to use for output.\n\
68 --maxcol N\tMaximum column to use for output.\n\
73 int splitter_app::execute()
75 command_line cmds(_global_argc, _global_argv); // parse the command line up.
77 // retrieve any specific flags first.
81 //hmmm: this whole thing is annoying. we need a better way to have a list of parms.
82 if (cmds.find("mincol", min_indy)) {
83 cmds.get_value("mincol", temp);
84 min_col = temp.convert(min_col);
88 if (cmds.find("maxcol", max_indy)) {
89 cmds.get_value("maxcol", temp);
90 max_col = temp.convert(max_col);
92 //printf("got max_col=%d\n", max_col);
93 // look for help command.
95 if (cmds.find("help", junk_index, false)
96 || cmds.find('h', junk_index, false)
97 || cmds.find("?", junk_index, false)
98 || cmds.find('?', junk_index, false) ) {
103 // see if we found any flags that would make us skip some of the parameters.
104 //hmmm: automate this!
106 if ( (min_indy >= 0) || (max_indy >= 0) ) {
107 skip_index = basis::maximum(min_indy, max_indy);
110 //printf("got a skip index of %d\n", skip_index);
112 // gather extra input files.
113 string_set input_files;
114 for (int i = skip_index; i < cmds.entries(); i++) {
115 const command_parameter &curr = cmds.get(i);
116 if (curr.type() == command_parameter::VALUE) {
117 //log(astring("adding input file:") + curr.text());
118 input_files += curr.text();
123 for (int q = 0; q < input_files.length(); q++) {
124 byte_filer current(input_files[q], "r");
125 if (!current.good()) continue;
126 while (!current.eof()) {
128 int num_chars = current.getline(line_read, MAX_BUFFER);
129 if (!num_chars) continue;
130 //printf("line len=%d, cont=%s\n", line_read.length(), line_read.s());
131 accumulator += line_read;
135 // now get from standard input if there weren't any files specified.
136 if (!input_files.length()) {
137 char input_line[MAX_BUFFER + 2];
138 while (!feof(stdin)) {
139 char *got = fgets(input_line, MAX_BUFFER, stdin);
141 //printf("line=%s\n", got);
145 //printf("splitting accum with %d chars...\n", accumulator.length());
147 string_manipulation::split_lines(accumulator, chewed, min_col, max_col);
148 //printf("chewed string now has %d chars...\n", chewed.length());
149 printf("%s", chewed.s());
155 HOOPLE_MAIN(splitter_app, )