Skip to content

Commit

Permalink
added option "-i" and changed "-s" to "-o"
Browse files Browse the repository at this point in the history
  • Loading branch information
plc-user authored Aug 14, 2022
1 parent a0b5a54 commit ad6c6c1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 48 deletions.
123 changes: 83 additions & 40 deletions QET_ElementScaler.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
// Debian/GNU Linux (unstable) and ReactOS (0.4.15-dev-4888)
//
// usage:
// QET_ElementScaler [-s] <file> <scaling-factor>
// QET_ElementScaler [-i] [-o] <file> <scaling-factor>
//
// 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
Expand All @@ -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
Expand All @@ -43,7 +46,7 @@
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp,
Classes, SysUtils, IOStream, CustApp,
DOM, XMLRead, XMLWrite;

type
Expand All @@ -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
Expand Down Expand Up @@ -270,21 +273,28 @@ 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 * * *');
Terminate;
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;
Expand All @@ -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
Expand All @@ -327,7 +337,6 @@ procedure TQETScaler.DoRun;
// Faktor für Y
if hasOption('y') then
begin
xExtMode := true;
try
gfFactorY := getOptionValue('y').ToDouble;
except
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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] <file> <scaling-factor>');
writeln(sExeName, ' [-i|--stdin] [-o|--stdout] <file> <scaling-factor>');
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('');
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 &quot;-s&quot; 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 &quot;-o&quot; or &quot;--stdout&quot; the scaled element is written to stdout and no new file is created.
The optional parameter &quot;-i&quot; or &quot;--stdin&quot; is used to read the input-data from stdin and the output is forced to stdout.<br>

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.<br>
Expand All @@ -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.<br><br>
Hint: <br>
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.<br>


usage:<br>
QET_ElementScaler [-s] &lt;file&gt; &lt;scaling-factor&gt; <br>
(as used in Batch-File &quot;scale.cmd&quot; and shell-script &quot;scale.sh&quot;)
QET_ElementScaler [-i] [-o] &lt;file&gt; &lt;scaling-factor&gt; <br>
(as used in Batch-File &quot;scale.cmd&quot; and shell-script &quot;scale.sh&quot;)<br>

or:<br>
QET_ElementScaler [-s] [-x FactorForX] [-y FactorForY] -f FILENAME


QET_ElementScaler [-i] [-o] [-x FactorForX] [-y FactorForY] -f FILENAME<br>

<br>
examples to use data from stdin: <br>
QET_ElementScaler -i 2.0 &lt; ElementToScale.elmt &gt; ScaledElement.elmt <br>
<br>
or use a pipe like this <br>
cat ElementToScale.elmt | QET_ElementScaler -i 2.0 | OtherSoftwareBinary <br>
<br>
extended mode and long-options are also possible:<br>
QET_ElementScaler --stdin -x 2 -y 3 &lt; ElementToScale.elmt &gt; ScaledElement.elmt <br>
<br>

0 comments on commit ad6c6c1

Please sign in to comment.