@@ -41,8 +41,8 @@ def parse_ini_files(file_paths, verbose=False):
41
41
current_section = None
42
42
with open (file_path ) as file :
43
43
for line in file :
44
- line = line .strip ()
45
- if line .startswith ('#' ) or not line :
44
+ line = line .rstrip ()
45
+ if line .startswith ('#' ):
46
46
continue
47
47
if line .startswith ('[' ) and line .endswith (']' ):
48
48
current_section = line [1 :- 1 ]
@@ -60,53 +60,69 @@ def parse_ini_files(file_paths, verbose=False):
60
60
def generate_c_code (parsed_data ):
61
61
# Generate C code with an array of filenames and their
62
62
# 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!
64
65
// This file generated by { sys .argv [0 ]}
65
66
66
- """
67
+ """ )
67
68
# Rather than escaping the C code {} in f strings, make this a
68
69
# separate (non-f-string) addition to c_code.
69
- c_code += """#include <stdio.h>
70
+ c_code . append ( """#include <stdio.h>
70
71
#include <string.h>
72
+ #include <stdlib.h>
73
+
74
+ #include "opal/util/argv.h"
75
+ #include "opal/util/show_help.h"
71
76
72
77
typedef struct {
73
78
const char *section;
74
- const char *content;
79
+ const char **content;
80
+ char *joined;
75
81
} ini_entry;
76
82
77
83
typedef struct {
78
84
const char *filename;
79
85
ini_entry *entries;
80
86
} file_entry;
81
87
82
- """
88
+ static file_entry help_files[] = { """)
83
89
84
90
ini_arrays = []
85
91
file_entries = []
86
92
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 ("""
107
123
const char *opal_show_help_get_content(const char *filename, const char* topic)
108
124
{
109
- file_entry *fe;
125
+ const file_entry *fe;
110
126
ini_entry *ie;
111
127
112
128
for (int i = 0; help_files[i].filename != NULL; ++i) {
@@ -115,17 +131,32 @@ def generate_c_code(parsed_data):
115
131
for (int j = 0; fe->entries[j].section != NULL; ++j) {
116
132
ie = &(fe->entries[j]);
117
133
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;
119
138
}
120
139
}
121
140
}
122
141
}
123
142
124
143
return NULL;
125
144
}
126
- """
127
145
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 )
129
160
130
161
#-------------------------------
131
162
0 commit comments