Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

More efficient way of checking parameters

tekiotekio Posts: 3Member
Hello,

I'm very new to Pascal, been learning about it for around 4hrs now. Below is the start of a program that is in the works. It will be a command line application taking three parameters: start ip (sIp) end ip (eIp) and a DNS server address.

[code]

program dnsaudit;

uses
sockets, inetaux;


var
i: integer;
sIp, eIp: AnsiString;
gtOpt: Pchar;

begin (* Main *)

for i := 0 to ParamCount do
begin
if ParamStr(i) = '-s' then
begin
sIp := ParamStr(i +1);
writeln(sIp);
end;
if ParamStr(i) = '-e' then
begin
eIp := ParamStr(i +1);
writeln(eIp);
end
else
begin
if ParamStr(i) <> '-s' then
begin
if ParamStr(i) <> '-e' then
begin
if ParamStr(i) <> eIp then
begin
if ParamStr(i) <> sIp then
begin
writeln('Invalid parameter or input specified, exiting...');
exit();
end

end
end

end
end;
end;

//writeln(ParamCount);


end. (* Main *)[/code]

I would like the application to check each parameter and either assign a value from the parameter or exit if invalid data is passed to the application. As you can see the nested if statements are not the most efficient way of checking for invalid input. Could someone please demonstrate a more efficient way of performing this task?

I've tried using the case statement to assign the parameters as a variable and or to logically check data passed to the application. Of course neither of these attempts worked because of the data-types used.


Comments

  • zibadianzibadian Posts: 6,349Member
    : Hello,
    :
    : I'm very new to Pascal, been learning about it for around 4hrs now.
    : Below is the start of a program that is in the works. It will be a
    : command line application taking three parameters: start ip (sIp) end
    : ip (eIp) and a DNS server address.
    :
    : [code]:
    :
    : program dnsaudit;
    :
    : uses
    : sockets, inetaux;
    :
    :
    : var
    : i: integer;
    : sIp, eIp: AnsiString;
    : gtOpt: Pchar;
    :
    : begin (* Main *)
    :
    : for i := 0 to ParamCount do
    : begin
    : if ParamStr(i) = '-s' then
    : begin
    : sIp := ParamStr(i +1);
    : writeln(sIp);
    : end;
    : if ParamStr(i) = '-e' then
    : begin
    : eIp := ParamStr(i +1);
    : writeln(eIp);
    : end
    : else
    : begin
    : if ParamStr(i) <> '-s' then
    : begin
    : if ParamStr(i) <> '-e' then
    : begin
    : if ParamStr(i) <> eIp then
    : begin
    : if ParamStr(i) <> sIp then
    : begin
    : writeln('Invalid parameter or input specified, exiting...');
    : exit();
    : end
    :
    : end
    : end
    :
    : end
    : end;
    : end;
    :
    : //writeln(ParamCount);
    :
    :
    : end. (* Main *)[/code]:
    :
    : I would like the application to check each parameter and either
    : assign a value from the parameter or exit if invalid data is passed
    : to the application. As you can see the nested if statements are not
    : the most efficient way of checking for invalid input. Could someone
    : please demonstrate a more efficient way of performing this task?
    :
    : I've tried using the case statement to assign the parameters as a
    : variable and or to logically check data passed to the application.
    : Of course neither of these attempts worked because of the data-types
    : used.
    :
    :
    :
    You could use an if-then-else-if-then-else-construction:
    [code]
    if ParamStr(i) = '-s' then
    begin
    end else if ParamStr(i) = '-e' then
    begin
    end else
    begin
    // ParamStr(i) is neither a -e nor a -s
    end;
    [/code]
    It is also better to use a while-loop instead of a for-loop, because you can control the counter:
    [code]
    [b]i := 1;[/b]
    while i <= ParamCount do
    begin
    if (ParamStr(i) = '-s') and (i <ParamCount) then
    begin
    i++;
    data = ParamStr(i);
    end else if ParamStr(i) = '-e' then
    begin
    i++;
    data = ParamStr(i);
    end else
    begin
    // Error in parameters
    end;
    i++;
    end;
    [/code]
    Also note how i begins at 1. ParamStr 0 = executable name.
  • ActorActor Posts: 438Member
    For your consideration:
    [code]
    program dnsaudit ;

    Function sIp_Valid (Str : String) : Boolean ;
    begin
    {
    a stub
    }
    end ;

    Function eIp_Valid (Str : String) : Boolean ;
    begin
    {
    a stub
    }
    end ;

    Function DNS_Valid (Str : String) : Boolean ;
    begin
    {
    a stub
    }
    end ;

    Procedure DoIt (sIp, eIp, DNS : String) ;
    {
    working part of the program
    }
    begin
    {
    a stub
    }
    end ;

    begin
    if ParamCount >= 3 then begin
    if
    (sIp_Valid(ParamStr(1)))
    and
    (eIP_Valid(ParamStr(2)))
    and
    (DNS_Valid(ParamStr(3)))
    then
    DoIt (ParamStr(1),ParamStr(2), ParamStr(3))
    else
    WriteLn ('Invalid Parameter!')
    end
    else
    WriteLn ('Not enough parameters!')
    end.
    [/code]

    Here the "main" part of the program merely does all the housekeeping and then turns the actual work over to the procedure [b]DoIt[/b]. The first thing it does is to check whether there are at least three parameters on the command line. If there are more then the extra ones are ignored. If there are fewer then presumably the program does hot have enough data to do its job and the program terminates with the message 'Not enough parameters!' I prefer to put the termination message in the [b]else[/b] part of the [b]if[/b] statement instead of using [b]halt[/b] or [b]exit[/b] to bug out. Whatever.

    Validity of each parameter is embodied in three boolean functions [b]sIp_Valid, eIp_Valid[/b] and [b]DNS_Valid[/b]. This assumes that the tests for each parameter are unique. If they are the same then one Function could be defined and called thus:
    [code]
    if
    Valid(ParamStr(1))
    and
    Valid(ParamStr(2))
    and
    Valid(ParamStr(3))
    then
    [/code]

    I've assumed that the values of sIp, eIP and Dns are simply ParamStr(1), ParamStr(2) and ParamStr(3). If not then functions for converting the parameters can be written and called:
    [code]
    DoIt (eIpVal(ParamStr(1)), sIpVal(ParamStr(2)), DnsVal(ParamStr(3)) ;
    [/code]

    Approaching it this way the main part of the program is only 15 lines and the complexities are shuffled off to other procedures and functions where the can be handled one at at time. The procedure [b]DoIt[/b] can repeat this strategy and ultimately the entire program can consist of simple, easily understood modules.

    Actor

  • tekiotekio Posts: 3Member
    Thanks. That was exactly the kind of feedback I was looking for. You guys are awesome!
Sign In or Register to comment.