Hi! I'm trying to create a program that processes params when run.
Basically, if you run the program with this syntax: "C:\Program.exe REQUEST INFO1 INFO2 INFO3" it should be able to process the commands accordingly and spit out result data.
Here's what I have so far. It compiles without any errors or warnings but when I run it in a live environment, I get an Error 216 as the result.
Any help would be highly appreciated.
Thanks!
program MsgProc;
uses
SysUtils;
function Receive(MsgData: Array of String): String;
begin
if MsgData[0] = 'HELP' then
begin
Result := 'When this application is complete, a list of valid keywords and their use will be displayed here.';
end
else
if MsgData[0] = 'REG' then
begin
Result := 'Congratulations ' + MsgData[1] + '! You are now a registered user.';
end
else
if MsgData[0] = 'DATA' then
begin
Result := 'Coming soon!';
end
else
Result := 'Sorry! ' + MsgData[0] + ' is not a valid keyword. Text HELP for a list of valid keywords and their use.';
end;
procedure ProcessQueue(const TxtDelimiter, TxtData: String; MaxArrayData: Integer = 0);
var
i, DataStart, ArrayItem, DelimLength: Integer;
ArrayData: Array of String;
procedure AddString(ArrayEnd: Integer = -1);
var
EndingPos: Integer;
begin
if (ArrayEnd = -1) then
EndingPos := i
else
EndingPos := ArrayEnd + 1;
if (DataStart < EndingPos) then
ArrayData[ArrayItem] := Copy(TxtData, DataStart, EndingPos - DataStart)
else
ArrayData[ArrayItem] := '';
Inc(ArrayItem);
end;
begin
if (TxtData = '') or (MaxArrayData < 0) then
begin
SetLength(ArrayData, 0);
Exit;
end;
if (TxtDelimiter = '') then
begin
SetLength(ArrayData, 1);
ArrayData[0] := TxtData;
Exit;
end;
DelimLength := Length(TxtDelimiter);
SetLength(ArrayData, (Length(TxtData) div DelimLength) + 1);
i := 1;
DataStart := i;
ArrayItem := 0;
while (i <= (Length(TxtData) - DelimLength + 1)) do
begin
if (TxtData = TxtDelimiter[1]) then
if (Copy(TxtData, i, DelimLength) = TxtDelimiter) then
begin
AddString;
if (ArrayItem = MaxArrayData) then
begin
SetLength(ArrayData, ArrayItem);
Exit;
end;
Inc(i, DelimLength - 1);
DataStart := i + 1;
end;
Inc(i);
end;
AddString(Length(TxtData));
SetLength(ArrayData, ArrayItem);
Receive(ArrayData);
end;
var
i: Integer;
ComBuf: String;
begin
for i := 0 to ParamCount do
begin
ComBuf := ParamStr(i);
ProcessQueue(' ', ComBuf);
end;
end.
Basically, if you run the program with this syntax: "C:\Program.exe REQUEST INFO1 INFO2 INFO3" it should be able to process the commands accordingly and spit out result data.
Here's what I have so far. It compiles without any errors or warnings but when I run it in a live environment, I get an Error 216 as the result.
Any help would be highly appreciated.
Thanks!
program MsgProc;
uses
SysUtils;
function Receive(MsgData: Array of String): String;
begin
if MsgData[0] = 'HELP' then
begin
Result := 'When this application is complete, a list of valid keywords and their use will be displayed here.';
end
else
if MsgData[0] = 'REG' then
begin
Result := 'Congratulations ' + MsgData[1] + '! You are now a registered user.';
end
else
if MsgData[0] = 'DATA' then
begin
Result := 'Coming soon!';
end
else
Result := 'Sorry! ' + MsgData[0] + ' is not a valid keyword. Text HELP for a list of valid keywords and their use.';
end;
procedure ProcessQueue(const TxtDelimiter, TxtData: String; MaxArrayData: Integer = 0);
var
i, DataStart, ArrayItem, DelimLength: Integer;
ArrayData: Array of String;
procedure AddString(ArrayEnd: Integer = -1);
var
EndingPos: Integer;
begin
if (ArrayEnd = -1) then
EndingPos := i
else
EndingPos := ArrayEnd + 1;
if (DataStart < EndingPos) then
ArrayData[ArrayItem] := Copy(TxtData, DataStart, EndingPos - DataStart)
else
ArrayData[ArrayItem] := '';
Inc(ArrayItem);
end;
begin
if (TxtData = '') or (MaxArrayData < 0) then
begin
SetLength(ArrayData, 0);
Exit;
end;
if (TxtDelimiter = '') then
begin
SetLength(ArrayData, 1);
ArrayData[0] := TxtData;
Exit;
end;
DelimLength := Length(TxtDelimiter);
SetLength(ArrayData, (Length(TxtData) div DelimLength) + 1);
i := 1;
DataStart := i;
ArrayItem := 0;
while (i <= (Length(TxtData) - DelimLength + 1)) do
begin
if (TxtData = TxtDelimiter[1]) then
if (Copy(TxtData, i, DelimLength) = TxtDelimiter) then
begin
AddString;
if (ArrayItem = MaxArrayData) then
begin
SetLength(ArrayData, ArrayItem);
Exit;
end;
Inc(i, DelimLength - 1);
DataStart := i + 1;
end;
Inc(i);
end;
AddString(Length(TxtData));
SetLength(ArrayData, ArrayItem);
Receive(ArrayData);
end;
var
i: Integer;
ComBuf: String;
begin
for i := 0 to ParamCount do
begin
ComBuf := ParamStr(i);
ProcessQueue(' ', ComBuf);
end;
end.