1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-10-31 23:33:03 +00:00

Replace stdio.h by fstream

This commit is contained in:
Carles Fernandez
2017-08-13 10:00:01 +02:00
parent 59164c33bd
commit 5f181ef915
3 changed files with 89 additions and 74 deletions

View File

@@ -45,32 +45,34 @@
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
#include <cctype>
#include <cstdlib>
#include "ini.h"
#include "INIReader.h" #include "INIReader.h"
#include <cctype> // for tolower
#include <cstdlib> // 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); _error = ini_parse(filename.c_str(), ValueHandler, this);
} }
int INIReader::ParseError() int INIReader::ParseError()
{ {
return _error; 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; 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(); const char* value = valstr.c_str();
char* end; char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex) // 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; 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 // Convert to lower case to make lookups case-insensitive
for (unsigned int i = 0; i < key.length(); i++) for (unsigned int i = 0; i < key.length(); i++)
key[i] = tolower(key[i]); key[i] = tolower(key[i]);
return key; return key;
} }
int INIReader::ValueHandler(void* user, const char* section, const char* name, int INIReader::ValueHandler(void* user, const char* section, const char* name,
const char* value) const char* value)
{ {

View File

@@ -53,11 +53,11 @@
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "ini.h" #include "ini.h"
#include <cctype>
#include <fstream>
#include <string>
#define MAX_LINE 200 #define MAX_LINE 200
#define MAX_SECTION 50 #define MAX_SECTION 50
@@ -107,25 +107,37 @@ int ini_parse(const char* filename,
char section[MAX_SECTION] = ""; char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = ""; char prev_name[MAX_NAME] = "";
FILE* file; std::ifstream file;
char* start; char* start;
char* end; char* end;
char* name; char* name;
char* value; char* value;
int lineno = 0; int lineno = 0;
int error = 0; int error = 0;
std::string line_str;
file = fopen(filename, "r"); file.open(filename, std::fstream::in);
if (!file) if (!file.is_open())
return -1; return -1;
/* Scan through file line by line */ /* Scan through file line by line */
while (fgets(line, sizeof(line), file) != NULL) { while (std::getline(file, line_str))
{
lineno++; 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)); start = lskip(rstrip(line));
#if INI_ALLOW_MULTILINE #if INI_ALLOW_MULTILINE
if (*prev_name && *start && start > line) { if (*prev_name && *start && start > line)
{
/* Non-black line with leading whitespace, treat as continuation /* Non-black line with leading whitespace, treat as continuation
of previous name's value (as per Python ConfigParser). */ of previous name's value (as per Python ConfigParser). */
if (!handler(user, section, prev_name, start) && !error) if (!handler(user, section, prev_name, start) && !error)
@@ -133,23 +145,28 @@ int ini_parse(const char* filename,
} }
else else
#endif #endif
if (*start == '[') { if (*start == '[')
{
/* A "[section]" line */ /* A "[section]" line */
end = find_char_or_comment(start + 1, ']'); end = find_char_or_comment(start + 1, ']');
if (*end == ']') { if (*end == ']')
{
*end = '\0'; *end = '\0';
strncpy0(section, start + 1, sizeof(section)); strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0'; *prev_name = '\0';
} }
else if (!error) { else if (!error)
{
/* No ']' found on section line */ /* No ']' found on section line */
error = lineno; error = lineno;
} }
} }
else if (*start && *start != ';') { else if (*start && *start != ';')
{
/* Not a comment, must be a name=value pair */ /* Not a comment, must be a name=value pair */
end = find_char_or_comment(start, '='); end = find_char_or_comment(start, '=');
if (*end == '=') { if (*end == '=')
{
*end = '\0'; *end = '\0';
name = rstrip(start); name = rstrip(start);
value = lskip(end + 1); value = lskip(end + 1);
@@ -163,14 +180,15 @@ int ini_parse(const char* filename,
if (!handler(user, section, name, value) && !error) if (!handler(user, section, name, value) && !error)
error = lineno; error = lineno;
} }
else if (!error) { else if (!error)
{
/* No '=' found on name=value line */ /* No '=' found on name=value line */
error = lineno; error = lineno;
} }
} }
} }
fclose(file); file.close();
return error; return error;
} }

View File

@@ -53,13 +53,9 @@
* ------------------------------------------------------------------------- * -------------------------------------------------------------------------
*/ */
#ifndef __INI_H__ #ifndef GNSS_SDR_INI_H_
#define __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 /* Parse given INI-style file. May have [section]s, name=value pairs
(whitespace stripped), and comments starting with ';' (semicolon). Section (whitespace stripped), and comments starting with ';' (semicolon). Section
@@ -84,8 +80,5 @@ int ini_parse(const char* filename,
#define INI_ALLOW_MULTILINE 1 #define INI_ALLOW_MULTILINE 1
#endif #endif
#ifdef __cplusplus
}
#endif
#endif /* __INI_H__ */ #endif // GNSS_SDR_INI_H_