View previous topic :: View next topic |
Author |
Message |
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Tue Aug 18, 2009 10:01 am Post subject: assign char* to string type? |
|
|
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 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 |
|
|
michaelp
Joined: 27 Jul 2008 Posts: 114
|
Posted: Tue Aug 18, 2009 12:04 pm Post subject: |
|
|
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 |
|
|
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Tue Aug 18, 2009 1:15 pm Post subject: |
|
|
Thanks Michael I really appreciate your quick response. |
|
Back to top |
|
|
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Tue Aug 18, 2009 1:18 pm Post subject: |
|
|
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 |
|
|
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Tue Aug 18, 2009 1:42 pm Post subject: |
|
|
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 |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Tue Aug 18, 2009 9:51 pm Post subject: |
|
|
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 |
|
|
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Thu Aug 20, 2009 9:17 am Post subject: |
|
|
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 |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Fri Aug 21, 2009 12:29 am Post subject: |
|
|
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 |
|
|
jkinsey
Joined: 05 Aug 2009 Posts: 17
|
Posted: Fri Aug 21, 2009 9:43 am Post subject: |
|
|
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. |
|
Back to top |
|
|
|