cfgfile 0.2.11
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__UTILS_HPP__INCLUDED
32#define CFGFILE__UTILS_HPP__INCLUDED
33
34// cfgfile include.
35#include "types.hpp"
36#include "tag.hpp"
37#include "input_stream.hpp"
38#include "parser.hpp"
39#include "exceptions.hpp"
40
41#if defined( CFGFILE_QT_SUPPORT ) && defined( CFGFILE_XML_SUPPORT )
42// Qt include.
43#include <QDomDocument>
44#include <QDomElement>
45#endif
46
47
48namespace cfgfile {
49
50//
51// file_format_t
52//
53
55enum class file_format_t {
60}; // enum FileFormat
61
62
63namespace details {
64
65//
66// determine_format_t
67//
68
70template< typename Trait = string_trait_t >
72public:
73 explicit determine_format_t( typename Trait::istream_t & stream )
74 : m_stream( stream )
75 {
76 }
77
80 {
81 static const typename Trait::char_t xml = Trait::from_ascii( '<' );
82
83 typename Trait::char_t ch( 0x00 );
84
85 while( !Trait::is_at_end( m_stream ) )
86 {
87 m_stream >> ch;
88
89 if( Trait::is_space( ch ) )
90 continue;
91
92 if( ch == xml )
94 else
96 }
97
99 }
100
101private:
103 typename Trait::istream_t & m_stream;
104}; // class determine_format_t
105
106} /* namespace details */
107
108
109//
110// read_cfgfile
111//
112
114template< typename Trait = string_trait_t >
115static inline void read_cfgfile(
117 tag_t< Trait > & tag,
119 typename Trait::istream_t & stream,
121 const typename Trait::string_t & file_name )
122{
124
125 {
126 details::determine_format_t< Trait > d( stream );
127
128 fmt = d.format();
129 }
130
131 Trait::to_begin( stream );
132
133 switch( fmt )
134 {
136 {
137 input_stream_t< Trait > is( file_name, stream );
138
139 parser_t< Trait > parser( tag, is );
140
141 parser.parse( file_name );
142 }
143 break;
144
146 {
147#ifdef CFGFILE_QT_SUPPORT
148#ifdef CFGFILE_XML_SUPPORT
149 QDomDocument doc;
150
151 QString error;
152 int line = 0;
153 int column = 0;
154
155 const QString data = stream.readAll();
156
157 if( !doc.setContent( data, true, &error, &line, &column ) )
158 throw exception_t< Trait >( QString( "Unable to parse XML "
159 "from file: \"%1\". \"%2\" On line %3, column %4." )
160 .arg( file_name )
161 .arg( error )
162 .arg( QString::number( line ) )
163 .arg( QString::number( column ) ) );
164
165 parser_t< Trait > parser( tag, doc );
166
167 parser.parse( file_name );
168#else
169 throw exception_t< Trait >(
170 Trait::from_ascii( "To use XML format build cfgfile "
171 "with CFGFILE_XML_SUPPORT" ) );
172#endif // CFGFILE_XML_SUPPORT
173#else
174 throw exception_t< Trait >(
175 Trait::from_ascii( "XML supported only with Qt. Parsing of file \"" ) +
176 file_name + Trait::from_ascii( "\" failed." ) );
177#endif // CFGFILE_QT_SUPPORT
178 }
179 break;
180 }
181}
182
183
184//
185// write_cfgfile
186//
187
189template< typename Trait >
190static inline void write_cfgfile(
192 const tag_t< Trait > & tag,
194 typename Trait::ostream_t & stream,
197{
198 switch( fmt )
199 {
201 {
202 const typename Trait::string_t content = tag.print();
203
204 stream << content;
205 }
206 break;
207
209 {
210#ifdef CFGFILE_QT_SUPPORT
211#ifdef CFGFILE_XML_SUPPORT
212 QDomDocument doc;
213
214 tag.print( doc );
215
216 stream << doc.toString( 4 );
217#else
218 throw exception_t< Trait >(
219 Trait::from_ascii( "To use XML format build cfgfile "
220 "with CFGFILE_XML_SUPPORT" ) );
221#endif // CFGFILE_XML_SUPPORT
222#else
223 throw exception_t< Trait >(
224 Trait::from_ascii( "XML supported only with Qt." ) );
225#endif // CFGFILE_QT_SUPPORT
226 }
227 break;
228 }
229}
230
231} /* namespace cfgfile */
232
233#endif // CFGFILE__UTILS_HPP__INCLUDED
Determine format of the configuration file.
Definition utils.hpp:71
file_format_t format()
Determine file's format.
Definition utils.hpp:79
determine_format_t(typename Trait::istream_t &stream)
Definition utils.hpp:73
Exception in the library.
Definition exceptions.hpp:51
virtual Trait::string_t print(int indent=0) const =0
Print tag to the output.
Definition const.hpp:38
file_format_t
Format of the conf file.
Definition utils.hpp:55
@ xml_format
XML format.
@ cfgfile_format
cfgfile format.