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\tMinimum 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.
80 if (cmds.get_value("mincol", temp))
81 min_col = temp.convert(min_col);
83 if (cmds.get_value("maxcol", temp))
84 max_col = temp.convert(max_col);
85 // look for help command.
87 if (cmds.find("help", junk_index, false)
88 || cmds.find('h', junk_index, false)
89 || cmds.find("?", junk_index, false)
90 || cmds.find('?', junk_index, false) ) {
95 // gather extra input files.
96 string_set input_files;
97 for (int i = 0; i < cmds.entries(); i++) {
98 const command_parameter &curr = cmds.get(i);
99 if (curr.type() == command_parameter::VALUE) {
100 //log(astring("adding input file:") + curr.text());
101 input_files += curr.text();
106 for (int q = 0; q < input_files.length(); q++) {
107 byte_filer current(input_files[q], "r");
108 if (!current.good()) continue;
109 while (!current.eof()) {
111 int num_chars = current.getline(line_read, MAX_BUFFER);
112 if (!num_chars) continue;
113 //printf("line len=%d, cont=%s\n", line_read.length(), line_read.s());
114 accumulator += line_read;
115 //// accumulator += '\n';
119 // now get from standard input if there weren't any files specified.
120 if (!input_files.length()) {
121 char input_line[MAX_BUFFER + 2];
122 while (!feof(stdin)) {
123 char *got = fgets(input_line, MAX_BUFFER, stdin);
125 //printf("line=%s\n", got);
127 //// accumulator += '\n';
130 //printf("splitting accum with %d chars...\n", accumulator.length());
132 string_manipulation::split_lines(accumulator, chewed, min_col, max_col);
133 //printf("chewed string now has %d chars...\n", chewed.length());
134 printf("%s", chewed.s());
140 HOOPLE_MAIN(splitter_app, )