From 5f181ef915a5d10d13114226c20c8fa6f22dabc5 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Sun, 13 Aug 2017 10:00:01 +0200 Subject: [PATCH] Replace stdio.h by fstream --- src/core/libs/INIReader.cc | 26 ++++---- src/core/libs/ini.cc | 124 +++++++++++++++++++++---------------- src/core/libs/ini.h | 13 +--- 3 files changed, 89 insertions(+), 74 deletions(-) diff --git a/src/core/libs/INIReader.cc b/src/core/libs/INIReader.cc index 0e3af9242..e5be8babe 100644 --- a/src/core/libs/INIReader.cc +++ b/src/core/libs/INIReader.cc @@ -45,32 +45,34 @@ * ------------------------------------------------------------------------- */ -#include -#include -#include "ini.h" #include "INIReader.h" +#include // for tolower +#include // for strol +#include "ini.h" -using std::string; -INIReader::INIReader(string filename) +INIReader::INIReader(std::string filename) { _error = ini_parse(filename.c_str(), ValueHandler, this); } + int INIReader::ParseError() { return _error; } -string INIReader::Get(string section, string name, string default_value) + +std::string INIReader::Get(std::string section, std::string name, std::string default_value) { - string key = MakeKey(section, name); + std::string key = MakeKey(section, name); return _values.count(key) ? _values[key] : default_value; } -long INIReader::GetInteger(string section, string name, long default_value) + +long INIReader::GetInteger(std::string section, std::string name, long default_value) { - string valstr = Get(section, name, ""); + std::string valstr = Get(section, name, ""); const char* value = valstr.c_str(); char* end; // This parses "1234" (decimal) and also "0x4D2" (hex) @@ -78,15 +80,17 @@ long INIReader::GetInteger(string section, string name, long default_value) return end > value ? n : default_value; } -string INIReader::MakeKey(string section, string name) + +std::string INIReader::MakeKey(std::string section, std::string name) { - string key = section + "." + name; + std::string key = section + "." + name; // Convert to lower case to make lookups case-insensitive for (unsigned int i = 0; i < key.length(); i++) key[i] = tolower(key[i]); return key; } + int INIReader::ValueHandler(void* user, const char* section, const char* name, const char* value) { diff --git a/src/core/libs/ini.cc b/src/core/libs/ini.cc index 1e125bab7..c961676b0 100644 --- a/src/core/libs/ini.cc +++ b/src/core/libs/ini.cc @@ -53,11 +53,11 @@ * ------------------------------------------------------------------------- */ -#include -#include -#include - #include "ini.h" +#include +#include +#include + #define MAX_LINE 200 #define MAX_SECTION 50 @@ -107,70 +107,88 @@ int ini_parse(const char* filename, char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; - FILE* file; + std::ifstream file; char* start; char* end; char* name; char* value; int lineno = 0; int error = 0; + std::string line_str; - file = fopen(filename, "r"); - if (!file) + file.open(filename, std::fstream::in); + if (!file.is_open()) return -1; /* Scan through file line by line */ - while (fgets(line, sizeof(line), file) != NULL) { - lineno++; - start = lskip(rstrip(line)); + while (std::getline(file, line_str)) + { + lineno++; + int len_str = line_str.length(); + const char * read_line = line_str.data(); + if (len_str > (MAX_LINE - 1)) len_str = MAX_LINE - 1; + int i; + for(i = 0; i < len_str; i++) + { + line[i] = read_line[i]; + } + line[len_str] = '\0'; + start = lskip(rstrip(line)); #if INI_ALLOW_MULTILINE - if (*prev_name && *start && start > line) { - /* Non-black line with leading whitespace, treat as continuation - of previous name's value (as per Python ConfigParser). */ - if (!handler(user, section, prev_name, start) && !error) - error = lineno; - } - else -#endif - if (*start == '[') { - /* A "[section]" line */ - end = find_char_or_comment(start + 1, ']'); - if (*end == ']') { - *end = '\0'; - strncpy0(section, start + 1, sizeof(section)); - *prev_name = '\0'; - } - else if (!error) { - /* No ']' found on section line */ - error = lineno; - } - } - else if (*start && *start != ';') { - /* Not a comment, must be a name=value pair */ - end = find_char_or_comment(start, '='); - if (*end == '=') { - *end = '\0'; - name = rstrip(start); - value = lskip(end + 1); - end = find_char_or_comment(value, ';'); - if (*end == ';') - *end = '\0'; - rstrip(value); - - /* Valid name=value pair found, call handler */ - strncpy0(prev_name, name, sizeof(prev_name)); - if (!handler(user, section, name, value) && !error) + if (*prev_name && *start && start > line) + { + /* Non-black line with leading whitespace, treat as continuation + of previous name's value (as per Python ConfigParser). */ + if (!handler(user, section, prev_name, start) && !error) error = lineno; } - else if (!error) { - /* No '=' found on name=value line */ - error = lineno; - } - } - } + else +#endif + if (*start == '[') + { + /* A "[section]" line */ + end = find_char_or_comment(start + 1, ']'); + if (*end == ']') + { + *end = '\0'; + strncpy0(section, start + 1, sizeof(section)); + *prev_name = '\0'; + } + else if (!error) + { + /* No ']' found on section line */ + error = lineno; + } + } + else if (*start && *start != ';') + { + /* Not a comment, must be a name=value pair */ + end = find_char_or_comment(start, '='); + if (*end == '=') + { + *end = '\0'; + name = rstrip(start); + value = lskip(end + 1); + end = find_char_or_comment(value, ';'); + if (*end == ';') + *end = '\0'; + rstrip(value); - fclose(file); + /* Valid name=value pair found, call handler */ + strncpy0(prev_name, name, sizeof(prev_name)); + if (!handler(user, section, name, value) && !error) + error = lineno; + } + else if (!error) + { + /* No '=' found on name=value line */ + error = lineno; + } + } + } + + file.close(); return error; } diff --git a/src/core/libs/ini.h b/src/core/libs/ini.h index 2fa9e5251..45f1f33d2 100644 --- a/src/core/libs/ini.h +++ b/src/core/libs/ini.h @@ -53,13 +53,9 @@ * ------------------------------------------------------------------------- */ -#ifndef __INI_H__ -#define __INI_H__ +#ifndef GNSS_SDR_INI_H_ +#define GNSS_SDR_INI_H_ -/* Make this header file easier to include in C++ code */ -#ifdef __cplusplus -extern "C" { -#endif /* Parse given INI-style file. May have [section]s, name=value pairs (whitespace stripped), and comments starting with ';' (semicolon). Section @@ -84,8 +80,5 @@ int ini_parse(const char* filename, #define INI_ALLOW_MULTILINE 1 #endif -#ifdef __cplusplus -} -#endif -#endif /* __INI_H__ */ +#endif // GNSS_SDR_INI_H_