first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / applications / utilities / splitter.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : splitter                                                          *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *  Purpose:                                                                   *
7 *                                                                             *
8 *    Takes text as input and splits the lines so that they will fit on a      *
9 *  standard 80 column terminal.                                               *
10 *                                                                             *
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 \*****************************************************************************/
19
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>
29
30 #include <stdio.h>
31
32 using namespace application;
33 using namespace basis;
34 using namespace filesystem;
35 using namespace loggers;
36 using namespace structures;
37 using namespace textual;
38
39 const int MAX_BUFFER = 1024;
40
41 class splitter_app : public application_shell
42 {
43 public:
44   splitter_app() : application_shell() {}
45
46   DEFINE_CLASS_NAME("splitter_app");
47
48   virtual int execute();
49
50   int print_instructions();
51
52 private:
53 };
54
55 //////////////
56
57 int splitter_app::print_instructions()
58 {
59   astring name = filename(_global_argv[0]).basename().raw();
60   log(a_sprintf("%s usage:", name.s()));
61   log(astring::empty_string());
62   log(a_sprintf("\
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\
69 "));
70   return -3;
71 }
72
73 int splitter_app::execute()
74 {
75   command_line cmds(_global_argc, _global_argv);  // parse the command line up.
76
77   // retrieve any specific flags first.
78   astring temp;
79   int min_col = 0;
80   if (cmds.get_value("mincol", temp))
81     min_col = temp.convert(min_col);
82   int max_col = 77;
83   if (cmds.get_value("maxcol", temp))
84     max_col = temp.convert(max_col);
85   // look for help command.
86   int junk_index = 0;
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) ) {
91     print_instructions();
92     return 0;
93   }
94
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();
102     }
103   }
104
105   astring accumulator;
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()) {
110       astring line_read;
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';
116     }
117   }
118
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);
124       if (!got) break;
125 //printf("line=%s\n", got);
126       accumulator += got;
127 ////      accumulator += '\n';
128     }
129   }
130 //printf("splitting accum with %d chars...\n", accumulator.length());
131   astring chewed;
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());
135   return 0;
136 }
137
138 //////////////
139
140 HOOPLE_MAIN(splitter_app, )
141