diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/stable/sio.cpp | 38 | ||||
-rw-r--r-- | src/stable/sio.h | 8 | ||||
-rw-r--r-- | src/stable/string.cpp | 19 | ||||
-rw-r--r-- | src/stable/string.h | 32 | ||||
-rw-r--r-- | src/tests/print.cpp | 13 |
5 files changed, 88 insertions, 22 deletions
diff --git a/src/stable/sio.cpp b/src/stable/sio.cpp index 02ba631..e7f7565 100644 --- a/src/stable/sio.cpp +++ b/src/stable/sio.cpp | |||
@@ -10,25 +10,45 @@ | |||
10 | Bu::StdStream Bu::sioRaw; | 10 | Bu::StdStream Bu::sioRaw; |
11 | Bu::Formatter Bu::sio( Bu::sioRaw ); | 11 | Bu::Formatter Bu::sio( Bu::sioRaw ); |
12 | 12 | ||
13 | Bu::size Bu::print( Bu::Stream &s, const Bu::String &str ) | 13 | class PrintEndAction : public Bu::String::FormatProxyEndAction |
14 | { | 14 | { |
15 | return s.write( str.getStr(), str.getSize() ); | 15 | public: |
16 | PrintEndAction( Bu::Stream &s, bool bEndLn ) : | ||
17 | s( s ), | ||
18 | bEndLn( bEndLn ) | ||
19 | { | ||
20 | } | ||
21 | |||
22 | virtual void operator()( const Bu::String &sFinal ) | ||
23 | { | ||
24 | s.write( sFinal.getStr(), sFinal.getSize() ); | ||
25 | if( bEndLn ) | ||
26 | { | ||
27 | s.write("\n", 1); | ||
28 | s.flush(); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | Bu::Stream &s; | ||
33 | bool bEndLn; | ||
34 | }; | ||
35 | |||
36 | Bu::String::FormatProxy Bu::print( Bu::Stream &s, const Bu::String &str ) | ||
37 | { | ||
38 | return str.format( new PrintEndAction( s, false ) ); | ||
16 | } | 39 | } |
17 | 40 | ||
18 | Bu::size Bu::print( const Bu::String &str ) | 41 | Bu::String::FormatProxy Bu::print( const Bu::String &str ) |
19 | { | 42 | { |
20 | return print( sioRaw, str ); | 43 | return print( sioRaw, str ); |
21 | } | 44 | } |
22 | 45 | ||
23 | Bu::size Bu::println( Bu::Stream &s, const Bu::String &str ) | 46 | Bu::String::FormatProxy Bu::println( Bu::Stream &s, const Bu::String &str ) |
24 | { | 47 | { |
25 | Bu::size sRet = s.write( str.getStr(), str.getSize() ); | 48 | return str.format( new PrintEndAction( s, true ) ); |
26 | sRet += s.write("\n", 1 ); | ||
27 | s.flush(); | ||
28 | return sRet; | ||
29 | } | 49 | } |
30 | 50 | ||
31 | Bu::size Bu::println( const Bu::String &str ) | 51 | Bu::String::FormatProxy Bu::println( const Bu::String &str ) |
32 | { | 52 | { |
33 | return println( sioRaw, str ); | 53 | return println( sioRaw, str ); |
34 | } | 54 | } |
diff --git a/src/stable/sio.h b/src/stable/sio.h index cc7b098..d9761b2 100644 --- a/src/stable/sio.h +++ b/src/stable/sio.h | |||
@@ -16,11 +16,11 @@ namespace Bu | |||
16 | extern Bu::StdStream sioRaw; | 16 | extern Bu::StdStream sioRaw; |
17 | extern Bu::Formatter sio; | 17 | extern Bu::Formatter sio; |
18 | 18 | ||
19 | Bu::size print( Bu::Stream &s, const Bu::String &str ); | 19 | Bu::String::FormatProxy print( Bu::Stream &s, const Bu::String &str ); |
20 | Bu::size print( const Bu::String &str ); | 20 | Bu::String::FormatProxy print( const Bu::String &str ); |
21 | 21 | ||
22 | Bu::size println( Bu::Stream &s, const Bu::String &str ); | 22 | Bu::String::FormatProxy println( Bu::Stream &s, const Bu::String &str ); |
23 | Bu::size println( const Bu::String &str ); | 23 | Bu::String::FormatProxy println( const Bu::String &str ); |
24 | }; | 24 | }; |
25 | 25 | ||
26 | #endif | 26 | #endif |
diff --git a/src/stable/string.cpp b/src/stable/string.cpp index 4cef6b1..d1f76aa 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp | |||
@@ -1228,16 +1228,23 @@ bool Bu::String::isFlat() const | |||
1228 | // Sub-class Bu::String::FormatProxy | 1228 | // Sub-class Bu::String::FormatProxy |
1229 | // | 1229 | // |
1230 | 1230 | ||
1231 | Bu::String::FormatProxy::FormatProxy( const String &rFmt ) : | 1231 | Bu::String::FormatProxy::FormatProxy( const String &rFmt, |
1232 | rFmt( rFmt ) | 1232 | String::FormatProxyEndAction *pAct ) : |
1233 | rFmt( rFmt ), | ||
1234 | pAct( pAct ), | ||
1235 | bOpen( true ) | ||
1233 | { | 1236 | { |
1234 | } | 1237 | } |
1235 | 1238 | ||
1236 | Bu::String::FormatProxy::~FormatProxy() | 1239 | Bu::String::FormatProxy::~FormatProxy() |
1237 | { | 1240 | { |
1241 | if( pAct && bOpen ) | ||
1242 | end(); | ||
1243 | |||
1244 | delete pAct; | ||
1238 | } | 1245 | } |
1239 | 1246 | ||
1240 | Bu::String::FormatProxy::operator Bu::String() const | 1247 | Bu::String Bu::String::FormatProxy::end() const |
1241 | { | 1248 | { |
1242 | int iCount = lArgs.getSize(); | 1249 | int iCount = lArgs.getSize(); |
1243 | ArgList::const_iterator *aArg = | 1250 | ArgList::const_iterator *aArg = |
@@ -1287,8 +1294,12 @@ Bu::String::FormatProxy::operator Bu::String() const | |||
1287 | } | 1294 | } |
1288 | s++; | 1295 | s++; |
1289 | } | 1296 | } |
1290 | 1297 | ||
1298 | bOpen = false; | ||
1291 | delete[] aArg; | 1299 | delete[] aArg; |
1300 | |||
1301 | if( pAct ) | ||
1302 | (*pAct)( mbOut.getString() ); | ||
1292 | return mbOut.getString(); | 1303 | return mbOut.getString(); |
1293 | } | 1304 | } |
1294 | 1305 | ||
diff --git a/src/stable/string.h b/src/stable/string.h index 82b903b..c92a704 100644 --- a/src/stable/string.h +++ b/src/stable/string.h | |||
@@ -946,12 +946,21 @@ namespace Bu | |||
946 | void flatten() const; | 946 | void flatten() const; |
947 | bool isFlat() const; | 947 | bool isFlat() const; |
948 | 948 | ||
949 | public: | ||
950 | class FormatProxyEndAction | ||
951 | { | ||
952 | public: | ||
953 | virtual void operator()( const Bu::String &sFinal )=0; | ||
954 | }; | ||
955 | |||
949 | class FormatProxy | 956 | class FormatProxy |
950 | { | 957 | { |
958 | friend class Bu::String; | ||
959 | private: | ||
960 | FormatProxy( const String &rFmt, FormatProxyEndAction *pAct=NULL ); | ||
961 | |||
951 | public: | 962 | public: |
952 | FormatProxy( const String &rFmt ); | ||
953 | virtual ~FormatProxy(); | 963 | virtual ~FormatProxy(); |
954 | |||
955 | template<typename T> | 964 | template<typename T> |
956 | FormatProxy &arg( const T &x ) | 965 | FormatProxy &arg( const T &x ) |
957 | { | 966 | { |
@@ -968,7 +977,8 @@ namespace Bu | |||
968 | return *this; | 977 | return *this; |
969 | } | 978 | } |
970 | 979 | ||
971 | operator String() const; | 980 | String end() const; |
981 | operator String() const { return end(); } | ||
972 | 982 | ||
973 | private: | 983 | private: |
974 | const String &rFmt; | 984 | const String &rFmt; |
@@ -993,20 +1003,32 @@ namespace Bu | |||
993 | }; | 1003 | }; |
994 | typedef Bu::List<Arg> ArgList; | 1004 | typedef Bu::List<Arg> ArgList; |
995 | ArgList lArgs; | 1005 | ArgList lArgs; |
1006 | FormatProxyEndAction *pAct; | ||
1007 | mutable bool bOpen; | ||
996 | }; | 1008 | }; |
997 | 1009 | ||
998 | public: | 1010 | public: |
999 | template<typename ArgType> | 1011 | template<typename ArgType> |
1000 | FormatProxy arg( const ArgType &x ) | 1012 | FormatProxy arg( const ArgType &x ) const |
1001 | { | 1013 | { |
1002 | return FormatProxy( *this ).arg( x ); | 1014 | return FormatProxy( *this ).arg( x ); |
1003 | } | 1015 | } |
1004 | 1016 | ||
1005 | template<typename ArgType> | 1017 | template<typename ArgType> |
1006 | FormatProxy arg( const ArgType &x, const Bu::Fmt &f ) | 1018 | FormatProxy arg( const ArgType &x, const Bu::Fmt &f ) const |
1007 | { | 1019 | { |
1008 | return FormatProxy( *this ).arg( x, f ); | 1020 | return FormatProxy( *this ).arg( x, f ); |
1009 | } | 1021 | } |
1022 | |||
1023 | FormatProxy format() const | ||
1024 | { | ||
1025 | return FormatProxy( *this ); | ||
1026 | } | ||
1027 | |||
1028 | FormatProxy format( FormatProxyEndAction *pEndAction ) const | ||
1029 | { | ||
1030 | return FormatProxy( *this, pEndAction ); | ||
1031 | } | ||
1010 | }; | 1032 | }; |
1011 | 1033 | ||
1012 | template<class T> String operator+( const T *pLeft, const String &rRight ) | 1034 | template<class T> String operator+( const T *pLeft, const String &rRight ) |
diff --git a/src/tests/print.cpp b/src/tests/print.cpp new file mode 100644 index 0000000..7d55554 --- /dev/null +++ b/src/tests/print.cpp | |||
@@ -0,0 +1,13 @@ | |||
1 | #include <bu/sio.h> | ||
2 | |||
3 | int main() | ||
4 | { | ||
5 | Bu::print("hello there %1!\n").arg("Bob"); | ||
6 | |||
7 | Bu::println("This is %1 crazy, like %2 times over!"). | ||
8 | arg("totally").arg( 47.2 ); | ||
9 | Bu::println("This is unsubstituted?"); | ||
10 | |||
11 | return 0; | ||
12 | } | ||
13 | |||