From cc0046b7e7e191443a027a8f1f38855726ae2871 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 2 Sep 2004 16:30:12 +0000 Subject: Allow conversions on stdin/stdout --- src/rtfx.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/rtfx.cpp b/src/rtfx.cpp index 2a9f3fc..bb26777 100644 --- a/src/rtfx.cpp +++ b/src/rtfx.cpp @@ -46,7 +46,7 @@ int usage() { - fprintf(stderr, "usage: rtfx [-p] \n"); + fprintf(stderr, "usage: rtfx [-p] [inrtf] [outxml]\n"); exit(2); } @@ -85,20 +85,45 @@ int main(int argc, char* argv[]) break; } - if(argc < 2) - usage(); + if(argc > 2) + usage(); try { - // The input file - FILE* file = fopen(argv[0], "rb"); + // By default no files + FILE* file = NULL; + FILE* out = stdout; - if(!file) + // An input file + if(argc > 0) { - fprintf(stderr, "rtfx: couldn't open rtf file: %s: %s\n", argv[0], strerror(errno)); - return 1; + file = fopen(argv[0], "rb"); + + if(!file) + { + fprintf(stderr, "rtfx: couldn't open rtf file: %s: %s\n", argv[0], strerror(errno)); + return 1; + } + + argc--; + argv++; + } + + // Use stdin + else + { + // We have to do this instead of just use 'stdin' because windows + // screws with the line endings of stdin. + file = fdopen(0, "rb"); + + if(!file) + { + fprintf(stderr, "rtfx: couldn't open stdin: %s", strerror(errno)); + return 1; + } } + // Reads RTF tags and blocks RtfParser rtf; @@ -117,15 +142,24 @@ int main(int argc, char* argv[]) DOM::Document doc = composer.getDocument(); - FILE* out = fopen(argv[1], "wb"); - if(!out) + if(argc > 0) { - fprintf(stderr, "rtfx: couldn't open file: %s: %s\n", argv[1], strerror(errno)); - return 1; + out = fopen(argv[0], "wb"); + if(!out) + { + fprintf(stderr, "rtfx: couldn't open file: %s: %s\n", argv[0], strerror(errno)); + return 1; + } + + argc--; + argv++; } doc.serialize(out); - fclose(out); + + if(out != stdout) + fclose(out); + return 0; } catch(DOM::DOMException& e) -- cgit v1.2.3