473,329 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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?
Jan 7 '09 #1
12 12265
Banfa
9,065 Expert Mod 8TB
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.
Jan 7 '09 #2
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.
Jan 7 '09 #3
donbock
2,426 Expert 2GB
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.
Jan 7 '09 #4
@donbock

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?
Jan 7 '09 #5
donbock
2,426 Expert 2GB
@dissectcode
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.  
Jan 7 '09 #6
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 ?
Jan 7 '09 #7
donbock
2,426 Expert 2GB
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.
Jan 8 '09 #8
@donbock

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?
Jan 8 '09 #9
Tassos Souris
152 100+
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?
Jan 8 '09 #10
donbock
2,426 Expert 2GB
@dissectcode
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.
Jan 8 '09 #11
Tassos Souris
152 100+
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..
Jan 10 '09 #12
@Tassos Souris

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
Jan 12 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Colin Steadman | last post by:
Apoligies if this post appears twice, but my first attempt at posting it was hours ago and there is still no sign of it in the forum (says five minutes in the confirmation message). ...
3
by: news.individual.net | last post by:
Hi! Can I capitalize the first letter of the first paragraph without using a special class for that <p> ? I tried it with this: body > p:first-child:first-letter { font-size: 270%;
3
by: gil | last post by:
I have a script that will capitalize the first letter of every word in my descriptions. But some of my descriptions contain ( ) and /. For example bezel (black). I need the script to capitalize...
1
by: Dwight Shubert | last post by:
Hi all, This is my first try using Access. I understand how to format a text field for uppercase and lowercase, but how do you capitalize only the first letter of each word? tia Dwight
5
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
1
blyxx86
by: blyxx86 | last post by:
I have the code to capitalize an entire string when it is input, but would like to capitalize just the first letter of a box, or match the case of a drop down list.. My current code is thus: ...
8
by: romo14 | last post by:
I'm working on a program that will take an address in on all one line separated by pounds (eg karen smith # p.o. box 123 # new york, new york #) and outputs it in user-friendly format. So far my...
7
by: dizzylizzyd514 | last post by:
I need to make this: introductoRy speEch ==> Introductory Speech pRecalCulus 1 and 2 ==> Precalculus 1 And 2 I know how to capitalize the first letter of a word, but not multiple words in a...
12
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.