Added option to create C/C++ header file.

This commit is contained in:
n-a-c-h
2005-06-27 15:06:39 +00:00
parent 7924ad7328
commit 721cbb0869

View File

@@ -486,6 +486,31 @@ void output_parser_start(ostream& c_stream)
<< "\n"; << "\n";
} }
void output_cheader_start(ostream& cheader_stream)
{
cheader_stream << "/*\n"
<< "Config file handler header generated by Nach's Config file handler creator.\n"
<< "*/\n"
<< "\n"
<< "#ifdef __cplusplus\n"
<< " extern \"C\" {\n"
<< "#endif\n"
<< "\n"
<< "unsigned char read_cfg_vars(const char *);\n"
<< "unsigned char write_cfg_vars(const char *);\n"
<< "\n";
}
void output_cheader_end(ostream& cheader_stream)
{
cheader_stream << "\n"
<< "#ifdef __cplusplus\n"
<< " }\n"
<< "#endif\n"
<< "\n";
}
void output_init_var(ostream& c_stream) void output_init_var(ostream& c_stream)
{ {
c_stream << "\n" c_stream << "\n"
@@ -753,10 +778,15 @@ void handle_directive(char *instruction, char *label)
} }
} }
void parser_generate(istream& psr_stream, ostream& c_stream) void parser_generate(istream& psr_stream, ostream& c_stream, ostream& cheader_stream)
{ {
output_parser_start(c_stream); output_parser_start(c_stream);
if (cheader_stream)
{
output_cheader_start(cheader_stream);
}
while (!psr_stream.eof()) while (!psr_stream.eof())
{ {
char *token; char *token;
@@ -801,7 +831,8 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
if (var_type) if (var_type)
{ {
string initial_value = get_token(0, " ,\n"); string initial_value = get_token(0, " ,\n");
ostringstream var_init("");
if (((initial_value[0] == '\"') && (initial_value[initial_value.length()-1] == '\"')) || if (((initial_value[0] == '\"') && (initial_value[initial_value.length()-1] == '\"')) ||
((initial_value[0] == '\'') && (initial_value[initial_value.length()-1] == '\''))) ((initial_value[0] == '\'') && (initial_value[initial_value.length()-1] == '\'')))
{ {
@@ -814,7 +845,7 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
array = initial_value.length()-1; //Size minus quotes plus null array = initial_value.length()-1; //Size minus quotes plus null
} }
c_stream << "char " << varname << "[" << array << "];"; var_init << "char " << varname << "[" << array << "];";
ostringstream memset_line; ostringstream memset_line;
@@ -840,12 +871,12 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
var_type += strlen("unsigned "); var_type += strlen("unsigned ");
} }
c_stream << var_type << " " << varname; var_init << var_type << " " << varname;
if (array) if (array)
{ {
if (var_type_is_char(var_type) || !init_value_num) if (var_type_is_char(var_type) || !init_value_num)
{ {
c_stream << "[" << array << "]"; var_init << "[" << array << "]";
ostringstream memset_line; ostringstream memset_line;
memset_line << "memset(" << varname << ", " << init_value_num << ", " << array; memset_line << "memset(" << varname << ", " << init_value_num << ", " << array;
@@ -864,12 +895,12 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
} }
else else
{ {
c_stream << "[" << array << "] = {"; var_init << "[" << array << "] = {";
for (size_t i = array; i > 1; i--) for (size_t i = array; i > 1; i--)
{ {
c_stream << init_value_num << ","; c_stream << init_value_num << ",";
} }
c_stream << init_value_num << "%d}"; var_init << init_value_num << "%d}";
} }
add_config_var(varname, asm_type, parameterized_value, array); add_config_var(varname, asm_type, parameterized_value, array);
@@ -879,24 +910,37 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
if ((token = get_token(0, " ,\n"))) if ((token = get_token(0, " ,\n")))
{ {
array = 1; array = 1;
c_stream << "[] = {" << init_value_num; var_init << "[] = {" << init_value_num;
do do
{ {
c_stream << "," << atoi(token); var_init << "," << atoi(token);
array++; array++;
} while((token = get_token(0, " ,\n"))); } while((token = get_token(0, " ,\n")));
c_stream << "}"; var_init << "}";
add_config_var(varname, asm_type, parameterized_value, array); add_config_var(varname, asm_type, parameterized_value, array);
} }
else else
{ {
c_stream << " = " << init_value_num; var_init << " = " << init_value_num;
add_config_var(varname, asm_type, single_value, 0); add_config_var(varname, asm_type, single_value, 0);
} }
} }
c_stream << ";"; var_init << ";";
c_stream << var_init.str();
if (cheader_stream)
{
string header_data = var_init.str();
size_t equal_pos;
if ((equal_pos = header_data.find("=")) != string::npos)
{
header_data.erase(equal_pos-1);
header_data.append(";");
}
cheader_stream << "extern " << header_data << "\n";
}
} }
} }
//Else already handled //Else already handled
@@ -925,6 +969,11 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
c_stream << "\n"; c_stream << "\n";
if (cheader_stream)
{
output_cheader_end(cheader_stream);
}
if (!ifs.empty()) if (!ifs.empty())
{ {
cerr << "Error: " << ifs.size() << " ifdef segments have no endif." << endl; cerr << "Error: " << ifs.size() << " ifdef segments have no endif." << endl;
@@ -933,6 +982,8 @@ void parser_generate(istream& psr_stream, ostream& c_stream)
int main(size_t argc, const char **argv) int main(size_t argc, const char **argv)
{ {
const char *cheader_file = 0;
size_t param_pos = 1; size_t param_pos = 1;
for (; param_pos < argc; param_pos++) for (; param_pos < argc; param_pos++)
{ {
@@ -940,6 +991,11 @@ int main(size_t argc, const char **argv)
{ {
defines.insert(argv[param_pos]+2); defines.insert(argv[param_pos]+2);
} }
else if (!strcmp(argv[param_pos], "-cheader"))
{
param_pos++;
cheader_file = argv[param_pos];
}
else else
{ {
break; break;
@@ -951,10 +1007,16 @@ int main(size_t argc, const char **argv)
cout << "Config file handler creator by Nach (C) 2005\n" cout << "Config file handler creator by Nach (C) 2005\n"
<< "\n" << "\n"
<< "Usage:\n" << "Usage:\n"
<< "parsegen [-Ddefine] <output> <input>\n" << "parsegen [options] <output> <input>\n"
<< "\n"
<< "\n"
<< "Options:\n"
<< "\n" << "\n"
<< " -Ddefine Define a processor director. Example: -D__LINUX__\n" << " -Ddefine Define a processor director. Example: -D__LINUX__\n"
<< " Can specify multiple defines.\n" << " Can specify multiple defines.\n"
<< "\n"
<< " -cheader Create a C/C++ header with the following name.\n"
<< " Example -cheader cfgvars.h\n"
<< endl; << endl;
return(1); return(1);
@@ -969,8 +1031,26 @@ int main(size_t argc, const char **argv)
ofstream c_stream(c_file); ofstream c_stream(c_file);
if (c_stream) if (c_stream)
{ {
parser_generate(psr_stream, c_stream); ofstream cheader_stream;
if (cheader_file)
{
cheader_stream.open(cheader_file);
if (cheader_stream)
{
parser_generate(psr_stream, c_stream, cheader_stream);
}
else
{
cerr << "Error opening " << cheader_file << " for writing." << endl;
ret_val |= 8;
}
cheader_stream.close();
}
else
{
parser_generate(psr_stream, c_stream, cheader_stream);
}
c_stream.close(); c_stream.close();
} }
else else