471,123 Members | 791 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

match enum to char* ?

The following enum is given to me, and I can't change it:

enum yo { ONE, TWO, THREE };

I have the following:

char test[10] = "ONE";

Any ideas on how to see if the string in "test" is in the enum, and return
the enum number if true?

Thanks,
Randy

Nov 14 '05 #1
6 4334

randy1200 wrote:
The following enum is given to me, and I can't change it:

enum yo { ONE, TWO, THREE };

I have the following:

char test[10] = "ONE";

Any ideas on how to see if the string in "test" is in the enum, and return the enum number if true?

Thanks,
Randy


No.

Nov 14 '05 #2
randy1200 <ra*******@yahoo.com> wrote:
The following enum is given to me, and I can't change it:

enum yo { ONE, TWO, THREE };

I have the following:

char test[10] = "ONE";

Any ideas on how to see if the string in "test" is in the enum, and return
the enum number if true?


There are optimizations, but effectively you must do something similar to...

#include <string.h> /* strncmp */

#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)

enum yo { ONE, TWO, THREE };

#define ENUM(name) {STRINGIFY(name),sizeof STRINGIFY(name) - 1,name}

const struct yo_enum {
const char *name;
size_t len;
size_t code;
} enum_map[] = {
ENUM(ONE),
ENUM(TWO),
ENUM(THREE)
};

int str2enum(enum yo *code, const char *name, size_t len) {
const struct yo_enum *pos, *end;

pos = enum_map;
end = pos + (sizeof enum_map / sizeof *enum_map);

for (; pos < end; pos++) {
if (pos->len == len && 0 == strncmp(pos->name,name,len)) {
*code = pos->code;
return 1;
}
}

return 0;
}
Nov 14 '05 #3
randy1200 wrote:
The following enum is given to me, and I can't change it:

enum yo { ONE, TWO, THREE };

I have the following:

char test[10] = "ONE";

Any ideas on how to see if the string in "test" is in the enum
and return the enum number if true? cat main.c #include <stdio.h>
#include <string.h>

enum yo { ONE, TWO, THREE };
const
char* const yoString[] = { "ONE", "TWO", "THREE" };

int main(int argc, char* argv[]) {
if (1 < argc) {
char* test = argv[1];
for (enum yo j = ONE; j <= THREE; ++j)
if (0 == strcmp(yoString[j], test))
fprintf(stdout, "yoString[%d] = %s\n", j, yoString[j]);
}
else {
fprintf(stderr, "usage: %s [ONE|TWO|THREE]\n", argv[0]);
}

return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main usage: ./main [ONE|TWO|THREE] ./main ZERO
./main ONE yoString[0] = ONE ./main TWO yoString[1] = TWO ./main THREE

yoString[2] = THREE

Nov 14 '05 #4
"randy1200" <ra*******@yahoo.com> wrote in message news:<26******************************@localhost.t alkaboutprogramming.com>...
The following enum is given to me, and I can't change it:

enum yo { ONE, TWO, THREE };

I have the following:

char test[10] = "ONE";

Any ideas on how to see if the string in "test" is in the enum, and return
the enum number if true?


You can think "generative programming", or how to automate some
programming tasks.
I wrote you a little scrit in CodeWorker (http://www.codeworker.org).
It parses a C file, looking for every enum declaration inside. Then,
it writes a string-to-enum function for each of them.

Advantage: if someone adds or renames or removes some enum identifiers
in a enum type, you just have to run the script, which could be a part
of your build process.

The script also handles enum declarations like :
enum pets { GOAT, DOG = 3, CAT = 0x05, MOUSE };

And it generates the following code:
int str2yo_enum(const char* name, enum yo* val) {
int err = 0;
if (strcmp(name, "ONE") == 0) *val = ONE;
else if (strcmp(name, "TWO") == 0) *val = TWO;
else if (strcmp(name, "THREE") == 0) *val = THREE;
else err = -1;
return err;
}

int str2pets_enum(const char* name, enum pets* val) {
int err = 0;
if (strcmp(name, "GOAT") == 0) *val = GOAT;
else if (strcmp(name, "DOG") == 0) *val = DOG;
else if (strcmp(name, "CAT") == 0) *val = CAT;
else if (strcmp(name, "MOUSE") == 0) *val = MOUSE;
else err = -1;
return err;
}
The script is (save it to "str2enum.cwp"):
str2enum ::=
// ignore whitespaces and C comments
// between each BNF terminal or non-terminal
#ignore(C++)
// look for every enum declaration
[
// jump to the next enum declaration
->[
#readIdentifier:sId
#nextStep
#check(sId == "enum")
// interesting only if it's a named enum type
#readIdentifier:sType
#continue
'{'
// will store enums into this association table
=> local allEnums;
// iterate on enum ids; at least one expected
enum_id(allEnums)
[
','
enum_id(allEnums)
]*
// may end with a single comma!
[',']?
'}'
=> {
// the parsing on this enum has finished.
// Now, let's generate the str2enum function:
@int str2@sType@_enum(const char* name, enum @sType@* val) {
int err = 0;
@
foreach i in allEnums {
if i.first() {
@ @
} else {
@ else @
}
@if (strcmp(name, "@i@") == 0) *val = @i@;
@
}
@ else err = -1;
return err;
}

@
}
]
]*
;

enum_id(allEnums : node) ::=
#readIdentifier:sEnum
=> insert allEnums[sEnum]= sEnum;
// don't care about the value, if any
['=' [~[',' | '}']]*]?
;

Then run the command line:
codeworker -translate str2enum.cwp yourCFileWithEnums.c functions.c
Nov 14 '05 #5
Hum! About my CodeWorker's script: I forgot to remove the line 13,
which I needed for debug. Please remove it (this line is unindented
and is worth '#continue').
Nov 14 '05 #6
Many thanks for the excellent coding tips! This really helps me out...

Randy

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rafal 'Raf256' Maj | last post: by
9 posts views Thread by AngleWyrm | last post: by
2 posts views Thread by Voronkov Konstantin | last post: by
2 posts views Thread by randy1200 | last post: by
18 posts views Thread by Nebula | last post: by
4 posts views Thread by adrian suri | last post: by
10 posts views Thread by kar1107 | last post: by
7 posts views Thread by Travis | last post: by
16 posts views Thread by andreyvul | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.