Change case of string using PreProcessor

  • Thread starter CPA Study Group
  • Start date
C

CPA Study Group

Is it possible to write a C Preprocessor macro which changes the case
of a string. This will save me time by not having to use awk of write
my own preprocessor. Here is a sample code which I want to work

---------
#ifdef LOWER
#define NORMAL(x) MKLOWER(x)
#else
#define NORMAL(x) MKUPPER(x)
#endif

extern void NORMAL(Foo)(int x,int y);
extern void NORMAL(Bar)(int x,int y);
extern void NORMAL(Baz)(int x,int y);

....

NORMAL(Foo)(5,6);
NORMAL(Bar)(5,7);
NORMAL(Baz)(7,8);
--------

Basically I want to call the external function "foo" or "FOO" depending
on the preprocessor directive. If all I was worried about was just foo,
I could #define Foo as foo or FOO depending on LOWER. But in my case I
will have to do the #defines for Foo, Bar and Baz (in my application I
will have about 30-40 functions).

Basically is there a string version of toupper()?

- Murali
 
E

Eric Sosman

CPA said:
Is it possible to write a C Preprocessor macro which changes the case
of a string. This will save me time by not having to use awk of write
my own preprocessor. Here is a sample code which I want to work

---------
#ifdef LOWER
#define NORMAL(x) MKLOWER(x)
#else
#define NORMAL(x) MKUPPER(x)
#endif

extern void NORMAL(Foo)(int x,int y);
extern void NORMAL(Bar)(int x,int y);
extern void NORMAL(Baz)(int x,int y);

...

NORMAL(Foo)(5,6);
NORMAL(Bar)(5,7);
NORMAL(Baz)(7,8);
--------

Basically I want to call the external function "foo" or "FOO" depending
on the preprocessor directive. If all I was worried about was just foo,
I could #define Foo as foo or FOO depending on LOWER. But in my case I
will have to do the #defines for Foo, Bar and Baz (in my application I
will have about 30-40 functions).

Basically is there a string version of toupper()?

No, there's not a string version of toupper(). But
that's not what you need, anyhow: you're not trying to
manipulate strings, you're trying to manipulate identifiers.
Or, more precisely, preprocessing tokens. Since the letters
have already "lost their identities" in the process of
becoming preprocessing tokens, I don't think there's much
hope.

One possibility would be to do the case conversions
by hand and choose between them:

#ifdef LOWER
#define NORMAL(uc,lc) lc
#else
#define NORMAL(uc,lc) uc
#endif

extern void NORMAL(FOO,foo)(int x, int y);

However, if you've only got "about 30-40 functions" I think
you'd probably be better off with the more straightforward

#idfef LOWER
#define Foo foo
#define Bar bar
#else
#define Foo FOO
#define Bar BAR
#endif

extern void Foo(int x, int y);
extern void Bar(int x, int y);

"About 30-40 functions" means fewer than 90 lines worth of
typing -- some of which your editor can do for you. Go
for it, I'd say.

By the way, why do you want to do this renaming? Seems
like a pretty strange thing to want to do ...
 
U

Ulrich Eckhardt

CPA said:
Basically I want to call the external function "foo" or "FOO" depending
on the preprocessor directive. If all I was worried about was just foo,
I could #define Foo as foo or FOO depending on LOWER. But in my case I
will have to do the #defines for Foo, Bar and Baz (in my application I
will have about 30-40 functions).

Just a question, but what do programmers normally do with stupid,
repetitive tasks like writing preprocessor code for 40 functions?

Uli
 
B

Ben Pfaff

Ulrich Eckhardt said:
Just a question, but what do programmers normally do with stupid,
repetitive tasks like writing preprocessor code for 40 functions?

Automate it with a short script and a Makefile rule.
 

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
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top