Connecting Tech Pros Worldwide Help | Site Map

MACRO to capitalize first letter of a word in header.

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 7th, 2009, 04:27 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default MACRO to capitalize first letter of a word in header.

I know about toupper() and tolower() but I need to capitalize (or de-capitalize)the first letter of a word in a header file. I know there are no preprocessor functions that do that but is there another way?
Reply
  #2  
Old January 7th, 2009, 04:46 PM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 5,731
Default

I think I would be forced to ask why? and over how many files?

For a small number of files then a text editor is probably your best bet.

For more files you might be able to use an awk script to automate a lot of the process or a perl script.

Ultimately you could write you own text processing program.
Reply
  #3  
Old January 7th, 2009, 05:23 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default

This would be done over 50 files, and they are needed to be capitalized in order to feed the word in the proper format, as an input into another macro.

I would like to keep this in a C header file.
Reply
  #4  
Old January 7th, 2009, 06:50 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Age: 52
Posts: 568
Default

I suggest you edit the header files to get the capitalization correct. You might have been able to alter 50 header files in the time since this thread was started.

How many different words do you need to alter? The following is a really bad idea ... suppose you only need to change "big" to "Big" and "Small" to "small"
Expand|Select|Wrap|Line Numbers
  1. #define big Big
  2. #define Small small
  3. ...
  4. #include <header>
  5. ...
  6. call the macros that use "big" and "Small"
  7. ...
  8. #undef big
  9. #undef Small
  10. ...
This is a bad idea because
1. There may be instances of "big" and "Small" that you don't want to change.
2. You are going out of your way to make your code hard for anybody to understand.
3. Corollary to #1 and #2 above ... someday you may modify the program to insert the word "big" or "Small" without any desire for it to be changed.
Reply
  #5  
Old January 7th, 2009, 08:35 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default

Quote:
Originally Posted by donbock View Post
This is a bad idea because
1. There may be instances of "big" and "Small" that you don't want to change.
2. You are going out of your way to make your code hard for anybody to understand.
3. Corollary to #1 and #2 above ... someday you may modify the program to insert the word "big" or "Small" without any desire for it to be changed.

This is definitely on the right track - to answer your question, I need to change to upper case, AND lower case in the same macro.

For example:

Expand|Select|Wrap|Line Numbers
  1. #define mac(name, val)  (Name *)(name->myMember.anum) = val
  2.  
See how name input is Name AND name in the macro?
Reply
  #6  
Old January 7th, 2009, 09:42 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Age: 52
Posts: 568
Default

Quote:
Originally Posted by dissectcode View Post
I need to change to upper case, AND lower case in the same macro. For example:
Expand|Select|Wrap|Line Numbers
  1. #define mac(name, val)  (Name *)(name->myMember.anum) = val
  2.  
See how name input is Name AND name in the macro?
How about this ...
Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef Name *ptr_name;
  3. ...
  4. #define mac(name, val) (ptr_##name)(name->myMember.anum) = val
  5.  
Reply
  #7  
Old January 7th, 2009, 09:52 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default

I need to change "name" to Name for the cast, and make sure it is in lower case for the structure name.

Also - "name" is arbitrary so I couldn't typedef it to one thing ?
Reply
  #8  
Old January 8th, 2009, 12:27 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Age: 52
Posts: 568
Default

What I meant was ... suppose you have three "names" (foo, bar, and squid) ... that means you want the following code to appear after all macro expansions:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Foo foo;
  3. Bar bar;
  4. Squid squid;
  5. ...
  6. (Foo *) (foo->myMember.anum) = fooVal;
  7. (Bar *) (bar->myMember.anum) = barVal;
  8. (Squid *) (squid->myMember.anum) = squidVal;
  9.  
My suggestion is to get the equivalent through the following ...
Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef Foo *ptr_foo;
  3. typedef Bar *ptr_bar;
  4. typedef Squid *ptr_squid;
  5. ...
  6. #define mac(name, val) (ptr_ ## name)(name->myMember.anum) = val
  7. ...
  8. Foo foo;
  9. Bar bar;
  10. Squid squid;
  11. ...
  12. mac(foo, fooVal);
  13. mac(bar, barVal);
  14. mac(squid, squidVal);
  15.  
These macros expand into the three lines:
Expand|Select|Wrap|Line Numbers
  1.  
  2. (ptr_foo)(foo->myMember.anum) = fooVal;
  3. (ptr_bar)(bar->myMember.anum) = barVal;
  4. (ptr_squid)(squid->myMember.anum) = squidVal;
  5.  
Which, because of the typedefs, is equivalent to what you want.

I haven't tried this out on a compiler, so there might be syntax issues for you to work out, but I'm pretty sure the concept is valid.
Reply
  #9  
Old January 8th, 2009, 08:38 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default

Quote:
Originally Posted by donbock View Post
These macros expand into the three lines:
Expand|Select|Wrap|Line Numbers
  1.  
  2. (ptr_foo)(foo->myMember.anum) = fooVal;
  3. (ptr_bar)(bar->myMember.anum) = barVal;
  4. (ptr_squid)(squid->myMember.anum) = squidVal;
  5.  

Thank you for your response. After looking through this code and concept - I am concerned that I cannot create a more general macro for any input during run-time....since it is a preprocessor thing, it does the concatenation at compile-time. Is this true?
Reply
  #10  
Old January 8th, 2009, 09:09 PM
Member
 
Join Date: Aug 2008
Posts: 88
Default

Yes it is true.. this is done at compile time..

Can you give us a example of how you want this to work at run-time?
Reply
  #11  
Old January 8th, 2009, 09:59 PM
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Age: 52
Posts: 568
Default

Quote:
Originally Posted by dissectcode View Post
Expand|Select|Wrap|Line Numbers
  1. #define mac(name, val)  (Name *)(name->myMember.anum) = val
  2.  
See how name input is Name AND name in the macro?
Wait a minute ... why do you need the cast? The cast suggests that either the "name" argument to the macro or the "anum" field of the structure is not intrinsically the proper pointer type. Is it a void*?

In general, if you want run-time resolution of pointer types you have a table where each entry contains a void* pointer and an enumeration for all of the possible types that might be pointed to. You then have a big switch on the enumeration value with a case for each possible type. Within each case you explicitly cast the pointer to right kind of pointer type. However, realize that you have no safety net. You have almost no hope of getting a compiler warning/error if you mess up one of those cases by casting to the wrong type.
Reply
  #12  
Old January 10th, 2009, 08:26 PM
Member
 
Join Date: Aug 2008
Posts: 88
Default

donbock is right..
can you give us please exactly how you need this to work?
I am very curious... what must the name and Name be?
It seems to be that you must follow A LOT OF naming conventions... which of course is only a burden for your mind... things that the compiler cannot check can be tricky..
Reply
  #13  
Old January 12th, 2009, 10:45 PM
Member
 
Join Date: Jul 2008
Posts: 61
Default

Quote:
Originally Posted by Tassos Souris View Post
donbock is right..
can you give us please exactly how you need this to work?
I am very curious... what must the name and Name be?
It seems to be that you must follow A LOT OF naming conventions... which of course is only a burden for your mind... things that the compiler cannot check can be tricky..

I think what I am trying to do can only be done using actual C code in functions since the name will depend on run-time inputs. I am using to it access HW registers. Please look at how the word "name" has certain letters in CAPS, or not.

Expand|Select|Wrap|Line Numbers
  1. #define REG_ADDR            ((void *) (0x00054000))
  2.  
  3. #define REG_CASTADDR   ((Name *)(REG_ADDR))
  4.  
  5. #define RD_REG(name, reg)    NAME##_CASTADDR->name##reg.myUint)
  6. #define WR_REG(name, reg, val)  /
  7. ((NAME_REG_BASE->name##reg.myUint) = val)
  8.  
and then from a C program I call the macros:

Expand|Select|Wrap|Line Numbers
  1. int registerState = RD_REG(Regtest, Interrupt_stat);
  2.  
So I need it to turn into :

Expand|Select|Wrap|Line Numbers
  1. REGTEST_CASTADDR->regtestInterrupt_stat.myUint;
  2.  
It looks messy but I made is as general as possible
Reply
Reply

Bookmarks

Tags
capitalize, header file, macro, tolower, toupper

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.