E-MailRelay
goptionreader.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file goptionreader.cpp
19///
20
21#include "gdef.h"
22#include "goptionreader.h"
23#include "gstringview.h"
24#include "gstringtoken.h"
25#include "gfile.h"
26#include "gstr.h"
27#include "gexception.h"
28#include <fstream>
29
30G::StringArray G::OptionReader::read( const Path & filename , std::size_t limit )
31{
32 StringArray result ;
33 add( result , filename , limit ) ;
34 return result ;
35}
36
37std::size_t G::OptionReader::add( StringArray & out , const Path & filename , std::size_t limit )
38{
39 std::ifstream f ;
40 File::open( f , filename ) ; // (no G::Root)
41 if( !f.good() ) throw FileError( filename.str() ) ;
42 std::string line ;
43 std::size_t n = 0U ;
44 while( Str::readLine(f,line) && ( limit == 0U || n < limit ) )
45 {
46 if( line.find('\0') != std::string::npos )
47 throw G::Exception( "invalid character in configuration file" , filename.str() ) ;
48
49 Str::trimRight( line , "\r" ) ;
50 std::string_view sv( line ) ;
51 StringTokenView t( sv , " =\t" , 3U ) ;
52 std::string_view key = t() ;
53 if( key.empty() || key.find('#') == 0U )
54 continue ;
55
56 std::string_view value = (++t)() ;
57 if( !value.empty() )
58 value = Str::trimRightView( sv.substr(t.pos()) , " \t" ) ;
59 if( value.size() >= 2U && value[0] == '"' && value[value.size()-1U] == '"' )
60 value = value.substr( 1U , value.size() - 2U ) ;
61
62 out.push_back( std::string(2U,'-')
63 .append(key.data(),key.size())
64 .append(value.empty()?0U:1U,'=')
65 .append(value.data(),value.size()) ) ;
66
67 n++ ;
68 }
69 if( limit && n == limit )
70 throw G::Exception( "too many lines in configuration file" , filename.str() ) ;
71 return n ;
72}
73
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:64
static void open(std::ofstream &, const Path &)
Calls open() on the given output file stream.
Definition: gfile_unix.cpp:56
static StringArray read(const G::Path &, std::size_t limit=1000U)
Reads options from file as a list of strings like "--foo=bar".
static std::size_t add(StringArray &out, const G::Path &, std::size_t limit=1000U)
Adds options read from file to an existing list.
A Path object represents a file system path.
Definition: gpath.h:82
std::string str() const
Returns the path string.
Definition: gpath.h:243
static std::string & trimRight(std::string &s, std::string_view ws, std::size_t limit=0U)
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
Definition: gstr.cpp:313
static std::string_view trimRightView(std::string_view sv, std::string_view ws, std::size_t limit=0U) noexcept
Trims the rhs of s, taking off up to 'limit' of the 'ws' characters.
Definition: gstr.cpp:325
static std::istream & readLine(std::istream &stream, std::string &result, std::string_view eol={}, bool pre_erase_result=true, std::size_t limit=0U)
Reads a line from the stream using the given line terminator, which may be multi-character.
Definition: gstr.cpp:958
A zero-copy string token iterator where the token separators are runs of whitespace characters,...
Definition: gstringtoken.h:54
std::size_t pos() const noexcept
Returns the offset of data().
Definition: gstringtoken.h:169
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30