From ad6c6c122579c32d46d5c71fd71243b200bae548 Mon Sep 17 00:00:00 2001 From: plc-user <74435298+plc-user@users.noreply.github.com> Date: Sun, 14 Aug 2022 17:47:50 +0200 Subject: [PATCH] added option "-i" and changed "-s" to "-o" --- QET_ElementScaler.lpr | 123 ++++++++++++++++++++++++++++-------------- README.md | 26 ++++++--- 2 files changed, 101 insertions(+), 48 deletions(-) diff --git a/QET_ElementScaler.lpr b/QET_ElementScaler.lpr index 2d151ed..4aa8f62 100644 --- a/QET_ElementScaler.lpr +++ b/QET_ElementScaler.lpr @@ -8,10 +8,10 @@ // Debian/GNU Linux (unstable) and ReactOS (0.4.15-dev-4888) // // usage: -// QET_ElementScaler [-s] +// QET_ElementScaler [-i] [-o] // // OR -// QET_ElementScaler [-s] [-x FactorForX] [-y FactorForY] -f FILENAME +// QET_ElementScaler [-i] [-o] [-x FactorForX] [-y FactorForY] -f FILENAME // // // Result is a XML-File with an additional header-line @@ -23,8 +23,11 @@ // On Linux you may use something like this: // grep -v -i "xml version" "filename.elmt" > "filename_new.elmt" // -// Last Change(s): 13.08.2022 -// - add option "-s" to write scaled data to stdout instead of a renamed file +// Last Change(s): 14.08.2022 +// - add option "-i" and "--stdin" to read element-data from stdin instead +// of a file. With this option a filename is not necessary and will be +// ignored and the output is forced to stdout +// - renamed option "-s" to "-o" and added "--stdout" for writing to stdout // // Created: 17.12.2020 // Author: plc-user @@ -43,7 +46,7 @@ {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} - Classes, SysUtils, CustApp, + Classes, SysUtils, IOStream, CustApp, DOM, XMLRead, XMLWrite; type @@ -62,7 +65,7 @@ TQETScaler = class(TCustomApplication) { TQETScaler } const - sVersion: string = '0.2beta6'; + sVersion: string = '0.3beta1'; var QETfile: TXMLDocument; // holds the XML-Data from/to file @@ -270,14 +273,17 @@ procedure TQETScaler.DoRun; sDateiName: string = ''; ErrorMsg: string; xExtMode: boolean = false; - ssScaledXML: TStringStream; + xUseStdIO: boolean = false; + ios: TIOStream; // Lesen von Daten aus stdin + ssInputXML: TStringStream; // für Daten aus stdin + ssScaledXML: TStringStream; // für Daten nach stdout begin // Remember Setting for Decimal-Separator: cDecSepOrg := DefaultFormatSettings.DecimalSeparator; DefaultFormatSettings.DecimalSeparator := cDecSepNew; // Schnelltest der Parameter - ErrorMsg:=CheckOptions('hsx:y:f:v', 'help file: version'); + ErrorMsg:=CheckOptions('hiox:y:f:v', 'help stdin stdout file: version'); if ErrorMsg<>'' then begin ShowException(Exception.Create(ErrorMsg)); writeln(' * * * STOP * * *'); @@ -285,6 +291,10 @@ procedure TQETScaler.DoRun; Exit; end; + // ein paar Flags für die Programmsteuerung setzen + xUseStdIO := hasOption('i', 'stdin'); // wir holen die Daten von StdIn? + xExtMode := hasOption('x') or hasOption('y'); + // Version ausgeben und beenden if HasOption('v', 'version') then begin writeln; @@ -303,17 +313,17 @@ procedure TQETScaler.DoRun; end; // weniger als 2 Parameter gehen sonst gar nicht! - if (ParamCount < 2) then begin - WriteHelp; - writeln(' - - - no file read / changed - - -'); - Terminate; - Exit; - end; + if (ParamCount < 2) + then begin + WriteHelp; + writeln(' - - - no file read / changed - - -'); + Terminate; + Exit; + end; // Faktor für X if hasOption('x') then begin - xExtMode := true; try gfFactorX := getOptionValue('x').ToDouble; except @@ -327,7 +337,6 @@ procedure TQETScaler.DoRun; // Faktor für Y if hasOption('y') then begin - xExtMode := true; try gfFactorY := getOptionValue('y').ToDouble; except @@ -354,14 +363,17 @@ procedure TQETScaler.DoRun; // den ursprünglichen Modus versuchen: if (xExtMode = false) then begin - // gibt es die Datei überhaupt? - sDateiName := ParamStr(ParamCount - 1); - if (fileExists(sDateiName) = false) then - begin - writeln('File not found: ', sDateiName); - writeln(' - - - no file read / changed - - -'); - Terminate; - exit; + if (xUseStdIO = false) + then begin // nur, wenn Datei gelesen werden soll + // gibt es die Datei überhaupt? + sDateiName := ParamStr(ParamCount - 1); + if (fileExists(sDateiName) = false) then + begin + writeln('File not found: ', sDateiName); + writeln(' - - - no file read / changed - - -'); + Terminate; + exit; + end; end; try // Skalierungsfaktor als letzter Parameter gfFactor := ParamStr(ParamCount).ToDouble; @@ -379,23 +391,50 @@ procedure TQETScaler.DoRun; // hier geht das eigentliche Programm los! -// bevor es losgeht, den Dateinamen prüfen: - if (ExtractFileExt(sDateiName.ToLower) <> '.elmt') then begin + // bevor es losgeht, den Dateinamen und Existenz prüfen: + if (xUseStdIO = false) and (ExtractFileExt(sDateiName.ToLower) <> '.elmt') + then begin writeln('invalid file-type: ', sDateiName); writeln(' * * * STOP * * *'); Terminate; exit; end; - if not(FileExists(sDateiName)) then begin - writeln('File not found: ', sDateiName); - writeln(' * * * STOP * * *'); - Terminate; - exit; - end; - + if (xUseStdIO = false) and not(FileExists(sDateiName)) + then begin + writeln('File not found: ', sDateiName); + writeln(' * * * STOP * * *'); + Terminate; + exit; + end; // Read data to structure: - ReadXMLFile(QETfile, sDateiName); + if xUseStdIO // ohne Eingabedatei keine Ausgabedatei! + then begin + try // ... to read from StdIn + ios := TIOStream.Create(iosInput); + ssInputXML := TStringStream.Create(''); + ssInputXML.CopyFrom(ios, 0); + finally + ios.Free; + end; + if (ssInputXML.Size > 0) + then begin + ssInputXML.Position := 0; + ReadXMLFile(QETfile, ssInputXML); + ssInputXML.Free; + end + else begin + writeln('No Data from StdIn '); + writeln(' * * * STOP * * *'); + Terminate; + exit; + end; + end + else begin // read Data from file + ReadXMLFile(QETfile, sDateiName); + end; + + // process data-conversion: if (gfFactorX < gfFactorY) @@ -404,7 +443,8 @@ procedure TQETScaler.DoRun; ScaleGraphics(); // write changed data to stdout or new file - if hasOption('s') + // force output to stdout, when input comes from stdin! + if hasOption('o', 'stdout') or (xUseStdIO = true) then begin // write new data to stdout try @@ -448,19 +488,22 @@ procedure TQETScaler.WriteHelp; writeln(sExeName, ' version ', sVersion, ' needs some arguments!'); writeln(); writeln('usage for simple mode (both directions use the same factor):'); - writeln(sExeName, ' [-s] '); + writeln(sExeName, ' [-i|--stdin] [-o|--stdout] '); writeln(); - writeln('with option "-s" the scaled data is written to stdout. '); + writeln('With option "-o" or "--stdout" the scaled data is written to stdout. '); writeln('Otherwise the new data is written to a renamed file.'); writeln(); + writeln('With option "-i" or "--stdin" input-data is read from stdin, a given'); + writeln('filename is ignored and the scaled element will be written to stdout.'); + writeln(); writeln('In extended mode the scaling-factors for X and Y may differ and it is'); writeln('allowed to specify only one direction for scaling. In each case the'); writeln('Font-Sizes and Circle-Diameters are scaled by the smaller value.'); - writeln(''); + writeln(); writeln('usage for extended mode:'); - writeln(sExeName, ' [-s] [-x FactorForX] [-y FactorForY] -f FILENAME'); + writeln(sExeName, ' [-i] [-o] [-x FactorForX] [-y FactorForY] -f FILENAME'); writeln('or'); - writeln(sExeName, ' [-s] [-x FactorForX] [-y FactorForY] --file=FILENAME'); + writeln(sExeName, ' [-i] [-o] [-x FactorForX] [-y FactorForY] --file=FILENAME'); writeln(''); writeln('without parameters or with "-h" or "--help" this help is displayed'); writeln(''); diff --git a/README.md b/README.md index c2adb97..4c0f6a1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # QET_ElementScaler -QET_ElementScaler is a commandline-tool to scale QElectroTech-Elements with constant factor(s) and save the changed data to a renamed file. With the optional parameter "-s" the changed data is written to stdout and no new file is created. +QET_ElementScaler is a commandline-tool to scale QElectroTech-Elements with constant factor(s) and save the changed data to a renamed file. +With the optional parameter "-o" or "--stdout" the scaled element is written to stdout and no new file is created. +The optional parameter "-i" or "--stdin" is used to read the input-data from stdin and the output is forced to stdout.
It compiles with Lazarus 2.2.2 and FreePascal 3.2.2 on Debian/GNU Linux (unstable) and ReactOS (0.4.15-dev-4888). The ReactOS-Build can also be used with Win in a cmd-window.
@@ -11,15 +13,23 @@ If you want to edit all files in a directory, you need to call QET_ElementScaler You do not want to (or can't) compile the FreePascal program yourself? Download the executable file suitable for your operating system from the releases-page and use that.

Hint:
Get familiar with the command line before using this software. -General support for using the command line for the various systems cannot be provided here. - +General support for using the command line for the various systems cannot be provided here.
usage:
-QET_ElementScaler [-s] <file> <scaling-factor>
-(as used in Batch-File "scale.cmd" and shell-script "scale.sh") +QET_ElementScaler [-i] [-o] <file> <scaling-factor>
+(as used in Batch-File "scale.cmd" and shell-script "scale.sh")
or:
-QET_ElementScaler [-s] [-x FactorForX] [-y FactorForY] -f FILENAME - - +QET_ElementScaler [-i] [-o] [-x FactorForX] [-y FactorForY] -f FILENAME
+ +
+examples to use data from stdin:
+QET_ElementScaler -i 2.0 < ElementToScale.elmt > ScaledElement.elmt
+
+or use a pipe like this
+cat ElementToScale.elmt | QET_ElementScaler -i 2.0 | OtherSoftwareBinary
+
+extended mode and long-options are also possible:
+QET_ElementScaler --stdin -x 2 -y 3 < ElementToScale.elmt > ScaledElement.elmt
+