comp.lang.ada
 help / color / mirror / Atom feed
* String manipulation question
@ 1996-04-30  0:00 David Morton
  0 siblings, 0 replies; 2+ messages in thread
From: David Morton @ 1996-04-30  0:00 UTC (permalink / raw)



First, thanks to all who have replied to my previous two posts...

Now I have a new problem.
I'm making a database program (from scratch, to learn how)
and need to put the fields in a file
using direct_IO.  As expected, Unbounded_Strings
did not work with Direct_IO, as I assume Direct_IO
needs an exact byte count of the record to find succesive
records (makes sense).

So for strings, I have to use a bounded string...
for my purposes, let's say 1..20.
Right now, I have to "mask" out the parts of the string that doesn't
get used like so:

 type F_S_Flag is (Faculty, Student);   
 type User_Record is record
        UName, Real_Name, Street_Address, City, State, Zip, Phone,
        Added, Deleted, Exit_Status, Comments : String(1..40);
        FS_Flag : F_S_Flag;
 end record;
 User_Record_Mask : User_Record :=
    ("                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     "                                        ",
     Student);



        Put("User Name: ");
        Get_Line(data.UName,Last);
        data.Uname(Last+1..data.Uname'Last) :=
        User_Record_Mask.Uname(Last+1..User_Record_Mask.Uname'Last);

 this    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
seems messy, but it must be done in order to maintain the constant length.
Null terminated strings would work, but isn't the implentation of it in Ada
just as messy as this? (How would you implement it?)

Thanks!

-- 
David Morton
 mailto:dmorton@jinx.sckans.edu    // If you use Netscape 2.0,
 205 College, Winfield, KS 67156   // you can click on the mailto: part to reply!
                                   (HINT, HINT)  :)





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

* Re: String manipulation question
@ 1996-05-01  0:00 tmoran
  0 siblings, 0 replies; 2+ messages in thread
From: tmoran @ 1996-05-01  0:00 UTC (permalink / raw)



>as I assume Direct_IO needs an exact byte count of the record
>to find succesive records (makes sense).
>...
>type F_S_Flag is (Faculty, Student);
>type User_Record is record
>       UName, Real_Name, Street_Address, City, State, Zip, Phone,
>       Added, Deleted, Exit_Status, Comments : String(1..40);
>       FS_Flag : F_S_Flag;
>end record;
>       Put("User Name: ");
>       Get_Line(data.UName,Last);
>       data.Uname(Last+1..data.Uname'Last) :=
>       User_Record_Mask.Uname(Last+1..User_Record_Mask.Uname'Last);
>seems messy, but it must be done in order to maintain the constant length.
>Null terminated strings would work, but isn't the implentation of it in Ada
>just as messy as this? (How would you implement it?)
How about:
  Default_User_Record : constant User_Record
    :=(UName | Real_Name | Street_Address | City | State | Zip | Phone |
       Added | Deleted | Exit_Status | Comments => (1..40=>' '),
       FS_Flag => Student);

    procedure Fetch(prompt:in string; field:in out string) is
      buffer:string(field'range);
      last:integer;
    begin
      Put(prompt);
      Get_Line(buffer, last);
      -- just fill as much of Field as was entered.  Leave the rest alone.
      field(field'first .. last):=buffer(buffer'first .. last);
      -- if input was as long or longer than buffer, skip to next line
      if last = buffer'last then Skip_Line;end if;
    end Fetch;
and
    data:=Default_User_Record;     -- initialize all fields with defaults
    Fetch("User Name: ",      data.UName);
    Fetch("Real Name: ",      data.Real_Name);
    Fetch("Street Address: ", data.Street_Address);
etc.




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

end of thread, other threads:[~1996-05-01  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-01  0:00 String manipulation question tmoran
  -- strict thread matches above, loose matches on Subject: below --
1996-04-30  0:00 David Morton

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