Login | ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/userdetect2/trunk/testuser/testuser.dpr
Revision: 72
Committed: Sat Oct 3 16:28:02 2015 UTC (7 years, 1 month ago) by daniel-marschall
File size: 3577 byte(s)

File Contents

# Content
1 program testuser;
2
3 {$APPTYPE CONSOLE}
4
5 {$R *.res}
6
7 uses
8 SysUtils,
9 Functions in 'Functions.pas';
10
11 type
12 EInvalidName = class(Exception);
13
14 const
15 C_TEMPLATES: array[0..5] of String =
16 ('USER', 'COMP', 'SID', 'HOME', 'HOMESHARE', 'HOMECOMP');
17
18 resourcestring
19 C_TEMPLATE_MARKER = ':%s:';
20 C_EQUAL = '%s = %s';
21
22 function _GetArgExpect(param1: string): string;
23 resourcestring
24 LNG_EXCEPTION = 'Unknown value "%s"';
25 begin
26 if param1 = C_TEMPLATES[0] then
27 begin
28 result := Functions.GetUserName;
29 end
30 else if param1 = C_TEMPLATES[1] then
31 begin
32 result := Functions.GetComputerName;
33 end
34 else if param1 = C_TEMPLATES[2] then
35 begin
36 result := Functions.GetCurrentUserSid;
37 end
38 else if param1 = C_TEMPLATES[3] then
39 begin
40 result := Functions.GetHomeDir;
41 end
42 else if param1 = C_TEMPLATES[4] then
43 begin
44 result := ExpandEnvironmentStrings('%HOMESHARE%');
45 if result = '%HOMESHARE%' then result := '';
46 end
47 else if param1 = C_TEMPLATES[5] then
48 begin
49 result := Functions.GetHomeDir;
50 if result <> '' then
51 begin
52 result := '\\' + GetComputerName + '\' + StringReplace(result, ':', '$', []);
53 end;
54 end
55 else
56 begin
57 raise EInvalidName.CreateFmt(LNG_EXCEPTION, [param1]);
58 end;
59 end;
60
61 function _MaxTemplateLen: integer;
62 var
63 i, L: integer;
64 begin
65 result := -1;
66 for i := Low(C_TEMPLATES) to High(C_TEMPLATES) do
67 begin
68 L := Length(Format(C_TEMPLATE_MARKER, [C_TEMPLATES[i]]));
69 if L > result then result := L;
70 end;
71 end;
72
73 procedure _ShowSyntax;
74 resourcestring
75 LNG_SYNTAX_1 = 'Syntax:' + #13#10 + '%s [templateString] [comparisonValue]';
76 LNG_SYNTAX_2 = 'templateString may contain following variables:';
77 LNG_SYNTAX_3 = 'If comparisonValue is provided, the value will be compared with templateString ' + #13#10 +
78 'where variables are resolved. The ExitCode will be 0 if the values match ' + #13#10 +
79 '(case insensitive) or 1 if the value does not match.' + #13#10#13#10 +
80 'If comparisonValue is not provided, the value will be printed and the program' + #13#10 +
81 'terminates with ExitCode 0.';
82 var
83 i: integer;
84 s: string;
85 maxLen: integer;
86 begin
87 WriteLn(Format(LNG_SYNTAX_1, [UpperCase(ExtractFileName(ParamStr(0)))]));
88 WriteLn('');
89 WriteLn(LNG_SYNTAX_2);
90 maxLen := _MaxTemplateLen;
91 for i := Low(C_TEMPLATES) to High(C_TEMPLATES) do
92 begin
93 s := C_TEMPLATES[i];
94 WriteLn(Format(C_EQUAL, [EnforceLength(Format(C_TEMPLATE_MARKER, [s]),
95 maxLen, ' ', true), _GetArgExpect(s)]));
96 end;
97 WriteLn('');
98 WriteLn(LNG_SYNTAX_3);
99 WriteLn('');
100 end;
101
102 function _Expand(AInput: string): string;
103 var
104 i: integer;
105 s: string;
106 begin
107 result := AInput;
108 for i := Low(C_TEMPLATES) to High(C_TEMPLATES) do
109 begin
110 s := C_TEMPLATES[i];
111 result := StringReplace(result, Format(C_TEMPLATE_MARKER, [s]),
112 _GetArgExpect(s), [rfIgnoreCase, rfReplaceAll]);
113 end;
114 end;
115
116 function _Main: integer;
117 var
118 arg2expect: string;
119 begin
120 result := 0;
121
122 if (ParamCount() = 0) or (ParamCount() > 2) or (ParamStr(1) = '/?') then
123 begin
124 _ShowSyntax;
125 result := 2;
126 Exit;
127 end;
128
129 arg2expect := _Expand(ParamStr(1));
130
131 if ParamCount() = 1 then
132 begin
133 WriteLn(Format(C_EQUAL, [ParamStr(1), arg2expect]));
134 end
135 else if ParamCount() = 2 then
136 begin
137 if not StrICmp(ParamStr(2), arg2expect) then result := 1;
138 end;
139 end;
140
141 begin
142 ExitCode := _Main;
143 end.