cfgfile 0.2.11
Loading...
Searching...
No Matches
string_format.hpp
Go to the documentation of this file.
1
31#ifndef CFGFILE__STRING_FORMAT_HPP__INCLUDED
32#define CFGFILE__STRING_FORMAT_HPP__INCLUDED
33
34// cfgfile include.
35#include "types.hpp"
36#include "const.hpp"
37#include "exceptions.hpp"
38
39
40namespace cfgfile {
41
42//
43// to_cfgfile_format
44//
45
47template< typename Trait >
48static inline typename Trait::string_t to_cfgfile_format(
49 const typename Trait::string_t & what )
50{
51 if( what.empty() )
52 return Trait::from_ascii( "\"\"" );
53
54 if( what.find( const_t< Trait >::c_begin_tag ) == Trait::string_t::npos &&
55 what.find( const_t< Trait >::c_end_tag ) == Trait::string_t::npos &&
56 what.find( const_t< Trait >::c_quotes ) == Trait::string_t::npos &&
57 what.find( const_t< Trait >::c_carriage_return ) == Trait::string_t::npos &&
58 what.find( const_t< Trait >::c_line_feed ) == Trait::string_t::npos &&
59 what.find( const_t< Trait >::c_tab ) == Trait::string_t::npos &&
60 what.find( const_t< Trait >::c_back_slash ) == Trait::string_t::npos &&
61 what.find( const_t< Trait >::c_space ) == Trait::string_t::npos &&
62 what.find( const_t< Trait >::c_one_line_comment ) == Trait::string_t::npos &&
63 what.find( const_t< Trait >::c_start_multi_line_comment ) == Trait::string_t::npos &&
64 what.find( const_t< Trait >::c_finish_multi_line_comment ) == Trait::string_t::npos )
65 return what;
66 else
67 {
68 typename Trait::string_t result;
69
70 result.push_back( const_t< Trait >::c_quotes );
71
72 for( const typename Trait::char_t & ch : what )
73 {
75 result.append( Trait::from_ascii( "\\\"" ) );
77 result.append( Trait::from_ascii( "\\n" ) );
78 else if( ch == const_t< Trait >::c_line_feed )
79 result.append( Trait::from_ascii( "\\r" ) );
80 else if( ch == const_t< Trait >::c_tab )
81 result.append( Trait::from_ascii( "\\t" ) );
82 else if( ch == const_t< Trait >::c_back_slash )
83 result.append( Trait::from_ascii( "\\\\" ) );
84 else
85 result.push_back( ch );
86 }
87
88 result.push_back( const_t< Trait >::c_quotes );
89
90 return result;
91 }
92} // to_cfgfile_format
93
94
95//
96// from_cfgfile_format
97//
98
100template< typename Trait = string_trait_t >
101static inline typename Trait::string_t from_cfgfile_format(
102 const typename Trait::string_t & what )
103{
104 if( what.find( const_t< Trait >::c_quotes ) == 0 &&
105 what.rfind( const_t< Trait >::c_quotes ) == what.length() - 1 )
106 {
107 typename Trait::string_t tmp = what.substr( 1, what.length() - 2 );
108
109 typename Trait::string_t result;
110
111 for( typename Trait::string_t::size_type i = 0; i < tmp.length(); ++i )
112 {
113 if( tmp.at( i ) == const_t< Trait >::c_back_slash )
114 {
115 if( i + 1 < tmp.length() )
116 {
117 if( tmp.at( i + 1 ) == const_t< Trait >::c_quotes )
118 result.push_back( const_t< Trait >::c_quotes );
119 else if( tmp.at( i + 1 ) == const_t< Trait >::c_n )
120 result.push_back( const_t< Trait >::c_carriage_return );
121 else if( tmp.at( i + 1 ) == const_t< Trait >::c_r )
122 result.push_back( const_t< Trait >::c_line_feed );
123 else if( tmp.at( i + 1 ) == const_t< Trait >::c_t )
124 result.push_back( const_t< Trait >::c_tab );
125 else if( tmp.at( i + 1 ) == const_t< Trait >::c_back_slash )
126 result.push_back( const_t< Trait >::c_back_slash );
127 else
128 throw exception_t< Trait > ( Trait::from_ascii(
129 "Unrecognized backslash sequence \"\\" ) +
130 typename Trait::string_t( 1, tmp.at( i + 1 ) ) +
131 Trait::from_ascii( "\"." ) );
132
133 ++i;
134 }
135 else
136 throw exception_t< Trait > ( Trait::from_ascii(
137 "Unfinished backslash sequence \"\\\"." ) );
138 }
139 else
140 result.push_back( tmp.at( i ) );
141 }
142
143 return result;
144 }
145 else
146 return what;
147} // from_cfgfile_format
148
149} /* namespace cfgfile */
150
151#endif // CFGFILE__STRING_FORMAT_HPP__INCLUDED
Definition const.hpp:38
static const Trait::char_t c_line_feed
Definition const.hpp:56
static const Trait::char_t c_back_slash
Definition const.hpp:52
static const Trait::char_t c_r
Definition const.hpp:51
static const Trait::char_t c_tab
Definition const.hpp:54
static const Trait::char_t c_n
Definition const.hpp:49
static const Trait::string_t c_finish_multi_line_comment
Definition const.hpp:62
static const Trait::string_t c_one_line_comment
Definition const.hpp:60
static const Trait::char_t c_quotes
Definition const.hpp:48
static const Trait::char_t c_t
Definition const.hpp:50
static const Trait::char_t c_begin_tag
Definition const.hpp:46
static const Trait::char_t c_carriage_return
Definition const.hpp:55
static const Trait::string_t c_start_multi_line_comment
Definition const.hpp:61
static const Trait::char_t c_space
Definition const.hpp:53
static const Trait::char_t c_end_tag
Definition const.hpp:47