X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Fapplications%2Futilities%2Fsplitter.cpp;fp=nucleus%2Fapplications%2Futilities%2Fsplitter.cpp;h=0de8347ae5623fa1e4ef96affd25d58afa9e65d5;hb=457b128b77b5b4a0b7dd3094de543de2ce1477ad;hp=0000000000000000000000000000000000000000;hpb=32d7caf45d886d0d24e69eea00511c7815ac15d0;p=feisty_meow.git diff --git a/nucleus/applications/utilities/splitter.cpp b/nucleus/applications/utilities/splitter.cpp new file mode 100644 index 00000000..0de8347a --- /dev/null +++ b/nucleus/applications/utilities/splitter.cpp @@ -0,0 +1,141 @@ +/*****************************************************************************\ +* * +* Name : splitter * +* Author : Chris Koeritz * +* * +* Purpose: * +* * +* Takes text as input and splits the lines so that they will fit on a * +* standard 80 column terminal. * +* * +******************************************************************************* +* Copyright (c) 1993-$now By Author. This program is free software; you can * +* redistribute it and/or modify it under the terms of the GNU General Public * +* License as published by the Free Software Foundation; either version 2 of * +* the License or (at your option) any later version. This is online at: * +* http://www.fsf.org/copyleft/gpl.html * +* Please send any updates to: fred@gruntose.com * +\*****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace application; +using namespace basis; +using namespace filesystem; +using namespace loggers; +using namespace structures; +using namespace textual; + +const int MAX_BUFFER = 1024; + +class splitter_app : public application_shell +{ +public: + splitter_app() : application_shell() {} + + DEFINE_CLASS_NAME("splitter_app"); + + virtual int execute(); + + int print_instructions(); + +private: +}; + +////////////// + +int splitter_app::print_instructions() +{ + astring name = filename(_global_argv[0]).basename().raw(); + log(a_sprintf("%s usage:", name.s())); + log(astring::empty_string()); + log(a_sprintf("\ +This program splits long lines in input files into a more reasonable size.\n\ +Any filenames on the command line are split and sent to standard output.\n\ +The following options change how the splitting is performed:\n\ + --help or -?\tShow this help information.\n\ + --mincol N\tMinimum column to use for output.\n\ + --maxcol N\tMinimum column to use for output.\n\ +")); + return -3; +} + +int splitter_app::execute() +{ + command_line cmds(_global_argc, _global_argv); // parse the command line up. + + // retrieve any specific flags first. + astring temp; + int min_col = 0; + if (cmds.get_value("mincol", temp)) + min_col = temp.convert(min_col); + int max_col = 77; + if (cmds.get_value("maxcol", temp)) + max_col = temp.convert(max_col); + // look for help command. + int junk_index = 0; + if (cmds.find("help", junk_index, false) + || cmds.find('h', junk_index, false) + || cmds.find("?", junk_index, false) + || cmds.find('?', junk_index, false) ) { + print_instructions(); + return 0; + } + + // gather extra input files. + string_set input_files; + for (int i = 0; i < cmds.entries(); i++) { + const command_parameter &curr = cmds.get(i); + if (curr.type() == command_parameter::VALUE) { +//log(astring("adding input file:") + curr.text()); + input_files += curr.text(); + } + } + + astring accumulator; + for (int q = 0; q < input_files.length(); q++) { + byte_filer current(input_files[q], "r"); + if (!current.good()) continue; + while (!current.eof()) { + astring line_read; + int num_chars = current.getline(line_read, MAX_BUFFER); + if (!num_chars) continue; +//printf("line len=%d, cont=%s\n", line_read.length(), line_read.s()); + accumulator += line_read; +//// accumulator += '\n'; + } + } + + // now get from standard input if there weren't any files specified. + if (!input_files.length()) { + char input_line[MAX_BUFFER + 2]; + while (!feof(stdin)) { + char *got = fgets(input_line, MAX_BUFFER, stdin); + if (!got) break; +//printf("line=%s\n", got); + accumulator += got; +//// accumulator += '\n'; + } + } +//printf("splitting accum with %d chars...\n", accumulator.length()); + astring chewed; + string_manipulation::split_lines(accumulator, chewed, min_col, max_col); +//printf("chewed string now has %d chars...\n", chewed.length()); + printf("%s", chewed.s()); + return 0; +} + +////////////// + +HOOPLE_MAIN(splitter_app, ) +