|
-
September 28th, 2005, 08:55 AM
#1
using null terminated strings with switch/case
how can i use strings inside switch block in c/c++,
what macro i must create to make folowing code work?
Code:
#define CMD_1 "cmd 1"
#define CMD_2 "cmd 2"
Function( char* command )
{
switch(command){
case CMD_1:
...
break;
case CMD_2:
...
break;
}
}
-
September 28th, 2005, 08:58 AM
#2
Re: using null terminated strings with switch/case
You can't. A switch can only be used on itegral types. You should use a bunch of if statements for your case.
-
September 28th, 2005, 09:24 AM
#3
Re: using null terminated strings with switch/case
Well, if you want that, switch to C#. (the pun was intended )
-
September 28th, 2005, 09:29 AM
#4
Re: using null terminated strings with switch/case
When actions for each option are enclosed within separate functions rather than in single 'case block' maybe creating a map of strings to function pointers would be good idea. Basically it would be pretty the same, but it maybe would make your code clearer (if there is a lot of options, and not only 2).
Unfortunately, as Marc said, 'switch' is useful only for integral types.
Hob
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
September 28th, 2005, 10:04 AM
#5
Re: using null terminated strings with switch/case
 Originally Posted by cilu
Well, if you want that, switch to C#. (the pun was intended  )
i know what u mean , but id rather stay with regular c and use if-elseif set.
-
September 28th, 2005, 10:42 AM
#6
Re: using null terminated strings with switch/case
If it is C++ code, you can use a std::map<std::string, AnotherType>.
Here are the types you can use for AnotherType:
- A pointer to an abstract base class containing at least one ExecCommand virtual method, but maybe a more complex interface, depending on your projects.
- A pointer to a function (which executes the command).
- An integer.
This integer can be used as an identifier in a switch statement.
Use this solution, only if the switch clauses, really need the local variables and data of the function.
But, you probably want to code that in C.
In that case, you can try to emulate a std::map class (maybe with a less performant algorithm), or write your own hash table.
If performance is not an issue, or if there are very few switch clauses, you can even use an array of structures (each structure contains a const char* and a pointer to function, or identifier).
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 28th, 2005, 10:51 AM
#7
Re: using null terminated strings with switch/case
If you don't have very many commands to keep track of you could do something like this:
Code:
#define CMD_1 1
#define CMD_2 2
#define SWITCH(x) switch( (strcmp(x,"cmd 1") == 0) ? 1 : \
(strcmp(x ,"cmd 2") == 0) ? 2 : \
0)
void Function( char* command )
{
SWITCH(command){
case CMD_1:
break;
case CMD_2:
break;
}
}
Last edited by wschweit; September 28th, 2005 at 02:47 PM.
-
September 28th, 2005, 11:13 AM
#8
Re: using null terminated strings with switch/case
There is no such language as C/C++.
You either want to write it in C or in C++.
If programming in C++, I'd generally recommend SuperKoko's first method but it's more complex than it appears.
In order to use the polymorphism, you'll need a map from string to pointers to different implementations of an abstract base class. (The virtual method doesn't have to called ExecCommand).
How you'd set this map up and where it belongs will depend on your overall design. In particular, how the instances of these abstract classes are created, and how they are destroyed. If they have no data members then I'd recommend making global instances. Globals should generally be avoided though for any classes that have data members (thread-safety issues, for one. hidden side-effects, etc). You might want instances in the class that contains the map, or a special class that has the map and the instances.
Finally, you might want to create them on the heap, in which case they'd have to be deleted at some point. If you use that approach, you'll want to use reference-counted pointers or COW-pointers.
-
September 28th, 2005, 01:22 PM
#9
Re: using null terminated strings with switch/case
wschweit: your code is wrong :
== compares pointers and not the strings.
Instead, you should use strcmp.
And instead of using an ugly macro, it is preferable to use a function:
Code:
unsigned StringToCommandID(const char *cmd)
{
if (strcmp(cmd,"cmd 1")==0) return 1;
else if (strcmp(cmd,"cmd 2")==0) return 2;
else return 0;
}
And remember that this approach is only acceptable if there are very few cases.
In other cases, you should at least put the associations between strings and integers, in a container; at least a C-style array of structures, if you program in C, or a std::map with the method NMTop40 described if you program in C++
Last edited by SuperKoko; September 28th, 2005 at 02:09 PM.
Reason: replaced return 1, with return 2
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 28th, 2005, 01:44 PM
#10
Re: using null terminated strings with switch/case
yes, strcmp is the correct way to compare, my bad. Too used to c++ std::strings.
A function is the most maintainable, but the macro makes your code easier to read in this case.
std::map is definately the best way to go though.
Your code is wrong too! doesn't distinguish between 1 and 2.
-
September 28th, 2005, 02:09 PM
#11
Re: using null terminated strings with switch/case
 Originally Posted by wschweit
Your code is wrong too! doesn't distinguish between 1 and 2.

right, i edited it!
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 28th, 2005, 02:50 PM
#12
Re: using null terminated strings with switch/case
Fixed mine too in case someone tried to use it. It worked when I tried it before I posted...
-
September 29th, 2005, 03:50 PM
#13
Re: using null terminated strings with switch/case
thanx for the ideas, maybe ill try to figure out something with map class
-
September 29th, 2005, 04:01 PM
#14
Re: using null terminated strings with switch/case
 Originally Posted by NMTop40
There is no such language as C/C++.
You either want to write it in C or in C++.
That's what it means in American English But I can understand how it can be confusing to non-Americans or others not accustomed to reading gobbledegook.
-
September 29th, 2005, 04:18 PM
#15
Re: using null terminated strings with switch/case
You can also use enum for integer ID and populate map with pairs <string, id>. It's better for cases when you need just few of whole list (but more then 2-3 cases). When number of cases is big enough (about 5 or more), switch block would become messy and it would be better to use distinct functions/functors. And don't use those null terminated arrays, use strings instead.
"Programs must be written for people to read, and only incidentally for machines to execute."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|