C language automatically generates enum value and name Mapping code _c language

Source: Internet
Author: User

This year seems to continue to do C language is not many people, young people mostly to the Internet and mobile applications. Indeed, those two areas now come in fast, and are always in short supply. Just in an old colleague's micro-letter group, someone just released their own team can do the app for a few minutes, wow, several people said there are projects, asking to add friends private chat. I also want to change careers, unfortunately, no Internet or application team is willing to accept. It is estimated that in a few years, the C program in the world is only my little old man to continue to entertain themselves, the envy of the dead young people!

In normal work, often have to do some printing, or log. In this case, the enum type has a lot of data, and if you just print out its integer value, it obviously annoys the tester, who knows what the numbers say. Even if you give him a document to look at, also can not get the slightest forgiveness. Finally, you have to quietly print out the name of the enum to calm down.

Today, this is to help fix the problem of conversion.

For example, there is this example.h file:

Enum infostatee{
 Einfostateidle,
 einfostateiniting,
 einfostateinited,
 Einfostateready,
 Einfostateactive,
 Einfostatestandby,
 einfostaterelease,
};

We need a function that translates the enum value into a string, and a function that looks for the enum value of the string, and can provide a part of the name. P_enum.pl is doing this, just to execute:

Perl p_enum.pl Example.h-o:example_map
p_enum.pl Accept directories, wildcard characters, or multiple files. -o:example_map is the file name of the specified output. Here we get the Example_map.h and example_map.c two files:

/* File:example_map.h This FILE is created at Fri Dec 16:40:56 CST 2015 Auto-genera Ted Source.
Don ' t change it manually.
Contact with hhao020@gmail.com for bug reporting and supporting.  * * #ifndef example_map #define EXAMPLE_MAP #ifdef __cplusplus extern "C" {#endif #include "ztype_def.h" extern const
char* Infostatee2name (dword_t e);
extern dword_t Infostatee2value (char* name, int bappro);

extern int infostateemapshow (); #ifdef __cplusplus} #endif #endif/*example_map*/
/* FILE:EXAMPLE_MAP.C This FILE is created at Fri Dec 16:40:56 CST 2015 auto-generated source.
Don ' t change it manually.
Contact with hhao020@gmail.com for bug reporting and supporting. * * #define _gnu_source #include <string.h> #include <stdio.h> #include <stdlib.h> #include "zautocod

 EApi.h "#ifndef enum_primary_functions static char *zenum2name (dword_t E, zenum2namemap_t arr[], int nsize) {int i;
 For (i=0 i<nsize; i++) {if (arr[i].e = = e) return arr[i].name;
return 0;

 static int Zname2enum (char* name, zenum2namemap_t arr[], int nsize, int bappro) {int i;
  For (i=0 i<nsize; i++) {if (Bappro) {if (Strcasestr (arr[i].name, name)) return to ARR[I].E;
  else {if (!strcmp (arr[i].name, name)) return ARR[I].E;
}} return-1;
 static int zenummapshow (zenum2namemap_t arr[], int nsize) {int i;
 For (i=0 i<nsize; i++) {printf ("%3d%d->%s \ n", I, ARR[I].E, arr[i].name);
return 0; } #endif/*enum_primary_functions*/

 


#include "example_map.h"

#include "example.h"

zenum2namemap_t infostatee_map_t[]=
{
 { Einfostateidle, "Einfostateidle"},
 {einfostateiniting, "einfostateiniting"},
 {einfostateinited, " Einfostateinited "},
 {einfostateready," Einfostateready "},
 {einfostateactive," einfostateactive "},
 { Einfostatestandby, "Einfostatestandby"},
 {einfostaterelease, "einfostaterelease"},
};/*infostatee_map_ t*/

const char* infostatee2name (dword_t e)
{
 char* pname = Zenum2name (E, infostatee_map_t, Tbl_size ( infostatee_map_t));
 
 if (pname) return pname;
 return "*na*";
}

dword_t Infostatee2value (char* name, int bappro)
{return
 zname2enum (name, infostatee_map_t, Tbl_size ( infostatee_map_t), Bappro);

int Infostateemapshow ()
{return
 zenummapshow (infostatee_map_t, Tbl_size (infostatee_map_t));


P_enum.pl was using my own lex and YACC library. Originally is to learn the principle of compiling some of the experimental code, and later found that it is very powerful for text extraction, so there is an enum-related automatic generation tool. Interested, may refer to, or has the question to leave a message.

Well, also, p_enum.pl does not support the inclusion of compilation control macros in the enum definition. If you have this requirement, you need to modify Enum.lex and ENUM.YACC files to support yourself. I'm very disgusted with compiling macros, so I won't do this update.

How to set up supplemental instructions for using the environment:
Linux environment, Perl_zlib is recommended for decompression in your home directory. Then add the zlib path to your user profile, BASHRC (or. Profiles, slightly different from Linux). My configuration is like this:

Export path= "$HOME/perl/debug: $PATH"
export perl5lib= "$HOME/perl/zlib: $HOME/perl5/lib/perl5"
perl_mb_opt = "--install_base \" $HOME/perl5\ ""; Export perl_mb_opt;
perl_mm_opt= "install_base= $HOME/perl5"; Export perl_mm_opt;

Only the zlib path in perl5lib is necessary. PERL5 is what I use to install the Extra Perl library and post it together for reference.

Windows environment, you need to run Zlib.bat to set environment variables before you can run p_enum.pl. Of course, you also have to confirm the installation of ActivePerl.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.