comp.lang.ada
 help / color / mirror / Atom feed
* need a little string hint...
@ 2005-02-28 16:08 mferracini
  2005-02-28 16:26 ` Adrien Plisson
  2005-03-01  3:07 ` Steve
  0 siblings, 2 replies; 12+ messages in thread
From: mferracini @ 2005-02-28 16:08 UTC (permalink / raw)


i need to read from a file "config.sys" host&port number.
but i have a problem with host...

if i call: Get_Host_By_Name ("localhost") it work fine,
i i use the string read from the file don't work.

an hint?

grazie.

-------------------------------------------------------------

procedure Client is
   Server     : Sock_Addr_Type;
   Config     : Ada.Text_Io.File_Type;
   Host       : String (1 .. 20)      := (others => ' ');
   Port_Value : String (1 .. 20)      := (others => ' ');
   Last       : Natural               := 0;
   Port       : Port_type             := 0;

begin
   --Inizializza
    --leggi da config
   Ada.Text_Io.Open(
      File => Config,
      Mode => Ada.Text_Io.In_File,
      Name => "config.ini"); --case sensitive?

   Ada.Text_Io.Get_Line(
      File => Config,
      Item => Host,
      Last => Last);

   Ada.Text_Io.Get_Line(
      File => Config,
      Item => Port_Value,
      Last => Last);
   Ada.Integer_Text_Io.Get(Port_Value,natural(Port),Last);

    --open socket
   Initialize(False); --true??
   Server.Addr := Addresses (Get_Host_By_Name (host), 1);
   Server.Port := Port;
   Ada.Text_Io.Put_Line("OK");




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:08 need a little string hint mferracini
@ 2005-02-28 16:26 ` Adrien Plisson
  2005-02-28 16:49   ` mferracini
  2005-02-28 16:51   ` mferracini
  2005-03-01  3:07 ` Steve
  1 sibling, 2 replies; 12+ messages in thread
From: Adrien Plisson @ 2005-02-28 16:26 UTC (permalink / raw)


mferracini wrote:
> i need to read from a file "config.sys" host&port number.
> but i have a problem with host...
> 
> if i call: Get_Host_By_Name ("localhost") it work fine,
> i i use the string read from the file don't work.

what happens if you try Get_Host_By_Name ("localhost           ") ?

in your file, you read the host but discard the value of Last after 
the call, this value holds the length of the input string. so your 
string does not contain "localhost" but "localhost__________" (replace 
'_' by ' ').

(it may not be the problem, but i can't test it)

-- 
rien



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:26 ` Adrien Plisson
@ 2005-02-28 16:49   ` mferracini
  2005-02-28 16:53     ` Andre
  2005-02-28 17:49     ` Jeffrey Carter
  2005-02-28 16:51   ` mferracini
  1 sibling, 2 replies; 12+ messages in thread
From: mferracini @ 2005-02-28 16:49 UTC (permalink / raw)


mhh and how i can trim away the ' '?




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:26 ` Adrien Plisson
  2005-02-28 16:49   ` mferracini
@ 2005-02-28 16:51   ` mferracini
  2005-02-28 16:59     ` Adrien Plisson
  1 sibling, 1 reply; 12+ messages in thread
From: mferracini @ 2005-02-28 16:51 UTC (permalink / raw)



Adrien Plisson wrote:

>
> what happens if you try Get_Host_By_Name ("localhost           ") ?

raised GNAT.SOCKETS.HOST_ERROR : Host not found :(




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:49   ` mferracini
@ 2005-02-28 16:53     ` Andre
  2005-02-28 17:29       ` Adrien Plisson
  2005-02-28 17:49     ` Jeffrey Carter
  1 sibling, 1 reply; 12+ messages in thread
From: Andre @ 2005-02-28 16:53 UTC (permalink / raw)


Trim (String, Both)



mferracini wrote:
> mhh and how i can trim away the ' '?
> 


-- 
---------------------------------------------------
Two men say they're Jesus one of them must be wrong
Industrial disease - Love over Golde - Dire Straits

Andr�


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:51   ` mferracini
@ 2005-02-28 16:59     ` Adrien Plisson
  0 siblings, 0 replies; 12+ messages in thread
From: Adrien Plisson @ 2005-02-28 16:59 UTC (permalink / raw)


mferracini wrote:
> Adrien Plisson wrote:
> 
>>what happens if you try Get_Host_By_Name ("localhost           ") ?
> 
> raised GNAT.SOCKETS.HOST_ERROR : Host not found :(

so the solution is evident: make use of the Last parameter after 
reading the host name from the file...

-- 
rien



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:53     ` Andre
@ 2005-02-28 17:29       ` Adrien Plisson
  2005-03-01 21:25         ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Adrien Plisson @ 2005-02-28 17:29 UTC (permalink / raw)


Andre wrote:
> Trim (String, Both)
> 
> mferracini wrote:
> 
>> mhh and how i can trim away the ' '?
>> 

and what if the hostname contains a voluntary space at the end or at 
the beginning ?

this may happen because the configuration file writer wanted spaces 
converted to "%20", or it may just happen because somebody mistyped 
the hostname in the configuration file.

in any case, your solution prevent from taking the appropriate action 
since it removes the spaces... with all the security risks implied.

-- 
rien



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:49   ` mferracini
  2005-02-28 16:53     ` Andre
@ 2005-02-28 17:49     ` Jeffrey Carter
  1 sibling, 0 replies; 12+ messages in thread
From: Jeffrey Carter @ 2005-02-28 17:49 UTC (permalink / raw)


mferracini wrote:

> mhh and how i can trim away the ' '?

Note that they are only ' ' because you initialized Host to all spaces; 
otherwise they would be stack junk.

Because you know they are ' ', you could use Ada.Strings.Fixed.Trim, but 
that's probably overkill for this case. Another way, and one that works 
even if the unused positions contain stack junk, is

Ada.Text_IO.Get_Line (File => Config, Item => Host, Last => Host_Last);
... -- No change to Host_Last
Server.Addr :=
    Addresses (Get_Host_By_Name (Host (Host'First .. Host_Last) ), 1);

-- 
Jeff Carter
"Why don't you bore a hole in yourself and let the sap run out?"
Horse Feathers
49



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 16:08 need a little string hint mferracini
  2005-02-28 16:26 ` Adrien Plisson
@ 2005-03-01  3:07 ` Steve
  1 sibling, 0 replies; 12+ messages in thread
From: Steve @ 2005-03-01  3:07 UTC (permalink / raw)


In Ada a "String" is just an array of characters.  The length of the string 
is given when the string is declared, as in:

  Host       : String (1 .. 20)      := (others => ' ');

When the call
    Get_Host_By_Name ("localhost")

is used the string is implicitly declared to have the length of the string 
"localhost".

There are a number of ways of dealing with Ada's fixed length strings, the 
one to use depends on context and personal preference.  I'll give an 
example:

One approach is to create an unbounded string from the substring read by 
Get_Line:

with Ada.Strings.Unbounded;
 use Ada.Strings.Unbounded;
...
  Host : Unbounded_String;
...
  declare
    inputBuffer : String( 1 .. 32 );
    last             : Natural;
  begin
    Ada.Text_Io.Get_Line(
        File => Config,
        Item => inputBuffer ,
        Last => last );
    Host := To_Unbounded_String( inputBuffer( 1 .. last ) );
  end;

...
  Server.Addr := Addresses (Get_Host_By_Name (To_String( host )), 1);

  Another approach is to keep track of a separate "last" for each string, 
and then use the slice of the array directly.

  That is:

  Server.Addr := Addresses(Get_Host_By_Name( Host( 1 .. Last ) ) );


BTW: I haven't compiled the example above, but I know the basic technique 
works.

Steve
(The Duck)


"mferracini" <maurizio.ferracini@gmail.com> wrote in message 
news:1109606915.447479.184780@l41g2000cwc.googlegroups.com...
>i need to read from a file "config.sys" host&port number.
> but i have a problem with host...
>
> if i call: Get_Host_By_Name ("localhost") it work fine,
> i i use the string read from the file don't work.
>
> an hint?
>
> grazie.
>
> -------------------------------------------------------------
>
> procedure Client is
>   Server     : Sock_Addr_Type;
>   Config     : Ada.Text_Io.File_Type;
>   Host       : String (1 .. 20)      := (others => ' ');
>   Port_Value : String (1 .. 20)      := (others => ' ');
>   Last       : Natural               := 0;
>   Port       : Port_type             := 0;
>
> begin
>   --Inizializza
>    --leggi da config
>   Ada.Text_Io.Open(
>      File => Config,
>      Mode => Ada.Text_Io.In_File,
>      Name => "config.ini"); --case sensitive?
>
>   Ada.Text_Io.Get_Line(
>      File => Config,
>      Item => Host,
>      Last => Last);
>
>   Ada.Text_Io.Get_Line(
>      File => Config,
>      Item => Port_Value,
>      Last => Last);
>   Ada.Integer_Text_Io.Get(Port_Value,natural(Port),Last);
>
>    --open socket
>   Initialize(False); --true??
>   Server.Addr := Addresses (Get_Host_By_Name (host), 1);
>   Server.Port := Port;
>   Ada.Text_Io.Put_Line("OK");
> 





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-02-28 17:29       ` Adrien Plisson
@ 2005-03-01 21:25         ` Simon Wright
  2005-03-01 22:29           ` Adrien Plisson
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2005-03-01 21:25 UTC (permalink / raw)


Adrien Plisson <aplisson-news@stochastique.net> writes:

> Andre wrote:
> > Trim (String, Both)
> > mferracini wrote:
> >
> >> mhh and how i can trim away the ' '?
> >>
> 
> and what if the hostname contains a voluntary space at the end or at
> the beginning ?
> 
> this may happen because the configuration file writer wanted spaces
> converted to "%20", or it may just happen because somebody mistyped
> the hostname in the configuration file.
> 
> in any case, your solution prevent from taking the appropriate
> action since it removes the spaces... with all the security risks
> implied.

Of course you may not be talking about DNS -- but I'm pretty sure that
under DNS legal hostname characters are [a-z0-9.-] (case insensitive)
which doesn't include space.

A text-based config file format that includes significant whitespace
as you suggest and doesn't delimit it (eg, with quotes) is asking to
be derided. Just look at make(1).

-- 
Simon Wright                               100% Ada, no bugs.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-03-01 21:25         ` Simon Wright
@ 2005-03-01 22:29           ` Adrien Plisson
  2005-03-03  6:19             ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Adrien Plisson @ 2005-03-01 22:29 UTC (permalink / raw)


Simon Wright wrote:
> Of course you may not be talking about DNS -- but I'm pretty sure that
> under DNS legal hostname characters are [a-z0-9.-] (case insensitive)
> which doesn't include space.

it depends: look at IDN (Internationalized Domain Names), it allows to 
have Unicode characters in domain names. it then depends if your 
configuration file should hold only the ASCII-compatible value, in which 
case my remark is invalid, or if it is allowed to hold the non-encoded 
Unicode value, in which case trimming spaces will certainly yield a 
different result from the actual hostname in the file.

> A text-based config file format that includes significant whitespace
> as you suggest and doesn't delimit it (eg, with quotes) is asking to
> be derided. Just look at make(1).

there is also a problem of incompatibility: if the configuration file is 
intended to be read by many (> 1) different softwares, they all have to 
handle spaces the same way. i don't think trimming spaces is a common 
operation on hostnames, so he had better no do it this way.

-- 
rien




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: need a little string hint...
  2005-03-01 22:29           ` Adrien Plisson
@ 2005-03-03  6:19             ` Simon Wright
  0 siblings, 0 replies; 12+ messages in thread
From: Simon Wright @ 2005-03-03  6:19 UTC (permalink / raw)


Adrien Plisson <aplisson-news@stochastique.net> writes:

> Simon Wright wrote:
> > Of course you may not be talking about DNS -- but I'm pretty sure that
> > under DNS legal hostname characters are [a-z0-9.-] (case insensitive)
> > which doesn't include space.
> 
> it depends: look at IDN (Internationalized Domain Names), it allows
> to have Unicode characters in domain names. it then depends if your
> configuration file should hold only the ASCII-compatible value, in
> which case my remark is invalid, or if it is allowed to hold the
> non-encoded Unicode value, in which case trimming spaces will
> certainly yield a different result from the actual hostname in the
> file.

I did wonder whether IDN would affect matters ..

> > A text-based config file format that includes significant
> > whitespace as you suggest and doesn't delimit it (eg, with quotes)
> > is asking to be derided. Just look at make(1).
> 
> there is also a problem of incompatibility: if the configuration
> file is intended to be read by many (> 1) different softwares, they
> all have to handle spaces the same way. i don't think trimming
> spaces is a common operation on hostnames, so he had better no do it
> this way.

Nothing to do with host names, just the config file format -- which
must be defined somewhere. For example, the hosts(5) format bans
spaces (non-printables, rather) in host names entirely, not just at
the ends!

              IP_address canonical_hostname aliases

       Fields  of the entry are separated by any number of blanks
       and/or tab characters.


-- 
Simon Wright                               100% Ada, no bugs.



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-03-03  6:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-28 16:08 need a little string hint mferracini
2005-02-28 16:26 ` Adrien Plisson
2005-02-28 16:49   ` mferracini
2005-02-28 16:53     ` Andre
2005-02-28 17:29       ` Adrien Plisson
2005-03-01 21:25         ` Simon Wright
2005-03-01 22:29           ` Adrien Plisson
2005-03-03  6:19             ` Simon Wright
2005-02-28 17:49     ` Jeffrey Carter
2005-02-28 16:51   ` mferracini
2005-02-28 16:59     ` Adrien Plisson
2005-03-01  3:07 ` Steve

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox