@@ -41,8 +41,8 @@ def parse_ini_files(file_paths, verbose=False):
4141 current_section = None
4242 with open (file_path ) as file :
4343 for line in file :
44- line = line .strip ()
45- if line .startswith ('#' ) or not line :
44+ line = line .rstrip ()
45+ if line .startswith ('#' ):
4646 continue
4747 if line .startswith ('[' ) and line .endswith (']' ):
4848 current_section = line [1 :- 1 ]
@@ -60,53 +60,69 @@ def parse_ini_files(file_paths, verbose=False):
6060def generate_c_code (parsed_data ):
6161 # Generate C code with an array of filenames and their
6262 # corresponding INI sections.
63- c_code = f"""// THIS FILE IS GENERATED AUTOMATICALLY! EDITS WILL BE LOST!
63+ c_code = []
64+ c_code .append (f"""// THIS FILE IS GENERATED AUTOMATICALLY! EDITS WILL BE LOST!
6465// This file generated by { sys .argv [0 ]}
6566
66- """
67+ """ )
6768 # Rather than escaping the C code {} in f strings, make this a
6869 # separate (non-f-string) addition to c_code.
69- c_code += """#include <stdio.h>
70+ c_code . append ( """#include <stdio.h>
7071#include <string.h>
72+ #include <stdlib.h>
73+
74+ #include "opal/util/argv.h"
75+ #include "opal/util/show_help.h"
7176
7277typedef struct {
7378 const char *section;
74- const char *content;
79+ const char **content;
80+ char *joined;
7581} ini_entry;
7682
7783typedef struct {
7884 const char *filename;
7985 ini_entry *entries;
8086} file_entry;
8187
82- """
88+ static file_entry help_files[] = { """)
8389
8490 ini_arrays = []
8591 file_entries = []
8692
87- for idx , (filename , sections ) in enumerate (parsed_data .items ()):
88- var_name = filename .replace ('-' , '_' ).replace ('.' , '_' )
89-
90- ini_entries = []
91- for section , content_list in sections .items ():
92- content = '\n ' .join (content_list )
93- c_content = content .replace ('"' ,'\\ "' ).replace ("\n " , '\\ n"\n "' )
94- ini_entries .append (f' {{ "{ section } ", "{ c_content } " }}' )
95- ini_entries .append (f' {{ NULL, NULL }}' )
96-
97- ini_array_name = f"ini_entries_{ idx } "
98- ini_arrays .append (f"static ini_entry { ini_array_name } [] = {{\n " + ",\n " .join (ini_entries ) + "\n };\n " )
99- file_entries .append (f' {{ "{ filename } ", { ini_array_name } }}' )
100- file_entries .append (f' {{ NULL, NULL }}' )
101-
102- c_code += "\n " .join (ini_arrays ) + "\n "
103- c_code += "static file_entry help_files[] = {\n " + ",\n " .join (file_entries ) + "\n };\n "
104-
105- c_code += """
106-
93+ sp4 = f'{ " " * 4 } '
94+ sp8 = f'{ " " * 8 } '
95+ sp12 = f'{ " " * 12 } '
96+ sp16 = f'{ " " * 16 } '
97+ sp20 = f'{ " " * 20 } '
98+
99+ for filename , sections in parsed_data .items ():
100+ c_code .append (f'{ sp4 } {{' )
101+ c_code .append (f'{ sp8 } .filename = "{ filename } ",' )
102+ c_code .append (f'{ sp8 } .entries = (ini_entry[]) {{' )
103+
104+ for section_name , lines in sections .items ():
105+ c_code .append (f'{ sp12 } {{' )
106+ c_code .append (f'{ sp16 } .section = "{ section_name } ",' )
107+ c_code .append (f'{ sp16 } .content = (const char*[]) {{' )
108+ for line in lines :
109+ c_string = line .replace ('"' ,'\\ "' ).replace ("\n " , '\\ n"\n "' )
110+ c_code .append (f'{ sp20 } "{ c_string } ",' )
111+ c_code .append (f'{ sp20 } NULL' )
112+ c_code .append (f'{ sp16 } }},' )
113+ c_code .append (f'{ sp12 } }}, // End of section { section_name } ' )
114+
115+ c_code .append (f'{ sp12 } {{0}}' )
116+ c_code .append (f'{ sp8 } }}, // End of file { filename } ' )
117+ c_code .append (f'{ sp4 } }},' )
118+
119+ c_code .append (f'{ sp4 } {{0}}' )
120+ c_code .append (f'}};' )
121+
122+ c_code .append ("""
107123const char *opal_show_help_get_content(const char *filename, const char* topic)
108124{
109- file_entry *fe;
125+ const file_entry *fe;
110126 ini_entry *ie;
111127
112128 for (int i = 0; help_files[i].filename != NULL; ++i) {
@@ -115,17 +131,32 @@ def generate_c_code(parsed_data):
115131 for (int j = 0; fe->entries[j].section != NULL; ++j) {
116132 ie = &(fe->entries[j]);
117133 if (strcmp(ie->section, topic) == 0) {
118- return ie->content;
134+ if (NULL == ie->joined) {
135+ ie->joined = opal_argv_join((char**)ie->content, '\\ n');
136+ }
137+ return ie->joined;
119138 }
120139 }
121140 }
122141 }
123142
124143 return NULL;
125144}
126- """
127145
128- return c_code
146+ void opal_show_help_content_free(void)
147+ {
148+ for (int i = 0; help_files[i].filename != NULL; ++i) {
149+ for (int j = 0; help_files[i].entries[j].section != NULL; j++) {
150+ if (NULL != help_files[i].entries[j].joined) {
151+ free(help_files[i].entries[j].joined);
152+ help_files[i].entries[j].joined = NULL;
153+ }
154+ }
155+ }
156+ }
157+ """ )
158+
159+ return '\n ' .join (c_code )
129160
130161#-------------------------------
131162
0 commit comments