mirror of
https://github.com/gnss-sdr/gnss-sdr
synced 2024-12-14 12:10:34 +00:00
Replace stdio.h by fstream
This commit is contained in:
parent
59164c33bd
commit
5f181ef915
@ -45,32 +45,34 @@
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include "ini.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);
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -53,11 +53,11 @@
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ini.h"
|
||||
#include <cctype>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
|
||||
#define MAX_LINE 200
|
||||
#define MAX_SECTION 50
|
||||
@ -107,25 +107,37 @@ 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) {
|
||||
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) {
|
||||
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)
|
||||
@ -133,23 +145,28 @@ int ini_parse(const char* filename,
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if (*start == '[') {
|
||||
if (*start == '[')
|
||||
{
|
||||
/* A "[section]" line */
|
||||
end = find_char_or_comment(start + 1, ']');
|
||||
if (*end == ']') {
|
||||
if (*end == ']')
|
||||
{
|
||||
*end = '\0';
|
||||
strncpy0(section, start + 1, sizeof(section));
|
||||
*prev_name = '\0';
|
||||
}
|
||||
else if (!error) {
|
||||
else if (!error)
|
||||
{
|
||||
/* No ']' found on section line */
|
||||
error = lineno;
|
||||
}
|
||||
}
|
||||
else if (*start && *start != ';') {
|
||||
else if (*start && *start != ';')
|
||||
{
|
||||
/* Not a comment, must be a name=value pair */
|
||||
end = find_char_or_comment(start, '=');
|
||||
if (*end == '=') {
|
||||
if (*end == '=')
|
||||
{
|
||||
*end = '\0';
|
||||
name = rstrip(start);
|
||||
value = lskip(end + 1);
|
||||
@ -163,14 +180,15 @@ int ini_parse(const char* filename,
|
||||
if (!handler(user, section, name, value) && !error)
|
||||
error = lineno;
|
||||
}
|
||||
else if (!error) {
|
||||
else if (!error)
|
||||
{
|
||||
/* No '=' found on name=value line */
|
||||
error = lineno;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
file.close();
|
||||
|
||||
return error;
|
||||
}
|
||||
|
@ -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_
|
||||
|
Loading…
Reference in New Issue
Block a user