FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

assign char* to string type?

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Tue Aug 18, 2009 10:01 am    Post subject: assign char* to string type? Reply with quote

Ok I have looked and looked. Is there a way to assign or copy a char* to a string type? I am a C programmer so I know nothing about string classes or classes in general. A class to me is were a you go to learn something from a teacher Wink What I am really trying to do is create an array of type string and copy the char* pointers into that array. I figured I would use the std.string class since it is there. That way I could walk them as a collection of strings with the foreach loop. Am I way off base here? I have looked for an example of this but I cannot find one.
Back to top
View user's profile Send private message
michaelp



Joined: 27 Jul 2008
Posts: 114

PostPosted: Tue Aug 18, 2009 12:04 pm    Post subject: Reply with quote

So you want to have a char*, and turn it into a char[]?
Code:
import std.string;
char* cString;
char[] dString;
dString = toString( cString );

or the other way around:
Code:
import std.string;
char[] dString = "ba";
char* cString;
cString = toStringz( dString );
Back to top
View user's profile Send private message
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Tue Aug 18, 2009 1:15 pm    Post subject: Reply with quote

Thanks Michael I really appreciate your quick response.
Back to top
View user's profile Send private message
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Tue Aug 18, 2009 1:18 pm    Post subject: Reply with quote

Ok when I try this for DMD2 I get the following error..

Quote:
toString(char*) called from Z:\dmd2\proj\GadgetD\GadgetD.d(44) is deprecated. Instead you may want to import std.conv and use to!string(x) instead of toString(x).
Back to top
View user's profile Send private message
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Tue Aug 18, 2009 1:42 pm    Post subject: Reply with quote

Nevermind I figured it out. I can use the DMD2 std.string.format(); method. I guess I should have quantify which version of D I am using. Anyway what I am trying to do is split multiple null terminated C strings into an array of D strings. This is the format of the Windows Registry type called REG_MULTI_SZ.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Tue Aug 18, 2009 9:51 pm    Post subject: Reply with quote

You don't need format(). Just import std.conv and use
Code:
to!(string)(cString)
, which is what that error message is trying to tell you, except that for some reason the template instance in the error message is losing its parentheses. Almost all conversions with D2 are done using to!(type)(value).

Edited to add that for some cases where you might use std.string.format(), you might consider using std.conv.text() instead, specifically those cases where you don't need any advanced formatting.
http://digitalmars.com/d/2.0/phobos/std_conv.html#text
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Thu Aug 20, 2009 9:17 am    Post subject: Reply with quote

Ok got that to work now. Can you explain what the '!' character... Basicly how do I say (ie...read/write,express) the following in psuedo code?

Code:
to!(string)(Cstring);


This syntax is very strange to me and I have never seen it in other languages. It looks like a cast but not sure what the extra set of '()' are for.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Fri Aug 21, 2009 12:29 am    Post subject: Reply with quote

The !() syntax is explicit template instantiation. In this case it is first instantiating one of the templates named "to" giving it the type "string" as a parameter, then the second parentheses are calling the generated function with cString as the parameter.

It is somewhat analogous to the A<B> syntax from C++ and Java, with some differences in usage. For example, the "to" templates actually all need more than one parameter, but the rest are implicit, such as the function parameter type, so you only actually need to specify the target type. Read more here:
http://digitalmars.com/d/2.0/template.html
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
jkinsey



Joined: 05 Aug 2009
Posts: 17

PostPosted: Fri Aug 21, 2009 9:43 am    Post subject: Reply with quote

csauls, I do appreciate your help and your patients with a NOOB like myself. Anyway here is one of my solutions that sparked this thread.

Code:

   // Local buffer to hold command parameters that is a
   // C buffer delimited by the \0 null character
   char* cmdparambuf = cast(char*)Params;
   // boolean used to control outter loop
   bool nextp = true;
   // holds the length of the current C string
   int paramlen;
   // tokenized command buffer
   string cmdtokenizedbuf;

   // loop through the \0 seperated command params buffer
   // replacing the \0 with ^ token.
   while (nextp == true)
   {
      paramlen = strlen(cmdparambuf);
      if(paramlen > 0)
      {
         
         // concat the command parameter to the tokenized command buffer
         if (cmdtokenizedbuf.length == 0)
         {
            cmdtokenizedbuf = to!(string)(cmdparambuf);
         } else
         {
            cmdtokenizedbuf ~= "~";
            cmdtokenizedbuf ~= to!(string)(cmdparambuf);
         }
      } else
      {
         nextp = false;
      }
      // move command params buffer pointer to the next \0
      cmdparambuf += paramlen;
      // move pointer to the first character of next C string;
      cmdparambuf++;
    }

   // split the tokenized command buffer into a D char array with the std.string.split method.
   immutable(char)[][] cmdparams = split(cmdtokenizedbuf,"~");
   // loop through command parameters and do what they tell you to do
   foreach(immutable(char)[] param;cmdparams) 
   {
      // for demo's sake send parameter as a event
      eventproc(cast(char*)param.dup);
   }


A little background here. This code is the beginnings of a plugin (ie... extension) for another language called Visual DialogScript. It allows me to walk the command parameters that are sent as a set of null terminated C strings and convert them into a D array so I can easily walk the commands with a foreach loop. This is probably not very effecient code but does make it simple to manage the command parameters and it keeps everything local to the function that the Visual DialogScript language is calling in my extension DLL. Where as the way I was doing this in C would have called for a global buffer this code keeps it local. Again I do appreciate your help. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group