diff options
| -rw-r--r-- | default.bld | 2 | ||||
| -rw-r--r-- | src/strfilter.h | 124 | ||||
| -rw-r--r-- | src/string.h | 2 |
3 files changed, 126 insertions, 2 deletions
diff --git a/default.bld b/default.bld index 6666fe2..1b6eb4d 100644 --- a/default.bld +++ b/default.bld | |||
| @@ -162,7 +162,7 @@ target files("src/tests/*.cpp").replace("src/","").replace(".cpp","") | |||
| 162 | 162 | ||
| 163 | // Some tests need extra libs and whatnot, that goes here. | 163 | // Some tests need extra libs and whatnot, that goes here. |
| 164 | 164 | ||
| 165 | target ["tests/bzip2", "tests/streamstack"] | 165 | target ["tests/bzip2", "tests/streamstack", "tests/enc"] |
| 166 | { | 166 | { |
| 167 | LDFLAGS += "-lbz2"; | 167 | LDFLAGS += "-lbz2"; |
| 168 | } | 168 | } |
diff --git a/src/strfilter.h b/src/strfilter.h new file mode 100644 index 0000000..8da0a3f --- /dev/null +++ b/src/strfilter.h | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | #ifndef STR_FILTER_H | ||
| 2 | #define STR_FILTER_H | ||
| 3 | |||
| 4 | #include "bu/string.h" | ||
| 5 | #include "bu/membuf.h" | ||
| 6 | |||
| 7 | namespace Bu | ||
| 8 | { | ||
| 9 | // | ||
| 10 | // Encoders | ||
| 11 | // | ||
| 12 | template<typename tFilter> | ||
| 13 | Bu::String encodeStr( const Bu::String &sIn ) | ||
| 14 | { | ||
| 15 | Bu::MemBuf mb; | ||
| 16 | { | ||
| 17 | tFilter fEnc( mb ); | ||
| 18 | fEnc.write( sIn.getStr(), sIn.getSize() ); | ||
| 19 | } | ||
| 20 | return mb.getString(); | ||
| 21 | } | ||
| 22 | |||
| 23 | template<typename tFilter, typename p1t> | ||
| 24 | Bu::String encodeStr( const Bu::String &sIn, p1t p1 ) | ||
| 25 | { | ||
| 26 | Bu::MemBuf mb; | ||
| 27 | { | ||
| 28 | tFilter fEnc( mb, p1 ); | ||
| 29 | fEnc.write( sIn.getStr(), sIn.getSize() ); | ||
| 30 | } | ||
| 31 | return mb.getString(); | ||
| 32 | } | ||
| 33 | |||
| 34 | template<typename tFilter, typename p1t, typename p2t> | ||
| 35 | Bu::String encodeStr( const Bu::String &sIn, p1t p1, p2t p2 ) | ||
| 36 | { | ||
| 37 | Bu::MemBuf mb; | ||
| 38 | { | ||
| 39 | tFilter fEnc( mb, p1, p2 ); | ||
| 40 | fEnc.write( sIn.getStr(), sIn.getSize() ); | ||
| 41 | } | ||
| 42 | return mb.getString(); | ||
| 43 | } | ||
| 44 | |||
| 45 | template<typename tFilter, typename p1t, typename p2t, typename p3t> | ||
| 46 | Bu::String encodeStr( const Bu::String &sIn, p1t p1, p2t p2, p3t p3 ) | ||
| 47 | { | ||
| 48 | Bu::MemBuf mb; | ||
| 49 | { | ||
| 50 | tFilter fEnc( mb, p1, p2 ); | ||
| 51 | fEnc.write( sIn.getStr(), sIn.getSize() ); | ||
| 52 | } | ||
| 53 | return mb.getString(); | ||
| 54 | } | ||
| 55 | |||
| 56 | // | ||
| 57 | // Decoders | ||
| 58 | // | ||
| 59 | template<typename tFilter> | ||
| 60 | Bu::String decodeStr( const Bu::String &sIn ) | ||
| 61 | { | ||
| 62 | Bu::MemBuf mb( sIn ); | ||
| 63 | tFilter fDec( mb ); | ||
| 64 | char buf[1024]; | ||
| 65 | String sRet; | ||
| 66 | for(;;) | ||
| 67 | { | ||
| 68 | int iRead = fDec.read( buf, 1024 ); | ||
| 69 | if( iRead == 0 ) | ||
| 70 | return sRet; | ||
| 71 | sRet.append( buf, iRead ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | template<typename tFilter, typename p1t> | ||
| 76 | Bu::String decodeStr( const Bu::String &sIn, p1t p1 ) | ||
| 77 | { | ||
| 78 | Bu::MemBuf mb( sIn ); | ||
| 79 | tFilter fDec( mb, p1 ); | ||
| 80 | char buf[1024]; | ||
| 81 | String sRet; | ||
| 82 | for(;;) | ||
| 83 | { | ||
| 84 | int iRead = fDec.read( buf, 1024 ); | ||
| 85 | if( iRead == 0 ) | ||
| 86 | return sRet; | ||
| 87 | sRet.append( buf, iRead ); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | template<typename tFilter, typename p1t, typename p2t> | ||
| 92 | Bu::String decodeStr( const Bu::String &sIn, p1t p1, p2t p2 ) | ||
| 93 | { | ||
| 94 | Bu::MemBuf mb( sIn ); | ||
| 95 | tFilter fDec( mb, p1, p2 ); | ||
| 96 | char buf[1024]; | ||
| 97 | String sRet; | ||
| 98 | for(;;) | ||
| 99 | { | ||
| 100 | int iRead = fDec.read( buf, 1024 ); | ||
| 101 | if( iRead == 0 ) | ||
| 102 | return sRet; | ||
| 103 | sRet.append( buf, iRead ); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | template<typename tFilter, typename p1t, typename p2t, typename p3t> | ||
| 108 | Bu::String decodeStr( const Bu::String &sIn, p1t p1, p2t p2, p3t p3 ) | ||
| 109 | { | ||
| 110 | Bu::MemBuf mb( sIn ); | ||
| 111 | tFilter fDec( mb, p1, p2, p3 ); | ||
| 112 | char buf[1024]; | ||
| 113 | String sRet; | ||
| 114 | for(;;) | ||
| 115 | { | ||
| 116 | int iRead = fDec.read( buf, 1024 ); | ||
| 117 | if( iRead == 0 ) | ||
| 118 | return sRet; | ||
| 119 | sRet.append( buf, iRead ); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | |||
| 124 | #endif | ||
diff --git a/src/string.h b/src/string.h index 2874e37..a9006d1 100644 --- a/src/string.h +++ b/src/string.h | |||
| @@ -18,12 +18,12 @@ | |||
| 18 | #include "bu/list.h" | 18 | #include "bu/list.h" |
| 19 | #include "bu/fmt.h" | 19 | #include "bu/fmt.h" |
| 20 | #include "bu/variant.h" | 20 | #include "bu/variant.h" |
| 21 | |||
| 22 | #include <string.h> | 21 | #include <string.h> |
| 23 | 22 | ||
| 24 | namespace Bu | 23 | namespace Bu |
| 25 | { | 24 | { |
| 26 | class String; | 25 | class String; |
| 26 | class MemBuf; | ||
| 27 | 27 | ||
| 28 | /** @cond DEVEL */ | 28 | /** @cond DEVEL */ |
| 29 | class StringCore | 29 | class StringCore |
