2525#include < cerrno>
2626#include < cstring>
2727#include < dirent.h>
28+ #include < fstream>
2829#include < glob.h>
30+ #include < string>
31+ #include < vector>
2932
3033// Note that Error and Debug calls won't actually go anywhere unless you
3134// set the relevant ENV vars because the logger gets it's setting from the
@@ -105,18 +108,34 @@ void zmLoadDBConfig() {
105108}
106109
107110void process_configfile (char const *configFile) {
108- FILE *cfg;
109- char line[512 ];
110- if ( (cfg = fopen (configFile, " r" )) == nullptr ) {
111+ std::ifstream cfg (configFile);
112+ if ( !cfg.is_open () ) {
111113 Fatal (" Can't open %s: %s" , configFile, strerror (errno));
112114 return ;
113115 }
114- while ( fgets (line, sizeof (line), cfg) != nullptr ) {
115- char *line_ptr = line;
116+ std::string raw;
117+ while ( std::getline (cfg, raw) ) {
118+ // Tolerate Windows line endings.
119+ if ( !raw.empty () && raw.back () == ' \r ' ) raw.pop_back ();
120+
121+ // Backslash before the newline means the value continues on the next
122+ // physical line. Trailing whitespace before the '\' is allowed.
123+ // Continuation lines have their leading whitespace stripped so callers
124+ // can indent for readability without it leaking into the value.
125+ while ( true ) {
126+ size_t last_non_ws = raw.find_last_not_of (" \t " );
127+ if ( last_non_ws == std::string::npos || raw[last_non_ws] != ' \\ ' ) break ;
128+ raw.erase (last_non_ws);
129+ std::string next;
130+ if ( !std::getline (cfg, next) ) break ;
131+ if ( !next.empty () && next.back () == ' \r ' ) next.pop_back ();
132+ size_t lead = next.find_first_not_of (" \t " );
133+ if ( lead != std::string::npos ) raw.append (next, lead, std::string::npos);
134+ }
116135
117- // Trim off any cr/lf line endings
118- int chomp_len = strcspn (line_ptr, " \r\n " );
119- line_ptr[chomp_len] = ' \0 ' ;
136+ std::vector< char > line (raw. begin (), raw. end ());
137+ line. push_back ( ' \0 ' );
138+ char * line_ptr = line. data () ;
120139
121140 // Remove leading white space
122141 int white_len = strspn (line_ptr, " \t " );
@@ -135,7 +154,7 @@ void process_configfile(char const *configFile) {
135154 // Now look for the '=' in the middle of the line
136155 temp_ptr = strchr (line_ptr, ' =' );
137156 if ( !temp_ptr ) {
138- Warning (" Invalid data in %s: '%s'" , configFile, line);
157+ Warning (" Invalid data in %s: '%s'" , configFile, line. data () );
139158 continue ;
140159 }
141160
@@ -204,7 +223,6 @@ void process_configfile(char const *configFile) {
204223 // Warning( "Invalid parameter '%s' in %s", name_ptr, ZM_CONFIG );
205224 }
206225 } // end foreach line of the config
207- fclose (cfg);
208226}
209227
210228StaticConfig staticConfig;
0 commit comments