comp.lang.ada
 help / color / mirror / Atom feed
* ObjectAda: LPCSTR -> STRING ?
@ 1998-01-05  0:00 matthew_snyder
  1998-01-06  0:00 ` Martin C. Carlisle
  1998-01-06  0:00 ` Robin Stephe Nora
  0 siblings, 2 replies; 9+ messages in thread
From: matthew_snyder @ 1998-01-05  0:00 UTC (permalink / raw)



I'm working with Aonix ObjectAda 7.1 and I need to figure
out a way to convert a LPCSTR to an Ada STRING.  I'd appreciate
any help.  Thanks.

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-05  0:00 matthew_snyder
  1998-01-06  0:00 ` Martin C. Carlisle
@ 1998-01-06  0:00 ` Robin Stephe Nora
  1998-01-06  0:00   ` Robert Dewar
  1 sibling, 1 reply; 9+ messages in thread
From: Robin Stephe Nora @ 1998-01-06  0:00 UTC (permalink / raw)



matthew_snyder@hotmail.com wrote:
> 
> I'm working with Aonix ObjectAda 7.1 and I need to figure
> out a way to convert a LPCSTR to an Ada STRING.  I'd appreciate
> any help.  Thanks.
> 

I assume you are using ObjectAda 7.1 for Windows (95 or NT?), with the
Win32Ada binding (it is helpful if you give these details). If you chase
thru all the subtype definitions, you will find that LPCSTR is actually
System.Address; it is the address of the first character in the string.
It is used to pass strings to/from Win32 API calls. The simplest way to
use it is to take the address of the first character of the Ada string.
Here is the code I use to erase an entry in a profile (.ini) file:

   function Win32_WritePrivateProfileStringA
      (LpSection  : in System.Address;
       lpKeyName  : in System.Address;
       lpString   : in System.Address;
       lpFileName : in System.Address)
      return Interfaces.C.Int;
   pragma Import (StdCall,
Win32_WritePrivateProfileStringA,                  
"WritePrivateProfileStringA");

   procedure Erase
      (File : in String;
       Section : In String)
   is
      -- null lpKeyName deletes Section in File.
      Status : Interfaces.C.Int;
      C_File : constant String := File & Character'First;
      C_Section : constant String := Section & Character'First;
   begin
      Status := Win32_WritePrivateProfileStringA
         (LpSection => C_Section (C_Section'First)'address,
          LpKeyName => System.Null_Address,
          LpString => C_Section (C_Section'First)'Address,
          LpFileName => C_File (C_File'First)'Address);
      Check_Status (Status);
   end Erase;

Check_Status raises an exception for an error status.

You can also use the facilities in Interfaces.C.String, but since the
win32ada bindings do not use those types directly, this always involves
unchecked conversion. I find the use of 'address cleaner.

-- Stephe




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-06  0:00 ` Robin Stephe Nora
@ 1998-01-06  0:00   ` Robert Dewar
  1998-01-07  0:00     ` Stephen Leake
  0 siblings, 1 reply; 9+ messages in thread
From: Robert Dewar @ 1998-01-06  0:00 UTC (permalink / raw)



Robin said

<<         (LpSection => C_Section (C_Section'First)'address,
          LpKeyName => System.Null_Address,
          LpString => C_Section (C_Section'First)'Address,
          LpFileName => C_File (C_File'First)'Address);
>>

Note that for any compiler following the implementation advice in the RM
(all currently available Ada 95 compilers do so), the use of the
X'First subscript here is obfuscation. In fact there is no real reason
to suppose that it will work any better than simply saying
C_File'Address, since neither is really guaranteed to work in an absolute
semantic sense, and in practice they are interchangable.

I think it is reasonable to avoid FUD about compilers not doing the
sensible thing if that FUD results in obscuring the code.





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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-05  0:00 matthew_snyder
@ 1998-01-06  0:00 ` Martin C. Carlisle
  1998-01-11  0:00   ` Jerry van Dijk
  1998-01-12  0:00   ` Pascal Obry
  1998-01-06  0:00 ` Robin Stephe Nora
  1 sibling, 2 replies; 9+ messages in thread
From: Martin C. Carlisle @ 1998-01-06  0:00 UTC (permalink / raw)



In article <884055877.1213464581@dejanews.com>,
 <matthew_snyder@hotmail.com> wrote:
>I'm working with Aonix ObjectAda 7.1 and I need to figure
>out a way to convert a LPCSTR to an Ada STRING.  I'd appreciate
>any help.  Thanks.

One must assume your question is a Win32Ada question, not specific
to Object Ada.

LPCSTR is equivalent to C's (const char *).

I'd suggest something along the following:

function To_String(c_pointer : in Win32.LPCSTR) return String is
   length       : natural;
   Ignore_lpstr : Win32.LPSTR;
begin
   length := Natural(Win32.Winbase.lstrlen(c_pointer));
   declare
      result : String(1..length+1);
   begin
      Ignore_lpstr := Win32.Winbase.lstrcpy(result'address,
         c_pointer);
      return result(1..length); -- omit null terminating character
   end;
end To_String;

--Martin
-- 
Martin C. Carlisle, Computer Science, US Air Force Academy
mcc@cs.usafa.af.mil, http://www.usafa.af.mil/dfcs/bios/carlisle.html
DISCLAIMER:  This content in no way reflects the opinions, standard or 
policy of the US Air Force Academy or the United States Government.




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-06  0:00   ` Robert Dewar
@ 1998-01-07  0:00     ` Stephen Leake
  0 siblings, 0 replies; 9+ messages in thread
From: Stephen Leake @ 1998-01-07  0:00 UTC (permalink / raw)



Robert Dewar wrote:
> 
> Robin said

Actually, I'm Stephe; my mailer is confused.

> 
> <<         (LpSection => C_Section (C_Section'First)'address,
>           LpKeyName => System.Null_Address,
>           LpString => C_Section (C_Section'First)'Address,
>           LpFileName => C_File (C_File'First)'Address);
> >>
> 
> Note that for any compiler following the implementation advice in the RM
> (all currently available Ada 95 compilers do so), the use of the
> X'First subscript here is obfuscation. In fact there is no real reason
> to suppose that it will work any better than simply saying
> C_File'Address, since neither is really guaranteed to work in an absolute
> semantic sense, and in practice they are interchangable.
> 
> I think it is reasonable to avoid FUD about compilers not doing the
> sensible thing if that FUD results in obscuring the code.

Ok, I'll change the code. This was a hold-over from the Ada 83 idiom,
where the advice I got was that 'address might return the address of the
descriptor in some compilers. One more place where I should have read
the relevant Ada 95 RM first!

-- Stephe




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-06  0:00 ` Martin C. Carlisle
@ 1998-01-11  0:00   ` Jerry van Dijk
  1998-01-12  0:00   ` Pascal Obry
  1 sibling, 0 replies; 9+ messages in thread
From: Jerry van Dijk @ 1998-01-11  0:00 UTC (permalink / raw)



In article <68tgdg$c7c$1@cnn.Princeton.EDU> mcc@tyrolia.cs.princeton.edu writes:

>>I'm working with Aonix ObjectAda 7.1 and I need to figure
>>out a way to convert a LPCSTR to an Ada STRING.  I'd appreciate
>>any help.  Thanks.

>LPCSTR is equivalent to C's (const char *).

Why not simply use something like:

with System.C.Strings; use System.C.Strings;

Win_String : Win32.LPCSTR := -- get the string;

Ada_String : String := Value(chars_ptr(Win_String));

--

-- Jerry van Dijk | Leiden, Holland
-- Consultant     | Team Ada
-- Ordina Finance | jdijk@acm.org




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-06  0:00 ` Martin C. Carlisle
  1998-01-11  0:00   ` Jerry van Dijk
@ 1998-01-12  0:00   ` Pascal Obry
  1 sibling, 0 replies; 9+ messages in thread
From: Pascal Obry @ 1998-01-12  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 902 bytes --]


What about :

   LP : Win32.LPCSTR;

   S : String := Interfaces.C.Strings.Value (Win32.To_Chars_Ptr (LP));

Pascal.

--

--|------------------------------------------------------------
--| Pascal Obry                               Team-Ada Member |
--|                                                           |
--| EDF-DER-IPN-SID- Ing�nierie des Syst�mes d'Informations   |
--|                                                           |
--| Bureau G1-010           e-mail: pascal.obry@der.edfgdf.fr |
--| 1 Av G�n�ral de Gaulle  voice : +33-1-47.65.50.91         |
--| 92141 Clamart CEDEX     fax   : +33-1-47.65.50.07         |
--| FRANCE                                                    |
--|------------------------------------------------------------
--|
--|   http://ourworld.compuserve.com/homepages/pascal_obry
--|
--|   "The best way to travel is by means of imagination"







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

* RE: ObjectAda: LPCSTR -> STRING ?
@ 1998-01-20  0:00 matthew_snyder
  1998-01-21  0:00 ` Pascal Obry
  0 siblings, 1 reply; 9+ messages in thread
From: matthew_snyder @ 1998-01-20  0:00 UTC (permalink / raw)



OK.  Here's what I ended up doing.  Please let me know if there's an
easier way.

   type CHARACTER_POINTER is access CHARACTER;

  function TO_CHARACTER_POINTER is new ADA.UNCHECKED_CONVERSION(
WIN32.LPCSTR, CHARACTER_POINTER );  function TO_UNSIGNED_32 is new
ADA.UNCHECKED_CONVERSION( WIN32.LPCSTR, UNSIGNED_32 );	function
TO_LPCSTR is new ADA.UNCHECKED_CONVERSION( UNSIGNED_32, WIN32.LPCSTR );


   procedure TO_ADA_STRING( STRING_PTR : in WIN32.LPCSTR;
                        NEW_STRING : in out STRING;
                        LAST       : in out INTEGER ) is

      TEMP_STRING_PTR : WIN32.LPCSTR := STRING_PTR;
      ADA_STRING      : STRING( 1 .. 80 );
      TEMP_32         : UNSIGNED_32;
      CHAR_PTR        : CHARACTER_POINTER;

   begin

      -- Get the length of the string
      LAST := INTEGER( WIN32.WINBASE.LSTRLEN( STRING_PTR ) );

      -- Loop through and copy "C" string to Ada string
      for STRING_INDEX in 1 .. LAST loop

         -- Convert the string pointer to a character pointer
         CHAR_PTR := TO_CHARACTER_POINTER( TEMP_STRING_PTR );
         -- Add the character to the ada string
         ADA_STRING( STRING_INDEX ) := CHAR_PTR.all;

         -- Convert the string pointer to an unsigned 32
         TEMP_32 := TO_UNSIGNED_32( TEMP_STRING_PTR );
         -- Increment the unsigned 32
         TEMP_32 := TEMP_32 + 1;
         -- Convert the unsigned 32 back to a string pointer
         TEMP_STRING_PTR := TO_LPCSTR( TEMP_32 );

      end loop;

  NEW_STRING( 1 .. NEW_STRING'LAST ) := ADA_STRING( 1 .. NEW_STRING'LAST
);

   end TO_ADA_STRING;

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet




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

* Re: ObjectAda: LPCSTR -> STRING ?
  1998-01-20  0:00 ObjectAda: LPCSTR -> STRING ? matthew_snyder
@ 1998-01-21  0:00 ` Pascal Obry
  0 siblings, 0 replies; 9+ messages in thread
From: Pascal Obry @ 1998-01-21  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2817 bytes --]


>OK.  Here's what I ended up doing.  Please let me know if there's an
>easier way.
>

Yes there is ! And it has already been posted (12/01/98) :

   LP : Win32.LPCSTR;

   S : String := Interfaces.C.Strings.Value (Win32.To_Chars_Ptr (LP));

Pascal.
--

--|------------------------------------------------------------
--| Pascal Obry                               Team-Ada Member |
--|                                                           |
--| EDF-DER-IPN-SID- Ing�nierie des Syst�mes d'Informations   |
--|                                                           |
--| Bureau G1-010           e-mail: pascal.obry@der.edfgdf.fr |
--| 1 Av G�n�ral de Gaulle  voice : +33-1-47.65.50.91         |
--| 92141 Clamart CEDEX     fax   : +33-1-47.65.50.07         |
--| FRANCE                                                    |
--|------------------------------------------------------------
--|
--|   http://ourworld.compuserve.com/homepages/pascal_obry
--|
--|   "The best way to travel is by means of imagination"

matthew_snyder@hotmail.com a �crit dans le message
<885329887.836370436@dejanews.com>...

>   type CHARACTER_POINTER is access CHARACTER;
>
>  function TO_CHARACTER_POINTER is new ADA.UNCHECKED_CONVERSION(
>WIN32.LPCSTR, CHARACTER_POINTER );  function TO_UNSIGNED_32 is new
>ADA.UNCHECKED_CONVERSION( WIN32.LPCSTR, UNSIGNED_32 ); function
>TO_LPCSTR is new ADA.UNCHECKED_CONVERSION( UNSIGNED_32, WIN32.LPCSTR );
>
>
>   procedure TO_ADA_STRING( STRING_PTR : in WIN32.LPCSTR;
>                        NEW_STRING : in out STRING;
>                        LAST       : in out INTEGER ) is
>
>      TEMP_STRING_PTR : WIN32.LPCSTR := STRING_PTR;
>      ADA_STRING      : STRING( 1 .. 80 );
>      TEMP_32         : UNSIGNED_32;
>      CHAR_PTR        : CHARACTER_POINTER;
>
>   begin
>
>      -- Get the length of the string
>      LAST := INTEGER( WIN32.WINBASE.LSTRLEN( STRING_PTR ) );
>
>      -- Loop through and copy "C" string to Ada string
>      for STRING_INDEX in 1 .. LAST loop
>
>         -- Convert the string pointer to a character pointer
>         CHAR_PTR := TO_CHARACTER_POINTER( TEMP_STRING_PTR );
>         -- Add the character to the ada string
>         ADA_STRING( STRING_INDEX ) := CHAR_PTR.all;
>
>         -- Convert the string pointer to an unsigned 32
>         TEMP_32 := TO_UNSIGNED_32( TEMP_STRING_PTR );
>         -- Increment the unsigned 32
>         TEMP_32 := TEMP_32 + 1;
>         -- Convert the unsigned 32 back to a string pointer
>         TEMP_STRING_PTR := TO_LPCSTR( TEMP_32 );
>
>      end loop;
>
>  NEW_STRING( 1 .. NEW_STRING'LAST ) := ADA_STRING( 1 .. NEW_STRING'LAST
>);
>
>   end TO_ADA_STRING;
>
>-------------------==== Posted via Deja News ====-----------------------
>      http://www.dejanews.com/     Search, Read, Post to Usenet






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

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

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-20  0:00 ObjectAda: LPCSTR -> STRING ? matthew_snyder
1998-01-21  0:00 ` Pascal Obry
  -- strict thread matches above, loose matches on Subject: below --
1998-01-05  0:00 matthew_snyder
1998-01-06  0:00 ` Martin C. Carlisle
1998-01-11  0:00   ` Jerry van Dijk
1998-01-12  0:00   ` Pascal Obry
1998-01-06  0:00 ` Robin Stephe Nora
1998-01-06  0:00   ` Robert Dewar
1998-01-07  0:00     ` Stephen Leake

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