Skip to content

Instantly share code, notes, and snippets.

@duckie
Created August 3, 2012 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckie/3246487 to your computer and use it in GitHub Desktop.
Save duckie/3246487 to your computer and use it in GitHub Desktop.
Utility to parse a header, extract error codes and store them into a *.map re-usable from C++ code.
#ifndef IBM_ERRORS
#define IBM_ERRORS
/**
* C'est un peu la fête du slip les codes d'erreurs en macros
*/
#define ERREUR_MARCEL_A_TROP_BU 1
#define ERREUR_ROBERT_A_TROP_MANGE 2
# define ERREUR_Y_A_PLUS_DE_PASTIS 3
#define ERREUR_LE_BARBEUC_S_EST_ETEINT 4
#endif
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
class ErrorCodeManager
{
std::map< int, std::string > m_errors;
public:
ErrorCodeManager(std::string const & iFileName)
{
std::ifstream donnee(iFileName.c_str());
if(donnee)
{
std::string ligne_str;
while(std::getline(donnee,ligne_str))
{
std::istringstream ligneSteam(ligne_str);
std::string error_code;
if(std::getline(ligneSteam,error_code,';'))
{
std::string error_msg;
if(std::getline(ligneSteam,error_msg,';'))
{
std::istringstream intStream(error_code);
int intError;
if(intStream >> intError)
{
m_errors.insert(std::make_pair(intError,error_msg));
}
}
}
}
}
}
std::string GetErrorName(int iCode) const
{
std::map< int, std::string >::const_iterator elem = m_errors.find(iCode);
if(elem != m_errors.end()) {
return elem->second;
}
return std::string();
}
};
int main(int argc, char * argv[])
{
ErrorCodeManager manager("errors.map");
std::cout << manager.GetErrorName(2) << std::endl;
std::cout << manager.GetErrorName(4) << std::endl;
std::cout << manager.GetErrorName(3) << std::endl;
std::cout << manager.GetErrorName(1) << std::endl;
return 0;
}
#!/usr/bin/python
# -*- coding: utf8 -*-
import os, argparse, re, sys
# Parsing command line arguments
parser = argparse.ArgumentParser(description='Parses a header to extract code from macros.')
parser.add_argument('--output', type=str, required=False, default="", help='Filename for the destination map.')
parser.add_argument('file', metavar='file', type=str, nargs='*', help='Header file to be processed.')
args = parser.parse_args()
# Gathering input
if (0 == len(args.file)):
print("A file must be given as argument.")
sys.exit(1)
if (1 < len(args.file)):
print("Too much arguments.")
sys.exit(1)
file_in = args.file[0]
if not os.path.exists(file_in):
print("The given file (" + file_in + ") does not exist.")
sys.exit(1)
file_pattern = re.compile("(?i)^(.*)\.h$")
m = file_pattern.match(file_in)
if None == m:
print("The given file (" + file_in + ") must be a header (*.h).")
sys.exit(1)
basename = m.group(1)
output = args.output
if "" == output:
output = basename + ".map"
if None == re.match("(?i)^(.*)\.map$", output):
print("The output file (" + output + ") must be a map (*.map).")
sys.exit(1)
file_pointer = open(file_in, 'r', newline='\n')
file_dest = open(output, 'w+', newline='\n')
define_error_line_pattern = re.compile("(?i)^\s*#\s*define\s+([a-zA-Z0-9_]+)\s+([0-9]+)\s+$")
for line in file_pointer:
m = define_error_line_pattern.match(line)
if None != m:
error_name = m.group(1)
error_code = m.group(2)
file_dest.write(error_code + ";" + error_name + os.linesep)
file_pointer.close()
file_dest.close()
@duckie
Copy link
Author

duckie commented Jan 15, 2013

Explanation:

  • parse_error_codes.py must be used to parse a *.h file (here you can try with errors.h) to generate a *.map file.
  • map_parser.cpp contains a simple object to parse the generated *.map file and get the name of the error from its error code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment