diff --git a/Makefile b/Makefile index 4b10997..7d60be2 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ SRCS = \ src/Channel.cpp, \ src/Item.cpp, \ src/ProtocolListener.cpp, \ + src/Config.cpp, \ src/webfetch.cpp, \ src/parsing.cpp, \ src/Rifen.cpp diff --git a/src/Channel.cpp b/src/Channel.cpp index b52d74d..acacbe2 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -2,6 +2,7 @@ #include #include "Channel.h" #include "Item.h" +#include "Config.h" #include "parsing.h" Channel::Channel ( BString path, BString outputPath ) @@ -17,7 +18,7 @@ Channel::Channel ( BString path, BString outputPath ) } void -Channel::Parse ( ) +Channel::Parse ( Config* cfg ) { int itemCount = countItemParser( filePath.String() ); items = BList(itemCount); diff --git a/src/Channel.h b/src/Channel.h index aa2f302..3f214de 100644 --- a/src/Channel.h +++ b/src/Channel.h @@ -5,6 +5,7 @@ #include #include #include +#include "Config.h" class Channel { public: @@ -25,7 +26,7 @@ public: Channel ( BString, BString ); // Channel ( BEntry ); // Channel ( BUrl ); - void Parse ( void ); + void Parse ( Config* ); }; #endif diff --git a/src/Config.cpp b/src/Config.cpp new file mode 100644 index 0000000..4b46ebc --- /dev/null +++ b/src/Config.cpp @@ -0,0 +1,9 @@ +#include +#include "Config.h" + +Config::Config () { + verbose = false; + daemon = true; + mimetype = BString("text/xml"); + outDir = BString("/boot/home/feeds/"); +} diff --git a/src/Config.h b/src/Config.h new file mode 100644 index 0000000..c0fd5b5 --- /dev/null +++ b/src/Config.h @@ -0,0 +1,17 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#include + +class Config { +public: + bool verbose; + bool daemon; + BString mimetype; + BString outDir; + BString targetFeed; // file or url + + Config ( ); +}; + +#endif diff --git a/src/Rifen.cpp b/src/Rifen.cpp index 570d6b6..beedb2b 100644 --- a/src/Rifen.cpp +++ b/src/Rifen.cpp @@ -1,26 +1,93 @@ #include #include +#include +#include #include "Channel.h" #include "Item.h" -#include "parsing.h" // +#include "parsing.h" +#include "Config.h" +#include "Rifen.h" +int +usage () +{ + fprintf(stderr, "%s", usageMsg.String()); + return 2; +} bool create_item ( void* item ) { + printf("hi"); Item* itemPtr = (Item*)item; itemPtr->Filetize( false ); return false; } + +int +invocation ( int argc, char** argv, Config** cfgPtr ) +{ + Config* cfg = *(cfgPtr); + bool suicide = false; + static struct option sLongOptions[] = { + { "help", no_argument, 0, 'h' }, + { "verbose", no_argument, 0, 'v' }, + { "output", required_argument, 0, 'O' }, + { "mimetype", required_argument, 0, 'm' }, + { "foreground", no_argument, 0, 'D' }, + { 0, 0, 0, 0 } + }; + + while (true) { + opterr = 0; + int c = getopt_long(argc, argv, "+hvDm:O:", sLongOptions, NULL); + + switch (c) { + case -1: + if ( optind < argc ) + cfg->targetFeed = BString(argv[optind]); + return 0; + case 'h': + return usage(); + case 'm': + cfg->mimetype = BString(optarg); + break; + case 'O': + cfg->outDir = BString(optarg); + break; + case 'v': + cfg->verbose = true; + break; + case 'D': + cfg->daemon = false; + break; + case '?': + if ( optopt == 'O' || optopt == 'm' ) + fprintf( stderr, "Option `-%c` requires an argument.\n\n", optopt ); + else + fprintf( stderr, "Unknown option `-%c`.\n\n", optopt ); + return 2; + } + } + + return 0; +} + + int main ( int argc, char** argv ) { - BString outputDir("/boot/home/feeds/"); - Channel* chan = (Channel*)malloc( sizeof(Channel) ); +// +// + Config* cfg = new Config; + usageMsg.ReplaceAll("%app%", "Rifen"); - chan = new Channel(argv[1], outputDir); - chan->Parse(); + invocation( argc, argv, &cfg ); + + Channel* chan = (Channel*)malloc( sizeof(Channel) ); + chan = new Channel(cfg->targetFeed, cfg->outDir); + chan->Parse(cfg); BList items = chan->items; items.DoForEach(&create_item); diff --git a/src/Rifen.h b/src/Rifen.h new file mode 100644 index 0000000..8793f2f --- /dev/null +++ b/src/Rifen.h @@ -0,0 +1,41 @@ +#include + +int main ( int, char** ); +int invocation ( int, char**, Config** ); +int usage ( ); +bool create_item ( void* ); + +// ---------------------------------------------------------------------------- + +BString usageMsg = + "Usage: %app% [-hvD] [-m mimetype] [-tT datetime] [-O directory] \n" + " %app% [-hv] [-mtTO] ( | | )\n" + "\n" + "%app%, a RSS and Atom feed parser/daemon.\n" + "\n" + "Options:\n" + " -h, --help - Print this usage info.\n" + " -v, --verbose - Print verbose (debug) info.\n" + " -m, --mimetype - Mimetype of new item files. (Default: text/xml)\n" + " -O, --output - Output dir for item files. (Default: ~/feeds/)\n" + " -t, --before - Only return items published before this datetime.\n" + " -T, --after - Only return items published after this datetime.\n" + " -D, --foreground - Run in the foreground, do not daemonize.\n" + " Only applies when running without file/url arg.\n" + "\n" + "When invoked without a file or URL, will search for any new feed items\n" + "published since last check by by any 'feed file' placed in the config\n" + "directory (default: ~/config/settings/Rifen/feeds/) and create their\n" + "corresponding files.\n" + "\n" + "When invoked with a file or URL, will create files from items contained\n" + "within the given file or URL.\n" + "\n" + "'Feed files' are files with a 'META:url' attribute containing the\n" + "corresponding URL to the feed's XML.\n" + "\n" + "Both -t and -T use the ISO 8601 format for specifying datetimes:\n" + " YYYY-MM-DDTHH:MM:SS - 2020-01-01T07:07:07\n" + "You can leave out seconds, minutes, or hours, but YMD are required.\n" +; + diff --git a/src/parsing.cpp b/src/parsing.cpp index 2def08f..93cb90d 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -5,9 +5,6 @@ #include "Item.h" #include "parsing.h" -/* predicate == sweet http version of tag - subject == parent - object == data */ // ============================================================================ // PARSERS