sponsor Vim development Vim logo Vim Book Ad

basic Tip #107: C/C++: convert enum to string table

 tip karma   Rating 8/4, Viewed by 4556 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   September 5, 2001 0:01      complexity:   basic
author:   Thomas Ramming      as of Vim:   5.7

When testing your own C/C++ programs you sometimes wish to have a trace output,
which shows you, which enum value is used.
You can do this by creating a string table for that enum type,
which contains the enum identifyer as a string.
e.g.
printf ("%s", MyEnumStringTable [ MyEnumVal] );

You can create the complete string table by
- marking the lines containing the complete typedef enum
- select menu C/C++.transform enum2Stringtab

You can create string table entries by
- marking the lines within the typedef enum
- select menu C/C++.transform enum2String

This makes it easy to keep the enum (on changes) consistent to the string table.

Add the following lines to your _GVIMRC file:
31amenu C/C++.transform\ enum2Stringtab :s#[ ]*\\(\\w\\+\\)#/* \\1 */ "\\1"#<CR>o};<ESC>uOstatic const char* const Names[] = {<ESC><CR>/sdfsdf<CR>
31vmenu C/C++.transform\ enum2Stringtab :s#[ ]*\\(\\w\\+\\)#/* \\1 */ "\\1"#<CR>o};<ESC>uOstatic const char* const Names[] = {<ESC><CR>/sdfsdf<CR>

31amenu C/C++.transform\ enum2String :s#[ ]*\\(\\w\\+\\)#/* \\1 */ "\\1"#<CR>o}<ESC>/sdfsdf<CR>
31vmenu C/C++.transform\ enum2String :s#[ ]*\\(\\w\\+\\)#/* \\1 */ "\\1"#<CR>o}<ESC>/sdfsdf<CR>

hint: '/sdfsdf' is added for deactivating search highlighting,
ok, you'll sure find a better way to do this.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Mail signature rotation: Supersimple one-line solution | Toggle a fold with a single keystroke >>

Additional Notes

[email protected], September 5, 2001 6:56
You can use :noh to reset highlighting.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo