-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistprn_config.pas
170 lines (132 loc) · 3.4 KB
/
listprn_config.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
unit listprn_config;
interface
uses sysutils,classes,vcl.Printers;
Type
TConfigLoader = class
private
procedure HaltWithError(ErrorNo: Integer; Msg: String);
function DefaultPrinterName: String;
public
procedure Load(Params: TStrings);
procedure Log(Msg: string);
procedure WriteOut(Msg: String);
end;
implementation
procedure TConfigLoader.Load(Params: TStrings);
var f , iParam, idx: integer;
pstr : string;
id, value, DefaultPrnName : string;
Output : TStringList;
I: Integer;
begin
//Initialise
iParam := 0;
Output := TStringList.Create;
log('Loading Configuration...');
log('Parameter Count is ' + inttostr(params.Count));
if Params.Count = 0 then
begin
// log('Parameters Expected: -H for help');
// halt(1);
for Idx := 0 to Printer.Printers.Count -1 do
begin
Output.Add(Printer.Printers.Strings[Idx]);
end;
end ;
While iParam <= Params.Count -1 do
begin
pstr := Params[iParam];
id := UpperCase( pstr);
if ParamCount -1 > iParam then
begin
try
value := Trim(Params[iParam +1]);
except on E: Exception do
HaltWithError(202,E.message);
end;
end
else
begin
value := '';
end;
//jump to next id + value
inc(iParam,2);
if (id = '-D') or
(id = '--DEFAULT') then
begin
Output.Add(DefaultPrinterName());
end
else if (id = '-J') or
(id = '--JSON') then
begin
Output.Add('{"Printers":[');
DefaultPrnName := DefaultPrinterName();
for Idx := 0 to Printer.Printers.Count -1 do
begin
if IDx > 0 then
begin
Output.Add(',');
end;
Output.Add(' {"PrinterName" : "' +Printer.Printers.Strings[Idx] + '"');
if Printer.Printers[Idx] = DefaultPrnName then
begin
Output.Add(' ,"Default": true');
end else begin
Output.Add(' ,"Default": false');
end;
Output.Add(' } ' );
end;
Output.Add(' ]}');
end
else if (id = '-V') or
(id = '--VERSION') then
begin
WriteOut('Version:0.2');
end
else if (id = '-H') or
(id = '-?') or
(id = '--HELP') then
begin
Output.Add('ListPrn.exe');
Output.Add('Commandline utility for listing Windows Printers.');
Output.Add('SourceCode:https://github.com/tobya/listprn');
Output.Add('Version:0.2');
Output.Add('Call with no params to get list of printers');
Output.Add('-H :Help Message');
Output.Add('-J :Output as JSON ');
Output.Add('-D :Show Default Printer');
end
else
begin
HaltWithError(203,'Unknown Switch:' + pstr);
end;
end;
for I := 0 to Output.Count -1 do
begin
WriteOut(Output.Strings[i]);
end;
Output.Free;
end;
procedure TConfigLoader.HaltWithError(ErrorNo: Integer; Msg: String);
begin
WriteOut(Msg);
WriteOut('Exiting with Error Code : ' + inttostr(ErrorNo));
Halt(ErrorNo);
end;
procedure TConfigLoader.Log(Msg: string);
begin
// writeln(Msg);
end;
procedure TConfigLoader.WriteOut(Msg: String);
begin
writeln(Msg);
end;
function TConfigLoader.DefaultPrinterName : String;
var
DefaultPrnName : String;
begin
Printer.PrinterIndex := -1;
DefaultPrnName := Printer.Printers.Strings[Printer.PrinterIndex];
Result := DefaultPrnName;
end;
end.