match enum to char* ?

R

randy1200

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
 
K

Kenneth Bull

randy1200 said:
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.
 
E

E. Robert Tisdale

randy1200 said:
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
 
W

William Ahern

randy1200 said:
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;
}
 
C

Cedric LEMAIRE

randy1200 said:
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
 
C

Cedric LEMAIRE

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').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top