From 469bbcf0701e1eb8a6670c23145b0da87357e178 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 25 Mar 2012 20:00:08 +0000 Subject: Code is all reorganized. We're about ready to release. I should write up a little explenation of the arrangement. --- src/archival.cpp | 35 - src/archival.h | 52 -- src/archive.cpp | 89 -- src/archive.h | 138 ---- src/archivebase.cpp | 197 ----- src/archivebase.h | 75 -- src/array.cpp | 10 - src/array.h | 703 ---------------- src/atom.cpp | 8 - src/atom.h | 147 ---- src/base64.cpp | 219 ----- src/base64.h | 59 -- src/bitstring.cpp | 479 ----------- src/bitstring.h | 224 ----- src/buffer.cpp | 168 ---- src/buffer.h | 58 -- src/bzip2.cpp | 233 ------ src/bzip2.h | 49 -- src/cache.cpp | 8 - src/cache.h | 437 ---------- src/cachecalc.cpp | 8 - src/cachecalc.h | 63 -- src/cachestore.cpp | 9 - src/cachestore.h | 46 -- src/cachestorefiles.cpp | 9 - src/cachestorefiles.h | 207 ----- src/cachestoremyriad.cpp | 9 - src/cachestoremyriad.h | 158 ---- src/client.cpp | 320 ------- src/client.h | 133 --- src/clientlink.cpp | 17 - src/clientlink.h | 25 - src/clientlinkfactory.cpp | 17 - src/clientlinkfactory.h | 26 - src/condition.cpp | 49 -- src/condition.h | 90 -- src/conduit.cpp | 233 ------ src/conduit.h | 64 -- src/crypt.cpp | 47 -- src/crypt.h | 19 - src/cryptohash.cpp | 38 - src/cryptohash.h | 33 - src/csvreader.cpp | 130 --- src/csvreader.h | 45 - src/csvwriter.cpp | 81 -- src/csvwriter.h | 45 - src/deflate.cpp | 253 ------ src/deflate.h | 66 -- src/exceptionbase.cpp | 93 --- src/exceptionbase.h | 190 ----- src/experimental/cache.cpp | 8 + src/experimental/cache.h | 437 ++++++++++ src/experimental/cachecalc.cpp | 8 + src/experimental/cachecalc.h | 63 ++ src/experimental/cachestore.cpp | 9 + src/experimental/cachestore.h | 46 ++ src/experimental/cachestorefiles.cpp | 9 + src/experimental/cachestorefiles.h | 207 +++++ src/experimental/cachestoremyriad.cpp | 9 + src/experimental/cachestoremyriad.h | 158 ++++ src/experimental/fastcgi.cpp | 372 +++++++++ src/experimental/fastcgi.h | 133 +++ src/experimental/httpget.cpp | 126 +++ src/experimental/httpget.h | 66 ++ src/experimental/lexer.cpp | 40 + src/experimental/lexer.h | 58 ++ src/experimental/parser.cpp | 311 +++++++ src/experimental/parser.h | 135 +++ src/experimental/random.cpp | 0 src/experimental/random.h | 0 src/experimental/randombase.cpp | 0 src/experimental/randombase.h | 0 src/experimental/randombasic.cpp | 0 src/experimental/randombasic.h | 0 src/experimental/regex.cpp | 95 +++ src/experimental/regex.h | 44 + src/experimental/regexengine.cpp | 5 + src/experimental/regexengine.h | 142 ++++ src/experimental/xmlreader.cpp | 173 ++++ src/experimental/xmlreader.h | 64 ++ src/extratypes.h | 30 - src/fastcgi.cpp | 372 --------- src/fastcgi.h | 133 --- src/fifo.cpp | 162 ---- src/fifo.h | 72 -- src/file.cpp | 305 ------- src/file.h | 106 --- src/filter.cpp | 113 --- src/filter.h | 83 -- src/fmt.h | 92 --- src/formatter.cpp | 547 ------------ src/formatter.h | 303 ------- src/formula.cpp | 14 - src/formula.h | 430 ---------- src/hash.cpp | 69 -- src/hash.h | 1306 ----------------------------- src/heap.cpp | 10 - src/heap.h | 612 -------------- src/hex.cpp | 69 -- src/hex.h | 57 -- src/httpget.cpp | 126 --- src/httpget.h | 66 -- src/itoserver.cpp | 242 ------ src/itoserver.h | 141 ---- src/lexer.cpp | 40 - src/lexer.h | 58 -- src/linkmessage.cpp | 50 -- src/linkmessage.h | 48 -- src/list.cpp | 9 - src/list.h | 1014 ----------------------- src/logger.cpp | 209 ----- src/logger.h | 125 --- src/lzma.cpp | 248 ------ src/lzma.h | 59 -- src/md5.cpp | 246 ------ src/md5.h | 54 -- src/membuf.cpp | 177 ---- src/membuf.h | 68 -- src/minicron.cpp | 477 ----------- src/minicron.h | 336 -------- src/minimacro.cpp | 187 ----- src/minimacro.h | 130 --- src/multiserver.cpp | 55 -- src/multiserver.h | 57 -- src/mutex.cpp | 34 - src/mutex.h | 68 -- src/mutexlocker.cpp | 24 - src/mutexlocker.h | 21 - src/myriad.cpp | 663 --------------- src/myriad.h | 222 ----- src/myriadfs.cpp | 703 ---------------- src/myriadfs.h | 203 ----- src/myriadstream.cpp | 309 ------- src/myriadstream.h | 61 -- src/newline.cpp | 68 -- src/newline.h | 41 - src/nullstream.cpp | 127 --- src/nullstream.h | 67 -- src/optparser.cpp | 492 ----------- src/optparser.h | 223 ----- src/parser.cpp | 311 ------- src/parser.h | 135 --- src/pearsonhash.cpp | 66 -- src/pearsonhash.h | 46 -- src/plugger.cpp | 11 - src/plugger.h | 289 ------- src/process.cpp | 441 ---------- src/process.h | 153 ---- src/protocol.cpp | 35 - src/protocol.h | 38 - src/protocolhttp.cpp | 353 -------- src/protocolhttp.h | 106 --- src/protocoltelnet.cpp | 620 -------------- src/protocoltelnet.h | 220 ----- src/queue.cpp | 8 - src/queue.h | 40 - src/queuebuf.cpp | 278 ------- src/queuebuf.h | 66 -- src/random.cpp | 0 src/random.h | 0 src/randombase.cpp | 0 src/randombase.h | 0 src/randombasic.cpp | 0 src/randombasic.h | 0 src/regex.cpp | 95 --- src/regex.h | 44 - src/regexengine.cpp | 5 - src/regexengine.h | 142 ---- src/ringbuffer.cpp | 9 - src/ringbuffer.h | 228 ----- src/server.cpp | 214 ----- src/server.h | 108 --- src/set.cpp | 11 - src/set.h | 44 - src/sha1.cpp | 194 ----- src/sha1.h | 53 -- src/sharedcore.cpp | 9 - src/sharedcore.h | 193 ----- src/signals.cpp | 10 - src/singleton.h | 68 -- src/sio.cpp | 35 - src/sio.h | 26 - src/sptr.cpp | 8 - src/sptr.h | 229 ----- src/stable/archival.cpp | 35 + src/stable/archival.h | 52 ++ src/stable/archive.cpp | 89 ++ src/stable/archive.h | 138 ++++ src/stable/archivebase.cpp | 197 +++++ src/stable/archivebase.h | 75 ++ src/stable/array.cpp | 10 + src/stable/array.h | 703 ++++++++++++++++ src/stable/atom.cpp | 8 + src/stable/atom.h | 147 ++++ src/stable/base64.cpp | 219 +++++ src/stable/base64.h | 59 ++ src/stable/buffer.cpp | 168 ++++ src/stable/buffer.h | 58 ++ src/stable/bzip2.cpp | 233 ++++++ src/stable/bzip2.h | 49 ++ src/stable/client.cpp | 320 +++++++ src/stable/client.h | 133 +++ src/stable/clientlink.cpp | 17 + src/stable/clientlink.h | 25 + src/stable/clientlinkfactory.cpp | 17 + src/stable/clientlinkfactory.h | 26 + src/stable/condition.cpp | 49 ++ src/stable/condition.h | 90 ++ src/stable/conduit.cpp | 233 ++++++ src/stable/conduit.h | 64 ++ src/stable/crypt.cpp | 47 ++ src/stable/crypt.h | 19 + src/stable/cryptohash.cpp | 38 + src/stable/cryptohash.h | 33 + src/stable/csvreader.cpp | 130 +++ src/stable/csvreader.h | 45 + src/stable/csvwriter.cpp | 81 ++ src/stable/csvwriter.h | 45 + src/stable/deflate.cpp | 253 ++++++ src/stable/deflate.h | 66 ++ src/stable/exceptionbase.cpp | 93 +++ src/stable/exceptionbase.h | 190 +++++ src/stable/extratypes.h | 30 + src/stable/file.cpp | 305 +++++++ src/stable/file.h | 106 +++ src/stable/filter.cpp | 113 +++ src/stable/filter.h | 83 ++ src/stable/fmt.h | 92 +++ src/stable/formatter.cpp | 547 ++++++++++++ src/stable/formatter.h | 303 +++++++ src/stable/formula.cpp | 14 + src/stable/formula.h | 430 ++++++++++ src/stable/hash.cpp | 69 ++ src/stable/hash.h | 1306 +++++++++++++++++++++++++++++ src/stable/heap.cpp | 10 + src/stable/heap.h | 612 ++++++++++++++ src/stable/hex.cpp | 69 ++ src/stable/hex.h | 57 ++ src/stable/list.cpp | 9 + src/stable/list.h | 1014 +++++++++++++++++++++++ src/stable/logger.cpp | 209 +++++ src/stable/logger.h | 125 +++ src/stable/lzma.cpp | 248 ++++++ src/stable/lzma.h | 59 ++ src/stable/md5.cpp | 246 ++++++ src/stable/md5.h | 54 ++ src/stable/membuf.cpp | 177 ++++ src/stable/membuf.h | 68 ++ src/stable/minicron.cpp | 477 +++++++++++ src/stable/minicron.h | 336 ++++++++ src/stable/multiserver.cpp | 55 ++ src/stable/multiserver.h | 57 ++ src/stable/mutex.cpp | 34 + src/stable/mutex.h | 68 ++ src/stable/mutexlocker.cpp | 24 + src/stable/mutexlocker.h | 21 + src/stable/nullstream.cpp | 127 +++ src/stable/nullstream.h | 67 ++ src/stable/optparser.cpp | 492 +++++++++++ src/stable/optparser.h | 223 +++++ src/stable/pearsonhash.cpp | 66 ++ src/stable/pearsonhash.h | 46 ++ src/stable/plugger.cpp | 11 + src/stable/plugger.h | 289 +++++++ src/stable/process.cpp | 441 ++++++++++ src/stable/process.h | 153 ++++ src/stable/protocol.cpp | 35 + src/stable/protocol.h | 38 + src/stable/protocolhttp.cpp | 353 ++++++++ src/stable/protocolhttp.h | 106 +++ src/stable/protocoltelnet.cpp | 620 ++++++++++++++ src/stable/protocoltelnet.h | 220 +++++ src/stable/queue.cpp | 8 + src/stable/queue.h | 40 + src/stable/queuebuf.cpp | 278 +++++++ src/stable/queuebuf.h | 66 ++ src/stable/ringbuffer.cpp | 9 + src/stable/ringbuffer.h | 228 +++++ src/stable/server.cpp | 214 +++++ src/stable/server.h | 108 +++ src/stable/sha1.cpp | 194 +++++ src/stable/sha1.h | 53 ++ src/stable/sharedcore.cpp | 9 + src/stable/sharedcore.h | 193 +++++ src/stable/signals.cpp | 10 + src/stable/singleton.h | 68 ++ src/stable/sio.cpp | 35 + src/stable/sio.h | 26 + src/stable/sptr.cpp | 8 + src/stable/sptr.h | 229 +++++ src/stable/stack.cpp | 8 + src/stable/stack.h | 85 ++ src/stable/staticmembuf.cpp | 135 +++ src/stable/staticmembuf.h | 65 ++ src/stable/stdstream.cpp | 117 +++ src/stable/stdstream.h | 50 ++ src/stable/stream.cpp | 53 ++ src/stable/stream.h | 205 +++++ src/stable/streamstack.cpp | 234 ++++++ src/stable/streamstack.h | 144 ++++ src/stable/strfilter.h | 124 +++ src/stable/string.cpp | 1470 +++++++++++++++++++++++++++++++++ src/stable/string.h | 1053 +++++++++++++++++++++++ src/stable/substream.cpp | 109 +++ src/stable/substream.h | 63 ++ src/stable/synchroatom.h | 63 ++ src/stable/synchrocounter.cpp | 8 + src/stable/synchrocounter.h | 49 ++ src/stable/synchroheap.cpp | 9 + src/stable/synchroheap.h | 151 ++++ src/stable/synchroqueue.h | 240 ++++++ src/stable/taf.h | 18 + src/stable/tafcomment.cpp | 37 + src/stable/tafcomment.h | 36 + src/stable/tafgroup.cpp | 224 +++++ src/stable/tafgroup.h | 71 ++ src/stable/tafnode.cpp | 25 + src/stable/tafnode.h | 44 + src/stable/tafproperty.cpp | 37 + src/stable/tafproperty.h | 36 + src/stable/tafreader.cpp | 252 ++++++ src/stable/tafreader.h | 49 ++ src/stable/tafwriter.cpp | 114 +++ src/stable/tafwriter.h | 45 + src/stable/tcpserversocket.cpp | 249 ++++++ src/stable/tcpserversocket.h | 64 ++ src/stable/tcpsocket.cpp | 478 +++++++++++ src/stable/tcpsocket.h | 126 +++ src/stable/thread.cpp | 55 ++ src/stable/thread.h | 107 +++ src/stable/trace.cpp | 67 ++ src/stable/trace.h | 187 +++++ src/stable/unitsuite.cpp | 255 ++++++ src/stable/unitsuite.h | 139 ++++ src/stable/util.cpp | 65 ++ src/stable/util.h | 187 +++++ src/stable/variant.cpp | 99 +++ src/stable/variant.h | 236 ++++++ src/stack.cpp | 8 - src/stack.h | 85 -- src/staticmembuf.cpp | 135 --- src/staticmembuf.h | 65 -- src/stdstream.cpp | 117 --- src/stdstream.h | 50 -- src/stream.cpp | 53 -- src/stream.h | 205 ----- src/streamstack.cpp | 234 ------ src/streamstack.h | 144 ---- src/strfilter.h | 124 --- src/string.cpp | 1470 --------------------------------- src/string.h | 1053 ----------------------- src/substream.cpp | 109 --- src/substream.h | 63 -- src/synchroatom.h | 63 -- src/synchrocounter.cpp | 8 - src/synchrocounter.h | 49 -- src/synchroheap.cpp | 9 - src/synchroheap.h | 151 ---- src/synchroqueue.h | 240 ------ src/taf.h | 18 - src/tafcomment.cpp | 37 - src/tafcomment.h | 36 - src/tafgroup.cpp | 224 ----- src/tafgroup.h | 71 -- src/tafnode.cpp | 25 - src/tafnode.h | 44 - src/tafproperty.cpp | 37 - src/tafproperty.h | 36 - src/tafreader.cpp | 252 ------ src/tafreader.h | 49 -- src/tafwriter.cpp | 114 --- src/tafwriter.h | 45 - src/tcpserversocket.cpp | 249 ------ src/tcpserversocket.h | 64 -- src/tcpsocket.cpp | 478 ----------- src/tcpsocket.h | 126 --- src/thread.cpp | 55 -- src/thread.h | 107 --- src/trace.cpp | 67 -- src/trace.h | 187 ----- src/udpsocket.cpp | 240 ------ src/udpsocket.h | 84 -- src/unitsuite.cpp | 255 ------ src/unitsuite.h | 139 ---- src/unstable/bitstring.cpp | 479 +++++++++++ src/unstable/bitstring.h | 224 +++++ src/unstable/fifo.cpp | 162 ++++ src/unstable/fifo.h | 72 ++ src/unstable/itoserver.cpp | 242 ++++++ src/unstable/itoserver.h | 141 ++++ src/unstable/minimacro.cpp | 187 +++++ src/unstable/minimacro.h | 130 +++ src/unstable/myriad.cpp | 663 +++++++++++++++ src/unstable/myriad.h | 222 +++++ src/unstable/myriadfs.cpp | 703 ++++++++++++++++ src/unstable/myriadfs.h | 203 +++++ src/unstable/myriadstream.cpp | 309 +++++++ src/unstable/myriadstream.h | 61 ++ src/unstable/newline.cpp | 68 ++ src/unstable/newline.h | 41 + src/unstable/udpsocket.cpp | 240 ++++++ src/unstable/udpsocket.h | 84 ++ src/unstable/url.cpp | 285 +++++++ src/unstable/url.h | 85 ++ src/unstable/urn.cpp | 8 + src/unstable/urn.h | 8 + src/unstable/utfstring.cpp | 539 ++++++++++++ src/unstable/utfstring.h | 174 ++++ src/unstable/uuid.cpp | 117 +++ src/unstable/uuid.h | 56 ++ src/url.cpp | 285 ------- src/url.h | 85 -- src/urn.cpp | 8 - src/urn.h | 8 - src/utfstring.cpp | 539 ------------ src/utfstring.h | 174 ---- src/util.cpp | 65 -- src/util.h | 187 ----- src/uuid.cpp | 117 --- src/uuid.h | 56 -- src/variant.cpp | 99 --- src/variant.h | 236 ------ src/xmlreader.cpp | 173 ---- src/xmlreader.h | 64 -- 424 files changed, 32702 insertions(+), 32855 deletions(-) delete mode 100644 src/archival.cpp delete mode 100644 src/archival.h delete mode 100644 src/archive.cpp delete mode 100644 src/archive.h delete mode 100644 src/archivebase.cpp delete mode 100644 src/archivebase.h delete mode 100644 src/array.cpp delete mode 100644 src/array.h delete mode 100644 src/atom.cpp delete mode 100644 src/atom.h delete mode 100644 src/base64.cpp delete mode 100644 src/base64.h delete mode 100644 src/bitstring.cpp delete mode 100644 src/bitstring.h delete mode 100644 src/buffer.cpp delete mode 100644 src/buffer.h delete mode 100644 src/bzip2.cpp delete mode 100644 src/bzip2.h delete mode 100644 src/cache.cpp delete mode 100644 src/cache.h delete mode 100644 src/cachecalc.cpp delete mode 100644 src/cachecalc.h delete mode 100644 src/cachestore.cpp delete mode 100644 src/cachestore.h delete mode 100644 src/cachestorefiles.cpp delete mode 100644 src/cachestorefiles.h delete mode 100644 src/cachestoremyriad.cpp delete mode 100644 src/cachestoremyriad.h delete mode 100644 src/client.cpp delete mode 100644 src/client.h delete mode 100644 src/clientlink.cpp delete mode 100644 src/clientlink.h delete mode 100644 src/clientlinkfactory.cpp delete mode 100644 src/clientlinkfactory.h delete mode 100644 src/condition.cpp delete mode 100644 src/condition.h delete mode 100644 src/conduit.cpp delete mode 100644 src/conduit.h delete mode 100644 src/crypt.cpp delete mode 100644 src/crypt.h delete mode 100644 src/cryptohash.cpp delete mode 100644 src/cryptohash.h delete mode 100644 src/csvreader.cpp delete mode 100644 src/csvreader.h delete mode 100644 src/csvwriter.cpp delete mode 100644 src/csvwriter.h delete mode 100644 src/deflate.cpp delete mode 100644 src/deflate.h delete mode 100644 src/exceptionbase.cpp delete mode 100644 src/exceptionbase.h create mode 100644 src/experimental/cache.cpp create mode 100644 src/experimental/cache.h create mode 100644 src/experimental/cachecalc.cpp create mode 100644 src/experimental/cachecalc.h create mode 100644 src/experimental/cachestore.cpp create mode 100644 src/experimental/cachestore.h create mode 100644 src/experimental/cachestorefiles.cpp create mode 100644 src/experimental/cachestorefiles.h create mode 100644 src/experimental/cachestoremyriad.cpp create mode 100644 src/experimental/cachestoremyriad.h create mode 100644 src/experimental/fastcgi.cpp create mode 100644 src/experimental/fastcgi.h create mode 100644 src/experimental/httpget.cpp create mode 100644 src/experimental/httpget.h create mode 100644 src/experimental/lexer.cpp create mode 100644 src/experimental/lexer.h create mode 100644 src/experimental/parser.cpp create mode 100644 src/experimental/parser.h create mode 100644 src/experimental/random.cpp create mode 100644 src/experimental/random.h create mode 100644 src/experimental/randombase.cpp create mode 100644 src/experimental/randombase.h create mode 100644 src/experimental/randombasic.cpp create mode 100644 src/experimental/randombasic.h create mode 100644 src/experimental/regex.cpp create mode 100644 src/experimental/regex.h create mode 100644 src/experimental/regexengine.cpp create mode 100644 src/experimental/regexengine.h create mode 100644 src/experimental/xmlreader.cpp create mode 100644 src/experimental/xmlreader.h delete mode 100644 src/extratypes.h delete mode 100644 src/fastcgi.cpp delete mode 100644 src/fastcgi.h delete mode 100644 src/fifo.cpp delete mode 100644 src/fifo.h delete mode 100644 src/file.cpp delete mode 100644 src/file.h delete mode 100644 src/filter.cpp delete mode 100644 src/filter.h delete mode 100644 src/fmt.h delete mode 100644 src/formatter.cpp delete mode 100644 src/formatter.h delete mode 100644 src/formula.cpp delete mode 100644 src/formula.h delete mode 100644 src/hash.cpp delete mode 100644 src/hash.h delete mode 100644 src/heap.cpp delete mode 100644 src/heap.h delete mode 100644 src/hex.cpp delete mode 100644 src/hex.h delete mode 100644 src/httpget.cpp delete mode 100644 src/httpget.h delete mode 100644 src/itoserver.cpp delete mode 100644 src/itoserver.h delete mode 100644 src/lexer.cpp delete mode 100644 src/lexer.h delete mode 100644 src/linkmessage.cpp delete mode 100644 src/linkmessage.h delete mode 100644 src/list.cpp delete mode 100644 src/list.h delete mode 100644 src/logger.cpp delete mode 100644 src/logger.h delete mode 100644 src/lzma.cpp delete mode 100644 src/lzma.h delete mode 100644 src/md5.cpp delete mode 100644 src/md5.h delete mode 100644 src/membuf.cpp delete mode 100644 src/membuf.h delete mode 100644 src/minicron.cpp delete mode 100644 src/minicron.h delete mode 100644 src/minimacro.cpp delete mode 100644 src/minimacro.h delete mode 100644 src/multiserver.cpp delete mode 100644 src/multiserver.h delete mode 100644 src/mutex.cpp delete mode 100644 src/mutex.h delete mode 100644 src/mutexlocker.cpp delete mode 100644 src/mutexlocker.h delete mode 100644 src/myriad.cpp delete mode 100644 src/myriad.h delete mode 100644 src/myriadfs.cpp delete mode 100644 src/myriadfs.h delete mode 100644 src/myriadstream.cpp delete mode 100644 src/myriadstream.h delete mode 100644 src/newline.cpp delete mode 100644 src/newline.h delete mode 100644 src/nullstream.cpp delete mode 100644 src/nullstream.h delete mode 100644 src/optparser.cpp delete mode 100644 src/optparser.h delete mode 100644 src/parser.cpp delete mode 100644 src/parser.h delete mode 100644 src/pearsonhash.cpp delete mode 100644 src/pearsonhash.h delete mode 100644 src/plugger.cpp delete mode 100644 src/plugger.h delete mode 100644 src/process.cpp delete mode 100644 src/process.h delete mode 100644 src/protocol.cpp delete mode 100644 src/protocol.h delete mode 100644 src/protocolhttp.cpp delete mode 100644 src/protocolhttp.h delete mode 100644 src/protocoltelnet.cpp delete mode 100644 src/protocoltelnet.h delete mode 100644 src/queue.cpp delete mode 100644 src/queue.h delete mode 100644 src/queuebuf.cpp delete mode 100644 src/queuebuf.h delete mode 100644 src/random.cpp delete mode 100644 src/random.h delete mode 100644 src/randombase.cpp delete mode 100644 src/randombase.h delete mode 100644 src/randombasic.cpp delete mode 100644 src/randombasic.h delete mode 100644 src/regex.cpp delete mode 100644 src/regex.h delete mode 100644 src/regexengine.cpp delete mode 100644 src/regexengine.h delete mode 100644 src/ringbuffer.cpp delete mode 100644 src/ringbuffer.h delete mode 100644 src/server.cpp delete mode 100644 src/server.h delete mode 100644 src/set.cpp delete mode 100644 src/set.h delete mode 100644 src/sha1.cpp delete mode 100644 src/sha1.h delete mode 100644 src/sharedcore.cpp delete mode 100644 src/sharedcore.h delete mode 100644 src/signals.cpp delete mode 100644 src/singleton.h delete mode 100644 src/sio.cpp delete mode 100644 src/sio.h delete mode 100644 src/sptr.cpp delete mode 100644 src/sptr.h create mode 100644 src/stable/archival.cpp create mode 100644 src/stable/archival.h create mode 100644 src/stable/archive.cpp create mode 100644 src/stable/archive.h create mode 100644 src/stable/archivebase.cpp create mode 100644 src/stable/archivebase.h create mode 100644 src/stable/array.cpp create mode 100644 src/stable/array.h create mode 100644 src/stable/atom.cpp create mode 100644 src/stable/atom.h create mode 100644 src/stable/base64.cpp create mode 100644 src/stable/base64.h create mode 100644 src/stable/buffer.cpp create mode 100644 src/stable/buffer.h create mode 100644 src/stable/bzip2.cpp create mode 100644 src/stable/bzip2.h create mode 100644 src/stable/client.cpp create mode 100644 src/stable/client.h create mode 100644 src/stable/clientlink.cpp create mode 100644 src/stable/clientlink.h create mode 100644 src/stable/clientlinkfactory.cpp create mode 100644 src/stable/clientlinkfactory.h create mode 100644 src/stable/condition.cpp create mode 100644 src/stable/condition.h create mode 100644 src/stable/conduit.cpp create mode 100644 src/stable/conduit.h create mode 100644 src/stable/crypt.cpp create mode 100644 src/stable/crypt.h create mode 100644 src/stable/cryptohash.cpp create mode 100644 src/stable/cryptohash.h create mode 100644 src/stable/csvreader.cpp create mode 100644 src/stable/csvreader.h create mode 100644 src/stable/csvwriter.cpp create mode 100644 src/stable/csvwriter.h create mode 100644 src/stable/deflate.cpp create mode 100644 src/stable/deflate.h create mode 100644 src/stable/exceptionbase.cpp create mode 100644 src/stable/exceptionbase.h create mode 100644 src/stable/extratypes.h create mode 100644 src/stable/file.cpp create mode 100644 src/stable/file.h create mode 100644 src/stable/filter.cpp create mode 100644 src/stable/filter.h create mode 100644 src/stable/fmt.h create mode 100644 src/stable/formatter.cpp create mode 100644 src/stable/formatter.h create mode 100644 src/stable/formula.cpp create mode 100644 src/stable/formula.h create mode 100644 src/stable/hash.cpp create mode 100644 src/stable/hash.h create mode 100644 src/stable/heap.cpp create mode 100644 src/stable/heap.h create mode 100644 src/stable/hex.cpp create mode 100644 src/stable/hex.h create mode 100644 src/stable/list.cpp create mode 100644 src/stable/list.h create mode 100644 src/stable/logger.cpp create mode 100644 src/stable/logger.h create mode 100644 src/stable/lzma.cpp create mode 100644 src/stable/lzma.h create mode 100644 src/stable/md5.cpp create mode 100644 src/stable/md5.h create mode 100644 src/stable/membuf.cpp create mode 100644 src/stable/membuf.h create mode 100644 src/stable/minicron.cpp create mode 100644 src/stable/minicron.h create mode 100644 src/stable/multiserver.cpp create mode 100644 src/stable/multiserver.h create mode 100644 src/stable/mutex.cpp create mode 100644 src/stable/mutex.h create mode 100644 src/stable/mutexlocker.cpp create mode 100644 src/stable/mutexlocker.h create mode 100644 src/stable/nullstream.cpp create mode 100644 src/stable/nullstream.h create mode 100644 src/stable/optparser.cpp create mode 100644 src/stable/optparser.h create mode 100644 src/stable/pearsonhash.cpp create mode 100644 src/stable/pearsonhash.h create mode 100644 src/stable/plugger.cpp create mode 100644 src/stable/plugger.h create mode 100644 src/stable/process.cpp create mode 100644 src/stable/process.h create mode 100644 src/stable/protocol.cpp create mode 100644 src/stable/protocol.h create mode 100644 src/stable/protocolhttp.cpp create mode 100644 src/stable/protocolhttp.h create mode 100644 src/stable/protocoltelnet.cpp create mode 100644 src/stable/protocoltelnet.h create mode 100644 src/stable/queue.cpp create mode 100644 src/stable/queue.h create mode 100644 src/stable/queuebuf.cpp create mode 100644 src/stable/queuebuf.h create mode 100644 src/stable/ringbuffer.cpp create mode 100644 src/stable/ringbuffer.h create mode 100644 src/stable/server.cpp create mode 100644 src/stable/server.h create mode 100644 src/stable/sha1.cpp create mode 100644 src/stable/sha1.h create mode 100644 src/stable/sharedcore.cpp create mode 100644 src/stable/sharedcore.h create mode 100644 src/stable/signals.cpp create mode 100644 src/stable/singleton.h create mode 100644 src/stable/sio.cpp create mode 100644 src/stable/sio.h create mode 100644 src/stable/sptr.cpp create mode 100644 src/stable/sptr.h create mode 100644 src/stable/stack.cpp create mode 100644 src/stable/stack.h create mode 100644 src/stable/staticmembuf.cpp create mode 100644 src/stable/staticmembuf.h create mode 100644 src/stable/stdstream.cpp create mode 100644 src/stable/stdstream.h create mode 100644 src/stable/stream.cpp create mode 100644 src/stable/stream.h create mode 100644 src/stable/streamstack.cpp create mode 100644 src/stable/streamstack.h create mode 100644 src/stable/strfilter.h create mode 100644 src/stable/string.cpp create mode 100644 src/stable/string.h create mode 100644 src/stable/substream.cpp create mode 100644 src/stable/substream.h create mode 100644 src/stable/synchroatom.h create mode 100644 src/stable/synchrocounter.cpp create mode 100644 src/stable/synchrocounter.h create mode 100644 src/stable/synchroheap.cpp create mode 100644 src/stable/synchroheap.h create mode 100644 src/stable/synchroqueue.h create mode 100644 src/stable/taf.h create mode 100644 src/stable/tafcomment.cpp create mode 100644 src/stable/tafcomment.h create mode 100644 src/stable/tafgroup.cpp create mode 100644 src/stable/tafgroup.h create mode 100644 src/stable/tafnode.cpp create mode 100644 src/stable/tafnode.h create mode 100644 src/stable/tafproperty.cpp create mode 100644 src/stable/tafproperty.h create mode 100644 src/stable/tafreader.cpp create mode 100644 src/stable/tafreader.h create mode 100644 src/stable/tafwriter.cpp create mode 100644 src/stable/tafwriter.h create mode 100644 src/stable/tcpserversocket.cpp create mode 100644 src/stable/tcpserversocket.h create mode 100644 src/stable/tcpsocket.cpp create mode 100644 src/stable/tcpsocket.h create mode 100644 src/stable/thread.cpp create mode 100644 src/stable/thread.h create mode 100644 src/stable/trace.cpp create mode 100644 src/stable/trace.h create mode 100644 src/stable/unitsuite.cpp create mode 100644 src/stable/unitsuite.h create mode 100644 src/stable/util.cpp create mode 100644 src/stable/util.h create mode 100644 src/stable/variant.cpp create mode 100644 src/stable/variant.h delete mode 100644 src/stack.cpp delete mode 100644 src/stack.h delete mode 100644 src/staticmembuf.cpp delete mode 100644 src/staticmembuf.h delete mode 100644 src/stdstream.cpp delete mode 100644 src/stdstream.h delete mode 100644 src/stream.cpp delete mode 100644 src/stream.h delete mode 100644 src/streamstack.cpp delete mode 100644 src/streamstack.h delete mode 100644 src/strfilter.h delete mode 100644 src/string.cpp delete mode 100644 src/string.h delete mode 100644 src/substream.cpp delete mode 100644 src/substream.h delete mode 100644 src/synchroatom.h delete mode 100644 src/synchrocounter.cpp delete mode 100644 src/synchrocounter.h delete mode 100644 src/synchroheap.cpp delete mode 100644 src/synchroheap.h delete mode 100644 src/synchroqueue.h delete mode 100644 src/taf.h delete mode 100644 src/tafcomment.cpp delete mode 100644 src/tafcomment.h delete mode 100644 src/tafgroup.cpp delete mode 100644 src/tafgroup.h delete mode 100644 src/tafnode.cpp delete mode 100644 src/tafnode.h delete mode 100644 src/tafproperty.cpp delete mode 100644 src/tafproperty.h delete mode 100644 src/tafreader.cpp delete mode 100644 src/tafreader.h delete mode 100644 src/tafwriter.cpp delete mode 100644 src/tafwriter.h delete mode 100644 src/tcpserversocket.cpp delete mode 100644 src/tcpserversocket.h delete mode 100644 src/tcpsocket.cpp delete mode 100644 src/tcpsocket.h delete mode 100644 src/thread.cpp delete mode 100644 src/thread.h delete mode 100644 src/trace.cpp delete mode 100644 src/trace.h delete mode 100644 src/udpsocket.cpp delete mode 100644 src/udpsocket.h delete mode 100644 src/unitsuite.cpp delete mode 100644 src/unitsuite.h create mode 100644 src/unstable/bitstring.cpp create mode 100644 src/unstable/bitstring.h create mode 100644 src/unstable/fifo.cpp create mode 100644 src/unstable/fifo.h create mode 100644 src/unstable/itoserver.cpp create mode 100644 src/unstable/itoserver.h create mode 100644 src/unstable/minimacro.cpp create mode 100644 src/unstable/minimacro.h create mode 100644 src/unstable/myriad.cpp create mode 100644 src/unstable/myriad.h create mode 100644 src/unstable/myriadfs.cpp create mode 100644 src/unstable/myriadfs.h create mode 100644 src/unstable/myriadstream.cpp create mode 100644 src/unstable/myriadstream.h create mode 100644 src/unstable/newline.cpp create mode 100644 src/unstable/newline.h create mode 100644 src/unstable/udpsocket.cpp create mode 100644 src/unstable/udpsocket.h create mode 100644 src/unstable/url.cpp create mode 100644 src/unstable/url.h create mode 100644 src/unstable/urn.cpp create mode 100644 src/unstable/urn.h create mode 100644 src/unstable/utfstring.cpp create mode 100644 src/unstable/utfstring.h create mode 100644 src/unstable/uuid.cpp create mode 100644 src/unstable/uuid.h delete mode 100644 src/url.cpp delete mode 100644 src/url.h delete mode 100644 src/urn.cpp delete mode 100644 src/urn.h delete mode 100644 src/utfstring.cpp delete mode 100644 src/utfstring.h delete mode 100644 src/util.cpp delete mode 100644 src/util.h delete mode 100644 src/uuid.cpp delete mode 100644 src/uuid.h delete mode 100644 src/variant.cpp delete mode 100644 src/variant.h delete mode 100644 src/xmlreader.cpp delete mode 100644 src/xmlreader.h (limited to 'src') diff --git a/src/archival.cpp b/src/archival.cpp deleted file mode 100644 index 687e8a3..0000000 --- a/src/archival.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/archival.h" - -Bu::Archival::Archival() -{ -} - -Bu::Archival::~Archival() -{ -} - -Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, const Bu::Archival &p) -{ - const_cast(p).archive( s ); - return s; -} - -Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, Bu::Archival &p) -{ - p.archive( s ); - return s; -} - -Bu::ArchiveBase &Bu::operator>>(Bu::ArchiveBase &s, Bu::Archival &p) -{ - p.archive( s ); - return s; -} - diff --git a/src/archival.h b/src/archival.h deleted file mode 100644 index 946167a..0000000 --- a/src/archival.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARCHIVAL_H -#define BU_ARCHIVAL_H - -#include "bu/archivebase.h" - -namespace Bu -{ - /** - * The base class for any class you want to archive. Simply include this as - * a base class, implement the purely virtual archive function and you've - * got an easily archiveable class. - * - * Archival: "of or pertaining to archives or valuable records; contained - * in or comprising such archives or records." - */ - class Archival - { - public: - /** - * Does nothing, here for completeness. - */ - Archival(); - - /** - * Here to ensure the deconstructor is virtual. - */ - virtual ~Archival(); - - /** - * This is the main workhorse of the archive system, just override and - * you've got a archiveable class. A reference to the Archive - * used is passed in as your only parameter, query it to discover if - * you are loading or saving. - * @param ar A reference to the Archive object to use. - */ - virtual void archive( class ArchiveBase &ar )=0; - }; - - ArchiveBase &operator<<(ArchiveBase &, const class Bu::Archival &); - ArchiveBase &operator<<(ArchiveBase &, class Bu::Archival &); - ArchiveBase &operator>>(ArchiveBase &, class Bu::Archival &); - -} - -#endif diff --git a/src/archive.cpp b/src/archive.cpp deleted file mode 100644 index d300a87..0000000 --- a/src/archive.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/archive.h" -#include "bu/stream.h" -#include "bu/archival.h" - -#include "bu/sio.h" - -Bu::Archive::Archive( Stream &rStream, bool bLoading ) : - bLoading( bLoading ), - rStream( rStream ), - nNextID( 1 ) -{ -} - -Bu::Archive::~Archive() -{ -} - -void Bu::Archive::write( const void *pData, size_t nSize ) -{ - if( nSize == 0 || pData == NULL ) - return; - - rStream.write( (const char *)pData, nSize ); -} - -void Bu::Archive::read( void *pData, size_t nSize ) -{ - if( nSize == 0 || pData == NULL ) - return; - - if( rStream.read( (char *)pData, nSize ) < nSize ) - throw Bu::ExceptionBase("Insufficient data to unarchive object."); -} - -void Bu::Archive::close() -{ - rStream.close(); -} - -bool Bu::Archive::isLoading() -{ - return bLoading; -} - -uint32_t Bu::Archive::getID( const void *ptr ) -{ - if( hPtrID.has( (ptrdiff_t)ptr ) ) - return hPtrID.get( (ptrdiff_t)ptr ); - hPtrID.insert( (ptrdiff_t)ptr, nNextID ); - return nNextID++; -} - -void Bu::Archive::assocPtrID( void **ptr, uint32_t id ) -{ - if( hPtrID.has( id ) ) - { - *ptr = (void *)hPtrID.get( id ); - return; - } - - if( !hPtrDest.has( id ) ) - hPtrDest.insert( id, List() ); - - hPtrDest[id].getValue().append( ptr ); -} - -void Bu::Archive::readID( const void *ptr, uint32_t id ) -{ - hPtrID.insert( id, (ptrdiff_t)ptr ); - - if( hPtrDest.has( id ) ) - { - Bu::List &l = hPtrDest.get( id ); - for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) - { - *(*i) = (void *)ptr; - } - - hPtrDest.erase( id ); - } -} - diff --git a/src/archive.h b/src/archive.h deleted file mode 100644 index 61474a4..0000000 --- a/src/archive.h +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARCHIVE_H -#define BU_ARCHIVE_H - -#include -#include "bu/archivebase.h" -#include "bu/hash.h" -#include "bu/util.h" -#include "bu/variant.h" - -namespace Bu -{ - class Archival; - class Stream; - - /** - * Provides a framework for serialization of objects and primitives. The - * archive will handle any basic primitive, a few special types, like char * - * strings, as well as STL classes and anything that inherits from the - * Archival class. Each Archive operates on a Stream, so you can send the - * data using an Archive almost anywhere. - * - * In order to use an Archive to store something to a file, try something - * like: - *@code - * File sOut("output", "wb"); // This is a stream subclass - * Archive ar( sOut, Archive::save ); - * ar << myClass; - @endcode - * In this example myClass is any class that inherits from Archival. When - * the storage operator is called, the Archival::archive() function in the - * myClass object is called with a reference to the Archive. This can be - * handled in one of two ways: - *@code - * void MyClass::archive( Archive &ar ) - * { - * ar && sName && nAge && sJob; - * } - @endcode - * Here we don't worry about weather we're loading or saving by using the - * smart && operator. This allows us to write very consistent, very simple - * archive functions that really do a lot of work. If we wanted to do - * something different in the case of loading or saving we would do: - *@code - * void MyClass::archive( Archive &ar ) - * { - * if( ar.isLoading() ) - * { - * ar >> sName >> nAge >> sJob; - * } else - * { - * ar << sName << nAge << sJob; - * } - * } - @endcode - * Archive currently does not provide facility to make fully portable - * archives. For example, it will not convert between endianness for you, - * nor will it take into account differences between primitive sizes on - * different platforms. This, at the moment, is up to the user to ensure. - * One way of dealing with the latter problem is to make sure and use - * explicit primitive types from the stdint.h header, i.e. int32_t. - */ - class Archive : public ArchiveBase - { - private: - bool bLoading; - public: - bool isLoading(); - - enum - { - load = true, - save = false - }; - - Archive( Stream &rStream, bool bLoading ); - virtual ~Archive(); - virtual void close(); - - virtual void write( const void *pData, size_t iSize ); - virtual void read( void *pData, size_t iSize ); - - /** - * For storage, get an ID for the pointer to the object you're going to - * write. - */ - uint32_t getID( const void *ptr ); - - /** - * For loading. Assosiates an empty pointer with an id. When you wind - * up loading an id reference to a pointer for an object that may or - * may not have loaded yet, call this with the id, if it has been loaded - * already, you'll immediately get a pointer, if not, it will write one - * for you when the time comes. - */ - void assocPtrID( void **ptr, uint32_t id ); - - /** - * For loading. Call this when you load an object that other things may - * have pointers to. It will assosiate every pointer that's been - * registered with assocPtrID to the pointer passed in, and id passed - * in. It will also set things up so future calls to assocPtrID will - * automatically succeed immediately. - */ - void readID( const void *ptr, uint32_t id ); - - template - void setProp( const Bu::String &sId, const t &val ) - { - if( !hProps.has( sId ) ) - { - hProps.insert( sId, Variant() ); - } - hProps.get( sId ) = val; - } - - template - t getProp( const Bu::String &sId ) - { - return hProps.get( sId ); - } - - private: - Stream &rStream; - uint32_t nNextID; - Hash hPtrID; - Hash > hPtrDest; - Hash hProps; - }; -} - -#endif diff --git a/src/archivebase.cpp b/src/archivebase.cpp deleted file mode 100644 index d00b1a5..0000000 --- a/src/archivebase.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/archivebase.h" - -Bu::ArchiveBase::ArchiveBase() -{ -} - -Bu::ArchiveBase::~ArchiveBase() -{ -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, bool p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed short p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned short p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed int p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned int p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, float p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, double p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, long double p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, bool &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed short &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned short &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed int &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned int &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, float &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, double &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, long double &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - diff --git a/src/archivebase.h b/src/archivebase.h deleted file mode 100644 index 4745d91..0000000 --- a/src/archivebase.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARCHIVE_BASE_H -#define BU_ARCHIVE_BASE_H - -#include -#include - -namespace Bu -{ - class ArchiveBase - { - public: - ArchiveBase(); - virtual ~ArchiveBase(); - - virtual void close()=0; - virtual void write( const void *pData, size_t iLength )=0; - virtual void read( void *pData, size_t iLength )=0; - virtual bool isLoading()=0; - }; - - template ArchiveBase &operator&&( ArchiveBase &ar, T &dat ) - { - if( ar.isLoading() ) - { - return ar >> dat; - } - else - { - return ar << dat; - } - } - - ArchiveBase &operator<<( ArchiveBase &ar, bool p ); - ArchiveBase &operator<<( ArchiveBase &ar, char p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed char p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned char p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed short p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned short p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed int p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned int p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed long p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned long p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed long long p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned long long p ); - ArchiveBase &operator<<( ArchiveBase &ar, float p ); - ArchiveBase &operator<<( ArchiveBase &ar, double p ); - ArchiveBase &operator<<( ArchiveBase &ar, long double p ); - - ArchiveBase &operator>>( ArchiveBase &ar, bool &p ); - ArchiveBase &operator>>( ArchiveBase &ar, char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed short &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned short &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed int &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned int &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed long long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned long long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, float &p ); - ArchiveBase &operator>>( ArchiveBase &ar, double &p ); - ArchiveBase &operator>>( ArchiveBase &ar, long double &p ); - - -}; - -#endif diff --git a/src/array.cpp b/src/array.cpp deleted file mode 100644 index b776fed..0000000 --- a/src/array.cpp +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/array.h" - -namespace Bu { subExceptionDef( ArrayException ) } diff --git a/src/array.h b/src/array.h deleted file mode 100644 index fcd800e..0000000 --- a/src/array.h +++ /dev/null @@ -1,703 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARRAY_H -#define BU_ARRAY_H - -#include -#include "bu/exceptionbase.h" -#include "bu/archivebase.h" -#include "bu/sharedcore.h" - -namespace Bu -{ - subExceptionDecl( ArrayException ) - - template - class Array; - - /** @cond DEVEL */ - template - class ArrayCore - { - friend class Array; - friend class SharedCore< - Array, - ArrayCore - >; - private: - ArrayCore() : - pData( NULL ), - iSize( 0 ), - iCapacity( 0 ) - { } - - void setCapacity( int iNewLen ) - { - //clear(); - //iCapacity = iCapacity; - //pData = va.allocate( iCapacity ); - if( iNewLen <= iCapacity ) return; - value *pNewData = va.allocate( iNewLen ); - if( pData ) - { - for( int j = 0; j < iSize; j++ ) - { - va.construct( &pNewData[j], pData[j] ); - va.destroy( &pData[j] ); - } - va.deallocate( pData, iCapacity ); - } - pData = pNewData; - iCapacity = iNewLen; - } - - virtual ~ArrayCore() - { - clear(); - } - - void clear() - { - if( pData ) - { - for( int j = 0; j < iSize; j++ ) - { - va.destroy( &pData[j] ); - } - va.deallocate( pData, iCapacity ); - pData = NULL; - } - iSize = 0; - iCapacity = 0; - } - - void erase( int iPos ) - { - for( int j = iPos; j < iSize; j++ ) - { - va.destroy( &pData[j] ); - if( j == iSize-1 ) - { - iSize--; - return; - } - va.construct( &pData[j], pData[j+1] ); - } - } - - void swapErase( int iPos ) - { - if( iPos == iSize-1 ) - { - erase( iPos ); - return; - } - va.destroy( &pData[iPos] ); - va.construct( &pData[iPos], pData[iSize-1] ); - va.destroy( &pData[iSize-1] ); - iSize--; - } - - valuealloc va; - value *pData; - long iSize; - long iCapacity; - }; - /** @endcond */ - - /** - * Array type container, just like a normal array only flexible and keeps - * track of your memory for you. - * - *@param value (typename) The type of data to store in your list - *@param valuealloc (typename) Memory Allocator for your value type - *@param linkalloc (typename) Memory Allocator for the list links. - *@ingroup Containers - */ - template > - class Array : public SharedCore< - Array, - ArrayCore - > - { - private: - typedef class Array MyType; - typedef class ArrayCore Core; - - protected: - using SharedCore::core; - using SharedCore::_hardCopy; - using SharedCore::_resetCore; - using SharedCore::_allocateCore; - - public: - struct const_iterator; - struct iterator; - - Array() - { - } - - Array( const MyType &src ) : - SharedCore( src ) - { - } - - Array( long iSetCap ) - { - setCapacity( iSetCap ); - } - - ~Array() - { - } - - bool operator==( const MyType &src ) const - { - if( core == src.core ) - return true; - if( core->iSize != src.core->iSize ) - return false; - - for( int j = 0; j < core->iSize; j++ ) - { - if( core->pData[j] != src.core->pData[j] ) - return false; - } - return true; - } - - bool operator!=( const MyType &src ) const - { - return !(*this == src); - } - - /** - * Clear the array. - */ - void clear() - { - _resetCore(); - } - - MyType &append( const value &rVal ) - { - _hardCopy(); - if( core->iSize == core->iCapacity ) - { - core->setCapacity( core->iCapacity + inc ); - } - - core->va.construct( &core->pData[core->iSize++], rVal ); - - return *this; - } - - MyType &append( const MyType &rVal ) - { - _hardCopy(); - - if( core->iSize + rVal.core->iSize > core->iCapacity ) - { - core->setCapacity( core->iSize + rVal.core->iSize + inc ); - } - - for( int j = 0; j < rVal.core->iSize; j++ ) - { - core->va.construct( - &core->pData[core->iSize++], - rVal.core->pData[j] - ); - } - - return *this; - } - - //operator - value &operator[]( long iIndex ) - { - _hardCopy(); - if( iIndex < 0 || iIndex >= core->iSize ) - throw ArrayException( - "Index %d out of range 0:%d", iIndex, core->iSize ); - - return core->pData[iIndex]; - } - - const value &operator[]( long iIndex ) const - { - if( iIndex < 0 || iIndex >= core->iSize ) - throw ArrayException( - "Index %d out of range 0:%d", iIndex, core->iSize ); - - return core->pData[iIndex]; - } - - value &get( long iIndex ) - { - _hardCopy(); - if( iIndex < 0 || iIndex >= core->iSize ) - throw ArrayException( - "Index %d out of range 0:%d", iIndex, core->iSize ); - - return core->pData[iIndex]; - } - - const value &get( long iIndex ) const - { - if( iIndex < 0 || iIndex >= core->iSize ) - throw ArrayException( - "Index %d out of range 0:%d", iIndex, core->iSize ); - - return core->pData[iIndex]; - } - - value &first() - { - _hardCopy(); - return core->pData[0]; - } - - const value &first() const - { - return core->pData[0]; - } - - value &last() - { - _hardCopy(); - return core->pData[core->iSize-1]; - } - - const value &last() const - { - return core->pData[core->iSize-1]; - } - - /** - * Get the current size of the array. - *@returns The current size of the array. - */ - long getSize() const - { - return core->iSize; - } - - /** - * Get the capacity of the array. This number will grow as data is - * added, and is mainly for the curious, it doesn't really determine - * much for the end user. - *@returns The current capacity of the array. - */ - long getCapacity() const - { - return core->iCapacity; - } - - /** - * Change the capacity of the array, very useful if you know you'll be - * adding a large amount of already counted items to the array, makes - * the appending much faster afterwords. - *@param iNewLen The new capacity of the array. - *@todo Set this up so it can reduce the size of the array as well as - * make it bigger. - */ - void setCapacity( long iNewLen ) - { - _hardCopy(); - core->setCapacity( iNewLen ); - } - - typedef struct iterator - { - friend class Array; - private: - iterator( MyType &src, long iPos=0 ) : - src( src ), - iPos( iPos ) - { - if( this->iPos >= src.getSize() ) - this->iPos = -1; - } - - MyType &src; - long iPos; - - public: - iterator operator++( int ) - { - if( iPos < 0 ) - throw ArrayException( - "Cannot increment iterator past end of array."); - iPos++; - if( iPos >= src.getSize() ) - iPos = -1; - return *this; - } - - iterator operator++() - { - if( iPos >= 0 ) - iPos++; - if( iPos >= src.getSize() ) - iPos = -1; - return *this; - } - - iterator operator+( int iAmnt ) - { - if( iPos < 0 ) - throw ArrayException( - "Cannot increment iterator past end of array."); - iPos += iAmnt; - if( iPos >= src.getSize() ) - iPos = -1; - return *this; - } - - iterator operator--( int ) - { - if( iPos < 0 ) - throw ArrayException( - "Cannot increment iterator past end of array."); - iPos--; - if( iPos < 0 ) - iPos = -1; - return *this; - } - - iterator operator--() - { - if( iPos < src.getSize() ) - iPos--; - if( iPos <= 0 ) - iPos = -1; - return *this; - } - - iterator operator-( int iAmnt ) - { - if( iPos < src.getSize() ) - iPos -= iAmnt; - if( iPos <= 0 ) - iPos = -1; - return *this; - } - - bool operator==( const iterator &oth ) const - { - return iPos == oth.iPos; - } - - bool operator!=( const iterator &oth ) const - { - return iPos != oth.iPos; - } - - iterator operator=( const iterator &oth ) - { - if( &src != &oth.src ) - throw ArrayException( - "Cannot mix iterators from different array objects."); - iPos = oth.iPos; - } - - value &operator*() - { - if( iPos < 0 ) - throw ArrayException( - "Cannot dereference finished iterator."); - return src[iPos]; - } - - long getIndex() const - { - return iPos; - } - - operator bool() const - { - return iPos >= 0; - } - - bool isValid() const - { - return iPos >= 0; - } - } iterator; - - typedef struct const_iterator - { - friend class Array; - private: - const_iterator( const MyType &src, long iPos=0 ) : - src( src ), - iPos( iPos ) - { - if( this->iPos >= src.getSize() ) - this->iPos = -1; - } - - const MyType &src; - long iPos; - - public: - const_iterator( iterator &rSrc ) : - src( rSrc.src ), - iPos( rSrc.iPos ) - { - } - const_iterator operator++( int ) - { - if( iPos < 0 ) - throw ArrayException( - "Cannot increment iterator past end of array."); - iPos++; - if( iPos >= src.getSize() ) - iPos = -1; - return *this; - } - - const_iterator operator++() - { - if( iPos >= 0 ) - iPos++; - if( iPos >= src.getSize() ) - iPos = -1; - return *this; - } - - const_iterator operator--( int ) - { - if( iPos < 0 ) - throw ArrayException( - "Cannot increment iterator past end of array."); - iPos--; - if( iPos < 0 ) - iPos = -1; - return *this; - } - - const_iterator operator--() - { - if( iPos < src.getSize() ) - iPos--; - if( iPos <= 0 ) - iPos = -1; - return *this; - } - - bool operator==( const const_iterator &oth ) const - { - return iPos == oth.iPos; - } - - bool operator!=( const const_iterator &oth ) const - { - return iPos != oth.iPos; - } - - const_iterator operator=( const const_iterator &oth ) - { - if( &src != &oth.src ) - throw ArrayException( - "Cannot mix iterators from different array objects."); - iPos = oth.iPos; - } - - const value &operator*() const - { - if( iPos < 0 ) - throw ArrayException( - "Cannot dereference finished iterator."); - return src[iPos]; - } - - long getIndex() const - { - return iPos; - } - - operator bool() const - { - return iPos >= 0; - } - - bool isValid() const - { - return iPos >= 0; - } - } const_iterator; - - iterator begin() - { - return iterator( *this ); - } - - const_iterator begin() const - { - return const_iterator( *this ); - } - - iterator end() - { - return iterator( *this, -1 ); - } - - const_iterator end() const - { - return const_iterator( *this, -1 ); - } - - MyType &insert( iterator i, const value &rVal ) - { - if( i.iPos == -1 ) - { - append( rVal ); - return *this; - } - - _hardCopy(); - if( core->iSize == core->iCapacity ) - { - core->setCapacity( core->iCapacity + inc ); - } - core->iSize++; - - core->va.construct( - &core->pData[core->iSize-1], - core->pData[core->iSize-2] - ); - for( int iPos = core->iSize-2; iPos > i.iPos; iPos-- ) - { - core->va.destroy( &core->pData[iPos] ); - core->va.construct( &core->pData[iPos], core->pData[iPos-1] ); - } - core->va.destroy( &core->pData[i.iPos] ); - core->va.construct( &core->pData[i.iPos], rVal ); - - return *this; - } - - /** - * If order is important, use this. It will delete the suggested item - * and move the rest of the data up a spot. This is a time O(n) - * operation. If the order isn't important, check swapErase - */ - void erase( iterator i ) - { - _hardCopy(); - core->erase( i.iPos ); - } - - void erase( const value &v ) - { - _hardCopy(); - for( int j = 0; j < core->iSize; j++ ) - { - if( core->pData[j] == v ) - { - core->erase( j ); - return; - } - } - } - - void eraseLast() - { - _hardCopy(); - core->erase( core->iSize-1 ); - } - - void eraseFirst() - { - _hardCopy(); - core->erase( 0 ); - } - - /** - * In order to make swapErase faster, what it does is swap the given - * item in the array with the last item, then make the array shorter - * by one. It changes the order of the elements in the array, so it - * should be used carefully, but it is time O(1) instead of O(n) like - * erase. - */ - void swapErase( iterator i ) - { - _hardCopy(); - core->swapErase( i.iPos ); - } - - protected: - virtual Core *_copyCore( Core *src ) - { - Core *pRet = _allocateCore(); - pRet->setCapacity( src->iCapacity ); - pRet->iSize = src->iSize; - for( int j = 0; j < src->iSize; j++ ) - { - pRet->va.construct( &pRet->pData[j], src->pData[j] ); - } - return pRet; - } - - private: - }; - - class Formatter; - Formatter &operator<<( Formatter &rOut, char *sStr ); - Formatter &operator<<( Formatter &rOut, signed char c ); - template - Formatter &operator<<( Formatter &f, const Bu::Array &a ) - { - f << '['; - for( typename Bu::Array::const_iterator i = a.begin(); i; i++ ) - { - if( i != a.begin() ) - f << ", "; - f << *i; - } - f << ']'; - - return f; - } - - template - ArchiveBase &operator<<( ArchiveBase &ar, - const Array &h ) - { - ar << h.getSize(); - for( typename Array::const_iterator i = - h.begin(); i != h.end(); i++ ) - { - ar << (*i); - } - - return ar; - } - - template - ArchiveBase &operator>>(ArchiveBase &ar, Array &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - h.setCapacity( nSize ); - for( long j = 0; j < nSize; j++ ) - { - value v; - ar >> v; - h.append( v ); - } - return ar; - } - -} - -#endif diff --git a/src/atom.cpp b/src/atom.cpp deleted file mode 100644 index 3c77b90..0000000 --- a/src/atom.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/atom.h" diff --git a/src/atom.h b/src/atom.h deleted file mode 100644 index fd88f2d..0000000 --- a/src/atom.h +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ATOM_H -#define BU_ATOM_H - -#include -#include -#include "bu/config.h" -#include "bu/exceptionbase.h" - -namespace Bu -{ - /** - * - *@ingroup Containers - */ - template > - class Atom - { - private: - typedef struct Atom MyType; - - public: - Atom() : - pData( NULL ) - { - } - - Atom( const MyType &oth ) : - pData( NULL ) - { - if( oth.pData ) - set( *oth.pData ); - } - - Atom( const t &oth ) : - pData( NULL ) - { - set( oth ); - } - - virtual ~Atom() - { - clear(); - } - - bool has() const - { - return (pData != NULL); - } - - void set( const t &val ) - { - clear(); - pData = ta.allocate( 1 ); - ta.construct( pData, val ); - } - - t &get() - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return *pData; - } - - const t &get() const - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return *pData; - } - - void clear() - { - if( pData ) - { - ta.destroy( pData ); - ta.deallocate( pData, 1 ); - pData = NULL; - } - } - - operator const t &() const - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return *pData; - } - - operator t &() - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return *pData; - } - - MyType &operator =( const t &oth ) - { - set( oth ); - - return *this; - } - - MyType &operator =( const MyType &oth ) - { - if( oth.pData ) - set( *oth.pData ); - - return *this; - } - - bool operator ==( const MyType &oth ) - { - return (*pData) == (*oth.pData); - } - - bool operator ==( const t &oth ) - { - return (*pData) == oth; - } - - t *operator ->() - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return pData; - } - - t &operator *() - { - if( !pData ) - throw Bu::ExceptionBase("Not set"); - return *pData; - } - - private: - t *pData; - talloc ta; - }; -} - -#endif diff --git a/src/base64.cpp b/src/base64.cpp deleted file mode 100644 index 4d659f0..0000000 --- a/src/base64.cpp +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/base64.h" - -namespace Bu { subExceptionDef( Base64Exception ) } - -const char Bu::Base64::tblEnc[65] = { - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" -}; - -Bu::Base64::Base64( Bu::Stream &rNext, int iChunkSize ) : - Bu::Filter( rNext ), - iBPos( 0 ), - iBuf( 0 ), - iRPos( 0 ), - iChars( 0 ), - bEosIn( false ), - iTotalIn( 0 ), - iTotalOut( 0 ), - eMode( Nothing ), - iChunkSize( iChunkSize ), - iCurChunk( 0 ) -{ - start(); - - memset( tblDec, 0, 80 ); - for( int j = 0; j < 64; j++ ) - { - tblDec[tblEnc[j]-'+'] = j; - // printf("'%c' = %d\n", tblEnc[j], j ); - } - /* - for( int j = 0; j < 64; j++ ) - { - printf("'%c' = '%c' (%d = %d)\n", - tblEnc[j], tblEnc[tblDec[tblEnc[j]-'+']], - j, tblDec[tblEnc[j]-'+'] ); - }*/ - - // The following is used to compute the table size for the decoding table. - /* - char low='A', high='A'; - for( int j = 0; j < 64; j++ ) - { - if( tblEnc[j] < low ) - low = tblEnc[j]; - if( tblEnc[j] > high ) - high = tblEnc[j]; - } - - printf("'%c' - '%c' (%d - %d) (%d)\n", low, high, low, high, high-low ); - */ -} - -Bu::Base64::~Base64() -{ - stop(); -} - -void Bu::Base64::start() -{ - iCurChunk = 0; -} - -Bu::size Bu::Base64::stop() -{ - if( eMode == Encode ) - { - char outBuf[4]; - int iBUsed = 4-(3-iBPos); - if( iBPos == 0 ) - return iTotalOut; - for( int k = 0; k < 4; k++ ) - { - outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; - } - for( int k = iBUsed; k < 4; k++ ) - { - outBuf[k] = '='; - } - iCurChunk += 4; - if( iChunkSize && iCurChunk >= iChunkSize ) - { - iCurChunk = iCurChunk-iChunkSize; - iTotalOut += rNext.write( outBuf, 4-iCurChunk ); - iTotalOut += rNext.write("\r\n", 2 ); - iTotalOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk ); - } - else - iTotalOut += rNext.write( outBuf, 4 ); - return iTotalOut; - } - else - { - return iTotalIn; - } -} - -Bu::size Bu::Base64::read( void *pBuf, Bu::size nBytes ) -{ - if( eMode == Encode ) - throw Bu::Base64Exception("Cannot read from an output stream."); - eMode = Decode; - - if( bEosIn == true && iRPos == iChars ) - return 0; - Bu::size sIn = 0; - char buf[4]; - while( sIn < nBytes ) - { - for(; iRPos < iChars && sIn < nBytes; iRPos++, sIn++ ) - { - ((unsigned char *)pBuf)[sIn] = (iBuf>>(8*(2-iRPos)))&0xFF; - } - if( iRPos == iChars ) - { - if( bEosIn == true ) - return sIn; - else - iRPos = 0; - } - else if( sIn == nBytes ) - return sIn; - //if( rNext.read( buf, 4 ) == 0 ) - // return sIn; - for( int j = 0; j < 4; j++ ) - { - if( rNext.read( &buf[j], 1 ) == 0 ) - { - if( rNext.isEos() ) - { - if( iRPos == 0 ) - iRPos = iChars; - bEosIn = true; - if( j != 0 ) - { - throw Base64Exception( - "Premature end of stream detected while " - "decoding Base64 data." - ); - } - } - return sIn; - } - if( buf[j] == ' ' || buf[j] == '\t' || - buf[j] == '\n' || buf[j] == '\r' ) - { - j--; - } - } - iChars = 3; - iBuf = 0; - for( int j = 0; j < 4; j++ ) - { - if( buf[j] == '=' ) - { - iChars--; - bEosIn = true; - } - else - iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6); - //printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); - } - } - - return sIn; -} - -Bu::size Bu::Base64::write( const void *pBuf, Bu::size nBytes ) -{ - if( eMode == Decode ) - throw Bu::Base64Exception("Cannot write to an input stream."); - eMode = Encode; - - Bu::size sOut = 0; - char outBuf[4]; - for( Bu::size j = 0; j < nBytes; j++ ) - { - iBuf |= (((uint8_t *)pBuf)[j])<<((2-iBPos++)*8); - if( iBPos == 3 ) - { - for( int k = 0; k < 4; k++ ) - { - outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; - } - iCurChunk += 4; - if( iChunkSize && iCurChunk >= iChunkSize ) - { - iCurChunk = iCurChunk-iChunkSize; - sOut += rNext.write( outBuf, 4-iCurChunk ); - sOut += rNext.write("\r\n", 2 ); - sOut += rNext.write( outBuf+(4-iCurChunk), iCurChunk ); - } - else - sOut += rNext.write( outBuf, 4 ); - iBPos = iBuf = 0; - } - } - iTotalOut += sOut; - return sOut; -} - -bool Bu::Base64::isOpen() -{ - return true; -} - -bool Bu::Base64::isEos() -{ - if( bEosIn == true && iRPos == iChars ) - return true; - return false; -} - diff --git a/src/base64.h b/src/base64.h deleted file mode 100644 index c081ac1..0000000 --- a/src/base64.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_BASE64_H -#define BU_BASE64_H - -#include "bu/filter.h" -#include "bu/exceptionbase.h" - -namespace Bu -{ - subExceptionDecl( Base64Exception ); - - /** - * - *@ingroup Streams - */ - class Base64 : public Bu::Filter - { - public: - Base64( Bu::Stream &rNext, int iChunkSize=0 ); - virtual ~Base64(); - - virtual void start(); - virtual Bu::size stop(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - - virtual bool isOpen(); - - virtual bool isEos(); - - private: - int iBPos; - int iBuf; - int iRPos; - int iChars; - bool bEosIn; - Bu::size iTotalIn; - Bu::size iTotalOut; - static const char tblEnc[65]; - char tblDec[80]; - enum Mode - { - Nothing = 0x00, - Encode = 0x01, - Decode = 0x02, - }; - Mode eMode; - int iChunkSize; - int iCurChunk; - }; -}; - -#endif diff --git a/src/bitstring.cpp b/src/bitstring.cpp deleted file mode 100644 index bdd1bc2..0000000 --- a/src/bitstring.cpp +++ /dev/null @@ -1,479 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/bitstring.h" -#include -#include -#include - -#include "bu/exceptionbase.h" - -#ifdef _WIN32 -#define random() rand() -#endif - -#define bitsToBytes( iBits ) (((iBits/8)+((iBits%8)?(1):(0)))); - -Bu::BitString::BitString() -{ - caData = NULL; - cTopByteMask = 0; - iBits = iBytes = 0; -} - -Bu::BitString::BitString( const Bu::BitString &xSrc ) -{ - iBits = xSrc.iBits; - iBytes = xSrc.iBytes; - cTopByteMask = xSrc.cTopByteMask; - caData = new unsigned char[iBytes]; - memcpy( caData, xSrc.caData, iBytes ); - - fixup(); -} - -Bu::BitString::BitString( long iNewBits, bool bFillRandomly ) -{ - long j; - iBits = iNewBits; - iBytes = bitsToBytes( iNewBits );//(iNewBits/8)+((iNewBits%8)?(1):(0)); - caData = new unsigned char[iBytes]; - - setMask(); - - if( bFillRandomly ) - { - // rand() only returns a value up to RAND_MAX (0x7FFF on my system) so - // I'll just use the low order byte) - for( j = 0; j < iBytes; j++ ) - { - caData[j] = (unsigned char)(random() & 0xFF); - } - } - else - { - clear(); - } - - fixup(); -} - -Bu::BitString::~BitString() -{ - if( caData != NULL ) delete[] caData; -} - -Bu::BitString &Bu::BitString::operator=( const Bu::BitString &xSrc ) -{ - if( caData != NULL ) - { - delete[] caData; - } - iBits = xSrc.iBits; - iBytes = xSrc.iBytes; - cTopByteMask = xSrc.cTopByteMask; - caData = new unsigned char[iBytes]; - memcpy( caData, xSrc.caData, iBytes ); - - fixup(); - - return *this; -} - -Bu::BitString Bu::BitString::operator~() -{ - Bu::BitString xRet( *this ); - - for( int j = 0; j < xRet.iBytes; j++ ) - { - xRet.caData[j] = ~xRet.caData[j]; - } - - xRet.fixup(); - - return xRet; -} - -Bu::BitString Bu::BitString::operator<<( const long iAmt ) -{ - if( iAmt == 0 ) - { - return (*this); - } - //int iByteShift = iAmt/8; - - Bu::BitString xSub( getSize() ); - - long shft = (iAmt%8); - long base = (iAmt/8); - unsigned char lowmask=0; - for( long j = 0; j < 8-shft; j++ ) - { - lowmask |= (1<>shft)&(lowmask)) | ((caData[j+1]<<(8-shft))&(~lowmask)); - } - xSub.fixup(); - - return xSub; -} - -Bu::BitString Bu::BitString::operator>>( const long iAmt ) -{ - if( iAmt == 0 ) - { - return (*this); - } - return (*this); -} - -void Bu::BitString::shiftLeft( long iAmt ) -{ - if( iAmt == 0 ) - { - return; - } - else if( iAmt < 0 ) - { - shiftRight( -iAmt ); - return; - } - - long iByteShift = iAmt/8; - long iBitShift = iAmt%8; - - long j; - for( j = iBytes-1; j >= 0; j-- ) - { - caData[j] = (((j-iByteShift)<0)?(0):((caData[j-iByteShift]<>(8-iBitShift)))); - } - - fixup(); -} - -void Bu::BitString::shiftRight( long iAmt ) -{ - if( iAmt == 0 ) - { - return; - } - else if( iAmt < 0 ) - { - shiftLeft( -iAmt ); - return; - } - - long iByteShift = iAmt/8; - long iBitShift = iAmt%8; - - long j; - for( j = 0; j < iBytes; j++ ) - { - caData[j] = (((j+iByteShift)>iBytes)?(0):((caData[j+iByteShift]>>iBitShift))) | (((j+iByteShift+1)>iBytes)?(0):((caData[j+iByteShift+1]<<(8-iBitShift)))); - } - - fixup(); -} -/* -long Bu::BitString::bitsToBytes( long iBits ) -{ - return (iBits/8)+((iBits%8)?(1):(0)); -} -*/ -void Bu::BitString::fixup() -{ - if( caData != NULL ) - { - caData[iBytes-1] &= cTopByteMask; - } -} - -void Bu::BitString::setBit( long iBit, bool bBitState ) -{ - if( iBit < 0 || iBit >= iBits ) - throw Bu::ExceptionBase("bit out of range: %d in (0-%d)", iBit, iBits ); - if( bBitState ) - { - caData[iBit/8] |= (1<<(iBit%8)); - } - else - { - caData[iBit/8] &= ~(1<<(iBit%8)); - } -} - -void Bu::BitString::flipBit( long iBit ) -{ - caData[iBit/8] ^= (1<<(iBit%8)); -} - -bool Bu::BitString::getBit( long iBit ) -{ - if( iBit >= iBits || iBit < 0 ) return false; - if( (caData[iBit/8] & (1<<(iBit%8))) == 0 ) - { - return false; - } - return true; -} - -long Bu::BitString::getBitLength() -{ - return iBits; -} - -long Bu::BitString::getSize() -{ - return iBits; -} - -class Bu::BitString Bu::BitString::getSubString( long iLower, long iUpper ) -{ - if( iUpper == 0 || iUpper < iLower ) iUpper = iBits; - - Bu::BitString xSub( iUpper-iLower+1 ); - - long shft = (iLower%8); - long base = (iLower/8); - unsigned char lowmask=0; - for( long j = 0; j < 8-shft; j++ ) - { - lowmask |= (1<>shft)&(lowmask)) | ((caData[base+j+1]<<(8-shft))&(~lowmask)); - } - xSub.fixup(); - - return xSub; -} - -long Bu::BitString::toLong( long iStart, long iSize ) -{ - if( iSize < 1 ) iSize = 1; - if( iSize > 32 ) iSize = 32; - if( iStart < 0 ) return 0; - if( iStart+iSize > getSize() ) return 0; - - Bu::BitString tmpo; - tmpo = getSubString( iStart, iStart+iSize-1 ); - long x = *((long *)tmpo.caData); - - return x; -} -/* -std::string Bu::BitString::toString( bool bAddSpacers ) -{ - long iSz = iBits; - if( bAddSpacers ) - { - iSz += (iBits/8); - if( iBits%8 == 0 ) iSz--; - } - std::string xStr; - - int bw=0; - int of=0; - for( int j = iBits-1; j >= 0; j-- ) - { - if( getBit( j ) ) - { - xStr += '1'; - } - else - { - xStr += '0'; - } - - if( bAddSpacers ) - { - bw++; - if( bw >= 8 && j < iBits-1 ) - { - bw = 0; - of++; - xStr += ' '; - } - } - } - - return xStr; -} -*/ -void Bu::BitString::clear() -{ - if( caData != NULL ) - { - memset( caData, 0, iBytes ); - } -} - -bool Bu::BitString::setBitLength( long iLength, bool bClear ) -{ - return setSize( iLength, bClear ); -} - -bool Bu::BitString::setSize( long iLength, bool bClear ) -{ - // First, if there's nothing, then allocate an empty one. - if( caData == NULL ) - { - iBits = iLength; - iBytes = bitsToBytes( iLength ); - caData = new unsigned char[iBytes]; - memset( caData, 0, iBytes ); - return true; - } - - // If the new length is the same as the old, don't do anything, but do - // check to see if we should still clear the data. - if( iBits != iLength ) - { - // Ok, we are changing the number if bits, but are we changing the - // number of bytes? - long iNewBytes = bitsToBytes( iLength ); - if( iBytes == iNewBytes ) - { - // No? That makes life easier - iBits = iLength; - setMask(); - if( bClear ) - { - clear(); - } - } - else - { - // Ok, reallocate and copy... - iBits = iLength; -// long iNewBytes = bitsToBytes( iLength ); - if( bClear ) - { - delete[] caData; - caData = new unsigned char[iNewBytes]; - memset( caData, 0, iNewBytes ); - } - else - { - unsigned char *tmp = caData; - caData = new unsigned char[iNewBytes]; - if( iNewBytes < iBytes ) - { - memcpy( caData, tmp, iNewBytes ); - } - else - { - memcpy( caData, tmp, iBytes ); - } - delete[] tmp; - } - iBytes = iNewBytes; - - setMask(); - } - - } - else if( bClear ) - { - clear(); - } - - return true; -} - -void Bu::BitString::setMask() -{ - // This can either mean that there are a multiple of eight bits or - // zero, if there are zero you're an idiot (zero can't happen, because - // we would allocate an extra byte and never use it) - if( (iBits%8 == 0) ) - { - cTopByteMask = 0xFF; - } - else - { - cTopByteMask = 0; - for( long j = 0; j < (iBits%8); j++ ) - { - cTopByteMask |= (1<= 0; j-- ) - { - if( getBit( j ) ) - { - return j; - } - } - - return -1; -} - -Bu::String Bu::BitString::toString() -{ - Bu::String sRet; - for( int j = iBits-1; j >= 0; j-- ) - sRet.append( getBit( j )?'1':'0' ); - return sRet; -} - -/* -bool Bu::BitString::writeToFile( FILE *fh ) -{ - fwrite( &iBits, sizeof(long), 1, fh ); - fwrite( caData, sizeof(char), iBytes, fh ); - - return true; -} - -bool Bu::BitString::readFromFile( FILE *fh ) -{ - fread( &iBits, sizeof(long), 1, fh ); - - iBytes = bitsToBytes( iBits ); - if( caData ) delete[] caData; - caData = new unsigned char[iBytes]; - - fread( caData, sizeof(char), iBytes, fh ); - - setMask(); - - fixup(); - - return true; -}*/ diff --git a/src/bitstring.h b/src/bitstring.h deleted file mode 100644 index 7a8fc48..0000000 --- a/src/bitstring.h +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_BITSTRING_H -#define BU_BITSTRING_H - -#include "bu/util.h" -#include "bu/string.h" - -namespace Bu -{ - /** - * Manages an arbitrarily sized string of bits, and allows basic interaction - * with them. This includes basic non-mathematical bitwise operations such - * as setting and testing bits, shifting the string, inversion and a few - * extras like randomization. On linux systems this takes advantage of long - * longs giving you a maximum size of about 2tb per string. - * - * For more general and mathematical type interaction see BitStringInt. - * - */ - class BitString - { - public: - /** - * Constructs a blank and basic BitString. This is actually useful - * since you can resize BitStrings at will, and even retain the data - * that was in them. - */ - BitString(); - - /** - * Constructs a BitString object as a copy of another BitString. This - * is a standard copy constructor and produces an exact duplicate of - * the original BitString object. - *@param xSrc Source BitString to copy data from. - */ - BitString( const BitString &xSrc ); - - /** - * Constructs a BitString with length iBits and optionally fills it with - * random data. The default setting, to not fill randomly, will produce - * a blank (all zeros) string of the specified size. - *@param iBits The length of the new BitString in bits. - *@param bFillRandomly Wether or not to randomize this BitString. - */ - BitString( long iBits, bool bFillRandomly=false ); - - /** - * Virtual deconstructor for the BitString. Takes care of cleanup for - * you. What more do you really want to know? - */ - virtual ~BitString(); - - // basic interaction - /** - * Sets a bit in the BitString. In it's normal mode it will always turn - * the given bit on, to clear a bit set bBitState to false instead of - * true. This operation runs in O(1). - *@param iBit The zero-based index of the bit to modify. - *@param bBitState Set to true to set the bit to 1, set to false to set - * the bit to 0. - */ - void setBit( long iBit, bool bBitState=true ); - - /** - * Reverses the state of the given bit. This will set the given bit - * to a 1 if it was 0, and to 0 if it was 1. This operation runs in - * O(1), and it should be noted that using this is marginally faster - * than doing the test and flip yourself with getBit and setBit since - * it uses a bitwise not operation and doesn't actually test the bit - * itself. - *@param iBit The index of the bit to flip. - */ - void flipBit( long iBit ); - - /** - * Gets the state of the given bit. This follows the standard - * convention used so far, a returned value of true means the bit in - * question is 1, and a value of flase means the bit is 0. All bits - * out of range of the BitString are treated as off, but are - * "accessable" in that this does not produce any kind of error - * message. This is intentional. This operation runs in O(1). - *@param iBit The index of the bit to test. - *@returns True for a 1, false for a 0. - */ - bool getBit( long iBit ); - - /** - * Inverts the entire BitString, in effect this calls flipBit on every - * bit in the string but is faster since it can operate on whole bytes - * at a time instead of individual bits. This operation runs in O(N). - */ - void invert(); - - /** - * Returns the number of bits allocated in this BitString. This - * operation runs in O(1) time since this value is cached and not - * computed. - *@returns The number of bits allocated in this BitString. - */ - DEPRECATED - long getBitLength(); - - long getSize(); - - /** - * Sets the entire BitString to zeros, but it does it very quickly. - * This operation runs in O(N). - */ - void clear(); - - /** - * Gets another BitString that is autonomous of the current one - * (contains a copy of the memory, not a pointer) and contains a subset - * of the data in the current BitString. This is an inclusive - * operation, so grabbing bits 0-5 will give you 6 bits. This is based - * on a very tricky bit-shifting algorithm and runs very quickly, in - * O(N) time. Passing in a value of zero for iUpper, or any value for - * iUpper that is less than iLower will set iUpper equal to the number - * of bits in the BitString. - *@param iLower The first bit in the current string, will be the first - * bit (0 index) in the new sub string. - *@param iUpper The last bit in the current string, will be the last - * bit in the new sub string. iUpper is included in the sub string. - *@returns A new BitString object ready to be used. Please note that - * managing this new object is up to whomever calls this function. - */ - class BitString getSubString( long iLower, long iUpper ); - - /** - * Sets the number of bits in the BitString, allocating more memory if - * necesarry, or freeing extra if able. The default operation of this - * function clears all data in the BitString while resizing it. If you - * would like to keep as much of the data that you had in your BitString - * as possible, then set bClear to false, and any data that will fit - * into the new BitString length will be retained. If increasing the - * number of bits, the new bits will come into existance cleared (set - * to 0). - *@param iLength The number of bits to set the BitString to. - *@param bClear When true, all data is eradicated and zeroed, when set - * to false an effort is made to retain the existing data. - *@returns true on success, false on failure. - */ - DEPRECATED - bool setBitLength( long iLength, bool bClear=true ); - bool setSize( long iLength, bool bClear=true ); - - /** - * Randomize the entire BitString, one bit at a time. This is actually - * the function called by the constructor when the user selects initial - * randomization. This function uses the system random() function, so - * srandom may be used to effect this process at will. - */ - void randomize(); - - /** - * Operates exactly like <<. All data in the BitString is shifted to - * the left by some number of bits, any data pushed off the edge of the - * BitString is lost, and all new data coming in will be zeroed. - * Using a negative value in the shiftLeft function will turn it into - * the shiftRight function. - *@param iAmt The number of bit positions to shift all data. - */ - void shiftLeft( long iAmt ); // just like << - - /** - * Operates exactly like >>. All data in the BitString is shifted to - * the right by some number of bits, any data pushed off the edge of the - * BitString is lost, and all new data coming in will be zeroed. - * Using a negative value in the shiftRight function will turn it into - * the shiftLeft function. - *@param iAmt The number of bit positions to shift all data. - */ - void shiftRight( long iAmt ); // just like >> - - /** - * Searches through the BitString and returns the index of the highest - * order bit position (the highest index) with an on bit (a bit set to - * 1). This is a handy helper function and rather faster than calling - * getBit() over and over again. - *@returns The index of the highest indexed on bit. - */ - long getHighestOrderBitPos(); - - // Conversion - /** - * Convert a block of data (no more than 32 bits) to a primitive long - * type. - * This is done in a little bit interesting way, so it may not always be - * the fastest way to access the data that you want, although it will - * always ensure that the long that is written makes numerical sense, as - * we write numbers, regaurdless of platform. - *@param iStart The first bit in the BitString to include in the long - *@param iSize THe number of bits to include, if this value is set over - * 32 it will be automatically truncated to, or however many bits there - * are in a long in your system. - *@returns A long converted from your raw BitString data. - */ - long toLong( long iStart = 0, long iSize = 32 ); - - Bu::String toString(); - - //operators - BitString &operator=( const BitString &xSrc ); - BitString operator~(); - BitString operator<<( const long iAmt ); - BitString operator>>( const long iAmt ); - - private: - void fixup(); - void setMask(); - unsigned char *caData; - long iBits; - long iBytes; - unsigned char cTopByteMask; - }; -}; - -#endif diff --git a/src/buffer.cpp b/src/buffer.cpp deleted file mode 100644 index 47fab70..0000000 --- a/src/buffer.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/buffer.h" - -Bu::Buffer::Buffer( Bu::Stream &rNext, int iWhat, int iBufSize ) : - Bu::Filter( rNext ), - sSoFar( 0 ), - iBufSize( iBufSize ), - sReadBuf( NULL ), - sWriteBuf( NULL ), - iReadBufFill( 0 ), - iReadPos( 0 ), - iWriteBufFill( 0 ), - iWritePos( 0 ), - iWhat( iWhat ) -{ - sReadBuf = new char[iBufSize]; - sWriteBuf = new char[iBufSize]; -} - -Bu::Buffer::~Buffer() -{ - delete[] sReadBuf; - delete[] sWriteBuf; -} - -void Bu::Buffer::start() -{ -} - -Bu::size Bu::Buffer::stop() -{ - iReadBufFill = iReadPos = iWriteBufFill = iWritePos = 0; - return sSoFar; -} - -void Bu::Buffer::fillReadBuf() -{ - if( iReadBufFill+iReadPos < iBufSize ) - { - iReadBufFill += rNext.read( - sReadBuf+iReadPos+iReadBufFill, - iBufSize-iReadBufFill-iReadPos - ); - } -} - -Bu::size Bu::Buffer::read( void *pBuf, Bu::size nBytes ) -{ - if( (iWhat&Read) == 0 ) - return rNext.read( pBuf, nBytes ); - - if( nBytes <= 0 ) - { - fillReadBuf(); - return 0; - } - - Bu::size nTotRead = 0; -// fillReadBuf(); - - do - { - int iAmnt = nBytes-nTotRead; - if( iAmnt > iReadBufFill ) - { - iAmnt = iReadBufFill; - } - if( iAmnt > 0 ) - { - memcpy( ((char *)pBuf)+nTotRead, sReadBuf+iReadPos, iAmnt ); - iReadPos += iAmnt; - nTotRead += iAmnt; - iReadBufFill -= iAmnt; - } - if( iReadBufFill == 0 ) - { - iReadPos = 0; - fillReadBuf(); - } - } - while( nTotRead < nBytes && iReadBufFill > 0 ); - - //printf("Buffer: %db returned, %db remain in buffer.\n", nTotRead, iReadBufFill ); - - return nTotRead; -} - -Bu::size Bu::Buffer::write( const void *pBuf, Bu::size nBytes ) -{ - if( (iWhat&Write) == 0 ) - return rNext.write( pBuf, nBytes ); - - Bu::size nTotWrote = 0; - - do - { - int iAmnt = nBytes-nTotWrote; - if( iAmnt > iBufSize-iWritePos-iWriteBufFill ) - { - iAmnt = iBufSize-iWritePos-iWriteBufFill; - } - if( iAmnt > 0 ) - { - memcpy( - sWriteBuf+iWritePos+iWriteBufFill, - ((char *)pBuf)+nTotWrote, - iAmnt - ); - nTotWrote += iAmnt; - iWriteBufFill += iAmnt; - //printf("Buffer: Moved %db to write buffer, %db filled now.\n", - //iAmnt, iWriteBufFill ); - } - while( iWritePos+iWriteBufFill == iBufSize ) - { - //printf("iWritePos = %d\n", iWritePos ); - int iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); - //printf("Buffer: Wrote %db from buffer to stream, %db filled now.\n", iWr, iWriteBufFill-iWr ); - if( iWr == 0 ) - { - return nTotWrote; - } - else if( iWr == iWriteBufFill ) - { - iWritePos = iWriteBufFill = 0; - } - else - { - iWritePos += iWr; - iWriteBufFill -= iWr; - } - } - } - while( nTotWrote < nBytes && iWriteBufFill < iBufSize+iWritePos ); - - return nTotWrote; -} - -void Bu::Buffer::flush() -{ - if( (iWhat&Write) == 0 ) - return rNext.flush(); - - if( iWriteBufFill > 0 ) - { - //printf("Buffer: Flushing remaining data, %db.\n", iWriteBufFill ); - int iWr = 0; - do - { - iWr = rNext.write( sWriteBuf+iWritePos, iWriteBufFill ); - //printf("Buffer: %db written to stream.\n", iWr ); - iWritePos += iWr; - iWriteBufFill -= iWr; - } while( iWriteBufFill > 0 && iWr > 0 ); - } -} - -bool Bu::Buffer::isEos() -{ - return (iReadPos >= (iReadBufFill-1)) && (rNext.isEos()); -} - diff --git a/src/buffer.h b/src/buffer.h deleted file mode 100644 index 91ec9c2..0000000 --- a/src/buffer.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_BUFFER_H -#define BU_BUFFER_H - -#include "bu/filter.h" - -namespace Bu -{ - class Buffer : public Bu::Filter - { - public: - Buffer( Bu::Stream &rNext, int iWhat=Both, int iBufSize=4096 ); - virtual ~Buffer(); - - enum - { - Write = 1, - Read = 2, - Both = 3 - }; - - virtual void start(); - virtual Bu::size stop(); - - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Stream::write; - - Bu::size getReadFill() { return iReadBufFill; } - bool isWritePending() { return iWriteBufFill > 0; } - - virtual void flush(); - - virtual bool isEos(); - - private: - void fillReadBuf(); - - private: - Bu::size sSoFar; - int iBufSize; - char *sReadBuf; - char *sWriteBuf; - int iReadBufFill; - int iReadPos; - int iWriteBufFill; - int iWritePos; - int iWhat; - }; -}; - -#endif diff --git a/src/bzip2.cpp b/src/bzip2.cpp deleted file mode 100644 index ca007b0..0000000 --- a/src/bzip2.cpp +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/bzip2.h" -#include "bu/trace.h" - -#include - -#define pState ((bz_stream *)prState) - -using namespace Bu; - -Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : - Bu::Filter( rNext ), - prState( NULL ), - nCompression( nCompression ), - sTotalOut( 0 ) -{ - TRACE( nCompression ); - start(); -} - -Bu::BZip2::~BZip2() -{ - TRACE(); - stop(); -} - -void Bu::BZip2::start() -{ - TRACE(); - - prState = new bz_stream; - pState->state = NULL; - pState->bzalloc = NULL; - pState->bzfree = NULL; - pState->opaque = NULL; - - nBufSize = 64*1024; - pBuf = new char[nBufSize]; -} - -Bu::size Bu::BZip2::stop() -{ - TRACE(); - if( pState->state ) - { - if( bReading ) - { - BZ2_bzDecompressEnd( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return 0; - } - else - { -// Bu::size sTotal = 0; - for(;;) - { - pState->next_in = NULL; - pState->avail_in = 0; - pState->avail_out = nBufSize; - pState->next_out = pBuf; - int res = BZ2_bzCompress( pState, BZ_FINISH ); - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( res == BZ_STREAM_END ) - break; - } - BZ2_bzCompressEnd( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return sTotalOut; - } - } - return 0; -} - -void Bu::BZip2::bzError( int code ) -{ - TRACE( code ); - switch( code ) - { - case BZ_OK: - case BZ_RUN_OK: - case BZ_FLUSH_OK: - case BZ_FINISH_OK: - return; - - case BZ_CONFIG_ERROR: - throw ExceptionBase("BZip2: Library configured improperly, reinstall."); - - case BZ_SEQUENCE_ERROR: - throw ExceptionBase("BZip2: Functions were called in an invalid sequence."); - - case BZ_PARAM_ERROR: - throw ExceptionBase("BZip2: Invalid parameter was passed into a function."); - - case BZ_MEM_ERROR: - throw ExceptionBase("BZip2: Couldn't allocate sufficient memory."); - - case BZ_DATA_ERROR: - throw ExceptionBase("BZip2: Data was corrupted before decompression."); - - case BZ_DATA_ERROR_MAGIC: - throw ExceptionBase("BZip2: Stream does not appear to be bzip2 data."); - - case BZ_IO_ERROR: - throw ExceptionBase("BZip2: File couldn't be read from / written to."); - - case BZ_UNEXPECTED_EOF: - throw ExceptionBase("BZip2: End of file encountered before end of stream."); - - case BZ_OUTBUFF_FULL: - throw ExceptionBase("BZip2: Buffer not large enough to accomidate data."); - - default: - throw ExceptionBase("BZip2: Unknown error encountered."); - - } -} - -Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( !pState->state ) - { - bReading = true; - BZ2_bzDecompressInit( pState, 0, 0 ); - pState->next_in = pBuf; - pState->avail_in = 0; - } - if( bReading == false ) - throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); - - int nRead = 0; - int nReadTotal = pState->total_out_lo32; - pState->next_out = (char *)pData; - pState->avail_out = nBytes; - for(;;) - { - int ret = BZ2_bzDecompress( pState ); - - nReadTotal += nRead-pState->avail_out; - - if( ret == BZ_STREAM_END ) - { - if( pState->avail_in > 0 ) - { - if( rNext.isSeekable() ) - { - rNext.seek( -pState->avail_in ); - } - } - return nBytes-pState->avail_out; - } - bzError( ret ); - - if( pState->avail_out ) - { - if( pState->avail_in == 0 ) - { - nRead = rNext.read( pBuf, nBufSize ); - if( nRead == 0 && rNext.isEos() ) - { - throw Bu::ExceptionBase("Premature end of underlying " - "stream found reading bzip2 stream."); - } - pState->next_in = pBuf; - pState->avail_in = nRead; - } - } - else - { - return nBytes-pState->avail_out; - } - } - return 0; -} - -Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( !pState->state ) - { - bReading = false; - BZ2_bzCompressInit( pState, nCompression, 0, 30 ); - } - if( bReading == true ) - throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); - -// Bu::size sTotalOut = 0; - pState->next_in = (char *)pData; - pState->avail_in = nBytes; - for(;;) - { - pState->avail_out = nBufSize; - pState->next_out = pBuf; - - bzError( BZ2_bzCompress( pState, BZ_RUN ) ); - - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( pState->avail_in == 0 ) - break; - } - - return nBytes; -} - -bool Bu::BZip2::isOpen() -{ - TRACE(); - return (pState->state != NULL); -} - -Bu::size Bu::BZip2::getCompressedSize() -{ - return sTotalOut; -} - diff --git a/src/bzip2.h b/src/bzip2.h deleted file mode 100644 index 9a8d172..0000000 --- a/src/bzip2.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_BZIP2_H -#define BU_BZIP2_H - -#include - -#include "bu/filter.h" - -namespace Bu -{ - /** - * Provides BZip2 type compression and decompression. - * - *@ingroup Streams - *@ingroup Compression - */ - class BZip2 : public Bu::Filter - { - public: - BZip2( Bu::Stream &rNext, int nCompression=9 ); - virtual ~BZip2(); - - virtual void start(); - virtual Bu::size stop(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - - virtual bool isOpen(); - - Bu::size getCompressedSize(); - - private: - void bzError( int code ); - void *prState; - bool bReading; - int nCompression; - char *pBuf; - uint32_t nBufSize; - Bu::size sTotalOut; - }; -} - -#endif diff --git a/src/cache.cpp b/src/cache.cpp deleted file mode 100644 index 52bd9fc..0000000 --- a/src/cache.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cache.h" diff --git a/src/cache.h b/src/cache.h deleted file mode 100644 index 455cf1b..0000000 --- a/src/cache.h +++ /dev/null @@ -1,437 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CACHE_H -#define BU_CACHE_H - -// #include "bu/cptr.h" -#include "bu/hash.h" -#include "bu/list.h" -#include "bu/cachestore.h" -#include "bu/cachecalc.h" - -#include "bu/trace.h" - -namespace Bu -{ -// template -// keytype __cacheGetKey( obtype *&pObj ); - template - keytype __cacheGetKey( const obtype *pObj ) - { - return pObj->getKey(); - } - - template - class Cache - { - public: - /** - * Cache Pointer - Provides access to data that is held within the - * cache. This provides safe, refcounting access to data stored in - * the cache, with support for lazy loading. - */ - class Ptr - { - friend class Bu::Cache; - private: - Ptr( Cache *pCache, obtype *pData, - const keytype &kId ) : - pCache( pCache ), - pData( pData ), - kId( kId ) - { - if( pCache ) - pCache->incRef( kId ); - } - - Ptr( Cache *pCache, const keytype &kId ) : - pCache( pCache ), - pData( NULL ), - kId( kId ) - { - } - - public: - Ptr( const Ptr &rSrc ) : - pCache( rSrc.pCache ), - pData( rSrc.pData ), - kId( rSrc.kId ) - { - if( pCache && pData ) - pCache->incRef( kId ); - } - - Ptr() : - pCache( 0 ), - pData( 0 ) - { - } - - virtual ~Ptr() - { - if( pCache && pData ) - pCache->decRef( kId ); - } - - obtype &operator*() - { - checkPtr(); - return *pData; - } - - const obtype &operator*() const - { - checkPtr(); - return *pData; - } - - obtype *operator->() - { - checkPtr(); - return pData; - } - - const obtype *operator->() const - { - checkPtr(); - return pData; - } - - bool isValid() const - { - return pCache != NULL; - } - - bool isBound() const - { - return pData != NULL; - } - - bool isSet() const - { - return pCache != NULL; - } - - const keytype &getKey() const - { - return kId; - } - - void unbind() - { - if( pCache && pData ) - pCache->decRef( kId ); - pData = NULL; - } - - void clear() - { - unbind(); - pCache = NULL; - } - - void unset() - { - clear(); - } - - Ptr &operator=( const Ptr &rRhs ) - { - if( pCache && pData ) - pCache->decRef( kId ); - pCache = rRhs.pCache; - pData = rRhs.pData; - kId = rRhs.kId; - if( pCache && pData ) - pCache->incRef( kId ); - return *this; - } - - bool operator==( const Ptr &rRhs ) const - { - return pCache == rRhs.pCache && kId == rRhs.kId; - } - - bool operator!=( const Ptr &rRhs ) const - { - return pCache != rRhs.pCache || kId != rRhs.kId; - } - - private: - void checkPtr() const - { - if( pCache && !pData ) - { - pData = pCache->getRaw( kId ); - pCache->incRef( kId ); - } - } - - private: - Bu::Cache *pCache; - mutable obtype *pData; - mutable keytype kId; - }; - - private: - typedef Bu::CacheStore Store; - typedef Bu::List StoreList; - typedef Bu::CacheCalc Calc; - - typedef struct CacheEntry - { - obtype *pData; - int iRefs; - time_t tLastSync; - } CacheEntry; - - typedef Bu::Hash CidHash; - - public: - typedef keytype Key; - Cache( Calc *pCalc, Store *pStore ) : - pCalc( pCalc ), - pStore( pStore ) - { - TRACE(); - pCalc->setCache( this ); - } - - virtual ~Cache() - { - TRACE(); - - // Better safe than sorry, better try a sync before anything - // else happens. - sync(); - - // Cycle through and unload all objects from the system. - for( typename CidHash::iterator i = hEnt.begin(); - i != hEnt.end(); i++ ) - { - if( i.getValue().iRefs > 0 ) - { - // TODO: Throw an error in this case? iRefs != 0 for an - // object when the Cache is destroyed. - throw Bu::ExceptionBase("iRefs not zero."); - } - pStore->unload( - i.getValue().pData, - i.getKey() - ); - } - delete pCalc; - delete pStore; - } - - Ptr insert( obtype *pData ) - { - TRACE( pData ); - if( pStore->has( __cacheGetKey( pData ) ) ) - throw Bu::ExceptionBase("Key already exists in cache."); - CacheEntry e = {pData, 0, 0}; - keytype k = pStore->create( pData ); - hEnt.insert( k, e ); - - pCalc->onLoad( pData, k ); - - pStore->sync(); - - return Ptr( this, pData, k ); - } - - bool has( const keytype &cId ) - { - return hEnt.has( cId ) || pStore->has( cId ); - } - - /** - * Retrieve an object from the cache and return a pointer to it. - * The object returned may be loaded from backend storage if needed, - * or the currently live object will be returned. - *@param cId The id of the object to load. - *@returns A pointer to the object. - */ - Ptr get( const keytype &cId ) - { - TRACE( cId ); - try { - return Ptr( this, hEnt.get( cId ).pData, cId ); - } - catch( Bu::HashException &e ) { - CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; - pCalc->onLoad( e.pData, cId ); - hEnt.insert( cId, e ); - return Ptr( this, e.pData, cId ); - } - } - - /** - * Retrieve a handle to an object without loading it now. This function - * will return a pointer that has not yet been "realized" but can be - * used normally. Upon initial use in any way the object will be - * loaded from the cache, either linking against the already loaded - * object or loading it fresh from the backend storage. The advantage - * of this is that you recieve a usable handle to the data, but it - * does not count as a reference yet, meaning that the data is loaded - * when you need it, not before. - */ - Ptr getLazy( const keytype &cId ) - { - TRACE( cId ); - return Ptr( this, cId ); - } - - int getRefCount( const keytype &cId ) - { - TRACE( cId ); - return hEnt.get( cId ).iRefs; - } - - void unload( const keytype &cId ) - { - TRACE( cId ); - try { - if( hEnt.get( cId ).iRefs > 0 ) - { - printf("Shouldn't unload, references still exist!\n"); - return; - } - } - catch( Bu::HashException &e ) { - // It's not here? Eh, return. - return; - } - obtype *pObj = hEnt.get( cId ).pData; - pCalc->onUnload( pObj, cId ); - hEnt.erase( cId ); - - // The unload has to happen last just in case cId is a reference - // to data that is about to be deleted from memory by the unload. - pStore->unload( pObj, cId ); - } - - void erase( const keytype &cId ) - { - TRACE( cId ); - try { - if( hEnt.get( cId ).iRefs > 0 ) - { - printf("Shouldn't erase, references still exist!\n"); - return; - } - - obtype *pObj = hEnt.get( cId ).pData; - pCalc->onDestroy( pObj, cId ); - hEnt.erase( cId ); - - pStore->destroy( pObj, cId ); - pStore->sync(); - } - catch( Bu::HashException &e ) { - pCalc->onDestroy( cId ); - - if( hEnt.has( cId ) ) - { - // The object was loaded by onDestroy - erase( cId ); - } - else - { - pStore->destroy( cId ); - pStore->sync(); - } - } - } - - typedef Bu::List KeyList; - KeyList getKeys() - { - return pStore->getKeys(); - } - - KeyList getActiveKeys() - { - return hEnt.getKeys(); - } - - int getSize() - { - return pStore->getSize(); - } - - /** - * Make sure all currently loaded but not-in-use objects are synced to - * the store. - */ - void sync() - { - TRACE(); - int iSynced = 0; - for( typename CidHash::iterator i = hEnt.begin(); - i != hEnt.end(); i++ ) - { - if( i.getValue().iRefs == 0 ) - { - if( pCalc->shouldSync( - i.getValue().pData, - i.getKey(), - i.getValue().tLastSync - ) ) - { - pStore->sync( - i.getValue().pData, - i.getKey() - ); - iSynced++; - i.getValue().tLastSync = time( NULL ); - } - } - } - if( iSynced > 0 ) - { - pStore->sync(); - } - } - - private: - void incRef( const keytype &cId ) - { - TRACE( cId ); - hEnt.get( cId ).iRefs++; - } - - void decRef( const keytype &cId ) - { - TRACE( cId ); - CacheEntry &e = hEnt.get( cId ); - e.iRefs--; - } - - obtype *getRaw( const keytype &cId ) - { - TRACE( cId ); - try { - return hEnt.get( cId ).pData; - } - catch( Bu::HashException &e ) { - CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; - pCalc->onLoad( e.pData, cId ); - hEnt.insert( cId, e ); - return e.pData; - } - } - - private: - CidHash hEnt; - Calc *pCalc; - Store *pStore; - }; -}; - -#endif diff --git a/src/cachecalc.cpp b/src/cachecalc.cpp deleted file mode 100644 index 7b8a10a..0000000 --- a/src/cachecalc.cpp +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cachecalc.h" diff --git a/src/cachecalc.h b/src/cachecalc.h deleted file mode 100644 index 89cfadc..0000000 --- a/src/cachecalc.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CACHE_CALC_H -#define BU_CACHE_CALC_H - -#include "bu/trace.h" - -#include - -namespace Bu -{ - template class Cache; - - template - class CacheCalc - { - friend class Cache; - private: - typedef Cache MyCache; - public: - CacheCalc() : - pCache( (MyCache *)0 ) - { - TRACE(); - } - - virtual ~CacheCalc() - { - TRACE(); - } - - virtual void onLoad( obtype *pSrc, const keytype &key )=0; - virtual void onUnload( obtype *pSrc, const keytype &key )=0; - virtual void onDestroy( obtype *pSrc, const keytype &key )=0; - virtual void onDestroy( const keytype &key )=0; - virtual bool shouldSync( obtype *pSrc, const keytype &key, - time_t tLastSync )=0; - virtual void onTick() { }; - - protected: - MyCache *getCache() - { - TRACE(); - return pCache; - } - - private: - void setCache( MyCache *pCache ) - { - TRACE(); - this->pCache = pCache; - } - - MyCache *pCache; - }; -}; - -#endif diff --git a/src/cachestore.cpp b/src/cachestore.cpp deleted file mode 100644 index af49548..0000000 --- a/src/cachestore.cpp +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cachestore.h" - diff --git a/src/cachestore.h b/src/cachestore.h deleted file mode 100644 index d0d91a2..0000000 --- a/src/cachestore.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CACHE_STORE_H -#define BU_CACHE_STORE_H - -#include "bu/list.h" - -namespace Bu -{ - /** - * Handles I/O for data in the cache. This also assigns ID's to the newly - * created objects that are requested through this system. - */ - template - class CacheStore - { - public: - CacheStore() - { - } - - virtual ~CacheStore() - { - } - - virtual obtype *load( const keytype &key )=0; - virtual void unload( obtype *pObj, const keytype &key )=0; - virtual keytype create( obtype *pSrc )=0; - virtual void sync()=0; - virtual void sync( obtype *pObj, const keytype &key )=0; - virtual void destroy( obtype *pObj, const keytype &key )=0; - virtual void destroy( const keytype &key )=0; - virtual bool has( const keytype &key )=0; - virtual Bu::List getKeys() { return Bu::List(); } - virtual int getSize() { return -1; } - - private: - }; -}; - -#endif diff --git a/src/cachestorefiles.cpp b/src/cachestorefiles.cpp deleted file mode 100644 index 66ce672..0000000 --- a/src/cachestorefiles.cpp +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cachestorefiles.h" - diff --git a/src/cachestorefiles.h b/src/cachestorefiles.h deleted file mode 100644 index 10b2c1b..0000000 --- a/src/cachestorefiles.h +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CACHE_STORE_FILES_H -#define BU_CACHE_STORE_FILES_H - -#include "bu/string.h" -#include "bu/file.h" -#include "bu/cachestore.h" -#include "bu/archive.h" -#include "bu/membuf.h" -#include "bu/formatter.h" -#include "bu/sio.h" - -#include -#include -#include -#include - -namespace Bu -{ - template - keytype __cacheGetKey( const obtype *pObj ); - - template - obtype *__cacheStoreFilesAlloc( const keytype &key ) - { - return new obtype(); - } - - template - void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj, - const keytype & ) - { - Bu::Archive ar( s, Bu::Archive::save ); - ar << rObj; - } - - template - obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key ) - { - obtype *pObj = __cacheStoreFilesAlloc( key ); - Bu::Archive ar( s, Bu::Archive::load ); - ar >> (*pObj); - return pObj; - } - - template - class CacheStoreFiles : public CacheStore - { - public: - CacheStoreFiles( const Bu::String &sPrefix ) : - sPrefix( sPrefix ) - { - if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) ) - { - mkdir( sPrefix.getStr(), 0755 ); - } - } - - virtual ~CacheStoreFiles() - { - } - - virtual obtype *load( const keytype &key ) - { -// try -// { - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/" << key; - Bu::File fIn( mb.getString(), Bu::File::Read ); - obtype *pOb = __cacheStoreFilesLoad( fIn, key ); - return pOb; -// } -// catch( std::exception &e ) -// { -// throw Bu::HashException( e.what() ); -// } - } - - virtual void unload( obtype *pObj, const keytype & ) - { - delete pObj; - } - - virtual keytype create( obtype *pSrc ) - { - keytype key = __cacheGetKey( pSrc ); - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/" << key; - - Bu::File fTouch( mb.getString(), Bu::File::WriteNew ); - - return key; - } - - virtual void sync() - { - } - - virtual void sync( obtype *pSrc, const keytype &key ) - { - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/" << key; - - Bu::File fOut( mb.getString(), Bu::File::WriteNew ); - __cacheStoreFilesStore( fOut, *pSrc, key ); - } - - virtual void destroy( obtype *pObj, const keytype &key ) - { - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/" << key; - - unlink( mb.getString().getStr() ); - delete pObj; - } - - virtual void destroy( const keytype &key ) - { - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/" << key; - - unlink( mb.getString().getStr() ); - } - - virtual bool has( const keytype &key ) - { - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << sPrefix << "/"; - Bu::String sBase = mb.getString(); - f << key; - - if( sBase == mb.getString() ) - return false; - - return access( mb.getString().getStr(), F_OK ) == 0; - } - - virtual Bu::List getKeys() - { - DIR *dir = opendir( sPrefix.getStr() ); - struct dirent *de; - Bu::List lKeys; - - while( (de = readdir( dir ) ) ) - { - if( de->d_type != DT_REG ) - continue; - - keytype tmp; - Bu::MemBuf mb( de->d_name ); - Bu::Formatter f( mb ); - try - { - Fmt fm; - fm.tokenize( false ); - f << fm; - f >> tmp; - } - catch( Bu::ExceptionBase &e ) - { - Bu::sio << "Parse error in dir-scan: " << e.what() - << Bu::sio.nl; - } - lKeys.append( tmp ); - } - closedir( dir ); - - return lKeys; - } - - virtual int getSize() - { - DIR *dir = opendir( sPrefix.getStr() ); - struct dirent *de; - int iCount = 0; - - while( (de = readdir( dir ) ) ) - { - if( de->d_type != DT_REG ) - continue; - - iCount++; - } - closedir( dir ); - - return iCount; - } - - private: - Bu::String sPrefix; - }; - -}; - -#endif diff --git a/src/cachestoremyriad.cpp b/src/cachestoremyriad.cpp deleted file mode 100644 index 9d00dce..0000000 --- a/src/cachestoremyriad.cpp +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cachestoremyriad.h" - diff --git a/src/cachestoremyriad.h b/src/cachestoremyriad.h deleted file mode 100644 index e632a82..0000000 --- a/src/cachestoremyriad.h +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CACHE_STORE_MYRIAD_H -#define BU_CACHE_STORE_MYRIAD_H - -#include "bu/string.h" -#include "bu/stream.h" -#include "bu/myriad.h" -#include "bu/cachestore.h" -#include "bu/myriadstream.h" - -#include "bu/archive.h" - -namespace Bu -{ - template - keytype __cacheGetKey( const obtype *pObj ); - - template - obtype *__cacheStoreMyriadAlloc( const keytype &key ) - { - return new obtype(); - } - - template - void __cacheStoreMyriadStore( Bu::Stream &s, obtype &rObj, - const keytype & ) - { - Bu::Archive ar( s, Bu::Archive::save ); - ar << rObj; - } - - template - obtype *__cacheStoreMyriadLoad( Bu::Stream &s, const keytype &key ) - { - obtype *pObj = __cacheStoreMyriadAlloc( key ); - Bu::Archive ar( s, Bu::Archive::load ); - ar >> (*pObj); - return pObj; - } - - template - class CacheStoreMyriad : public CacheStore - { - public: - CacheStoreMyriad( Bu::Stream &sArch, - int iBlockSize=512, int iPreAllocate=8 ) : - mStore( sArch, iBlockSize, iPreAllocate ) - { - try - { - MyriadStream ns = mStore.openStream( 1 ); - Bu::Archive ar( ns, Bu::Archive::load ); - ar >> hId; - } - catch( Bu::MyriadException &e ) - { - int iStream = mStore.createStream(); - if( iStream != 1 ) - throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n", - iStream ); - MyriadStream ns = mStore.openStream( 1 ); - Bu::Archive ar( ns, Bu::Archive::save ); - ar << hId; - } - } - - virtual ~CacheStoreMyriad() - { - MyriadStream ns = mStore.openStream( 1 ); - Bu::Archive ar( ns, Bu::Archive::save ); - ar << hId; - } - - virtual obtype *load( const keytype &key ) - { - int iStream = hId.get( key ); - MyriadStream ns = mStore.openStream( iStream ); - obtype *pOb = __cacheStoreMyriadLoad( ns, key ); - return pOb; - } - - virtual void unload( obtype *pObj, const keytype & ) - { - delete pObj; - } - - virtual keytype create( obtype *pSrc ) - { - keytype key = __cacheGetKey( pSrc ); - int iStream = mStore.createStream(); - hId.insert( key, iStream ); - MyriadStream ns = mStore.openStream( iStream ); - __cacheStoreMyriadStore( ns, *pSrc, key ); - ns.setSize( ns.tell() ); - return key; - } - - virtual void sync() - { - MyriadStream ns = mStore.openStream( 1 ); - Bu::Archive ar( ns, Bu::Archive::save ); - ar << hId; - ns.setSize( ns.tell() ); - mStore.sync(); - } - - virtual void sync( obtype *pSrc, const keytype &key ) - { - int iStream = hId.get( key ); - MyriadStream ns = mStore.openStream( iStream ); - __cacheStoreMyriadStore( ns, *pSrc, key ); - ns.setSize( ns.tell() ); - } - - virtual void destroy( obtype *pObj, const keytype &key ) - { - int iStream = hId.get( key ); - mStore.deleteStream( iStream ); - hId.erase( key ); - delete pObj; - } - - virtual void destroy( const keytype &key ) - { - int iStream = hId.get( key ); - mStore.deleteStream( iStream ); - hId.erase( key ); - } - - virtual bool has( const keytype &key ) - { - return hId.has( key ); - } - - virtual Bu::List getKeys() - { - return hId.getKeys(); - } - - virtual int getSize() - { - return hId.getSize(); - } - - private: - Myriad mStore; - typedef Bu::Hash StreamHash; - StreamHash hId; - }; -}; - -#endif diff --git a/src/client.cpp b/src/client.cpp deleted file mode 100644 index 75f6158..0000000 --- a/src/client.cpp +++ /dev/null @@ -1,320 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/client.h" -#include "bu/tcpsocket.h" -#include -#include -#include "bu/protocol.h" -#include "bu/clientlink.h" -#include "bu/clientlinkfactory.h" - -/** Read buffer size. */ -#define RBS (2000) // 1500 is the nominal MTU for ethernet, it's a good guess - -Bu::Client::Client( Bu::TcpSocket *pSocket, - class Bu::ClientLinkFactory *pfLink ) : - pTopStream( pSocket ), - pSocket( pSocket ), - pProto( NULL ), - bWantsDisconnect( false ), - pfLink( pfLink ) -{ - lFilts.prepend( pSocket ); -} - -Bu::Client::~Client() -{ - for( FilterList::iterator i = lFilts.begin(); i; i++ ) - { - delete *i; - } - pTopStream = pSocket = NULL; - delete pfLink; -} - -void Bu::Client::processInput() -{ - char buf[RBS]; - Bu::size nRead, nTotal=0; - - for(;;) - { - try - { - nRead = pTopStream->read( buf, RBS ); - - if( nRead == 0 ) - { - break; - } - else - { - nTotal += nRead; - qbRead.write( buf, nRead ); - if( !pTopStream->canRead() ) - break; - } - } - catch( Bu::TcpSocketException &e ) - { - pTopStream->close(); - bWantsDisconnect = true; - break; - } - } - - if( nTotal == 0 ) - { - pTopStream->close(); - bWantsDisconnect = true; - } - - if( pProto && nTotal ) - { - pProto->onNewData( this ); - } -} - -void Bu::Client::processOutput() -{ - char buf[RBS]; - if( qbWrite.getSize() > 0 ) - { - int nAmnt = RBS; - nAmnt = qbWrite.peek( buf, nAmnt ); - int nReal = pTopStream->write( buf, nAmnt ); - qbWrite.seek( nReal ); - pTopStream->flush(); - } -} - -void Bu::Client::setProtocol( Protocol *pProto ) -{ - this->pProto = pProto; - this->pProto->onNewConnection( this ); -} - -Bu::Protocol *Bu::Client::getProtocol() -{ - return pProto; -} - -void Bu::Client::clearProtocol() -{ - pProto = NULL; -} -/* -Bu::String &Bu::Client::getInput() -{ - return sReadBuf; -} - -Bu::String &Bu::Client::getOutput() -{ - return sWriteBuf; -} -*/ - -bool Bu::Client::isOpen() -{ - if( !pTopStream ) return false; - return pTopStream->isOpen(); -} - -Bu::size Bu::Client::write( const Bu::String &sData ) -{ - return qbWrite.write( sData.getStr(), sData.getSize() ); -} - -Bu::size Bu::Client::write( const void *pData, Bu::size nBytes ) -{ - return qbWrite.write( pData, nBytes ); -} - -Bu::size Bu::Client::write( int8_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( int16_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( int32_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( int64_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( uint8_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( uint16_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( uint32_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::write( uint64_t nData ) -{ - return qbWrite.write( (const char *)&nData, sizeof(nData) ); -} - -Bu::size Bu::Client::read( void *pData, Bu::size nBytes ) -{ - return qbRead.read( pData, nBytes ); -} - -Bu::size Bu::Client::peek( void *pData, int nBytes, int nOffset ) -{ - return qbRead.peek( pData, nBytes, nOffset ); -} - -Bu::size Bu::Client::getInputSize() -{ - return qbRead.getSize(); -} - -Bu::size Bu::Client::getOutputSize() -{ - return qbWrite.getSize(); -} - -const Bu::TcpSocket *Bu::Client::getSocket() const -{ - return pSocket; -} - -void Bu::Client::disconnect() -{ - bWantsDisconnect = true; -} - -bool Bu::Client::wantsDisconnect() -{ - return bWantsDisconnect; -} - -void Bu::Client::close() -{ - pTopStream->close(); -} - -Bu::ClientLink *Bu::Client::getLink() -{ - return pfLink->createLink( this ); -} - -void Bu::Client::onMessage( const Bu::String &sMsg ) -{ - if( pProto ) - pProto->onMessage( this, sMsg ); -} - -void Bu::Client::tick() -{ - if( pProto ) - pProto->onTick( this ); -} - -Bu::size Bu::Client::tell() -{ - return 0; -} - -void Bu::Client::seek( Bu::size offset ) -{ - return qbRead.seek( offset ); -} - -void Bu::Client::setPos( Bu::size ) -{ - throw Bu::ExceptionBase(); -} - -void Bu::Client::setPosEnd( Bu::size ) -{ - throw Bu::ExceptionBase(); -} - -bool Bu::Client::isEos() -{ - return true; -} - -void Bu::Client::flush() -{ - processOutput(); -} - -bool Bu::Client::canRead() -{ - return qbRead.getSize() > 0; -} - -bool Bu::Client::canWrite() -{ - return true; -} - -bool Bu::Client::isReadable() -{ - return true; -} - -bool Bu::Client::isWritable() -{ - return true; -} - -bool Bu::Client::isSeekable() -{ - return false; -} - -bool Bu::Client::isBlocking() -{ - return false; -} - -void Bu::Client::setBlocking( bool ) -{ - throw Bu::ExceptionBase(); -} - -void Bu::Client::setSize( Bu::size ) -{ - throw Bu::ExceptionBase(); -} - -Bu::size Bu::Client::getSize() const -{ - return 0; -} - -Bu::size Bu::Client::getBlockSize() const -{ - return pSocket->getBlockSize(); -} - -Bu::String Bu::Client::getLocation() const -{ - return pSocket->getLocation(); -} - diff --git a/src/client.h b/src/client.h deleted file mode 100644 index 119c2c1..0000000 --- a/src/client.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CLIENT_H -#define BU_CLIENT_H - -#include - -#include "bu/config.h" -#include "bu/string.h" -#include "bu/queuebuf.h" - -namespace Bu -{ - class Protocol; - class Stream; - class TcpSocket; - class ClientLinkFactory; - - /** - *@ingroup Serving - */ - class Client : public Bu::Stream - { - public: - Client( Bu::TcpSocket *pSocket, Bu::ClientLinkFactory *pfLink ); - virtual ~Client(); - - void processInput(); - void processOutput(); - - //Bu::String &getInput(); - //Bu::String &getOutput(); - Bu::size write( const Bu::String &sData ); - Bu::size write( const void *pData, Bu::size nBytes ); - Bu::size write( int8_t nData ); - Bu::size write( int16_t nData ); - Bu::size write( int32_t nData ); - Bu::size write( int64_t nData ); - Bu::size write( uint8_t nData ); - Bu::size write( uint16_t nData ); - Bu::size write( uint32_t nData ); - Bu::size write( uint64_t nData ); - Bu::size read( void *pData, Bu::size nBytes ); - Bu::size peek( void *pData, int nBytes, int nOffset=0 ); -// void seek( int nBytes ); - Bu::size getInputSize(); - Bu::size getOutputSize(); - - void setProtocol( Protocol *pProto ); - Bu::Protocol *getProtocol(); - void clearProtocol(); - - bool isOpen(); - void close(); - void tick(); - - const Bu::TcpSocket *getSocket() const; - - void disconnect(); - bool wantsDisconnect(); - - class ClientLink *getLink(); - - void onMessage( const Bu::String &sMsg ); - - bool hasOutput() { return qbWrite.getSize() > 0; } - bool hasInput() { return qbRead.getSize() > 0; } - - template - void pushFilter() - { - filter *pFlt = new filter( *pTopStream ); - pTopStream = pFlt; - lFilts.prepend( pFlt ); - } - - template - void pushFilter( p1t p1 ) - { - filter *pFlt = new filter( *pTopStream, p1 ); - pTopStream = pFlt; - lFilts.prepend( pFlt ); - } - - template - void pushFilter( p1t p1, p2t p2 ) - { - filter *pFlt = new filter( *pTopStream, p1, p2 ); - pTopStream = pFlt; - lFilts.prepend( pFlt ); - } - - /* - * These are required to qualify as a stream, I dunno how many will - * be implemented. - */ - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual void flush(); - virtual bool canRead(); - virtual bool canWrite(); - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( Bu::size iSize ); - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - private: - typedef Bu::List FilterList; - FilterList lFilts; - Bu::Stream *pTopStream; - Bu::TcpSocket *pSocket; - Bu::Protocol *pProto; - Bu::QueueBuf qbRead; - Bu::QueueBuf qbWrite; - bool bWantsDisconnect; - class Bu::ClientLinkFactory *pfLink; - }; -} - -#endif diff --git a/src/clientlink.cpp b/src/clientlink.cpp deleted file mode 100644 index ce8b2cb..0000000 --- a/src/clientlink.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/clientlink.h" - -Bu::ClientLink::ClientLink() -{ -} - -Bu::ClientLink::~ClientLink() -{ -} - diff --git a/src/clientlink.h b/src/clientlink.h deleted file mode 100644 index e4618e7..0000000 --- a/src/clientlink.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CLIENT_LINK_H -#define BU_CLIENT_LINK_H - -#include "bu/string.h" - -namespace Bu -{ - class ClientLink - { - public: - ClientLink(); - virtual ~ClientLink(); - - virtual void sendMessage( const Bu::String &sMsg )=0; - }; -}; - -#endif diff --git a/src/clientlinkfactory.cpp b/src/clientlinkfactory.cpp deleted file mode 100644 index f48e11e..0000000 --- a/src/clientlinkfactory.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/clientlinkfactory.h" - -Bu::ClientLinkFactory::ClientLinkFactory() -{ -} - -Bu::ClientLinkFactory::~ClientLinkFactory() -{ -} - diff --git a/src/clientlinkfactory.h b/src/clientlinkfactory.h deleted file mode 100644 index 21d3363..0000000 --- a/src/clientlinkfactory.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CLIENT_LINK_FACTORY_H -#define BU_CLIENT_LINK_FACTORY_H - -namespace Bu -{ - class Client; - class ClientLink; - - class ClientLinkFactory - { - public: - ClientLinkFactory(); - virtual ~ClientLinkFactory(); - - virtual Bu::ClientLink *createLink( Bu::Client *pClient )=0; - }; -}; - -#endif diff --git a/src/condition.cpp b/src/condition.cpp deleted file mode 100644 index 2f55ce2..0000000 --- a/src/condition.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include - -#include "bu/condition.h" - -Bu::Condition::Condition() -{ - pthread_cond_init( &cond, NULL ); -} - -Bu::Condition::~Condition() -{ - pthread_cond_destroy( &cond ); -} - -int Bu::Condition::wait() -{ - return pthread_cond_wait( &cond, &mutex ); -} - -int Bu::Condition::wait( int nSec, int nUSec ) -{ - struct timeval now; - struct timespec timeout; - struct timezone tz; - - gettimeofday( &now, &tz ); - timeout.tv_sec = now.tv_sec + nSec + ((now.tv_usec + nUSec)/1000000); - timeout.tv_nsec = ((now.tv_usec + nUSec)%1000000)*1000; - - return pthread_cond_timedwait( &cond, &mutex, &timeout ); -} - -int Bu::Condition::signal() -{ - return pthread_cond_signal( &cond ); -} - -int Bu::Condition::broadcast() -{ - return pthread_cond_broadcast( &cond ); -} - diff --git a/src/condition.h b/src/condition.h deleted file mode 100644 index 71634f5..0000000 --- a/src/condition.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CONDITION_H -#define BU_CONDITION_H - -#include - -#include "bu/mutex.h" - -namespace Bu -{ - /** - * Ito condition. This is a fairly simple condition mechanism. As you may - * notice this class inherits from the Mutex class, this is because all - * conditions must be within a locked block. The standard usage of a - * condition is to pause one thread, perhaps indefinately, until another - * thread signals that it is alright to procede. - *
- * Standard usage for the thread that wants to wait is as follows: - *
-	 * Condition cond;
-	 * ... // Perform setup and enter your run loop
-	 * cond.lock();
-	 * while( !isFinished() ) // Could be anything you're waiting for
-	 *     cond.wait();
-	 * ...  // Take care of what you have to.
-	 * cond.unlock();
-	 * 
- * The usage for the triggering thread is much simpler, when it needs to - * tell the others that it's time to grab some data it calls either signal - * or broadcast. See both of those functions for the difference. - *@ingroup Threading - */ - class Condition : public Mutex - { - public: - /** - * Create a condition. - */ - Condition(); - - /** - * Destroy a condition. - */ - ~Condition(); - - /** - * Wait forever, or until signalled. This has to be called from within - * a locked section, i.e. before calling this this object's lock - * function should be called. - */ - int wait(); - - /** - * Wait for a maximum of nSec seconds and nUSec micro-seconds or until - * signalled. This is a little more friendly function if you want to - * perform other operations in the thrad loop that calls this function. - * Like the other wait function, this must be inside a locked section. - *@param nSec The seconds to wait. - *@param nUSec the micro-seconds to wait. - */ - int wait( int nSec, int nUSec ); - - /** - * Notify the next thread waiting on this condition that they can go - * ahead. This only signals one thread, the next one in the condition - * queue, that it is safe to procede with whatever operation was being - * waited on. - */ - int signal(); - - /** - * Notify all threads waiting on this condition that they can go ahead - * now. This function is slower than signal, but more effective in - * certain situations where you may not know how many threads should be - * activated. - */ - int broadcast(); - - private: - pthread_cond_t cond; /**< Internal condition reference. */ - }; -} - -#endif diff --git a/src/conduit.cpp b/src/conduit.cpp deleted file mode 100644 index c9ccdc4..0000000 --- a/src/conduit.cpp +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/conduit.h" - -Bu::Conduit::Conduit( int iBlockSize ) : - qb( iBlockSize ), - bBlocking( true ), - bOpen( true ) -{ -} - -Bu::Conduit::~Conduit() -{ -} - -void Bu::Conduit::close() -{ - im.lock(); -// qb.close(); - bOpen = false; - - cBlock.signal(); - im.unlock(); -} - -#include -Bu::size Bu::Conduit::read( void *pBuf, Bu::size nBytes ) -{ - if( !isOpen() ) - { - return 0; - } - im.lock(); - if( bBlocking ) - { - im.unlock(); - cBlock.lock(); - for(;;) - { - im.lock(); - if( qb.getSize() == 0 && bOpen == false ) - { - im.unlock(); - cBlock.unlock(); - return 0; - } - else if( qb.getSize() > 0 ) - { - im.unlock(); - break; - } - im.unlock(); - - cBlock.wait(); - } - - im.lock(); - Bu::size iRet = qb.read( pBuf, nBytes ); - im.unlock(); - - cBlock.unlock(); - return iRet; - } - else - { - Bu::size iRet = qb.read( pBuf, nBytes ); - im.unlock(); - - return iRet; - } -} - -Bu::size Bu::Conduit::peek( void *pBuf, Bu::size nBytes ) -{ - im.lock(); - Bu::size iRet = qb.peek( pBuf, nBytes ); - im.unlock(); - - return iRet; -} - -Bu::size Bu::Conduit::peek( void *pBuf, Bu::size nBytes, Bu::size nSkip ) -{ - im.lock(); - Bu::size iRet = qb.peek( pBuf, nBytes, nSkip ); - im.unlock(); - - return iRet; -} - -Bu::size Bu::Conduit::write( const void *pBuf, Bu::size nBytes ) -{ - im.lock(); - if( bOpen == false ) - { - im.unlock(); - return 0; - } - Bu::size sRet = qb.write( pBuf, nBytes ); - cBlock.signal(); - im.unlock(); - - return sRet; -} - -Bu::size Bu::Conduit::tell() -{ - im.lock(); - Bu::size sRet = qb.tell(); - im.unlock(); - return sRet; -} - -void Bu::Conduit::seek( Bu::size ) -{ -} - -void Bu::Conduit::setPos( Bu::size ) -{ -} - -void Bu::Conduit::setPosEnd( Bu::size ) -{ -} - -bool Bu::Conduit::isEos() -{ - im.lock(); - bool bRet = qb.isEos(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::isOpen() -{ - im.lock(); - bool bRet = bOpen || (qb.getSize() > 0); - im.unlock(); - return bRet; -} - -void Bu::Conduit::flush() -{ -} - -bool Bu::Conduit::canRead() -{ - im.lock(); - bool bRet = qb.canRead(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::canWrite() -{ - im.lock(); - bool bRet = qb.canWrite(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::isReadable() -{ - im.lock(); - bool bRet = qb.isReadable(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::isWritable() -{ - im.lock(); - bool bRet = qb.isWritable(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::isSeekable() -{ - im.lock(); - bool bRet = qb.isSeekable(); - im.unlock(); - return bRet; -} - -bool Bu::Conduit::isBlocking() -{ - im.lock(); - bool bRet = bBlocking; - im.unlock(); - return bRet; -} - -void Bu::Conduit::setBlocking( bool bBlocking ) -{ - im.lock(); - this->bBlocking = bBlocking; - im.unlock(); -} - -void Bu::Conduit::setSize( Bu::size ) -{ -} - -Bu::size Bu::Conduit::getSize() const -{ - im.lock(); - Bu::size sRet = qb.getSize(); - im.unlock(); - return sRet; -} - -Bu::size Bu::Conduit::getBlockSize() const -{ - im.lock(); - Bu::size sRet = qb.getBlockSize(); - im.unlock(); - return sRet; -} - -Bu::String Bu::Conduit::getLocation() const -{ - im.lock(); - Bu::String sRet = qb.getLocation(); - im.unlock(); - return sRet; -} - diff --git a/src/conduit.h b/src/conduit.h deleted file mode 100644 index 9babaaf..0000000 --- a/src/conduit.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CONDUIT_H -#define BU_CONDUIT_H - -#include "bu/stream.h" -#include "bu/string.h" -#include "bu/queuebuf.h" -#include "bu/mutex.h" -#include "bu/condition.h" - -namespace Bu -{ - /** - * Simple inter-thread communication stream. This acts like a pair of - * pipes for stream communication between any two things, but without the - * use of pipes, making this a bad choice for IPC. - */ - class Conduit : public Stream - { - public: - Conduit( int iBlockSize=256 ); - virtual ~Conduit(); - - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size peek( void *pBuf, Bu::size nBytes ); - virtual Bu::size peek( void *pBuf, Bu::size nBytes, Bu::size nSkip ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - virtual void flush(); - virtual bool canRead(); - virtual bool canWrite(); - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( Bu::size iSize ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - private: - QueueBuf qb; - mutable Mutex im; - Condition cBlock; - bool bBlocking; - bool bOpen; - }; -} - -#endif diff --git a/src/crypt.cpp b/src/crypt.cpp deleted file mode 100644 index eb87479..0000000 --- a/src/crypt.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/crypt.h" -#include "bu/md5.h" -#include "bu/base64.h" -#include "bu/membuf.h" -#include "bu/file.h" - -Bu::String Bu::cryptPass( const Bu::String &sPass, const Bu::String &sSalt ) -{ - Bu::Md5 md5; - Bu::MemBuf mbOut; - Bu::Base64 b64Out( mbOut ); - - Bu::String::const_iterator i = sSalt.find('$'); - Bu::String sSaltSml = sSalt.getSubStr( sSalt.begin(), i ); - - md5.addData( sPass ); - md5.addData( sSaltSml ); - md5.writeResult( b64Out ); - - b64Out.stop(); - - return sSaltSml + "$" + mbOut.getString(); -} - -Bu::String Bu::cryptPass( const Bu::String &sPass ) -{ - Bu::MemBuf mbSalt; - Bu::Base64 b64Salt( mbSalt ); - Bu::File fRand("/dev/urandom", Bu::File::Read ); - -#define STR 6 - char buf[STR]; - fRand.read( buf, STR ); - b64Salt.write( buf, STR ); - - b64Salt.stop(); - - return cryptPass( sPass, mbSalt.getString() ); -} - diff --git a/src/crypt.h b/src/crypt.h deleted file mode 100644 index a94402a..0000000 --- a/src/crypt.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CRYPT_H -#define BU_CRYPT_H - -#include "bu/string.h" - -namespace Bu -{ - String cryptPass( const Bu::String &sPass, const Bu::String &sSalt ); - String cryptPass( const Bu::String &sPass ); -}; - -#endif diff --git a/src/cryptohash.cpp b/src/cryptohash.cpp deleted file mode 100644 index ddd293c..0000000 --- a/src/cryptohash.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/cryptohash.h" - -Bu::CryptoHash::CryptoHash() -{ -} - -Bu::CryptoHash::~CryptoHash() -{ -} - -void Bu::CryptoHash::addData( const Bu::String &sData ) -{ - addData( sData.getStr(), sData.getSize() ); -} - -Bu::String Bu::CryptoHash::getHexResult() -{ - Bu::String sResult = getResult(); - Bu::String sRet( 2*sResult.getSize() ); - static const char hex_tab[] = {"0123456789abcdef"}; - - int k = 0; - for( int i = 0; i < sResult.getSize(); i++ ) - { - sRet[k++] = hex_tab[(((unsigned char)sResult[i])>>4) & 0xF]; - sRet[k++] = hex_tab[((unsigned char)sResult[i]) & 0xF]; - } - - return sRet; -} - diff --git a/src/cryptohash.h b/src/cryptohash.h deleted file mode 100644 index bc5435f..0000000 --- a/src/cryptohash.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CRYPTO_HASH_H -#define BU_CRYPTO_HASH_H - -#include "bu/string.h" - -namespace Bu -{ - class Stream; - - class CryptoHash - { - public: - CryptoHash(); - virtual ~CryptoHash(); - - virtual void reset() = 0; - virtual void setSalt( const Bu::String &sSalt ) = 0; - virtual void addData( const void *sData, int iSize ) = 0; - virtual void addData( const Bu::String &sData ); - virtual String getResult() = 0; - virtual void writeResult( Stream &sOut ) = 0; - virtual Bu::String getHexResult(); - }; -}; - -#endif diff --git a/src/csvreader.cpp b/src/csvreader.cpp deleted file mode 100644 index 4da7883..0000000 --- a/src/csvreader.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/csvreader.h" -#include "bu/stream.h" - -#include "bu/sio.h" -using namespace Bu; - -Bu::CsvReader::CsvReader( Bu::Stream &sIn, Bu::CsvReader::Style eStyle ) : - sIn( sIn ) -{ - switch( eStyle ) - { - case styleExcel: - sDecode = Bu::slot( &decodeExcel ); - break; - - case styleC: - sDecode = Bu::slot( &decodeC ); - break; - } -} - -Bu::CsvReader::CsvReader( Bu::Stream &sIn, - Bu::CsvReader::DecodeSignal sDecode ) : - sIn( sIn ), - sDecode( sDecode ) -{ -} - -Bu::CsvReader::~CsvReader() -{ -} - -Bu::StrArray Bu::CsvReader::readLine() -{ - Bu::StrArray aVals; - - Bu::String sLine = sIn.readLine(); - - if( !sLine.isSet() ) - return Bu::StrArray(); - - Bu::String::iterator i = sLine.begin(); - - aVals.append( sDecode( i ) ); - - while( i ) - { - if( *i == ',' ) - { - i++; - if( !i ) - { - aVals.append(""); - break; - } - aVals.append( sDecode( i ) ); - } - else - { - // Blanks and stuff? - sio << "Out of bound: '" << *i << "'" << sio.nl; - i++; - } - } - - return aVals; -} - -Bu::String Bu::CsvReader::decodeExcel( Bu::String::iterator &i ) -{ - Bu::String sRet; - - for(; i && (*i == ' ' || *i == '\t'); i++ ) { } - - if( !i ) - return sRet; - - if( *i == '\"' ) - { - for( i++ ; i; i++ ) - { - if( *i == '\"' ) - { - i++; - if( !i ) - { - return sRet; - } - else if( *i == '\"' ) - { - sRet += *i; - } - else - { - return sRet; - } - } - else - { - sRet += *i; - } - } - } - else - { - for( ; i; i++ ) - { - if( *i == ',' ) - { - return sRet; - } - sRet += *i; - } - } - - return sRet; -} - -Bu::String Bu::CsvReader::decodeC( Bu::String::iterator & ) -{ - return ""; -} - diff --git a/src/csvreader.h b/src/csvreader.h deleted file mode 100644 index 2e9e7b0..0000000 --- a/src/csvreader.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CSV_READER_H -#define BU_CSV_READER_H - -#include "bu/string.h" -#include "bu/array.h" -#include "bu/signals.h" - -namespace Bu -{ - class Stream; - typedef Bu::Array StrArray; - - class CsvReader - { - public: - typedef Bu::Signal1 DecodeSignal; - enum Style - { - styleExcel, ///< Excel style quotes around things that need em - styleC ///< Escape things that need it C-style - }; - - CsvReader( Stream &sIn, Style eStyle=styleExcel ); - CsvReader( Stream &sIn, DecodeSignal sDecode ); - virtual ~CsvReader(); - - StrArray readLine(); - - private: - Stream &sIn; - DecodeSignal sDecode; - - static Bu::String decodeExcel( Bu::String::iterator &i ); - static Bu::String decodeC( Bu::String::iterator &i ); - }; -}; - -#endif diff --git a/src/csvwriter.cpp b/src/csvwriter.cpp deleted file mode 100644 index d8910aa..0000000 --- a/src/csvwriter.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/csvwriter.h" -#include "bu/stream.h" - -Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, Bu::CsvWriter::Style eStyle ) : - sOut( sOut ) -{ - switch( eStyle ) - { - case styleExcel: - sEncode = Bu::slot( &encodeExcel ); - break; - - case styleC: - sEncode = Bu::slot( &encodeExcel ); - break; - } -} - -Bu::CsvWriter::CsvWriter( Bu::Stream &sOut, - Bu::CsvWriter::EncodeSignal sEncode ) : - sOut( sOut ), - sEncode( sEncode ) -{ -} - -Bu::CsvWriter::~CsvWriter() -{ -} - -void Bu::CsvWriter::writeLine( const StrArray &aStrs ) -{ - Bu::String sBuf; - for( StrArray::const_iterator i = aStrs.begin(); i; i++ ) - { - if( i != aStrs.begin() ) - sBuf += ","; - sBuf += sEncode( *i ); - } - sBuf += "\n"; - - sOut.write( sBuf ); -} - -Bu::String Bu::CsvWriter::encodeExcel( const Bu::String &sIn ) -{ - if( sIn.find('\"') || sIn.find(',') ) - { - Bu::String sOut = "\""; - for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) - { - if( *i == '\"' ) - sOut += "\"\""; - else - sOut += *i; - } - sOut += '\"'; - return sOut; - } - return sIn; -} - -Bu::String Bu::CsvWriter::encodeC( const Bu::String &sIn ) -{ - Bu::String sOut = ""; - for( Bu::String::const_iterator i = sIn.begin(); i; i++ ) - { - if( *i == ',' ) - sOut += "\\,"; - else - sOut += *i; - } - return sOut; -} - diff --git a/src/csvwriter.h b/src/csvwriter.h deleted file mode 100644 index 4291ed5..0000000 --- a/src/csvwriter.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_CSV_WRITER_H -#define BU_CSV_WRITER_H - -#include "bu/string.h" -#include "bu/array.h" -#include "bu/signals.h" - -namespace Bu -{ - class Stream; - typedef Bu::Array StrArray; - - class CsvWriter - { - public: - typedef Bu::Signal1 EncodeSignal; - enum Style - { - styleExcel, ///< Excel style quotes around things that need em - styleC ///< Escape things that need it C-style - }; - - CsvWriter( Stream &sOut, Style eStyle=styleExcel ); - CsvWriter( Stream &sOut, EncodeSignal sEncode ); - virtual ~CsvWriter(); - - void writeLine( const StrArray &aStrs ); - - private: - Stream &sOut; - EncodeSignal sEncode; - - static Bu::String encodeExcel( const Bu::String &sIn ); - static Bu::String encodeC( const Bu::String &sIn ); - }; -}; - -#endif diff --git a/src/deflate.cpp b/src/deflate.cpp deleted file mode 100644 index 704d172..0000000 --- a/src/deflate.cpp +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/deflate.h" -#include "bu/trace.h" - -#include - -#define pState ((z_stream *)prState) - -using namespace Bu; - -Bu::Deflate::Deflate( Bu::Stream &rNext, int nCompression, Format eFmt ) : - Bu::Filter( rNext ), - prState( NULL ), - nCompression( nCompression ), - sTotalOut( 0 ), - eFmt( eFmt ), - bEos( false ) -{ - TRACE( nCompression ); - start(); -} - -Bu::Deflate::~Deflate() -{ - TRACE(); - stop(); -} - -void Bu::Deflate::start() -{ - TRACE(); - prState = new z_stream; - pState->zalloc = NULL; - pState->zfree = NULL; - pState->opaque = NULL; - pState->state = NULL; - - nBufSize = 64*1024; - pBuf = new char[nBufSize]; -} - -Bu::size Bu::Deflate::stop() -{ - TRACE(); - if( pState && pState->state ) - { - if( bReading ) - { - inflateEnd( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return 0; - } - else - { - for(;;) - { - pState->next_in = NULL; - pState->avail_in = 0; - pState->avail_out = nBufSize; - pState->next_out = (Bytef *)pBuf; - int res = deflate( pState, Z_FINISH ); - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( res == Z_STREAM_END ) - break; - } - deflateEnd( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return sTotalOut; - } - } - return 0; -} - -void Bu::Deflate::zError( int code ) -{ - TRACE( code ); - switch( code ) - { - case Z_OK: - case Z_STREAM_END: - case Z_NEED_DICT: - return; - - case Z_ERRNO: - throw ExceptionBase("Deflate: Errno - %s", pState->msg ); - - case Z_STREAM_ERROR: - throw ExceptionBase("Deflate: Stream Error - %s", pState->msg ); - - case Z_DATA_ERROR: - throw ExceptionBase("Deflate: Data Error - %s", pState->msg ); - - case Z_MEM_ERROR: - throw ExceptionBase("Deflate: Mem Error - %s", pState->msg ); - - case Z_BUF_ERROR: - throw ExceptionBase("Deflate: Buf Error - %s", pState->msg ); - - case Z_VERSION_ERROR: - throw ExceptionBase("Deflate: Version Error - %s", pState->msg ); - - default: - throw ExceptionBase("Deflate: Unknown error encountered - %s.", pState->msg ); - - } -} - -Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( nBytes <= 0 ) - return 0; - if( !pState->state ) - { - bReading = true; - if( eFmt&AutoDetect ) - inflateInit2( pState, 32+15 ); // Auto-detect, large window - else if( eFmt == Raw ) - inflateInit2( pState, -15 ); // Raw - else if( eFmt == Zlib ) - inflateInit2( pState, 15 ); // Zlib - else if( eFmt == Gzip ) - inflateInit2( pState, 16+15 ); // GZip - else - throw Bu::ExceptionBase("Format mode for deflate read."); - pState->next_in = (Bytef *)pBuf; - pState->avail_in = 0; - } - if( bReading == false ) - throw ExceptionBase("This deflate filter is in writing mode, you can't read."); - - int nRead = 0; - int nReadTotal = pState->total_out; - pState->next_out = (Bytef *)pData; - pState->avail_out = nBytes; - for(;;) - { - int ret = inflate( pState, Z_NO_FLUSH ); - nReadTotal += nRead-pState->avail_out; - - if( ret == Z_STREAM_END ) - { - bEos = true; - if( pState->avail_in > 0 ) - { - if( rNext.isSeekable() ) - { - rNext.seek( -pState->avail_in ); - } - } - return nBytes-pState->avail_out; - } - if( ret != Z_BUF_ERROR ) - zError( ret ); - - if( pState->avail_out ) - { - if( pState->avail_in == 0 ) - { - nRead = rNext.read( pBuf, nBufSize ); - if( nRead == 0 && rNext.isEos() ) - { - throw Bu::ExceptionBase("Premature end of underlying " - "stream found reading deflate stream."); - } - pState->next_in = (Bytef *)pBuf; - pState->avail_in = nRead; - } - } - else - { - return nBytes-pState->avail_out; - } - } - return 0; -} - -Bu::size Bu::Deflate::write( const void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( nBytes <= 0 ) - return 0; - if( !pState->state ) - { - bReading = false; - int iFmt = eFmt&Gzip; - if( iFmt == Raw ) - deflateInit2( pState, nCompression, Z_DEFLATED, -15, 9, - Z_DEFAULT_STRATEGY ); - else if( iFmt == Zlib ) - deflateInit2( pState, nCompression, Z_DEFLATED, 15, 9, - Z_DEFAULT_STRATEGY ); - else if( iFmt == Gzip ) - deflateInit2( pState, nCompression, Z_DEFLATED, 16+15, 9, - Z_DEFAULT_STRATEGY ); - else - throw Bu::ExceptionBase("Invalid format for deflate."); - } - if( bReading == true ) - throw ExceptionBase("This deflate filter is in reading mode, you can't write."); - - pState->next_in = (Bytef *)pData; - pState->avail_in = nBytes; - for(;;) - { - pState->avail_out = nBufSize; - pState->next_out = (Bytef *)pBuf; - - zError( deflate( pState, Z_NO_FLUSH ) ); - - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( pState->avail_in == 0 ) - break; - } - - return nBytes; -} - -bool Bu::Deflate::isOpen() -{ - TRACE(); - return (pState != NULL && pState->state != NULL); -} - -bool Bu::Deflate::isEos() -{ - TRACE(); - return bEos; -} - -Bu::size Bu::Deflate::getCompressedSize() -{ - return sTotalOut; -} - diff --git a/src/deflate.h b/src/deflate.h deleted file mode 100644 index f835cfc..0000000 --- a/src/deflate.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_DEFLATE_H -#define BU_DEFLATE_H - -#include - -#include "bu/filter.h" - -namespace Bu -{ - /** - * Provides Deflate (LZ77) support via zlib. This provides zlib, raw, and - * gzip stream types. By default it will autodetect the input type and - * encode into a raw deflate stream. - * - *@ingroup Streams - *@ingroup Compression - */ - class Deflate : public Bu::Filter - { - public: - enum Format - { - Raw = 0x01, - Zlib = 0x02, - Gzip = 0x03, - AutoDetect = 0x04, - - AutoRaw = 0x04|0x01, - AutoZlib = 0x04|0x02, - AutoGzip = 0x04|0x03 - }; - - Deflate( Bu::Stream &rNext, int nCompression=-1, Format eFmt=AutoZlib ); - virtual ~Deflate(); - - virtual void start(); - virtual Bu::size stop(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - - virtual bool isOpen(); - virtual bool isEos(); - - Bu::size getCompressedSize(); - - private: - void zError( int code ); - void *prState; - bool bReading; - int nCompression; - char *pBuf; - uint32_t nBufSize; - Bu::size sTotalOut; - Format eFmt; - bool bEos; - }; -} - -#endif diff --git a/src/exceptionbase.cpp b/src/exceptionbase.cpp deleted file mode 100644 index 13a98db..0000000 --- a/src/exceptionbase.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/exceptionbase.h" -#include -#include -#include - -Bu::ExceptionBase::ExceptionBase( const char *lpFormat, ... ) throw() : - nErrorCode( 0 ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -Bu::ExceptionBase::ExceptionBase( int nCode, const char *lpFormat, ... ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ - va_list ap; - - va_start(ap, lpFormat); - setWhat( lpFormat, ap ); - va_end(ap); -} - -Bu::ExceptionBase::ExceptionBase( int nCode ) throw() : - nErrorCode( nCode ), - sWhat( NULL ) -{ -} - -Bu::ExceptionBase::ExceptionBase( const ExceptionBase &e ) throw () : - std::exception( e ), - nErrorCode( e.nErrorCode ), - sWhat( NULL ) -{ - setWhat( e.sWhat ); -} - -Bu::ExceptionBase::~ExceptionBase() throw() -{ - delete[] sWhat; - sWhat = NULL; -} - -void Bu::ExceptionBase::setWhat( const char *lpFormat, va_list &vargs ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - va_list vargs2; - va_copy( vargs2, vargs ); - nSize = vsnprintf( NULL, 0, lpFormat, vargs2 ); - va_end( vargs2 ); - sWhat = new char[nSize+1]; - vsnprintf( sWhat, nSize+1, lpFormat, vargs ); -} - -void Bu::ExceptionBase::setWhat( const char *lpText ) -{ - if( sWhat ) delete[] sWhat; - int nSize; - - nSize = strlen( lpText ); - sWhat = new char[nSize+1]; - strcpy( sWhat, lpText ); -} - -const char *Bu::ExceptionBase::what() const throw() -{ - return sWhat; -} - -int Bu::ExceptionBase::getErrorCode() -{ - return nErrorCode; -} - -Bu::UnsupportedException::UnsupportedException() throw() : - ExceptionBase( 0 ) -{ - setWhat("An unsupperted operation was attempted."); -} - diff --git a/src/exceptionbase.h b/src/exceptionbase.h deleted file mode 100644 index b6ad9ca..0000000 --- a/src/exceptionbase.h +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_EXCEPTION_BASE_H -#define BU_EXCEPTION_BASE_H - -#include -#include - -// This shouldn't normally be defined here, I don't think it's all that portable -// and it also changes the class interface, we should find out how much of -// an issue that is, we could just put in an empty getBacktrace() function for -// when you don't have support for it... - -namespace Bu -{ - /** - * A generalized Exception base class. This is nice for making general and - * flexible child classes that can create new error code classes. - * - * In order to create your own exception class use these two lines. - * - * in your header: subExceptionDecl( NewClassName ); - * - * in your source: subExcpetienDef( NewClassName ); - */ - class ExceptionBase : public std::exception - { - public: - /** - * Construct an exception with an error code of zero, but with a - * description. The use of this is not reccomended most of the time, - * it's generally best to include an error code with the exception so - * your program can handle the exception in a better way. - * @param sFormat The format of the text. See printf for more info. - */ - ExceptionBase( const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @param sFormat - */ - ExceptionBase( int nCode, const char *sFormat, ... ) throw(); - - /** - * - * @param nCode - * @return - */ - ExceptionBase( int nCode=0 ) throw(); - - ExceptionBase( const ExceptionBase &e ) throw (); - - /** - * - * @return - */ - virtual ~ExceptionBase() throw(); - - /** - * - * @return - */ - virtual const char *what() const throw(); - - /** - * - * @return - */ - int getErrorCode(); - - /** - * - * @param lpFormat - * @param vargs - */ - void setWhat( const char *lpFormat, va_list &vargs ); - - /** - * - * @param lpText - */ - void setWhat( const char *lpText ); - - private: - int nErrorCode; /**< The code for the error that occured. */ - char *sWhat; /**< The text string telling people what went wrong. */ - }; -} - -#define subExceptionDecl( name ) \ -class name : public Bu::ExceptionBase \ -{ \ - public: \ - name( const char *sFormat, ... ) throw (); \ - name( int nCode, const char *sFormat, ... ) throw(); \ - name( int nCode=0 ) throw (); \ - name( const name &e ) throw (); \ -}; - -#define subExceptionDeclChild( name, parent ) \ -class name : public parent \ -{ \ - public: \ - name( const char *sFormat, ... ) throw (); \ - name( int nCode, const char *sFormat, ... ) throw(); \ - name( int nCode=0 ) throw (); \ - name( const name &e ) throw (); \ -}; - -#define subExceptionDeclBegin( name ) \ -class name : public Bu::ExceptionBase \ -{ \ - public: \ - name( const char *sFormat, ... ) throw (); \ - name( int nCode, const char *sFormat, ... ) throw(); \ - name( int nCode=0 ) throw (); \ - name( const name &e ) throw (); - -#define subExceptionDeclEnd() \ -}; - -#define subExceptionDef( name ) \ -name::name( const char *lpFormat, ... ) throw() : \ - ExceptionBase( 0 ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode, const char *lpFormat, ... ) throw() : \ - ExceptionBase( nCode ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode ) throw() : \ - ExceptionBase( nCode ) \ -{ \ -} \ -name::name( const name &e ) throw() : \ - ExceptionBase( e ) \ -{ \ -} - -#define subExceptionDefChild( name, parent ) \ -name::name( const char *lpFormat, ... ) throw() : \ - parent( 0 ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode, const char *lpFormat, ... ) throw() : \ - parent( nCode ) \ -{ \ - va_list ap; \ - va_start( ap, lpFormat ); \ - setWhat( lpFormat, ap ); \ - va_end( ap ); \ -} \ -name::name( int nCode ) throw() : \ - parent( nCode ) \ -{ \ -} \ -name::name( const name &e ) throw() : \ - ExceptionBase( e ) \ -{ \ -} - -namespace Bu -{ - // Exceptions that are so general they could be used anywhere go here. - class UnsupportedException : public Bu::ExceptionBase - { - public: - UnsupportedException() throw (); - }; -} - -#endif diff --git a/src/experimental/cache.cpp b/src/experimental/cache.cpp new file mode 100644 index 0000000..52bd9fc --- /dev/null +++ b/src/experimental/cache.cpp @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/cache.h" diff --git a/src/experimental/cache.h b/src/experimental/cache.h new file mode 100644 index 0000000..455cf1b --- /dev/null +++ b/src/experimental/cache.h @@ -0,0 +1,437 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_H +#define BU_CACHE_H + +// #include "bu/cptr.h" +#include "bu/hash.h" +#include "bu/list.h" +#include "bu/cachestore.h" +#include "bu/cachecalc.h" + +#include "bu/trace.h" + +namespace Bu +{ +// template +// keytype __cacheGetKey( obtype *&pObj ); + template + keytype __cacheGetKey( const obtype *pObj ) + { + return pObj->getKey(); + } + + template + class Cache + { + public: + /** + * Cache Pointer - Provides access to data that is held within the + * cache. This provides safe, refcounting access to data stored in + * the cache, with support for lazy loading. + */ + class Ptr + { + friend class Bu::Cache; + private: + Ptr( Cache *pCache, obtype *pData, + const keytype &kId ) : + pCache( pCache ), + pData( pData ), + kId( kId ) + { + if( pCache ) + pCache->incRef( kId ); + } + + Ptr( Cache *pCache, const keytype &kId ) : + pCache( pCache ), + pData( NULL ), + kId( kId ) + { + } + + public: + Ptr( const Ptr &rSrc ) : + pCache( rSrc.pCache ), + pData( rSrc.pData ), + kId( rSrc.kId ) + { + if( pCache && pData ) + pCache->incRef( kId ); + } + + Ptr() : + pCache( 0 ), + pData( 0 ) + { + } + + virtual ~Ptr() + { + if( pCache && pData ) + pCache->decRef( kId ); + } + + obtype &operator*() + { + checkPtr(); + return *pData; + } + + const obtype &operator*() const + { + checkPtr(); + return *pData; + } + + obtype *operator->() + { + checkPtr(); + return pData; + } + + const obtype *operator->() const + { + checkPtr(); + return pData; + } + + bool isValid() const + { + return pCache != NULL; + } + + bool isBound() const + { + return pData != NULL; + } + + bool isSet() const + { + return pCache != NULL; + } + + const keytype &getKey() const + { + return kId; + } + + void unbind() + { + if( pCache && pData ) + pCache->decRef( kId ); + pData = NULL; + } + + void clear() + { + unbind(); + pCache = NULL; + } + + void unset() + { + clear(); + } + + Ptr &operator=( const Ptr &rRhs ) + { + if( pCache && pData ) + pCache->decRef( kId ); + pCache = rRhs.pCache; + pData = rRhs.pData; + kId = rRhs.kId; + if( pCache && pData ) + pCache->incRef( kId ); + return *this; + } + + bool operator==( const Ptr &rRhs ) const + { + return pCache == rRhs.pCache && kId == rRhs.kId; + } + + bool operator!=( const Ptr &rRhs ) const + { + return pCache != rRhs.pCache || kId != rRhs.kId; + } + + private: + void checkPtr() const + { + if( pCache && !pData ) + { + pData = pCache->getRaw( kId ); + pCache->incRef( kId ); + } + } + + private: + Bu::Cache *pCache; + mutable obtype *pData; + mutable keytype kId; + }; + + private: + typedef Bu::CacheStore Store; + typedef Bu::List StoreList; + typedef Bu::CacheCalc Calc; + + typedef struct CacheEntry + { + obtype *pData; + int iRefs; + time_t tLastSync; + } CacheEntry; + + typedef Bu::Hash CidHash; + + public: + typedef keytype Key; + Cache( Calc *pCalc, Store *pStore ) : + pCalc( pCalc ), + pStore( pStore ) + { + TRACE(); + pCalc->setCache( this ); + } + + virtual ~Cache() + { + TRACE(); + + // Better safe than sorry, better try a sync before anything + // else happens. + sync(); + + // Cycle through and unload all objects from the system. + for( typename CidHash::iterator i = hEnt.begin(); + i != hEnt.end(); i++ ) + { + if( i.getValue().iRefs > 0 ) + { + // TODO: Throw an error in this case? iRefs != 0 for an + // object when the Cache is destroyed. + throw Bu::ExceptionBase("iRefs not zero."); + } + pStore->unload( + i.getValue().pData, + i.getKey() + ); + } + delete pCalc; + delete pStore; + } + + Ptr insert( obtype *pData ) + { + TRACE( pData ); + if( pStore->has( __cacheGetKey( pData ) ) ) + throw Bu::ExceptionBase("Key already exists in cache."); + CacheEntry e = {pData, 0, 0}; + keytype k = pStore->create( pData ); + hEnt.insert( k, e ); + + pCalc->onLoad( pData, k ); + + pStore->sync(); + + return Ptr( this, pData, k ); + } + + bool has( const keytype &cId ) + { + return hEnt.has( cId ) || pStore->has( cId ); + } + + /** + * Retrieve an object from the cache and return a pointer to it. + * The object returned may be loaded from backend storage if needed, + * or the currently live object will be returned. + *@param cId The id of the object to load. + *@returns A pointer to the object. + */ + Ptr get( const keytype &cId ) + { + TRACE( cId ); + try { + return Ptr( this, hEnt.get( cId ).pData, cId ); + } + catch( Bu::HashException &e ) { + CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; + pCalc->onLoad( e.pData, cId ); + hEnt.insert( cId, e ); + return Ptr( this, e.pData, cId ); + } + } + + /** + * Retrieve a handle to an object without loading it now. This function + * will return a pointer that has not yet been "realized" but can be + * used normally. Upon initial use in any way the object will be + * loaded from the cache, either linking against the already loaded + * object or loading it fresh from the backend storage. The advantage + * of this is that you recieve a usable handle to the data, but it + * does not count as a reference yet, meaning that the data is loaded + * when you need it, not before. + */ + Ptr getLazy( const keytype &cId ) + { + TRACE( cId ); + return Ptr( this, cId ); + } + + int getRefCount( const keytype &cId ) + { + TRACE( cId ); + return hEnt.get( cId ).iRefs; + } + + void unload( const keytype &cId ) + { + TRACE( cId ); + try { + if( hEnt.get( cId ).iRefs > 0 ) + { + printf("Shouldn't unload, references still exist!\n"); + return; + } + } + catch( Bu::HashException &e ) { + // It's not here? Eh, return. + return; + } + obtype *pObj = hEnt.get( cId ).pData; + pCalc->onUnload( pObj, cId ); + hEnt.erase( cId ); + + // The unload has to happen last just in case cId is a reference + // to data that is about to be deleted from memory by the unload. + pStore->unload( pObj, cId ); + } + + void erase( const keytype &cId ) + { + TRACE( cId ); + try { + if( hEnt.get( cId ).iRefs > 0 ) + { + printf("Shouldn't erase, references still exist!\n"); + return; + } + + obtype *pObj = hEnt.get( cId ).pData; + pCalc->onDestroy( pObj, cId ); + hEnt.erase( cId ); + + pStore->destroy( pObj, cId ); + pStore->sync(); + } + catch( Bu::HashException &e ) { + pCalc->onDestroy( cId ); + + if( hEnt.has( cId ) ) + { + // The object was loaded by onDestroy + erase( cId ); + } + else + { + pStore->destroy( cId ); + pStore->sync(); + } + } + } + + typedef Bu::List KeyList; + KeyList getKeys() + { + return pStore->getKeys(); + } + + KeyList getActiveKeys() + { + return hEnt.getKeys(); + } + + int getSize() + { + return pStore->getSize(); + } + + /** + * Make sure all currently loaded but not-in-use objects are synced to + * the store. + */ + void sync() + { + TRACE(); + int iSynced = 0; + for( typename CidHash::iterator i = hEnt.begin(); + i != hEnt.end(); i++ ) + { + if( i.getValue().iRefs == 0 ) + { + if( pCalc->shouldSync( + i.getValue().pData, + i.getKey(), + i.getValue().tLastSync + ) ) + { + pStore->sync( + i.getValue().pData, + i.getKey() + ); + iSynced++; + i.getValue().tLastSync = time( NULL ); + } + } + } + if( iSynced > 0 ) + { + pStore->sync(); + } + } + + private: + void incRef( const keytype &cId ) + { + TRACE( cId ); + hEnt.get( cId ).iRefs++; + } + + void decRef( const keytype &cId ) + { + TRACE( cId ); + CacheEntry &e = hEnt.get( cId ); + e.iRefs--; + } + + obtype *getRaw( const keytype &cId ) + { + TRACE( cId ); + try { + return hEnt.get( cId ).pData; + } + catch( Bu::HashException &e ) { + CacheEntry e = {pStore->load( cId ), 0, time( NULL )}; + pCalc->onLoad( e.pData, cId ); + hEnt.insert( cId, e ); + return e.pData; + } + } + + private: + CidHash hEnt; + Calc *pCalc; + Store *pStore; + }; +}; + +#endif diff --git a/src/experimental/cachecalc.cpp b/src/experimental/cachecalc.cpp new file mode 100644 index 0000000..7b8a10a --- /dev/null +++ b/src/experimental/cachecalc.cpp @@ -0,0 +1,8 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/cachecalc.h" diff --git a/src/experimental/cachecalc.h b/src/experimental/cachecalc.h new file mode 100644 index 0000000..89cfadc --- /dev/null +++ b/src/experimental/cachecalc.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_CALC_H +#define BU_CACHE_CALC_H + +#include "bu/trace.h" + +#include + +namespace Bu +{ + template class Cache; + + template + class CacheCalc + { + friend class Cache; + private: + typedef Cache MyCache; + public: + CacheCalc() : + pCache( (MyCache *)0 ) + { + TRACE(); + } + + virtual ~CacheCalc() + { + TRACE(); + } + + virtual void onLoad( obtype *pSrc, const keytype &key )=0; + virtual void onUnload( obtype *pSrc, const keytype &key )=0; + virtual void onDestroy( obtype *pSrc, const keytype &key )=0; + virtual void onDestroy( const keytype &key )=0; + virtual bool shouldSync( obtype *pSrc, const keytype &key, + time_t tLastSync )=0; + virtual void onTick() { }; + + protected: + MyCache *getCache() + { + TRACE(); + return pCache; + } + + private: + void setCache( MyCache *pCache ) + { + TRACE(); + this->pCache = pCache; + } + + MyCache *pCache; + }; +}; + +#endif diff --git a/src/experimental/cachestore.cpp b/src/experimental/cachestore.cpp new file mode 100644 index 0000000..af49548 --- /dev/null +++ b/src/experimental/cachestore.cpp @@ -0,0 +1,9 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/cachestore.h" + diff --git a/src/experimental/cachestore.h b/src/experimental/cachestore.h new file mode 100644 index 0000000..d0d91a2 --- /dev/null +++ b/src/experimental/cachestore.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_STORE_H +#define BU_CACHE_STORE_H + +#include "bu/list.h" + +namespace Bu +{ + /** + * Handles I/O for data in the cache. This also assigns ID's to the newly + * created objects that are requested through this system. + */ + template + class CacheStore + { + public: + CacheStore() + { + } + + virtual ~CacheStore() + { + } + + virtual obtype *load( const keytype &key )=0; + virtual void unload( obtype *pObj, const keytype &key )=0; + virtual keytype create( obtype *pSrc )=0; + virtual void sync()=0; + virtual void sync( obtype *pObj, const keytype &key )=0; + virtual void destroy( obtype *pObj, const keytype &key )=0; + virtual void destroy( const keytype &key )=0; + virtual bool has( const keytype &key )=0; + virtual Bu::List getKeys() { return Bu::List(); } + virtual int getSize() { return -1; } + + private: + }; +}; + +#endif diff --git a/src/experimental/cachestorefiles.cpp b/src/experimental/cachestorefiles.cpp new file mode 100644 index 0000000..66ce672 --- /dev/null +++ b/src/experimental/cachestorefiles.cpp @@ -0,0 +1,9 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/cachestorefiles.h" + diff --git a/src/experimental/cachestorefiles.h b/src/experimental/cachestorefiles.h new file mode 100644 index 0000000..10b2c1b --- /dev/null +++ b/src/experimental/cachestorefiles.h @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_STORE_FILES_H +#define BU_CACHE_STORE_FILES_H + +#include "bu/string.h" +#include "bu/file.h" +#include "bu/cachestore.h" +#include "bu/archive.h" +#include "bu/membuf.h" +#include "bu/formatter.h" +#include "bu/sio.h" + +#include +#include +#include +#include + +namespace Bu +{ + template + keytype __cacheGetKey( const obtype *pObj ); + + template + obtype *__cacheStoreFilesAlloc( const keytype &key ) + { + return new obtype(); + } + + template + void __cacheStoreFilesStore( Bu::Stream &s, obtype &rObj, + const keytype & ) + { + Bu::Archive ar( s, Bu::Archive::save ); + ar << rObj; + } + + template + obtype *__cacheStoreFilesLoad( Bu::Stream &s, const keytype &key ) + { + obtype *pObj = __cacheStoreFilesAlloc( key ); + Bu::Archive ar( s, Bu::Archive::load ); + ar >> (*pObj); + return pObj; + } + + template + class CacheStoreFiles : public CacheStore + { + public: + CacheStoreFiles( const Bu::String &sPrefix ) : + sPrefix( sPrefix ) + { + if( access( sPrefix.getStr(), W_OK|R_OK|X_OK ) ) + { + mkdir( sPrefix.getStr(), 0755 ); + } + } + + virtual ~CacheStoreFiles() + { + } + + virtual obtype *load( const keytype &key ) + { +// try +// { + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/" << key; + Bu::File fIn( mb.getString(), Bu::File::Read ); + obtype *pOb = __cacheStoreFilesLoad( fIn, key ); + return pOb; +// } +// catch( std::exception &e ) +// { +// throw Bu::HashException( e.what() ); +// } + } + + virtual void unload( obtype *pObj, const keytype & ) + { + delete pObj; + } + + virtual keytype create( obtype *pSrc ) + { + keytype key = __cacheGetKey( pSrc ); + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/" << key; + + Bu::File fTouch( mb.getString(), Bu::File::WriteNew ); + + return key; + } + + virtual void sync() + { + } + + virtual void sync( obtype *pSrc, const keytype &key ) + { + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/" << key; + + Bu::File fOut( mb.getString(), Bu::File::WriteNew ); + __cacheStoreFilesStore( fOut, *pSrc, key ); + } + + virtual void destroy( obtype *pObj, const keytype &key ) + { + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/" << key; + + unlink( mb.getString().getStr() ); + delete pObj; + } + + virtual void destroy( const keytype &key ) + { + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/" << key; + + unlink( mb.getString().getStr() ); + } + + virtual bool has( const keytype &key ) + { + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << sPrefix << "/"; + Bu::String sBase = mb.getString(); + f << key; + + if( sBase == mb.getString() ) + return false; + + return access( mb.getString().getStr(), F_OK ) == 0; + } + + virtual Bu::List getKeys() + { + DIR *dir = opendir( sPrefix.getStr() ); + struct dirent *de; + Bu::List lKeys; + + while( (de = readdir( dir ) ) ) + { + if( de->d_type != DT_REG ) + continue; + + keytype tmp; + Bu::MemBuf mb( de->d_name ); + Bu::Formatter f( mb ); + try + { + Fmt fm; + fm.tokenize( false ); + f << fm; + f >> tmp; + } + catch( Bu::ExceptionBase &e ) + { + Bu::sio << "Parse error in dir-scan: " << e.what() + << Bu::sio.nl; + } + lKeys.append( tmp ); + } + closedir( dir ); + + return lKeys; + } + + virtual int getSize() + { + DIR *dir = opendir( sPrefix.getStr() ); + struct dirent *de; + int iCount = 0; + + while( (de = readdir( dir ) ) ) + { + if( de->d_type != DT_REG ) + continue; + + iCount++; + } + closedir( dir ); + + return iCount; + } + + private: + Bu::String sPrefix; + }; + +}; + +#endif diff --git a/src/experimental/cachestoremyriad.cpp b/src/experimental/cachestoremyriad.cpp new file mode 100644 index 0000000..9d00dce --- /dev/null +++ b/src/experimental/cachestoremyriad.cpp @@ -0,0 +1,9 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/cachestoremyriad.h" + diff --git a/src/experimental/cachestoremyriad.h b/src/experimental/cachestoremyriad.h new file mode 100644 index 0000000..e632a82 --- /dev/null +++ b/src/experimental/cachestoremyriad.h @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_CACHE_STORE_MYRIAD_H +#define BU_CACHE_STORE_MYRIAD_H + +#include "bu/string.h" +#include "bu/stream.h" +#include "bu/myriad.h" +#include "bu/cachestore.h" +#include "bu/myriadstream.h" + +#include "bu/archive.h" + +namespace Bu +{ + template + keytype __cacheGetKey( const obtype *pObj ); + + template + obtype *__cacheStoreMyriadAlloc( const keytype &key ) + { + return new obtype(); + } + + template + void __cacheStoreMyriadStore( Bu::Stream &s, obtype &rObj, + const keytype & ) + { + Bu::Archive ar( s, Bu::Archive::save ); + ar << rObj; + } + + template + obtype *__cacheStoreMyriadLoad( Bu::Stream &s, const keytype &key ) + { + obtype *pObj = __cacheStoreMyriadAlloc( key ); + Bu::Archive ar( s, Bu::Archive::load ); + ar >> (*pObj); + return pObj; + } + + template + class CacheStoreMyriad : public CacheStore + { + public: + CacheStoreMyriad( Bu::Stream &sArch, + int iBlockSize=512, int iPreAllocate=8 ) : + mStore( sArch, iBlockSize, iPreAllocate ) + { + try + { + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::load ); + ar >> hId; + } + catch( Bu::MyriadException &e ) + { + int iStream = mStore.createStream(); + if( iStream != 1 ) + throw Bu::ExceptionBase("That's...horrible...id = %d.\n\n", + iStream ); + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + } + + virtual ~CacheStoreMyriad() + { + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + } + + virtual obtype *load( const keytype &key ) + { + int iStream = hId.get( key ); + MyriadStream ns = mStore.openStream( iStream ); + obtype *pOb = __cacheStoreMyriadLoad( ns, key ); + return pOb; + } + + virtual void unload( obtype *pObj, const keytype & ) + { + delete pObj; + } + + virtual keytype create( obtype *pSrc ) + { + keytype key = __cacheGetKey( pSrc ); + int iStream = mStore.createStream(); + hId.insert( key, iStream ); + MyriadStream ns = mStore.openStream( iStream ); + __cacheStoreMyriadStore( ns, *pSrc, key ); + ns.setSize( ns.tell() ); + return key; + } + + virtual void sync() + { + MyriadStream ns = mStore.openStream( 1 ); + Bu::Archive ar( ns, Bu::Archive::save ); + ar << hId; + ns.setSize( ns.tell() ); + mStore.sync(); + } + + virtual void sync( obtype *pSrc, const keytype &key ) + { + int iStream = hId.get( key ); + MyriadStream ns = mStore.openStream( iStream ); + __cacheStoreMyriadStore( ns, *pSrc, key ); + ns.setSize( ns.tell() ); + } + + virtual void destroy( obtype *pObj, const keytype &key ) + { + int iStream = hId.get( key ); + mStore.deleteStream( iStream ); + hId.erase( key ); + delete pObj; + } + + virtual void destroy( const keytype &key ) + { + int iStream = hId.get( key ); + mStore.deleteStream( iStream ); + hId.erase( key ); + } + + virtual bool has( const keytype &key ) + { + return hId.has( key ); + } + + virtual Bu::List getKeys() + { + return hId.getKeys(); + } + + virtual int getSize() + { + return hId.getSize(); + } + + private: + Myriad mStore; + typedef Bu::Hash StreamHash; + StreamHash hId; + }; +}; + +#endif diff --git a/src/experimental/fastcgi.cpp b/src/experimental/fastcgi.cpp new file mode 100644 index 0000000..2f9161e --- /dev/null +++ b/src/experimental/fastcgi.cpp @@ -0,0 +1,372 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fastcgi.h" + +#ifndef WIN32 + #include +#endif + +#include +#include + +#include "bu/membuf.h" + +#include "bu/sio.h" +using Bu::sio; +using Bu::Fmt; + +Bu::FastCgi::FastCgi() : + pSrv( NULL ), + bRunning( true ) +{ + pSrv = new Bu::TcpServerSocket( STDIN_FILENO, false ); +} + +Bu::FastCgi::FastCgi( int iPort ) : + pSrv( NULL ), + bRunning( true ) +{ + pSrv = new Bu::TcpServerSocket( iPort ); +} + +Bu::FastCgi::~FastCgi() +{ +} + +bool Bu::FastCgi::isEmbedded() +{ +#ifndef WIN32 + struct sockaddr name; + socklen_t namelen = sizeof(name); + if( getpeername( STDIN_FILENO, &name, &namelen ) != 0 && + errno == ENOTCONN ) + { + sio << "errno = " << errno << " (" << strerror( errno ) << ")" << + sio.nl; + sio << "Socket found" << sio.nl; + return true; + } + else + { + sio << "errno = " << errno << " (" << strerror( errno ) << ")" << + sio.nl; + sio << "No socket detected, running in standalone mode" << sio.nl; + return false; + } +#else + #warning Bu::FastCgi::isEmbedded IS A STUB for WIN32!!!! + return false; +#endif +} + +void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::Record &r ) +{ + int iRead = s.read( &r, sizeof(Record) ); + if( iRead != sizeof(Record) ) + throw Bu::TcpSocketException("Hey, the size %d is wrong for Record. (%s)", + iRead, strerror( errno ) ); + r.uRequestId = ntohs( r.uRequestId ); + r.uContentLength = ntohs( r.uContentLength ); +} + +void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::Record r ) +{ +// sio << "Out -> " << r << sio.nl; + r.uRequestId = htons( r.uRequestId ); + r.uContentLength = htons( r.uContentLength ); + s.write( &r, sizeof(Record) ); +} + +void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::BeginRequestBody &b ) +{ + s.read( &b, sizeof(BeginRequestBody) ); + b.uRole = ntohs( b.uRole ); +} + +void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::EndRequestBody b ) +{ + b.uStatus = htonl( b.uStatus ); + s.write( &b, sizeof(b) ); +} + +uint32_t Bu::FastCgi::readLen( Bu::TcpSocket &s, uint16_t &uRead ) +{ + uint8_t uByte[4]; + s.read( uByte, 1 ); + uRead++; + if( uByte[0] >> 7 == 0 ) + return uByte[0]; + + s.read( uByte+1, 3 ); + uRead += 3; + return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]); +} + +void Bu::FastCgi::readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uRead ) +{ + uint32_t uName = readLen( s, uRead ); + uint32_t uValue = readLen( s, uRead ); + uRead += uName + uValue; + unsigned char *sName = new unsigned char[uName]; + s.read( sName, uName ); + Bu::String fsName( (char *)sName, uName ); + delete[] sName; + + if( uValue > 0 ) + { + unsigned char *sValue = new unsigned char[uValue]; + s.read( sValue, uValue ); + Bu::String fsValue( (char *)sValue, uValue ); + hParams.insert( fsName, fsValue ); + delete[] sValue; + } + else + { + hParams.insert( fsName, "" ); + } +} + +bool Bu::FastCgi::hasChannel( int iChan ) +{ + if( aChannel.getSize() < iChan ) + return false; + if( aChannel[iChan-1] == NULL ) + return false; + return true; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ) +{ + f << "[Ver=" << (uint32_t)r.uVersion << + ", Type=" << (uint32_t)r.uType << + ", Req=" << (uint32_t)r.uRequestId << + ", clen=" << (uint32_t)r.uContentLength << + ", plen=" << (uint32_t)r.uPaddingLength << + ", resv=" << (uint32_t)r.uReserved << + "]"; + return f; +} + +void Bu::FastCgi::run() +{ +// sio << "sizeof(Bu::FastCgi::Record) = " << sizeof(Record) << sio.nl; + bRunning = true; + while( bRunning ) + { + int iSock = pSrv->accept( 5 ); + if( iSock < 0 ) + continue; + + Bu::TcpSocket s( iSock ); + s.setBlocking( true ); +// sio << "Got connection, blocking? " << s.isBlocking() << sio.nl; + try + { + for(;;) + { + Record r; + memset( &r, 0, sizeof(r) ); +// try +// { + read( s, r ); +// } +// catch( Bu::ExceptionBase &e ) +// { +// sio << "Error: " << e.what() << ", " << s.isOpen() << +// sio.nl; +// continue; +// } + Channel *pChan = NULL; + if( r.uRequestId > 0 ) + { + if( !hasChannel( r.uRequestId ) && + r.uType != typeBeginRequest ) + { + sio << "Error, stream data without stream." << sio.nl; + sio << r << sio.nl; + if( r.uContentLength > 0 ) + { + char *buf = new char[r.uContentLength]; + sio << " (read " << s.read( buf, r.uContentLength ) + << "/" << r.uContentLength << "):" << sio.nl; + sio.write( buf, r.uContentLength ); + sio << sio.nl << sio.nl; + } + } + while( aChannel.getSize() < r.uRequestId ) + aChannel.append( NULL ); + if( r.uRequestId > 0 ) + pChan = aChannel[r.uRequestId-1]; + } + +// sio << "Record (id=" << r.uRequestId << ", len=" << +// r.uContentLength << ", pad=" << +// (unsigned int)r.uPaddingLength << "): "; +// fflush( stdout ); + + switch( (RequestType)r.uType ) + { + case typeBeginRequest: +// sio << "Begin Request."; + { + BeginRequestBody b; + read( s, b ); + if( pChan != NULL ) + { + sio << "Error!!!" << sio.nl; + return; + } + pChan = aChannel[r.uRequestId-1] = new Channel(); + } + break; + + case typeParams: +// sio << "Params."; + if( r.uContentLength == 0 ) + { + pChan->uFlags |= chflgParamsDone; + } + else + { + uint16_t uUsed = 0; + while( uUsed < r.uContentLength ) + { + readPair( s, pChan->hParams, uUsed ); + } + } + break; + + case typeStdIn: +// sio << "StdIn."; + if( r.uContentLength == 0 ) + { + pChan->uFlags |= chflgStdInDone; + } + else + { + char *buf = new char[r.uContentLength]; + int iTotal = 0; + do + { + size_t iRead = s.read( + buf, r.uContentLength-iTotal ); + iTotal += iRead; +// sio << " (read " << iRead << " " << iTotal +// << "/" << r.uContentLength << ")"; + pChan->sStdIn.append( buf, iRead ); + } while( iTotal < r.uContentLength ); + delete[] buf; + } + break; + + case typeData: +// sio << "Data."; + if( r.uContentLength == 0 ) + { + pChan->uFlags |= chflgDataDone; + } + else + { + char *buf = new char[r.uContentLength]; + s.read( buf, r.uContentLength ); + pChan->sData.append( buf, r.uContentLength ); + delete[] buf; + } + break; + + case typeStdOut: + case typeStdErr: + case typeEndRequest: + case typeAbortRequest: + case typeGetValuesResult: +// sio << "Scary."; + // ??? we shouldn't get these. + break; + + case typeGetValues: + break; + } + +// sio << sio.nl; + + if( pChan ) + { + if( pChan->uFlags == chflgAllDone ) + { +// sio << "All done, generating output." << sio.nl; + Bu::MemBuf mStdOut, mStdErr; + int iRet = onRequest( + pChan->hParams, pChan->sStdIn, + mStdOut, mStdErr + ); + + Bu::String &sStdOut = mStdOut.getString(); + Bu::String &sStdErr = mStdErr.getString(); + + Record rOut; + memset( &rOut, 0, sizeof(rOut) ); + rOut.uVersion = 1; + rOut.uRequestId = r.uRequestId; + rOut.uPaddingLength = 0; + rOut.uType = typeStdOut; + if( sStdOut.getSize() > 0 ) + { + for( int iPos = 0; iPos < sStdOut.getSize(); + iPos += 65528 ) + { + int iSize = sStdOut.getSize()-iPos; + if( iSize > 65528 ) + iSize = 65528; + rOut.uContentLength = iSize; + write( s, rOut ); + s.write( sStdOut.getStr()+iPos, iSize ); + } + } + rOut.uContentLength = 0; + write( s, rOut ); + + rOut.uType = typeStdErr; + if( sStdErr.getSize() > 0 ) + { + for( int iPos = 0; iPos < sStdErr.getSize(); + iPos += 65528 ) + { + int iSize = sStdErr.getSize()-iPos; + if( iSize > 65528 ) + iSize = 65528; + rOut.uContentLength = iSize; + write( s, rOut ); + s.write( sStdErr.getStr()+iPos, iSize ); + } + } + rOut.uContentLength = 0; + write( s, rOut ); + + rOut.uType = typeEndRequest; + rOut.uContentLength = 8; + write( s, rOut ); + + EndRequestBody b; + memset( &b, 0, sizeof(b) ); + b.uStatus = iRet; + write( s, b ); + + delete pChan; + aChannel[r.uRequestId-1] = NULL; + } + } + } + } + catch( Bu::TcpSocketException &e ) + { +// sio << "Bu::SocketException: " << e.what() << sio.nl << +// "\tSocket open: " << s.isOpen() << sio.nl; + } + } +} + diff --git a/src/experimental/fastcgi.h b/src/experimental/fastcgi.h new file mode 100644 index 0000000..d290c40 --- /dev/null +++ b/src/experimental/fastcgi.h @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_FAST_CGI_H +#define BU_FAST_CGI_H + +#include "bu/string.h" +#include "bu/hash.h" +#include "bu/array.h" +#include "bu/tcpsocket.h" +#include "bu/tcpserversocket.h" + +namespace Bu +{ + class Stream; + + class FastCgi + { + public: + FastCgi( int iPort ); + FastCgi(); + virtual ~FastCgi(); + + static bool isEmbedded(); + + typedef Bu::Hash StrHash; + enum RequestType + { + typeBeginRequest = 1, + typeAbortRequest = 2, + typeEndRequest = 3, + typeParams = 4, + typeStdIn = 5, + typeStdOut = 6, + typeStdErr = 7, + typeData = 8, + typeGetValues = 9, + typeGetValuesResult = 10 + }; + + enum Role + { + roleResponder = 1, + roleAuthorizer = 2, + roleFilter = 3 + }; + + enum Flags + { + flagsKeepConn = 1 + }; + + enum Status + { + statusRequestComplete = 0, + statusCantMpxConn = 1, + statusOverloaded = 2, + statusUnknownRole = 3 + }; + + typedef struct { + uint8_t uVersion; + uint8_t uType; + uint16_t uRequestId; + uint16_t uContentLength; + uint8_t uPaddingLength; + uint8_t uReserved; + } Record; + + typedef struct { + uint16_t uRole; + uint8_t uFlags; + uint8_t reserved[5]; + } BeginRequestBody; + + typedef struct { + uint32_t uStatus; + uint8_t uProtocolStatus; + uint8_t reserved[3]; + } EndRequestBody; + + typedef struct Channel { + Channel() : uFlags( 0 ) { } + StrHash hParams; + Bu::String sStdIn; + Bu::String sData; + uint8_t uFlags; + } Channel; + + enum ChannelFlags + { + chflgParamsDone = 0x01, + chflgStdInDone = 0x02, + chflgDataDone = 0x04, + + chflgAllDone = 0x03 + }; + + virtual void run(); + + void stopRunning() { bRunning = false; } + + virtual void onInit() { }; + virtual int onRequest( const StrHash &hParams, + const Bu::String &sStdIn, Bu::Stream &sStdOut, + Bu::Stream &sStdErr )=0; + virtual void onUninit() { }; + + private: + void read( Bu::TcpSocket &s, Record &r ); + void read( Bu::TcpSocket &s, BeginRequestBody &b ); + uint32_t readLen( Bu::TcpSocket &s, uint16_t &uUsed ); + void readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uUsed ); + + void write( Bu::TcpSocket &s, Record r ); + void write( Bu::TcpSocket &s, EndRequestBody b ); + + bool hasChannel( int iChan ); + + private: + Bu::TcpServerSocket *pSrv; + bool bRunning; + Bu::Array aChannel; + }; + + Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ); +}; + +#endif diff --git a/src/experimental/httpget.cpp b/src/experimental/httpget.cpp new file mode 100644 index 0000000..99492a2 --- /dev/null +++ b/src/experimental/httpget.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/httpget.h" + +Bu::HttpGet::HttpGet( const Bu::Url &uSrc, const Bu::String &sMethod ) : + uSrc( uSrc ), + sMethod( sMethod ), + sSrv( uSrc.getHost(), uSrc.getPort() ) +{ + sSrv.write( sMethod + " " + uSrc.getFullPath() + " HTTP/1.1\r\n" ); +} + +Bu::HttpGet::~HttpGet() +{ +} + +void Bu::HttpGet::close() +{ +} + +void Bu::HttpGet::get() +{ + for( MimeHash::iterator i = hMimeOut.begin(); i; i++ ) + { + sSrv.write( i.getKey() + ": " + i.getValue() + "\r\n" ); + } + sSrv.write("\r\n", 2 ); + +// sSrv.read( +} + +Bu::size Bu::HttpGet::read( void * /*pBuf*/, Bu::size /*nBytes*/ ) +{ + return 0; +} + +Bu::size Bu::HttpGet::write( const void * /*pBuf*/, Bu::size /*nBytes*/ ) +{ + return 0; +} + +Bu::size Bu::HttpGet::tell() +{ + return 0; +} + +void Bu::HttpGet::seek( Bu::size ) +{ +} + +void Bu::HttpGet::setPos( Bu::size ) +{ +} + +void Bu::HttpGet::setPosEnd( Bu::size ) +{ +} + +bool Bu::HttpGet::isEos() +{ + return false; +} + +bool Bu::HttpGet::isOpen() +{ + return true; +} + +void Bu::HttpGet::flush() +{ +} + +bool Bu::HttpGet::canRead() +{ + return true; +} + +bool Bu::HttpGet::canWrite() +{ + return false; +} + +bool Bu::HttpGet::isReadable() +{ + return true; +} + +bool Bu::HttpGet::isWritable() +{ + return false; +} + +bool Bu::HttpGet::isSeekable() +{ + return false; +} + +bool Bu::HttpGet::isBlocking() +{ + return true; +} + +void Bu::HttpGet::setBlocking( bool /*bBlocking*/ ) +{ +} + +Bu::size Bu::HttpGet::getSize() const +{ + return 0; +} + +Bu::size Bu::HttpGet::getBlockSize() const +{ + return 0; +} + +Bu::String Bu::HttpGet::getLocation() const +{ + return uSrc.getUrl(); +} + diff --git a/src/experimental/httpget.h b/src/experimental/httpget.h new file mode 100644 index 0000000..a58e8ac --- /dev/null +++ b/src/experimental/httpget.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_HTTP_GET_H +#define BU_HTTP_GET_H + +#include "bu/stream.h" +#include "bu/string.h" +#include "bu/url.h" +#include "bu/tcpsocket.h" +#include "bu/hash.h" + +namespace Bu +{ + class HttpGet : public Bu::Stream + { + public: + HttpGet( const Bu::Url &uSrc, const Bu::String &sMethod="GET" ); + virtual ~HttpGet(); + + void get(); + + // From Bu::Stream + virtual void close(); + virtual Bu::size read( void *pBuf, Bu::size nBytes ); + virtual Bu::size write( const void *pBuf, Bu::size nBytes ); + using Stream::write; + + virtual Bu::size tell(); + virtual void seek( Bu::size offset ); + virtual void setPos( Bu::size pos ); + virtual void setPosEnd( Bu::size pos ); + virtual bool isEos(); + virtual bool isOpen(); + + virtual void flush(); + + virtual bool canRead(); + virtual bool canWrite(); + + virtual bool isReadable(); + virtual bool isWritable(); + virtual bool isSeekable(); + + virtual bool isBlocking(); + virtual void setBlocking( bool bBlocking=true ); + + virtual size getSize() const; + virtual size getBlockSize() const; + virtual Bu::String getLocation() const; + + private: + Bu::Url uSrc; + Bu::String sMethod; + Bu::TcpSocket sSrv; + typedef Bu::Hash MimeHash; + MimeHash hMimeIn; + MimeHash hMimeOut; + }; +}; + +#endif diff --git a/src/experimental/lexer.cpp b/src/experimental/lexer.cpp new file mode 100644 index 0000000..185456a --- /dev/null +++ b/src/experimental/lexer.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/lexer.h" +#include "bu/membuf.h" +#include "bu/formatter.h" + +Bu::Lexer::Lexer() +{ +} + +Bu::Lexer::~Lexer() +{ +} + +Bu::Lexer::Token::Token() : + iToken( -1 ) +{ +} + +Bu::Lexer::Token::Token( Bu::Lexer::TokenType iToken ) : + iToken( iToken ) +{ +} + +Bu::String Bu::Lexer::tokenToString( const Bu::Lexer::Token &t ) +{ + Bu::MemBuf mb; + Bu::Formatter f( mb ); + f << "<" << t.iToken << ">"; + if( t.vExtra.isSet() ) + f << " (" << t.vExtra << ")"; + + return mb.getString(); +} + diff --git a/src/experimental/lexer.h b/src/experimental/lexer.h new file mode 100644 index 0000000..b057692 --- /dev/null +++ b/src/experimental/lexer.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_LEXER_H +#define BU_LEXER_H + +#include "bu/variant.h" + +namespace Bu +{ + class Stream; + + /** + * The base class for creating a lexical analyzer. This is designed to work + * in tandem with the Bu::Parser class, which uses this to tokenize textual + * input. It can be used by just about anything that cares about tokens + * more than raw input, though. + */ + class Lexer + { + public: + Lexer(); + virtual ~Lexer(); + + typedef int TokenType; + + class Token + { + public: + Token(); + Token( TokenType iToken ); + + template + Token( TokenType iToken, const t &v ) : + iToken( iToken )//, +// vExtra( v ) + { + vExtra = v; + } + TokenType iToken; + Bu::Variant vExtra; + int iStartCol; + int iStartRow; + int iEndCol; + int iEndRow; + }; + + virtual Token *nextToken()=0; + + virtual Bu::String tokenToString( const Token &t ); + }; +}; + +#endif diff --git a/src/experimental/parser.cpp b/src/experimental/parser.cpp new file mode 100644 index 0000000..4d9f793 --- /dev/null +++ b/src/experimental/parser.cpp @@ -0,0 +1,311 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/parser.h" +#include "bu/lexer.h" + +#include "bu/sio.h" +using namespace Bu; + +Bu::Parser::Parser() +{ +} + +Bu::Parser::~Parser() +{ +} + +void Bu::Parser::pushLexer( Lexer *pLex ) +{ + sLexer.push( pLex ); +} + +void Bu::Parser::popLexer() +{ + delete sLexer.peekPop(); +} + +Lexer::Token *Bu::Parser::popToken() +{ + return sToken.peekPop(); +} + +void Bu::Parser::pushToken( Lexer::Token *pTok ) +{ + sToken.push( pTok ); +} + +void Bu::Parser::parse() +{ + int iCurNt = iRootNonTerminal; + Lexer::Token *ptCur = sLexer.peek()->nextToken(); + sio << "Token(a): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + selectProduction( iCurNt, ptCur ); + + while( !sState.isEmpty() ) + { + switch( (*sState.peek()).eType ) + { + case State::typeTerminal: + sio << "terminal: " << ptCur->iToken << " == " + << (*sState.peek()).iIndex << sio.nl; + if( ptCur->iToken == (*sState.peek()).iIndex ) + { + advanceState(); + delete ptCur; + ptCur = sLexer.peek()->nextToken(); + sio << "Token(b): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + } + else + { + throw Bu::ExceptionBase("Error parsing code."); + } + break; + + case State::typeTerminalPush: + sio << "terminalpush: " << ptCur->iToken << " == " + << (*sState.peek()).iIndex << sio.nl; + if( ptCur->iToken == (*sState.peek()).iIndex ) + { + advanceState(); + sToken.push( ptCur ); + + ptCur = sLexer.peek()->nextToken(); + sio << "Token(c): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + } + else + { + throw Bu::ExceptionBase("Error parsing code."); + } + break; + + case State::typeNonTerminal: + sio << "nonterminal: " << ptCur->iToken << " --> " + << (*sState.peek()).iIndex << sio.nl; + { + int iNt = (*sState.peek()).iIndex; + sio << "Current state: " << *sState.peek() << sio.nl; + if( !selectProduction( iNt, ptCur ) ) + { + throw Bu::ExceptionBase("Error parsing code."); + } + } + break; + + case State::typeReduction: + sio << "reduction" << sio.nl; + aReduction[(*sState.peek()).iIndex]( *this ); + advanceState(); + break; + } + } +} + +bool Bu::Parser::selectProduction( int iNt, Lexer::Token *ptCur ) +{ + NonTerminal &nt = aNonTerminal[iNt]; + int j = 0; + for( NonTerminal::ProductionList::iterator i = nt.lProduction.begin(); + i; i++,j++ ) + { + if( (*i).isEmpty() ) + continue; + sio << "-->(Attempting production " << iNt << ":" << j << ": " + << (*i).first() << ")" << sio.nl; + if( (*i).first().eType == State::typeTerminal || + (*i).first().eType == State::typeTerminalPush ) + { + if( (*i).first().iIndex == ptCur->iToken ) + { + sState.push( (*i).begin() ); + sio.incIndent(); + sio << "Pushing production " << j << " from nt " << iNt + << sio.nl; + return true; + } + } + else if( (*i).first().eType == State::typeNonTerminal ) + { + sState.push( (*i).begin() ); + sio.incIndent(); + sio << "Pushing production " << j << " from nt " << iNt + << " as test." << sio.nl; + if( !selectProduction( (*i).first().iIndex, ptCur ) ) + { + sio.decIndent(); + sState.pop(); + sio << "Production " << j << " from nt " << iNt + << " didn't work out." << sio.nl; + } + else + { + return true; + } + } + } + if( nt.bCanSkip ) + { + sio << "Nothing matches, skipping non-terminal." << sio.nl; + advanceState(); + return true; + } + sio << "-->(Found nothing)" << sio.nl; + return false; +} + +void Bu::Parser::advanceState() +{ + if( sState.isEmpty() ) + return; + + sState.peek()++; + if( !sState.peek() ) + { + sio.decIndent(); + sState.pop(); + sio << "State advanced, End of production." << sio.nl; + advanceState(); + return; + } + sio << "State advanced, now: " << *(sState.peek()) << sio.nl; +} + +void Bu::Parser::setRootNonTerminal( int iRoot ) +{ + iRootNonTerminal = iRoot; +} + +void Bu::Parser::setRootNonTerminal( const Bu::String &sRoot ) +{ + setRootNonTerminal( hNonTerminalName.get( sRoot ) ); +} + +int Bu::Parser::addNonTerminal( const Bu::String &sName, NonTerminal &nt ) +{ + int iId = aNonTerminal.getSize(); + aNonTerminal.append( nt ); + hNonTerminalName.insert( sName, iId ); + sio << "nt '" << sName << "' = " << iId << sio.nl; + return iId; +} + +int Bu::Parser::addNonTerminal( const Bu::String &sName ) +{ + int iId = aNonTerminal.getSize(); + aNonTerminal.append( NonTerminal() ); + hNonTerminalName.insert( sName, iId ); + sio << "nt '" << sName << "' = " << iId << sio.nl; + return iId; +} + +void Bu::Parser::setNonTerminal( const Bu::String &sName, NonTerminal &nt ) +{ + aNonTerminal[hNonTerminalName.get(sName)] = nt; +} + +int Bu::Parser::getNonTerminalId( const Bu::String &sName ) +{ + return hNonTerminalName.get( sName ); +} + +bool Bu::Parser::hasNonTerminal( const Bu::String &sName ) +{ + return hNonTerminalName.has( sName ); +} + +int Bu::Parser::addReduction( const Bu::String &sName, const Reduction &r ) +{ + int iId = aReduction.getSize(); + aReduction.append( r ); + hReductionName.insert( sName, iId ); + return iId; +} + +int Bu::Parser::addReduction( const Bu::String &sName ) +{ + int iId = aReduction.getSize(); + aReduction.append( Reduction() ); + hReductionName.insert( sName, iId ); + return iId; +} + +void Bu::Parser::setReduction( const Bu::String &sName, const Reduction &r ) +{ + aReduction[hReductionName.get(sName)] = r; +} + +int Bu::Parser::getReductionId( const Bu::String &sName ) +{ + return hReductionName.get( sName ); +} + +bool Bu::Parser::hasReduction( const Bu::String &sName ) +{ + return hReductionName.has( sName ); +} + +// +// Bu::Parser::State +// + +Bu::Parser::State::State( Bu::Parser::State::Type eType, int iIndex ) : + eType( eType ), + iIndex( iIndex ) +{ +} + +Bu::Parser::State::~State() +{ +} + +// +// Bu::Parser::NonTerminal +// + +Bu::Parser::NonTerminal::NonTerminal() : + bCanSkip( false ) +{ +} + +Bu::Parser::NonTerminal::~NonTerminal() +{ +} + +void Bu::Parser::NonTerminal::addProduction( Production p ) +{ + lProduction.append( p ); +} + +void Bu::Parser::NonTerminal::setCanSkip() +{ + bCanSkip = true; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &f, Bu::Parser::State::Type t ) +{ + switch( t ) + { + case Bu::Parser::State::typeTerminal: + return f << "typeTerminal"; + + case Bu::Parser::State::typeTerminalPush: + return f << "typeTerminalPush"; + + case Bu::Parser::State::typeNonTerminal: + return f << "typeNonTerminal"; + + case Bu::Parser::State::typeReduction: + return f << "typeReduction"; + } + return f << "***error***"; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Parser::State &s ) +{ + return f << "{" << s.eType << ": " << s.iIndex << "}"; +} + diff --git a/src/experimental/parser.h b/src/experimental/parser.h new file mode 100644 index 0000000..a168c7b --- /dev/null +++ b/src/experimental/parser.h @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_PARSER_H +#define BU_PARSER_H + +#include "bu/string.h" +#include "bu/list.h" +#include "bu/array.h" +#include "bu/hash.h" +#include "bu/signals.h" + +#include "bu/lexer.h" + +namespace Bu +{ + /** + * The base framework for a LR(1) grammar parser. Provided a proper set of + * ParserStates this will prase any input the lexer can provide. + */ + class Parser + { + public: + Parser(); + virtual ~Parser(); + + /** + * When a Lexer is pushed onto the stack it becomes the source for + * future tokens read by the parser until it is popped off the stack. + * The Parser takes ownership of every Lexer pushed onto the stack, + * and will delete it when it is popped off the stack. + */ + void pushLexer( Lexer *pLex ); + + /** + * Pop a lexer off the stack, and delete it. + */ + void popLexer(); + + Lexer::Token *popToken(); + void pushToken( Lexer::Token *pTok ); + + /** + * Execute a parse. + */ + void parse(); + + void setRootNonTerminal( int iRoot ); + void setRootNonTerminal( const Bu::String &sRoot ); + + typedef Bu::Signal1 Reduction; + + /** + * Represents a possible state, either a terminal or non-terminal symbol + * in a Production. + */ + class State + { + public: + enum Type + { + typeTerminal, + typeTerminalPush, + typeNonTerminal, + typeReduction + }; + + State( Type eType, int iIndex ); + virtual ~State(); + + //private: + Type eType; + int iIndex; + }; + + typedef Bu::List Production; + + class NonTerminal + { + public: + NonTerminal(); + virtual ~NonTerminal(); + + void addProduction( Production p ); + void setCanSkip(); + +// private: + typedef Bu::List ProductionList; + ProductionList lProduction; + bool bCanSkip; + }; + + int addNonTerminal( const Bu::String &sName, NonTerminal &nt ); + int addNonTerminal( const Bu::String &sName ); + void setNonTerminal( const Bu::String &sName, NonTerminal &nt ); + int getNonTerminalId( const Bu::String &sName ); + bool hasNonTerminal( const Bu::String &sName ); + + int addReduction( const Bu::String &sName, const Reduction &r ); + int addReduction( const Bu::String &sName ); + void setReduction( const Bu::String &sName, const Reduction &r ); + int getReductionId( const Bu::String &sName ); + bool hasReduction( const Bu::String &sName ); + + private: + bool selectProduction( int iNt, Lexer::Token *ptCur ); + void advanceState(); + + private: + typedef Bu::List LexerStack; + typedef Bu::List TokenStack; + typedef Bu::List StateStack; + typedef Bu::Array ReductionArray; + typedef Bu::Hash NameIndexHash; + typedef Bu::Array NonTerminalArray; + + LexerStack sLexer; + TokenStack sToken; + StateStack sState; + ReductionArray aReduction; + NameIndexHash hReductionName; + NonTerminalArray aNonTerminal; + NameIndexHash hNonTerminalName; + int iRootNonTerminal; + }; +Bu::Formatter &operator<<( Bu::Formatter &f, Bu::Parser::State::Type t ); +Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::Parser::State &s ); +}; + + +#endif diff --git a/src/experimental/random.cpp b/src/experimental/random.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/random.h b/src/experimental/random.h new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/randombase.cpp b/src/experimental/randombase.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/randombase.h b/src/experimental/randombase.h new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/randombasic.cpp b/src/experimental/randombasic.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/randombasic.h b/src/experimental/randombasic.h new file mode 100644 index 0000000..e69de29 diff --git a/src/experimental/regex.cpp b/src/experimental/regex.cpp new file mode 100644 index 0000000..af0d364 --- /dev/null +++ b/src/experimental/regex.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/regex.h" + +#include // Please, please include the system regex.h file + +#define re ((regex_t *)pRegEx) +#define aSubStr ((regmatch_t *)paSubStr) + +Bu::RegEx::RegEx() : + pRegEx( NULL ), + bCompiled( false ), + paSubStr( NULL ) +{ +} + +Bu::RegEx::RegEx( const Bu::String &sSrc ) : + pRegEx( NULL ), + bCompiled( false ), + paSubStr( NULL ) +{ + compile( sSrc ); +} + +Bu::RegEx::~RegEx() +{ + if( bCompiled ) + { + regfree( re ); + delete re; + delete[] aSubStr; + } +} + +void Bu::RegEx::compile( const Bu::String &sSrc ) +{ + if( bCompiled ) + { + regfree( re ); + delete re; + delete[] aSubStr; + bCompiled = false; + } + pRegEx = (void *)(new regex_t); + + int nErr = regcomp( re, sSrc.getStr(), REG_EXTENDED|REG_NEWLINE ); + if( nErr ) + { + size_t length = regerror( nErr, re, NULL, 0 ); + char *buffer = new char[length]; + (void) regerror( nErr, re, buffer, length ); + Bu::String s( buffer ); + delete[] buffer; + throw "???"; // BuildException( s.getStr() ); + } + bCompiled = true; + this->sSrc = sSrc; + + nSubStr = re->re_nsub+1; + paSubStr = (void *)(new regmatch_t[nSubStr]); +} + +int Bu::RegEx::getNumSubStrings() +{ + return nSubStr; +} + +bool Bu::RegEx::execute( const Bu::String &sSrc ) +{ + sTest = sSrc; + if( regexec( re, sSrc.getStr(), nSubStr, aSubStr, 0 ) ) + return false; + return true; +} + +void Bu::RegEx::getSubStringRange( int nIndex, int &iStart, int &iEnd ) +{ + iStart = aSubStr[nIndex].rm_so; + iEnd = aSubStr[nIndex].rm_eo; +} + +Bu::String Bu::RegEx::getSubString( int nIndex ) +{ +// regmatch_t *Subs = aSubStr; + return Bu::String( + sTest.getStr()+aSubStr[nIndex].rm_so, + aSubStr[nIndex].rm_eo - aSubStr[nIndex].rm_so + ); +} + diff --git a/src/experimental/regex.h b/src/experimental/regex.h new file mode 100644 index 0000000..f0aa5d5 --- /dev/null +++ b/src/experimental/regex.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_REG_EX_H +#define BU_REG_EX_H + +#include "bu/string.h" + +#include + +namespace Bu +{ + class RegEx + { + public: + RegEx(); + RegEx( const Bu::String &sSrc ); + virtual ~RegEx(); + + void compile( const Bu::String &sSrc ); + int getNumSubStrings(); + bool execute( const Bu::String &sSrc ); + void getSubStringRange( int nIndex, int &iStart, int &iEnd ); + Bu::String getSubString( int nIndex ); + const Bu::String &getSource() + { + return sSrc; + } + + private: + Bu::String sSrc; + Bu::String sTest; + void *pRegEx; + bool bCompiled; + int nSubStr; + void *paSubStr; + }; +}; + +#endif diff --git a/src/experimental/regexengine.cpp b/src/experimental/regexengine.cpp new file mode 100644 index 0000000..72bc381 --- /dev/null +++ b/src/experimental/regexengine.cpp @@ -0,0 +1,5 @@ +#include "bu/regexengine.h" +#include "bu/utfstring.h" + +template class Bu::RegExEngine; +template class Bu::RegExEngine; diff --git a/src/experimental/regexengine.h b/src/experimental/regexengine.h new file mode 100644 index 0000000..ec181c1 --- /dev/null +++ b/src/experimental/regexengine.h @@ -0,0 +1,142 @@ +#ifndef BU_REG_EX_ENGINE_H +#define BU_REG_EX_ENGINE_H + +#include "bu/sharedcore.h" +#include "bu/array.h" +#include "bu/sio.h" + +namespace Bu +{ + template class RegExEngine; + + template + class RegExEngineCore + { + friend class RegExEngine; + friend class SharedCore, RegExEngineCore >; + private: + RegExEngineCore() + { + } + + virtual ~RegExEngineCore() + { + } + + class Range + { + public: + Range( chr cLower, chr cUpper, int iTrgState ) : + cLower( cLower ), cUpper( cUpper ), iTrgState( iTrgState ) + { + } + + chr cLower; + chr cUpper; + int iTrgState; + }; + + class State + { + public: + Bu::Array aRange; + }; + + int addState() + { + aState.append( State() ); + return aState.getSize()-1; + } + + void addCompletion( int iState, chr cLower, chr cUpper, int iTrgState ) + { + aState[iState].aRange.append( Range( cLower, cUpper, iTrgState ) ); + } + + template + bool match( const str &sIn, int &iSize, int &iCompletion ) + { + bool bMatch; + int iState = 0; + iSize = 0; + for( typename str::const_iterator i = sIn.begin(); i; i++ ) + { + Bu::sio << "Finding char " << *i << " in state " << iState + << ":" << Bu::sio.nl; + bMatch = false; + for( typename Bu::Array::iterator j = + aState[iState].aRange.begin(); j; j++ ) + { + Bu::sio << " Testing range " << (*j).cLower << " - " << (*j).cUpper << Bu::sio.nl; + if( *i >= (*j).cLower && *i <= (*j).cUpper ) + { + iState = (*j).iTrgState; + bMatch = true; + iSize++; + if( iState < 0 ) + { + iCompletion = iState; + return true; + } + } + } + if( bMatch == false ) + { + return false; + } + } + + iCompletion = 0; + return true; + } + + typedef Bu::Array StateArray; + StateArray aState; + }; + + template + class RegExEngine : public SharedCore, + RegExEngineCore > + { + private: + typedef class RegExEngine MyType; + typedef class RegExEngineCore Core; + typedef class Core::Range Range; + typedef class Core::State State; + + protected: + using SharedCore::core; + using SharedCore::_hardCopy; + using SharedCore::_resetCore; + using SharedCore::_allocateCore; + + public: + RegExEngine() + { + } + + virtual ~RegExEngine() + { + } + + int addState() + { + return core->addState(); + } + + void addCompletion( int iState, chr cLower, chr cUpper, int iTrgState ) + { + core->addCompletion( iState, cLower, cUpper, iTrgState ); + } + + template + bool match( const str &sIn, int &iSize, int &iCompletion ) + { + return core->match( sIn, iSize, iCompletion ); + } + + private: + }; +}; + +#endif diff --git a/src/experimental/xmlreader.cpp b/src/experimental/xmlreader.cpp new file mode 100644 index 0000000..ba7fb3d --- /dev/null +++ b/src/experimental/xmlreader.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/xmlreader.h" +#include "bu/stream.h" + +namespace Bu { subExceptionDef( XmlException ) } + +Bu::XmlReader::XmlReader( Stream &rInput ) : + rInput( rInput ), + iCurToken( 0 ), + iNextToken( 0 ), + bIgnoreWS( true ) +{ + nextToken(); + stDocument(); +} + +Bu::XmlReader::~XmlReader() +{ +} + +void Bu::XmlReader::fillBuffer() +{ + if( rInput.isEos() ) + return; + char buf[1024]; + int iSize = rInput.read( buf, 1024 ); + sBuf.append( buf, iSize ); +} + +void Bu::XmlReader::cleanupBuffer( int iUsed ) +{ + for( int j = 0; j < iUsed; j++ ) + { + if( sBuf[j] == '\n' ) + { + spNextToken.iLine++; + spNextToken.iChar = 1; + } + else + { + spNextToken.iChar++; + } + } + + printf("--Deleting %d bytes from front of buffer.\n", iUsed ); + sBuf.trimFront( iUsed ); +} + +int Bu::XmlReader::nextToken() +{ + fillBuffer(); + + int iUsed = 1; + + iCurToken = iNextToken; + spCurToken = spNextToken; + + switch( sBuf[0] ) + { + case '<': + if( !strncmp( sBuf.getStr(), "' ) + { + iNextToken = tokXmlDeclEnd; + iUsed = 2; + } + else + { + iNextToken = '?'; + } + break; + + case ' ': + case '\t': + case '\n': + case '\r': + for( int j = 1;; j++ ) + { + if( j == sBuf.getSize() ) + { + if( rInput.isEos() ) + error("Reached end of input while waiting for whitespace to end."); + + fillBuffer(); + } + if( sBuf[j] == ' ' || sBuf[j] == '\t' || + sBuf[j] == '\n' || sBuf[j] == '\r' ) + iUsed++; + else + break; + } + sStr.clear(); + sStr.append( sBuf, iUsed ); + iNextToken = tokWS; + break; + + case '=': + iNextToken = sBuf[0]; + break; + + default: + if( (sBuf[0] >= 'a' && sBuf[0] <= 'z') || + (sBuf[0] >= 'A' && sBuf[0] <= 'Z') ) + { + for( int j = 1;; j++ ) + { + if( j == sBuf.getSize() ) + { + if( rInput.isEos() ) + error("Reached end of input while waiting for a string to end."); + + fillBuffer(); + } + if( (sBuf[j] >= 'a' && sBuf[j] <= 'z') || + (sBuf[j] >= 'A' && sBuf[j] <= 'Z') ) + iUsed++; + else + break; + } + sStr.clear(); + sStr.append( sBuf, iUsed ); + iNextToken = tokIdent; + } + } + + cleanupBuffer( iUsed ); + + return iCurToken; +} + +void Bu::XmlReader::error( const char *sMessage ) +{ + throw Bu::XmlException("%d:%d: %s", + spCurToken.iLine, spCurToken.iChar, sMessage ); +} + +void Bu::XmlReader::stDocument() +{ + stProlog(); +} + +void Bu::XmlReader::stProlog() +{ + stXmlDecl(); +} + +void Bu::XmlReader::stXmlDecl() +{ + if( nextToken() != tokXmlDeclHead ) + error("You must begin your xml file with a declaration: "); + if( nextToken() != tokIdent ) + error("A version comes first!"); + if( sStr != "version" ) + error("No, a version!"); +} + diff --git a/src/experimental/xmlreader.h b/src/experimental/xmlreader.h new file mode 100644 index 0000000..7cb44c9 --- /dev/null +++ b/src/experimental/xmlreader.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2007-2011 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_XML_READER_H +#define BU_XML_READER_H + +#include "bu/string.h" +#include "bu/exceptionbase.h" + +namespace Bu +{ + class Stream; + + subExceptionDecl( XmlException ); + + class XmlReader + { + public: + XmlReader( Stream &rInput ); + virtual ~XmlReader(); + + private: + Stream &rInput; + int iCurToken; + int iNextToken; + Bu::String sBuf; + Bu::String sStr; + bool bIgnoreWS; + typedef struct StreamPos + { + StreamPos() : iLine( 1 ), iChar( 1 ) { } + int iLine; + int iChar; + } StreamPos; + StreamPos spCurToken; + StreamPos spNextToken; + + + enum + { + tokXmlDeclHead = 0x100, + tokXmlDeclEnd, + tokWS, + tokIdent, + tokString + }; + + void fillBuffer(); + void cleanupBuffer( int iUsed ); + int nextToken(); + + void stDocument(); + void stProlog(); + void stXmlDecl(); + + void error( const char *sMessage ); + }; +}; + +#endif diff --git a/src/extratypes.h b/src/extratypes.h deleted file mode 100644 index 656cd6d..0000000 --- a/src/extratypes.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef EXTRA_TYPES_H -#define EXTRA_TYPES_H - -#include "bu/config.h" - -#include - -#ifndef NULL -#define NULL 0 -#endif - -namespace Bu -{ -#ifdef USE_64BIT_IO - typedef int64_t size; - typedef uint64_t usize; -#else - typedef int32_t size; - typedef uint32_t usize; -#endif -}; - -#endif diff --git a/src/fastcgi.cpp b/src/fastcgi.cpp deleted file mode 100644 index 2f9161e..0000000 --- a/src/fastcgi.cpp +++ /dev/null @@ -1,372 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/fastcgi.h" - -#ifndef WIN32 - #include -#endif - -#include -#include - -#include "bu/membuf.h" - -#include "bu/sio.h" -using Bu::sio; -using Bu::Fmt; - -Bu::FastCgi::FastCgi() : - pSrv( NULL ), - bRunning( true ) -{ - pSrv = new Bu::TcpServerSocket( STDIN_FILENO, false ); -} - -Bu::FastCgi::FastCgi( int iPort ) : - pSrv( NULL ), - bRunning( true ) -{ - pSrv = new Bu::TcpServerSocket( iPort ); -} - -Bu::FastCgi::~FastCgi() -{ -} - -bool Bu::FastCgi::isEmbedded() -{ -#ifndef WIN32 - struct sockaddr name; - socklen_t namelen = sizeof(name); - if( getpeername( STDIN_FILENO, &name, &namelen ) != 0 && - errno == ENOTCONN ) - { - sio << "errno = " << errno << " (" << strerror( errno ) << ")" << - sio.nl; - sio << "Socket found" << sio.nl; - return true; - } - else - { - sio << "errno = " << errno << " (" << strerror( errno ) << ")" << - sio.nl; - sio << "No socket detected, running in standalone mode" << sio.nl; - return false; - } -#else - #warning Bu::FastCgi::isEmbedded IS A STUB for WIN32!!!! - return false; -#endif -} - -void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::Record &r ) -{ - int iRead = s.read( &r, sizeof(Record) ); - if( iRead != sizeof(Record) ) - throw Bu::TcpSocketException("Hey, the size %d is wrong for Record. (%s)", - iRead, strerror( errno ) ); - r.uRequestId = ntohs( r.uRequestId ); - r.uContentLength = ntohs( r.uContentLength ); -} - -void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::Record r ) -{ -// sio << "Out -> " << r << sio.nl; - r.uRequestId = htons( r.uRequestId ); - r.uContentLength = htons( r.uContentLength ); - s.write( &r, sizeof(Record) ); -} - -void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::BeginRequestBody &b ) -{ - s.read( &b, sizeof(BeginRequestBody) ); - b.uRole = ntohs( b.uRole ); -} - -void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::EndRequestBody b ) -{ - b.uStatus = htonl( b.uStatus ); - s.write( &b, sizeof(b) ); -} - -uint32_t Bu::FastCgi::readLen( Bu::TcpSocket &s, uint16_t &uRead ) -{ - uint8_t uByte[4]; - s.read( uByte, 1 ); - uRead++; - if( uByte[0] >> 7 == 0 ) - return uByte[0]; - - s.read( uByte+1, 3 ); - uRead += 3; - return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]); -} - -void Bu::FastCgi::readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uRead ) -{ - uint32_t uName = readLen( s, uRead ); - uint32_t uValue = readLen( s, uRead ); - uRead += uName + uValue; - unsigned char *sName = new unsigned char[uName]; - s.read( sName, uName ); - Bu::String fsName( (char *)sName, uName ); - delete[] sName; - - if( uValue > 0 ) - { - unsigned char *sValue = new unsigned char[uValue]; - s.read( sValue, uValue ); - Bu::String fsValue( (char *)sValue, uValue ); - hParams.insert( fsName, fsValue ); - delete[] sValue; - } - else - { - hParams.insert( fsName, "" ); - } -} - -bool Bu::FastCgi::hasChannel( int iChan ) -{ - if( aChannel.getSize() < iChan ) - return false; - if( aChannel[iChan-1] == NULL ) - return false; - return true; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ) -{ - f << "[Ver=" << (uint32_t)r.uVersion << - ", Type=" << (uint32_t)r.uType << - ", Req=" << (uint32_t)r.uRequestId << - ", clen=" << (uint32_t)r.uContentLength << - ", plen=" << (uint32_t)r.uPaddingLength << - ", resv=" << (uint32_t)r.uReserved << - "]"; - return f; -} - -void Bu::FastCgi::run() -{ -// sio << "sizeof(Bu::FastCgi::Record) = " << sizeof(Record) << sio.nl; - bRunning = true; - while( bRunning ) - { - int iSock = pSrv->accept( 5 ); - if( iSock < 0 ) - continue; - - Bu::TcpSocket s( iSock ); - s.setBlocking( true ); -// sio << "Got connection, blocking? " << s.isBlocking() << sio.nl; - try - { - for(;;) - { - Record r; - memset( &r, 0, sizeof(r) ); -// try -// { - read( s, r ); -// } -// catch( Bu::ExceptionBase &e ) -// { -// sio << "Error: " << e.what() << ", " << s.isOpen() << -// sio.nl; -// continue; -// } - Channel *pChan = NULL; - if( r.uRequestId > 0 ) - { - if( !hasChannel( r.uRequestId ) && - r.uType != typeBeginRequest ) - { - sio << "Error, stream data without stream." << sio.nl; - sio << r << sio.nl; - if( r.uContentLength > 0 ) - { - char *buf = new char[r.uContentLength]; - sio << " (read " << s.read( buf, r.uContentLength ) - << "/" << r.uContentLength << "):" << sio.nl; - sio.write( buf, r.uContentLength ); - sio << sio.nl << sio.nl; - } - } - while( aChannel.getSize() < r.uRequestId ) - aChannel.append( NULL ); - if( r.uRequestId > 0 ) - pChan = aChannel[r.uRequestId-1]; - } - -// sio << "Record (id=" << r.uRequestId << ", len=" << -// r.uContentLength << ", pad=" << -// (unsigned int)r.uPaddingLength << "): "; -// fflush( stdout ); - - switch( (RequestType)r.uType ) - { - case typeBeginRequest: -// sio << "Begin Request."; - { - BeginRequestBody b; - read( s, b ); - if( pChan != NULL ) - { - sio << "Error!!!" << sio.nl; - return; - } - pChan = aChannel[r.uRequestId-1] = new Channel(); - } - break; - - case typeParams: -// sio << "Params."; - if( r.uContentLength == 0 ) - { - pChan->uFlags |= chflgParamsDone; - } - else - { - uint16_t uUsed = 0; - while( uUsed < r.uContentLength ) - { - readPair( s, pChan->hParams, uUsed ); - } - } - break; - - case typeStdIn: -// sio << "StdIn."; - if( r.uContentLength == 0 ) - { - pChan->uFlags |= chflgStdInDone; - } - else - { - char *buf = new char[r.uContentLength]; - int iTotal = 0; - do - { - size_t iRead = s.read( - buf, r.uContentLength-iTotal ); - iTotal += iRead; -// sio << " (read " << iRead << " " << iTotal -// << "/" << r.uContentLength << ")"; - pChan->sStdIn.append( buf, iRead ); - } while( iTotal < r.uContentLength ); - delete[] buf; - } - break; - - case typeData: -// sio << "Data."; - if( r.uContentLength == 0 ) - { - pChan->uFlags |= chflgDataDone; - } - else - { - char *buf = new char[r.uContentLength]; - s.read( buf, r.uContentLength ); - pChan->sData.append( buf, r.uContentLength ); - delete[] buf; - } - break; - - case typeStdOut: - case typeStdErr: - case typeEndRequest: - case typeAbortRequest: - case typeGetValuesResult: -// sio << "Scary."; - // ??? we shouldn't get these. - break; - - case typeGetValues: - break; - } - -// sio << sio.nl; - - if( pChan ) - { - if( pChan->uFlags == chflgAllDone ) - { -// sio << "All done, generating output." << sio.nl; - Bu::MemBuf mStdOut, mStdErr; - int iRet = onRequest( - pChan->hParams, pChan->sStdIn, - mStdOut, mStdErr - ); - - Bu::String &sStdOut = mStdOut.getString(); - Bu::String &sStdErr = mStdErr.getString(); - - Record rOut; - memset( &rOut, 0, sizeof(rOut) ); - rOut.uVersion = 1; - rOut.uRequestId = r.uRequestId; - rOut.uPaddingLength = 0; - rOut.uType = typeStdOut; - if( sStdOut.getSize() > 0 ) - { - for( int iPos = 0; iPos < sStdOut.getSize(); - iPos += 65528 ) - { - int iSize = sStdOut.getSize()-iPos; - if( iSize > 65528 ) - iSize = 65528; - rOut.uContentLength = iSize; - write( s, rOut ); - s.write( sStdOut.getStr()+iPos, iSize ); - } - } - rOut.uContentLength = 0; - write( s, rOut ); - - rOut.uType = typeStdErr; - if( sStdErr.getSize() > 0 ) - { - for( int iPos = 0; iPos < sStdErr.getSize(); - iPos += 65528 ) - { - int iSize = sStdErr.getSize()-iPos; - if( iSize > 65528 ) - iSize = 65528; - rOut.uContentLength = iSize; - write( s, rOut ); - s.write( sStdErr.getStr()+iPos, iSize ); - } - } - rOut.uContentLength = 0; - write( s, rOut ); - - rOut.uType = typeEndRequest; - rOut.uContentLength = 8; - write( s, rOut ); - - EndRequestBody b; - memset( &b, 0, sizeof(b) ); - b.uStatus = iRet; - write( s, b ); - - delete pChan; - aChannel[r.uRequestId-1] = NULL; - } - } - } - } - catch( Bu::TcpSocketException &e ) - { -// sio << "Bu::SocketException: " << e.what() << sio.nl << -// "\tSocket open: " << s.isOpen() << sio.nl; - } - } -} - diff --git a/src/fastcgi.h b/src/fastcgi.h deleted file mode 100644 index d290c40..0000000 --- a/src/fastcgi.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_FAST_CGI_H -#define BU_FAST_CGI_H - -#include "bu/string.h" -#include "bu/hash.h" -#include "bu/array.h" -#include "bu/tcpsocket.h" -#include "bu/tcpserversocket.h" - -namespace Bu -{ - class Stream; - - class FastCgi - { - public: - FastCgi( int iPort ); - FastCgi(); - virtual ~FastCgi(); - - static bool isEmbedded(); - - typedef Bu::Hash StrHash; - enum RequestType - { - typeBeginRequest = 1, - typeAbortRequest = 2, - typeEndRequest = 3, - typeParams = 4, - typeStdIn = 5, - typeStdOut = 6, - typeStdErr = 7, - typeData = 8, - typeGetValues = 9, - typeGetValuesResult = 10 - }; - - enum Role - { - roleResponder = 1, - roleAuthorizer = 2, - roleFilter = 3 - }; - - enum Flags - { - flagsKeepConn = 1 - }; - - enum Status - { - statusRequestComplete = 0, - statusCantMpxConn = 1, - statusOverloaded = 2, - statusUnknownRole = 3 - }; - - typedef struct { - uint8_t uVersion; - uint8_t uType; - uint16_t uRequestId; - uint16_t uContentLength; - uint8_t uPaddingLength; - uint8_t uReserved; - } Record; - - typedef struct { - uint16_t uRole; - uint8_t uFlags; - uint8_t reserved[5]; - } BeginRequestBody; - - typedef struct { - uint32_t uStatus; - uint8_t uProtocolStatus; - uint8_t reserved[3]; - } EndRequestBody; - - typedef struct Channel { - Channel() : uFlags( 0 ) { } - StrHash hParams; - Bu::String sStdIn; - Bu::String sData; - uint8_t uFlags; - } Channel; - - enum ChannelFlags - { - chflgParamsDone = 0x01, - chflgStdInDone = 0x02, - chflgDataDone = 0x04, - - chflgAllDone = 0x03 - }; - - virtual void run(); - - void stopRunning() { bRunning = false; } - - virtual void onInit() { }; - virtual int onRequest( const StrHash &hParams, - const Bu::String &sStdIn, Bu::Stream &sStdOut, - Bu::Stream &sStdErr )=0; - virtual void onUninit() { }; - - private: - void read( Bu::TcpSocket &s, Record &r ); - void read( Bu::TcpSocket &s, BeginRequestBody &b ); - uint32_t readLen( Bu::TcpSocket &s, uint16_t &uUsed ); - void readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uUsed ); - - void write( Bu::TcpSocket &s, Record r ); - void write( Bu::TcpSocket &s, EndRequestBody b ); - - bool hasChannel( int iChan ); - - private: - Bu::TcpServerSocket *pSrv; - bool bRunning; - Bu::Array aChannel; - }; - - Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ); -}; - -#endif diff --git a/src/fifo.cpp b/src/fifo.cpp deleted file mode 100644 index b0cf1c7..0000000 --- a/src/fifo.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/fifo.h" -#include -#include -#include -#include -#include - -#include "bu/config.h" - -namespace Bu { subExceptionDef( FifoException ) } - -Bu::Fifo::Fifo( const Bu::String &sName, int iFlags, mode_t mAcc ) : - iFlags( iFlags ), - iIn( -1 ), - iOut( -1 ) -{ -#ifndef WIN32 - if( iFlags&Create ) - { - if( mkfifo( sName.getStr(), mAcc ) ) - { - throw FifoException("Error creating fifo: %s\n", strerror( errno ) ); - } - } - if( iFlags&Read ) - { - iIn = ::open( - sName.getStr(), - O_RDONLY|((iFlags&NonBlock)?O_NONBLOCK:0) - ); - } - if( iFlags&Write ) - { - iOut = ::open( - sName.getStr(), - O_WRONLY - ); - } -#else - #warning Bu::Fifo::Fifo IS A STUB for WIN32!!!! -#endif -} - -Bu::Fifo::~Fifo() -{ - close(); -} - -void Bu::Fifo::close() -{ - if( iIn > -1 ) - { - ::close( iIn ); - iIn = -1; - } - if( iOut > -1 ) - { - ::close( iOut ); - iOut = -1; - } -} - -Bu::size Bu::Fifo::read( void *pBuf, Bu::size nBytes ) -{ - if( iIn < 0 ) - throw FifoException("Fifo not open for reading."); - - return TEMP_FAILURE_RETRY( ::read( iIn, pBuf, nBytes ) ); -} - -Bu::size Bu::Fifo::write( const void *pBuf, Bu::size nBytes ) -{ - if( iOut < 0 ) - throw FifoException("Fifo not open for writing."); - - return TEMP_FAILURE_RETRY( ::write( iOut, pBuf, nBytes ) ); -} - -Bu::size Bu::Fifo::tell() -{ - return -1; -} - -void Bu::Fifo::seek( Bu::size ) -{ -} - -void Bu::Fifo::setPos( Bu::size ) -{ -} - -void Bu::Fifo::setPosEnd( Bu::size ) -{ -} - -bool Bu::Fifo::isEos() -{ - return false; -} - -bool Bu::Fifo::canRead() -{ - return (iIn>-1); -} - -bool Bu::Fifo::canWrite() -{ - return (iOut>-1); -} - -bool Bu::Fifo::isReadable() -{ - return (iIn>-1); -} - -bool Bu::Fifo::isWritable() -{ - return (iOut>-1); -} - -bool Bu::Fifo::isSeekable() -{ - return false; -} - -bool Bu::Fifo::isBlocking() -{ -#ifndef WIN32 - return ((fcntl( iIn, F_GETFL, 0 )&O_NONBLOCK) == O_NONBLOCK); -#else - #warning Bu::Fifo::isBlocking IS A STUB for WIN32!!!! -#endif -} - -void Bu::Fifo::setBlocking( bool bBlocking ) -{ -#ifndef WIN32 - if( bBlocking ) - fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )&(~O_NONBLOCK) ); - else - fcntl( iIn, F_SETFL, fcntl( iIn, F_GETFL, 0 )|O_NONBLOCK ); -#else - #warning Bu::Fifo::setBlocking IS A STUB for WIN32!!!! -#endif -} - -void Bu::Fifo::flush() -{ -} - -bool Bu::Fifo::isOpen() -{ - return (iIn > -1) || (iOut > -1); -} - diff --git a/src/fifo.h b/src/fifo.h deleted file mode 100644 index 18a70ba..0000000 --- a/src/fifo.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_FIFO_H -#define BU_FIFO_H - -#include -#include -#include - -#include "bu/stream.h" -#include "bu/string.h" -#include "bu/exceptionbase.h" - -namespace Bu -{ - subExceptionDecl( FifoException ); - - /** - * A fifo stream. - *@ingroup Streams - */ - class Fifo : public Bu::Stream - { - public: - Fifo( const Bu::String &sName, int iFlags, mode_t mAcc=-1 ); - virtual ~Fifo(); - - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Stream::write; - - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - - virtual void flush(); - - virtual bool canRead(); - virtual bool canWrite(); - - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - - enum { - Read = 0x01, - Write = 0x02, - Create = 0x04, - Delete = 0x08, - NonBlock = 0x10 - }; - - private: - int iFlags; - int iIn; - int iOut; - }; -} - -#endif diff --git a/src/file.cpp b/src/file.cpp deleted file mode 100644 index 8c8f540..0000000 --- a/src/file.cpp +++ /dev/null @@ -1,305 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/file.h" -#include -#include -#include -#include -#include -#include - -#include "bu/config.h" - -namespace Bu { subExceptionDef( FileException ) } - -Bu::File::File( const Bu::String &sName, int iFlags ) : - fd( -1 ), - bEos( true ) -{ -#ifdef USE_64BIT_IO - fd = ::open64( sName.getStr(), getPosixFlags( iFlags ), 0666 ); -#else - fd = ::open( sName.getStr(), getPosixFlags( iFlags ), 0666 ); -#endif - if( fd < 0 ) - { - throw Bu::FileException( errno, "%s: %s", - strerror(errno), sName.getStr() ); - } - bEos = false; -} - -Bu::File::File( int fd ) : - fd( fd ) -{ - bEos = false; -} - -Bu::File::~File() -{ - close(); -} - -void Bu::File::close() -{ - if( fd >= 0 ) - { - if( ::close( fd ) ) - { - throw Bu::FileException( errno, "%s", - strerror(errno) ); - } - fd = -1; - bEos = true; - } -} - -Bu::size Bu::File::read( void *pBuf, Bu::size nBytes ) -{ - if( fd < 0 ) - throw FileException("File not open."); - - Bu::size iRead = ::read( fd, pBuf, nBytes ); - if( iRead == 0 ) - bEos = true; - else if( iRead == -1 && errno == EAGAIN ) - return 0; - else if( iRead < 0 ) - throw FileException( errno, "%s", strerror( errno ) ); - return iRead; -} - -Bu::size Bu::File::write( const void *pBuf, Bu::size nBytes ) -{ - if( fd < 0 ) - throw FileException("File not open."); - - Bu::size iWrote = ::write( fd, pBuf, nBytes ); - if( iWrote < 0 ) - throw FileException( errno, "%s", strerror( errno ) ); - return iWrote; -} - -Bu::size Bu::File::tell() -{ - if( fd < 0 ) - throw FileException("File not open."); - - return lseek( fd, 0, SEEK_CUR ); -} - -void Bu::File::seek( Bu::size offset ) -{ - if( fd < 0 ) - throw FileException("File not open."); - - lseek( fd, offset, SEEK_CUR ); - bEos = false; -} - -void Bu::File::setPos( Bu::size pos ) -{ - if( fd < 0 ) - throw FileException("File not open."); - - lseek( fd, pos, SEEK_SET ); - bEos = false; -} - -void Bu::File::setPosEnd( Bu::size pos ) -{ - if( fd < 0 ) - throw FileException("File not open."); - - lseek( fd, pos, SEEK_END ); - bEos = false; -} - -bool Bu::File::isEos() -{ - return bEos; -} - -bool Bu::File::canRead() -{ -#ifdef WIN32 - return true; -#else - int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE; - if( iMode == O_RDONLY || iMode == O_RDWR ) - return true; - return false; -#endif -} - -bool Bu::File::canWrite() -{ -#ifdef WIN32 - return true; -#else - int iMode = fcntl( fd, F_GETFL, 0 )&O_ACCMODE; - if( iMode == O_WRONLY || iMode == O_RDWR ) - return true; - return false; -#endif -} - -bool Bu::File::isReadable() -{ - return true; -} - -bool Bu::File::isWritable() -{ - return true; -} - -bool Bu::File::isSeekable() -{ - return true; -} - -bool Bu::File::isBlocking() -{ - return true; -} - -void Bu::File::setBlocking( bool bBlocking ) -{ -#ifdef WIN32 - fprintf(stderr, "STUB: Bu::File::setBlocking\n"); -#else - if( bBlocking ) - fcntl( - fd, - F_SETFL, fcntl( fd, F_GETFL, 0 )&(~O_NONBLOCK) - ); - else - fcntl( - fd, - F_SETFL, fcntl( fd, F_GETFL, 0 )|O_NONBLOCK - ); -#endif -} - -Bu::File Bu::File::tempFile( Bu::String &sName ) -{ - uint32_t iX; - iX = time( NULL ) + getpid(); - int iXes; - for( iXes = sName.getSize()-1; iXes >= 0; iXes-- ) - { - if( sName[iXes] != 'X' ) - break; - } - iXes++; - if( iXes == sName.getSize() ) - throw Bu::ExceptionBase("Invalid temporary filename template."); - for( int iter = 0; iter < 100; iter++ ) - { - for( int j = iXes; j < sName.getSize(); j++ ) - { - iX = (1103515245 * iX + 12345); - sName[j] = ('A'+(iX%26)) | ((iX&0x1000)?(0x20):(0)); - } - - try - { - return Bu::File( sName, Bu::File::Read|Bu::File::Write - |Bu::File::Create|Bu::File::Exclusive ); - } catch(...) { } - } - throw Bu::FileException("Failed to create unique temporary file after 100" - " iterations."); -} - -void Bu::File::setSize( Bu::size iSize ) -{ -#ifdef WIN32 - chsize( fd, iSize ); -#else - ftruncate( fd, iSize ); -#endif -} - -Bu::size Bu::File::getSize() const -{ - struct stat st; - fstat( fd, &st ); - return st.st_size; -} - -Bu::size Bu::File::getBlockSize() const -{ -#ifdef WIN32 - return 4096; -#else - struct stat st; - fstat( fd, &st ); - return st.st_blksize; -#endif -} - -Bu::String Bu::File::getLocation() const -{ - return "to be implemented"; -} - -#ifndef WIN32 -void Bu::File::chmod( mode_t t ) -{ - fchmod( fd, t ); -} -#endif - -void Bu::File::flush() -{ - // There is no flushing with direct I/O... - //fflush( fh ); -} - -bool Bu::File::isOpen() -{ - return (fd > -1); -} - -int Bu::File::getPosixFlags( int iFlags ) -{ - int iRet = 0; - switch( (iFlags&ReadWrite) ) - { - // According to posix, O_RDWR is not necesarilly O_RDONLY|O_WRONLY, so - // lets be proper and use the right value in the right place. - case Read: iRet = O_RDONLY; break; - case Write: iRet = O_WRONLY; break; - case ReadWrite: iRet = O_RDWR; break; - default: - throw FileException( - "You must specify Read, Write, or both when opening a file."); - } - - if( (iFlags&Create) ) - iRet |= O_CREAT; - if( (iFlags&Append) ) - iRet |= O_APPEND; - if( (iFlags&Truncate) ) - iRet |= O_TRUNC; -#ifndef WIN32 - if( (iFlags&NonBlock) ) - iRet |= O_NONBLOCK; -#endif - if( (iFlags&Exclusive) == Exclusive ) - iRet |= O_EXCL; - -#ifdef O_BINARY - iRet |= O_BINARY; -#endif - - return iRet; -} - diff --git a/src/file.h b/src/file.h deleted file mode 100644 index e3225fa..0000000 --- a/src/file.h +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_FILE_H -#define BU_FILE_H - -#include -#include - -#include "bu/stream.h" -#include "bu/string.h" -#include "bu/exceptionbase.h" - -namespace Bu -{ - subExceptionDecl( FileException ); - - /** - * A file stream. - *@ingroup Streams - */ - class File : public Bu::Stream - { - public: - File( const Bu::String &sName, int iFlags ); - File( int fd ); - virtual ~File(); - - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Stream::write; - - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - - virtual void flush(); - - virtual bool canRead(); - virtual bool canWrite(); - - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - - enum { - // Flags - Read = 0x01, ///< Open file for reading - Write = 0x02, ///< Open file for writing - Create = 0x04, ///< Create file if it doesn't exist - Truncate = 0x08, ///< Truncate file if it does exist - Append = 0x10, ///< Always append on every write - NonBlock = 0x20, ///< Open file in non-blocking mode - Exclusive = 0x44, ///< Create file, if it exists then fail - - // Helpful mixes - ReadWrite = 0x03, ///< Open for reading and writing - WriteNew = 0x0E ///< Create a file (or truncate) for writing. - /// Same as Write|Create|Truncate - }; - - virtual void setSize( Bu::size iSize ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - /** - * Create a temp file and return its handle. The file is opened - * Read/Write. - *@param sName (Bu::String) Give in the form: "/tmp/tmpfileXXXXXXXX" - * It will alter your (sName) setting the 'X's to random - * characters. - *@returns (Bu::File) A file object representing your temp file. - */ - static Bu::File tempFile( Bu::String &sName ); - -#ifndef WIN32 - /** - * Change the file access permissions. - *@param t (mode_t) The new file access permissions. - */ - void chmod( mode_t t ); -#endif - - private: - int getPosixFlags( int iFlags ); - - private: - int fd; - bool bEos; - }; -} - -#endif diff --git a/src/filter.cpp b/src/filter.cpp deleted file mode 100644 index 3fe8f0e..0000000 --- a/src/filter.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/filter.h" - -Bu::Filter::Filter( Bu::Stream &rNext ) : - rNext( rNext ) -{ -} - -Bu::Filter::~Filter() -{ -} - -void Bu::Filter::close() -{ - stop(); - rNext.close(); -} - -Bu::size Bu::Filter::tell() -{ - return rNext.tell(); -} - -void Bu::Filter::seek( Bu::size offset ) -{ - rNext.seek( offset ); -} - -void Bu::Filter::setPos( Bu::size pos ) -{ - rNext.setPos( pos ); -} - -void Bu::Filter::setPosEnd( Bu::size pos ) -{ - rNext.setPosEnd( pos ); -} - -bool Bu::Filter::isEos() -{ - return rNext.isEos(); -} - -bool Bu::Filter::isOpen() -{ - return rNext.isOpen(); -} - -bool Bu::Filter::canRead() -{ - return rNext.canRead(); -} - -bool Bu::Filter::canWrite() -{ - return rNext.canWrite(); -} - -bool Bu::Filter::isReadable() -{ - return rNext.isReadable(); -} - -bool Bu::Filter::isWritable() -{ - return rNext.isWritable(); -} - -bool Bu::Filter::isSeekable() -{ - return rNext.isSeekable(); -} - -bool Bu::Filter::isBlocking() -{ - return rNext.isBlocking(); -} - -void Bu::Filter::setBlocking( bool bBlocking ) -{ - rNext.setBlocking( bBlocking ); -} - -void Bu::Filter::setSize( Bu::size ) -{ -} - -void Bu::Filter::flush() -{ - rNext.flush(); -} - -Bu::size Bu::Filter::getSize() const -{ - return rNext.getSize(); -} - -Bu::size Bu::Filter::getBlockSize() const -{ - return rNext.getBlockSize(); -} - -Bu::String Bu::Filter::getLocation() const -{ - return rNext.getLocation(); -} - diff --git a/src/filter.h b/src/filter.h deleted file mode 100644 index 2c57805..0000000 --- a/src/filter.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_FILTER_H -#define BU_FILTER_H - -#include - -#include "bu/stream.h" - -namespace Bu -{ - /** - * Data filter base class. Each data filter should contain a read and write - * section. Effectively, the write applies the filter, the read un-applies - * the filter, if possible. For example, BZip2 is a filter that compresses - * on write and decompresses on read. All bi-directional filters should - * follow: x == read( write( x ) ) (byte-for-byte comparison) - * - * Also, all returned buffers should be owned by the filter, and deleted - * when the filter is deleted. This means that the output of a read or - * write operation must be used before the next call to read or write or the - * data will be destroyed. Also, the internal buffer may be changed or - * recreated between calls, so always get a new pointer from a call to - * read or write. - * - * The close function can also return data, so make sure to check for it, - * many filters such as compression filters will buffer data until they have - * enough to create a compression block, in these cases the leftover data - * will be returned by close. - *@ingroup Streams - */ - class Filter : public Bu::Stream - { - public: - Filter( Bu::Stream &rNext ); - virtual ~Filter(); - - virtual void start()=0; - virtual Bu::size stop()=0; - virtual void close(); - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - - virtual void flush(); - - virtual bool canRead(); - virtual bool canWrite(); - - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - - /** - * Most filters won't re-implement this, it doesn't make a lot of sense - * for filters, in general. - */ - virtual void setSize( Bu::size iSize ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - protected: - Bu::Stream &rNext; - - private: - - }; -} - -#endif diff --git a/src/fmt.h b/src/fmt.h deleted file mode 100644 index 15d8efc..0000000 --- a/src/fmt.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef BU_FMT_H -#define BU_FMT_H - -namespace Bu -{ - typedef struct Fmt - { - enum Alignment - { - Left = 0, - Center = 1, - Right = 2 - }; - Fmt() : - uMinWidth( 0 ), - cFillChar(' '), - uRadix( 10 ), - uAlign( Right ), - bPlus( false ), - bCaps( true ), - bTokenize( true ) - { - } - - Fmt( unsigned int uMinWidth, unsigned int uRadix=10, - Alignment a=Right, bool bPlus=false, bool bCaps=true, - char cFill=' ') : - uMinWidth( uMinWidth ), - cFillChar(cFill), - uRadix( uRadix ), - uAlign( a ), - bPlus( bPlus ), - bCaps( bCaps ), - bTokenize( true ) - { - } - Fmt( unsigned int uMinWidth, Alignment a, - unsigned int uRadix=10, bool bPlus=false, bool bCaps=true, - char cFill=' ') : - uMinWidth( uMinWidth ), - cFillChar(cFill), - uRadix( uRadix ), - uAlign( a ), - bPlus( bPlus ), - bCaps( bCaps ), - bTokenize( true ) - { - } - - static Fmt hex( unsigned int uWidth=0, bool bCaps=true ) - { - return Fmt( uWidth, 16, Right, false, bCaps, '0' ); - } - - static Fmt oct( unsigned int uWidth=0 ) - { - return Fmt( uWidth, 8, Right, false, false, '0' ); - } - - static Fmt bin( unsigned int uWidth=0 ) - { - return Fmt( uWidth, 1, Right, false, false, '0' ); - } - - static Fmt ptr( bool bCaps=true ) - { - return Fmt( sizeof(ptrdiff_t)*2, 16, Right, false, bCaps, '0' ); - } - - Fmt &width( unsigned int uWidth ); - Fmt &fill( char cFill='0' ); - Fmt &radix( unsigned int uRadix ); - Fmt &align( Alignment eAlign ); - Fmt &plus( bool bPlus=true ); - Fmt &caps( bool bCaps=true ); - Fmt &tokenize( bool bTokenize=true ); - - Fmt &left(); - Fmt &right(); - Fmt ¢er(); - - unsigned char uMinWidth; - char cFillChar; - unsigned short uRadix : 6; - unsigned short uAlign : 2; - unsigned short bPlus : 1; - unsigned short bCaps : 1; - unsigned short bTokenize : 1; - } Fmt; -}; - -#endif diff --git a/src/formatter.cpp b/src/formatter.cpp deleted file mode 100644 index f275d71..0000000 --- a/src/formatter.cpp +++ /dev/null @@ -1,547 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/formatter.h" - -#include "bu/stream.h" -#include - -template<> float Bu::tlog( float x ) -{ - return logf( x ); -} - -template<> double Bu::tlog( double x ) -{ - return log( x ); -} - -template<> long double Bu::tlog( long double x ) -{ - return logl( x ); -} - -template<> float Bu::tfloor( float x ) -{ - return floorf( x ); -} - -template<> double Bu::tfloor( double x ) -{ - return floor( x ); -} - -template<> long double Bu::tfloor( long double x ) -{ - return floorl( x ); -} - -template<> float Bu::tpow( float x, float y ) -{ - return powf( x, y ); -} - -template<> double Bu::tpow( double x, double y ) -{ - return pow( x, y ); -} - -template<> long double Bu::tpow( long double x, long double y ) -{ - return powl( x, y ); -} - -Bu::Formatter::Formatter( Stream &rStream ) : - rStream( rStream ), - bTempFmt( false ), - uIndent( 0 ), - cIndent( '\t' ) -{ -} - -Bu::Formatter::~Formatter() -{ -} - -void Bu::Formatter::write( const Bu::String &sStr ) -{ - rStream.write( sStr ); -} - -void Bu::Formatter::write( const void *sStr, int iLen ) -{ - rStream.write( sStr, iLen ); -} - -void Bu::Formatter::writeAligned( const Bu::String &sStr ) -{ - int iLen = sStr.getSize(); - if( iLen > fLast.uMinWidth ) - { - write( sStr ); - } - else - { - int iRem = fLast.uMinWidth - iLen; - switch( fLast.uAlign ) - { - case Fmt::Right: - for( int k = 0; k < iRem; k++ ) - write( &fLast.cFillChar, 1 ); - write( sStr ); - break; - - case Fmt::Center: - { - int iHlf = iRem/2; - for( int k = 0; k < iHlf; k++ ) - write( &fLast.cFillChar, 1 ); - write( sStr ); - iHlf = iRem-iHlf;; - for( int k = 0; k < iHlf; k++ ) - write( &fLast.cFillChar, 1 ); - } - break; - - case Fmt::Left: - write( sStr ); - for( int k = 0; k < iRem; k++ ) - write( &fLast.cFillChar, 1 ); - break; - } - } - - usedFormat(); -} - -void Bu::Formatter::writeAligned( const char *sStr, int iLen ) -{ - if( iLen > fLast.uMinWidth ) - { - write( sStr, iLen ); - } - else - { - int iRem = fLast.uMinWidth - iLen; - switch( fLast.uAlign ) - { - case Fmt::Right: - for( int k = 0; k < iRem; k++ ) - write( &fLast.cFillChar, 1 ); - write( sStr, iLen ); - break; - - case Fmt::Center: - { - int iHlf = iRem/2; - for( int k = 0; k < iHlf; k++ ) - write( &fLast.cFillChar, 1 ); - write( sStr, iLen ); - iHlf = iRem-iHlf;; - for( int k = 0; k < iHlf; k++ ) - write( &fLast.cFillChar, 1 ); - } - break; - - case Fmt::Left: - write( sStr, iLen ); - for( int k = 0; k < iRem; k++ ) - write( &fLast.cFillChar, 1 ); - break; - } - } - - usedFormat(); -} - -void Bu::Formatter::read( void *sStr, int iLen ) -{ - rStream.read( sStr, iLen ); -} - -Bu::String Bu::Formatter::readToken() -{ - Bu::String sRet; - if( fLast.bTokenize ) - { - for(;;) - { - char buf; - int iRead = rStream.read( &buf, 1 ); - if( iRead == 0 ) - return sRet; - if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' ) - continue; - else - { - sRet += buf; - break; - } - } - for(;;) - { - char buf; - int iRead = rStream.read( &buf, 1 ); - if( iRead == 0 ) - return sRet; - if( buf == ' ' || buf == '\t' || buf == '\n' || buf == '\r' ) - return sRet; - else - sRet += buf; - } - } - else - { - for(;;) - { - char buf; - int iRead = rStream.read( &buf, 1 ); - if( iRead == 0 ) - return sRet; - else - sRet += buf; - } - } -} - -void Bu::Formatter::incIndent() -{ - if( uIndent < 0xFFU ) - uIndent++; -} - -void Bu::Formatter::decIndent() -{ - if( uIndent > 0 ) - uIndent--; -} - -void Bu::Formatter::setIndent( uint8_t uLevel ) -{ - uIndent = uLevel; -} - -void Bu::Formatter::clearIndent() -{ - uIndent = 0; -} - -void Bu::Formatter::setIndentChar( char cIndent ) -{ - this->cIndent = cIndent; -} - -void Bu::Formatter::doFlush() -{ - rStream.flush(); -} - -Bu::Fmt &Bu::Fmt::width( unsigned int uWidth ) -{ - this->uMinWidth = uWidth; - return *this; -} - -Bu::Fmt &Bu::Fmt::fill( char cFill ) -{ - this->cFillChar = (unsigned char)cFill; - return *this; -} - -Bu::Fmt &Bu::Fmt::radix( unsigned int uRadix ) -{ - this->uRadix = uRadix; - return *this; -} - -Bu::Fmt &Bu::Fmt::align( Alignment eAlign ) -{ - this->uAlign = eAlign; - return *this; -} - -Bu::Fmt &Bu::Fmt::left() -{ - this->uAlign = Fmt::Left; - return *this; -} - -Bu::Fmt &Bu::Fmt::center() -{ - this->uAlign = Fmt::Center; - return *this; -} - -Bu::Fmt &Bu::Fmt::right() -{ - this->uAlign = Fmt::Right; - return *this; -} - -Bu::Fmt &Bu::Fmt::plus( bool bPlus ) -{ - this->bPlus = bPlus; - return *this; -} - -Bu::Fmt &Bu::Fmt::caps( bool bCaps ) -{ - this->bCaps = bCaps; - return *this; -} - -Bu::Fmt &Bu::Fmt::tokenize( bool bTokenize ) -{ - this->bTokenize = bTokenize; - return *this; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Fmt &fmt ) -{ - f.setTempFormat( fmt ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, Bu::Formatter::Special s ) -{ - switch( s ) - { - case Formatter::nl: - { -#ifdef WIN32 - f.write("\r\n", 2 ); -#else - f.write("\n", 1 ); -#endif - char ci = f.getIndentChar(); - for( int j = 0; j < f.getIndent(); j++ ) - f.write( &ci, 1 ); - f.doFlush(); - } - break; - - case Formatter::flush: - f.doFlush(); - break; - } - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const char *sStr ) -{ - f.writeAligned( sStr, strlen( sStr ) ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char *sStr ) -{ - f.writeAligned( sStr, strlen( sStr ) ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::String &sStr ) -{ - f.writeAligned( sStr ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed char c ) -{ - f.ifmt( c ); - //f.write( (char *)&c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, char c ) -{ - f.write( (char *)&c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned char c ) -{ - f.ufmt( c ); - //f.write( (char *)&c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed short i ) -{ - f.ifmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned short i ) -{ - f.ufmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed int i ) -{ - f.ifmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned int i ) -{ - f.ufmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long i ) -{ - f.ifmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long i ) -{ - f.ufmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, signed long long i ) -{ - f.ifmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, unsigned long long i ) -{ - f.ufmt( i ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, float flt ) -{ - f.ffmt( flt ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, double flt ) -{ - f.ffmt( flt ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, long double flt ) -{ - f.ffmt( flt ); - return f; -} - -Bu::Formatter &Bu::operator<<( Bu::Formatter &f, bool b ) -{ - f.writeAligned( b?("true"):("false") ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, Bu::String &sStr ) -{ - sStr = f.readToken(); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed char &c ) -{ - f.read( &c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, char &c ) -{ - f.read( &c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned char &c ) -{ - f.read( &c, 1 ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed short &i ) -{ - f.iparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned short &i ) -{ - f.uparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed int &i ) -{ - f.iparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned int &i ) -{ - f.uparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long &i ) -{ - f.iparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long &i ) -{ - f.uparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, signed long long &i ) -{ - f.iparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, unsigned long long &i ) -{ - f.uparse( i, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, float &flt ) -{ - f.fparse( flt, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, double &flt ) -{ - f.fparse( flt, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, long double &flt ) -{ - f.fparse( flt, f.readToken() ); - return f; -} - -Bu::Formatter &Bu::operator>>( Bu::Formatter &f, bool &b ) -{ - Bu::String sStr = f.readToken(); - if( !sStr.isSet() ) - return f; - char c = *sStr.begin(); - if( c == 'y' || c == 'Y' || c == 't' || c == 'T' ) - b = true; - else if( c == 'n' || c == 'N' || c == 'f' || c == 'F' ) - b = false; - - return f; -} - diff --git a/src/formatter.h b/src/formatter.h deleted file mode 100644 index a440ec3..0000000 --- a/src/formatter.h +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_FORMATTER_H -#define BU_FORMATTER_H - -#include "bu/string.h" -#include "bu/fmt.h" - -#include - -namespace Bu -{ - class Stream; - - template t tlog( t x ); - template<> float tlog( float x ); - template<> double tlog( double x ); - template<> long double tlog( long double x ); - - template t tfloor( t x ); - template<> float tfloor( float x ); - template<> double tfloor( double x ); - template<> long double tfloor( long double x ); - - template t tpow( t x, t y ); - template<> float tpow( float x, float y ); - template<> double tpow( double x, double y ); - template<> long double tpow( long double x, long double y ); - - class Formatter - { - public: - Formatter( Stream &rStream ); - virtual ~Formatter(); - - void write( const Bu::String &sStr ); - void write( const void *sStr, int iLen ); - void writeAligned( const Bu::String &sStr ); - void writeAligned( const char *sStr, int iLen ); - - void read( void *sStr, int iLen ); - Bu::String readToken(); - - void incIndent(); - void decIndent(); - void setIndent( uint8_t uLevel ); - void clearIndent(); - uint8_t getIndent() const { return uIndent; } - void setIndentChar( char cIndent ); - char getIndentChar() const { return cIndent; } - - void setFormat( const Fmt &f ) - { - fLast = f; - bTempFmt = false; - } - - void setTempFormat( const Fmt &f ) - { - fLast = f; - bTempFmt = true; - } - - void usedFormat() - { - if( bTempFmt ) - fLast = Fmt(); - } - - template - void ifmt( type i ) - { - // This code is taken from Nango, hopefully we can make it better. - bool bNeg = i<0; - char cBase = fLast.bCaps?'A':'a'; - char buf[sizeof(type)*8+1]; - if( bNeg ) i = -i; - if( fLast.uRadix < 2 || fLast.uRadix > 36 ) - { - usedFormat(); - return; - } - - for( int j = sizeof(type)*8; j >= 0; j-- ) - { - int c = i%fLast.uRadix; - i /= fLast.uRadix; - buf[j] = (char)((c<10)?('0'+c):(cBase+c-10)); - if( i == 0 ) - { - if( bNeg ) buf[--j] = '-'; - else if( fLast.bPlus ) buf[--j] = '+'; - writeAligned( buf+j, sizeof(type)*8-j+1 ); - - return; - } - } - usedFormat(); - } - - template - void ufmt( type i ) - { - // This code is taken from Nango, hopefully we can make it better. - char buf[sizeof(type)*8+1]; - char cBase = fLast.bCaps?'A':'a'; - if( fLast.uRadix < 2 || fLast.uRadix > 36 ) - { - usedFormat(); - return; - } - - for( int j = sizeof(type)*8; j >= 0; j-- ) - { - int c = i%fLast.uRadix; - i /= fLast.uRadix; - buf[j] = (char)((c<10)?('0'+c):(cBase+c-10)); - if( i == 0 ) - { - if( fLast.bPlus ) buf[--j] = '+'; - writeAligned( buf+j, sizeof(type)*8-j+1 ); - - return; - } - } - usedFormat(); - } - - template - void ffmt( type f ) - { - Bu::String fTmp; - char cBase = fLast.bCaps?'A':'a'; - if( fLast.uRadix < 2 || fLast.uRadix > 36 ) - { - usedFormat(); - return; - } - - if( signbit(f) ) - { - f = -f; - fTmp += "-"; - } - int iScale = tfloor(tlog( f ) / tlog( (type)fLast.uRadix )); - f /= tpow( (type)fLast.uRadix, (type)iScale ); - - if( iScale < 0 ) - { - fTmp += "0."; - for( int j = 1; j < -iScale; j++ ) - fTmp += '0'; - } - int c = f; - fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); - f -= (int)f; - int j; - for( j = 0; j < 8 && f; j++ ) - { - if( iScale - j == 0 ) - fTmp += '.'; - f = f*fLast.uRadix; - int c = f; - fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); - f -= (int)f; - } - if( iScale >= j ) - { - for( int k = j; k < iScale; k++ ) - fTmp += '0'; - fTmp += ".0"; - } - - writeAligned( fTmp ); - usedFormat(); - } - - template - void iparse( type &i, const Bu::String &sBuf ) - { - if( !sBuf.isSet() ) - return; - if( sBuf[0] != '+' && sBuf[0] != '-' && - (sBuf[0] < '0' && sBuf[0] > '9') ) - return; - int j = 1; - int iMax = sBuf.getSize(); - for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } - i = 0; - type iPos = 1; - for(j--; j >= 0; j-- ) - { - if( sBuf[j] == '+' || sBuf[j] == '-' ) - continue; - i += (sBuf[j]-'0')*iPos; - iPos *= fLast.uRadix; - } - if( sBuf[0] == '-' ) - i = -i; - - usedFormat(); - } - - template - void uparse( type &i, const Bu::String &sBuf ) - { - if( !sBuf.isSet() ) - return; - if( sBuf[0] != '+' && - (sBuf[0] < '0' && sBuf[0] > '9') ) - return; - int j = 1; - int iMax = sBuf.getSize(); - for(; j < iMax && (sBuf[j] >= '0' && sBuf[j] <= '9'); j++ ) { } - i = 0; - type iPos = 1; - for(j--; j >= 0; j-- ) - { - if( sBuf[j] == '+' ) - continue; - i += (sBuf[j]-'0')*iPos; - iPos *= fLast.uRadix; - } - - usedFormat(); - } - - template - void fparse( type &f, const Bu::String &sBuf ) - { - double fIn; - sscanf( sBuf.getStr(), "%lf", &fIn ); - f = fIn; - usedFormat(); - } - - enum Special - { - nl, - flush - }; - - void doFlush(); - - private: - Stream &rStream; - Fmt fLast; - bool bTempFmt; - uint8_t uIndent; - char cIndent; - }; - - Formatter &operator<<( Formatter &f, const Fmt &fmt ); - Formatter &operator<<( Formatter &f, Formatter::Special s ); - Formatter &operator<<( Formatter &f, const char *sStr ); - Formatter &operator<<( Formatter &f, char *sStr ); - Formatter &operator<<( Formatter &f, const Bu::String &sStr ); - Formatter &operator<<( Formatter &f, signed char c ); - Formatter &operator<<( Formatter &f, char c ); - Formatter &operator<<( Formatter &f, unsigned char c ); - Formatter &operator<<( Formatter &f, signed short i ); - Formatter &operator<<( Formatter &f, unsigned short i ); - Formatter &operator<<( Formatter &f, signed int i ); - Formatter &operator<<( Formatter &f, unsigned int i ); - Formatter &operator<<( Formatter &f, signed long i ); - Formatter &operator<<( Formatter &f, unsigned long i ); - Formatter &operator<<( Formatter &f, signed long long i ); - Formatter &operator<<( Formatter &f, unsigned long long i ); - Formatter &operator<<( Formatter &f, float flt ); - Formatter &operator<<( Formatter &f, double flt ); - Formatter &operator<<( Formatter &f, long double flt ); - Formatter &operator<<( Formatter &f, bool b ); - - Formatter &operator>>( Formatter &f, Bu::String &sStr ); - Formatter &operator>>( Formatter &f, signed char &c ); - Formatter &operator>>( Formatter &f, char &c ); - Formatter &operator>>( Formatter &f, unsigned char &c ); - Formatter &operator>>( Formatter &f, signed short &i ); - Formatter &operator>>( Formatter &f, unsigned short &i ); - Formatter &operator>>( Formatter &f, signed int &i ); - Formatter &operator>>( Formatter &f, unsigned int &i ); - Formatter &operator>>( Formatter &f, signed long &i ); - Formatter &operator>>( Formatter &f, unsigned long &i ); - Formatter &operator>>( Formatter &f, signed long long &i ); - Formatter &operator>>( Formatter &f, unsigned long long &i ); - Formatter &operator>>( Formatter &f, float &flt ); - Formatter &operator>>( Formatter &f, double &flt ); - Formatter &operator>>( Formatter &f, long double &flt ); - Formatter &operator>>( Formatter &f, bool &b ); - - template - Formatter &operator<<( Formatter &f, const type *p ) - { - return f << "0x" << Fmt::hex(sizeof(ptrdiff_t)*2) << (ptrdiff_t)(p); - } -}; - -#endif diff --git a/src/formula.cpp b/src/formula.cpp deleted file mode 100644 index ac435ed..0000000 --- a/src/formula.cpp +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/formula.h" - -namespace Bu -{ - subExceptionDef( FormulaException ); -} - diff --git a/src/formula.h b/src/formula.h deleted file mode 100644 index 687e6c3..0000000 --- a/src/formula.h +++ /dev/null @@ -1,430 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef FORMULA_H -#define FORMULA_H - -#include -#include - -#include -//#include "sbuffer.h" - -#include "bu/stack.h" -#include "bu/exceptionbase.h" -#include "bu/hash.h" -#include "bu/string.h" - -namespace Bu -{ - subExceptionDecl( FormulaException ); - /** - * Implements a very simple formula parser that allows use of variables and - * custom functions. This is based on a simple calculator-type parser that - * executes as it processes, accounting for operator precedence and - * grouping. - * - * prec = precision, a type to use for all math (except binary ops) - * bin = binary type, a type to hard cast all data to for binary ops - */ - template - class Formula - { - public: - class Func - { - public: - virtual prec operator()( prec )=0; - }; - - typedef Hash varHash; - typedef Hash funcHash; - - Formula() - { - } - - virtual ~Formula() - { - for( typename funcHash::iterator i = hFunc.begin(); - i != hFunc.end(); i++ ) - { - delete (*i); - } - } - - prec run( const Bu::String &sFormulaSrc ) - { - if( sFormulaSrc.isEmpty() ) - throw FormulaException("Empty formula, nothing to do."); - try - { - const char *sFormula = sFormulaSrc.getStr(); - for(;;) - { - uint8_t tNum = nextToken( &sFormula ); - if( tNum == symSubtract ) - { - sOper.push( symNegate ); - continue; - } - else if( tNum == symNot ) - { - sOper.push( symNot ); - continue; - } - else if( tNum == symOpenParen ) - { - sOper.push( tNum ); - continue; - } - else if( tNum == symFunction ) - { - sOper.push( symFunction ); - continue; - } - else if( tNum == symEOS ) - { - throw Bu::FormulaException( - "Cannot end with an operator."); - } - - oppart: uint8_t tOpr = nextToken( &sFormula ); - if( tOpr == symEOS ) - { - reduce(); - prec ret = sValue.top(); - sValue.clear(); - sFunc.clear(); - sOper.clear(); - return ret; - } - if( !sOper.isEmpty() && getPrec( sOper.top() ) > - getPrec( tOpr ) ) - { - reduce(); - } - if( tOpr != symCloseParen ) - { - sOper.push( tOpr ); - } - else - { - reduce( true ); - goto oppart; - } - } - } - catch( ... ) - { - sValue.clear(); - sFunc.clear(); - sOper.clear(); - throw; - } - } - - varHash hVars; - funcHash hFunc; - - private: - enum - { - symEOS, - symAdd, - symSubtract, - symMultiply, - symDivide, - symOpenParen, - symCloseParen, - symNumber, - symVariable, - symFunction, - symExponent, - symNegate, - symModulus, - - symAnd, - symOr, - symXor, - symNot - }; - - typedef uint8_t symType; - - Bu::Stack sOper; - Bu::Stack sValue; - Bu::Stack sFunc; - - private: - symType getPrec( symType nOper ) - { - switch( nOper ) - { - case symNumber: - case symVariable: - case symOpenParen: - case symCloseParen: - return 0; - - case symAdd: - case symSubtract: - return 1; - - case symMultiply: - case symDivide: - case symModulus: - return 2; - - case symAnd: - case symOr: - case symXor: - return 2; - - case symExponent: - case symNot: - case symNegate: - case symFunction: - return 3; - - default: - return 0; - } - } - - symType nextToken( const char **sBuf ) - { - for(;;) - { - char cbuf = **sBuf; - ++(*sBuf); - switch( cbuf ) - { - case '+': - return symAdd; - - case '-': - return symSubtract; - - case '*': - return symMultiply; - - case '/': - return symDivide; - - case '^': - return symExponent; - - case '%': - return symModulus; - - case '(': - return symOpenParen; - - case ')': - return symCloseParen; - - case '|': - return symOr; - - case '&': - return symAnd; - - case '#': - return symXor; - - case '~': - return symNot; - - case ' ': - case '\t': - case '\n': - case '\r': - break; - - case '\0': - return symEOS; - - default: - if( cbuf == '.' || (cbuf >= '0' && cbuf <= '9') ) - { - char num[50]={cbuf}; - int nPos = 1; - bool bDot = false; - - for(;;) - { - cbuf = **sBuf; - if( cbuf == '.' ) - { - if( bDot == false ) - bDot = true; - else - throw FormulaException( - "Numbers cannot have more than one " - ". in them." - ); - } - if( cbuf == '.' || - (cbuf >= '0' && cbuf <= '9') ) - { - num[nPos++] = cbuf; - } - else - { - num[nPos] = '\0'; - sValue.push( - static_cast( - strtod( num, NULL ) - ) - ); - return symNumber; - } - ++(*sBuf); - } - } - else if( (cbuf >= 'a' && cbuf <= 'z') || - (cbuf >= 'A' && cbuf <= 'Z') || - (cbuf == '_') ) - { - char tok[50]={cbuf}; - int nPos = 1; - - for(;;) - { - cbuf = **sBuf; - if( (cbuf >= 'a' && cbuf <= 'z') || - (cbuf >= 'A' && cbuf <= 'Z') || - (cbuf >= '0' && cbuf <= '9') || - cbuf == '_' || cbuf == '.' || cbuf == ':' ) - { - tok[nPos++] = cbuf; - } - else - { - tok[nPos] = '\0'; - if( hVars.has( tok ) ) - { - sValue.push( hVars[tok] ); - return symNumber; - } - else if( hFunc.has( tok ) ) - { - sFunc.push( tok ); - return symFunction; - } - else - { - throw FormulaException( - "No variable or function named " - "\"%s\" exists.", - tok - ); - } - } - ++(*sBuf); - } - } - break; - } - } - } - - void reduce( bool bCloseParen = false ) - { - while( !sOper.isEmpty() ) - { - uint8_t nOpr = sOper.top(); - if( nOpr == symOpenParen ) - { - if( bCloseParen == true ) - sOper.pop(); - return; - } - sOper.pop(); - - prec dTop = sValue.top(); - sValue.pop(); - - switch( nOpr ) - { - case symAdd: - sValue.top() += dTop; - break; - - case symSubtract: - sValue.top() -= dTop; - break; - - case symMultiply: - sValue.top() *= dTop; - break; - - case symDivide: - sValue.top() /= dTop; - break; - - case symExponent: - sValue.top() = static_cast( - pow( sValue.top(), dTop ) - ); - break; - - case symModulus: - sValue.top() = static_cast( - fmod( sValue.top(), dTop ) - ); - break; - - case symOr: - sValue.top() = static_cast( - static_cast(sValue.top()) | - static_cast(dTop) - ); - break; - - case symAnd: - sValue.top() = static_cast( - static_cast(sValue.top()) & - static_cast(dTop) - ); - break; - - case symXor: - sValue.top() = static_cast( - static_cast(sValue.top()) ^ - static_cast(dTop) - ); - break; - - case symFunction: - sValue.push( (*hFunc.get( sFunc.pop() ))( dTop ) ); - break; - - case symNegate: - sValue.push( -dTop ); - break; - - case symNot: - sValue.push( static_cast( - ~static_cast(dTop) - ) ); - break; - } - } - - if( bCloseParen == true ) - { - throw FormulaException( - "Close-paren found without matching open-paren." - ); - } - } - }; -} - -#endif diff --git a/src/hash.cpp b/src/hash.cpp deleted file mode 100644 index 59572ec..0000000 --- a/src/hash.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/hash.h" - -namespace Bu { subExceptionDef( HashException ) } - -template<> -uint32_t Bu::__calcHashCode( const char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool Bu::__cmpHashKeys( const char * const &a, const char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - -template<> -uint32_t Bu::__calcHashCode( char * const &k ) -{ - if (k == NULL) - { - return 0; - } - - unsigned long int nPos = 0; - for( const char *s = k; *s; s++ ) - { - nPos = *s + (nPos << 6) + (nPos << 16) - nPos; - } - - return nPos; -} - -template<> bool Bu::__cmpHashKeys( char * const &a, char * const &b ) -{ - if( a == b ) - return true; - - for(int j=0; a[j] == b[j]; j++ ) - if( a[j] == '\0' ) - return true; - - return false; -} - diff --git a/src/hash.h b/src/hash.h deleted file mode 100644 index 71aec73..0000000 --- a/src/hash.h +++ /dev/null @@ -1,1306 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_HASH_H -#define BU_HASH_H - -#include -#include "bu/exceptionbase.h" -#include "bu/list.h" -#include "bu/util.h" -#include "bu/archivebase.h" -#include "bu/sharedcore.h" - -namespace Bu -{ - subExceptionDecl( HashException ) - - enum eHashException - { - excodeNotFilled - }; - - template - uint32_t __calcHashCode( const T &k ); - - template - bool __cmpHashKeys( const T &a, const T &b ); - - /** - * Default functor used to compute the size of hash tables. This version - * effectively doubles the size of the table when space is low, ensuring - * that you always wind up with an odd number for the table size. A - * better but slower option is to always find the next prime number that's - * above double your current table size, but that has the potential to be - * slower. - */ - struct __calcNextTSize_fast - { - uint32_t operator()( uint32_t nCapacity, uint32_t nFilled, - uint32_t nDeleted ) const - { - // This frist case will allow hashtables that are mostly deleted - // items to reset to small allocations - if( nFilled-nDeleted <= nCapacity/4 ) - { - nCapacity = 11; - while( nCapacity < nFilled*5/4 ) - nCapacity = nCapacity*2+1; - return nCapacity; - } - // This will hopefully prevent hash tables from growing needlessly - if( nFilled-nDeleted <= nCapacity/2 ) - return nCapacity; - // Otherwise, just increase the capacity - return nCapacity*2+1; - } - }; - - template - int bitsTo( int iCount ) - { - return ( (iCount/(sizeof(totype)*8)) - + (iCount%(sizeof(totype)*8)>0 ? 1 : 0)); - } - - template - class Hash; - - /** @cond DEVEL */ - template - class HashCore - { - friend class Hash; - friend class SharedCore< - Hash, - HashCore - >; - private: - HashCore() : - nCapacity( 0 ), - nFilled( 0 ), - nDeleted( 0 ), - bFilled( NULL ), - bDeleted( NULL ), - aKeys( NULL ), - aValues( NULL ), - aHashCodes( NULL ) - { - } - - virtual ~HashCore() - { - clear(); - } - - void init() - { - if( nCapacity > 0 ) - return; - - nCapacity = 11; - nKeysSize = bitsTo( nCapacity ); - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - } - - void clearBits() - { - if( nCapacity == 0 ) - return; - - for( uint32_t j = 0; j < nKeysSize; j++ ) - { - bFilled[j] = bDeleted[j] = 0; - } - } - - void fill( uint32_t loc, const key &k, const value &v, uint32_t hash ) - { - init(); - - bFilled[loc/32] |= (1<<(loc%32)); - va.construct( &aValues[loc], v ); - ka.construct( &aKeys[loc], k ); - aHashCodes[loc] = hash; - nFilled++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - void _erase( uint32_t loc ) - { - if( nCapacity == 0 ) - return; - - bDeleted[loc/32] |= (1<<(loc%32)); - va.destroy( &aValues[loc] ); - ka.destroy( &aKeys[loc] ); - nDeleted++; - //printf("Filled: %d, Deleted: %d, Capacity: %d\n", - // nFilled, nDeleted, nCapacity ); - } - - key &getKeyAtPos( uint32_t nPos ) - { - if( nPos >= nCapacity ) - throw HashException("Referenced position invalid."); - return aKeys[nPos]; - } - - const key &getKeyAtPos( uint32_t nPos ) const - { - if( nPos >= nCapacity ) - throw HashException("Referenced position invalid."); - return aKeys[nPos]; - } - - value &getValueAtPos( uint32_t nPos ) - { - if( nPos >= nCapacity ) - throw HashException("Referenced position invalid."); - return aValues[nPos]; - } - - const value &getValueAtPos( uint32_t nPos ) const - { - if( nPos >= nCapacity ) - throw HashException("Referenced position invalid."); - return aValues[nPos]; - } - - uint32_t getFirstPos( bool &bFinished ) const - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - uint32_t getNextPos( uint32_t nPos, bool &bFinished ) const - { - for( uint32_t j = nPos+1; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - return j; - } - - bFinished = true; - return 0; - } - - uint32_t probe( uint32_t hash, const key &k, bool &bFill, bool rehash=true ) - { - init(); - - uint32_t nCur = hash%nCapacity; - - // First we scan to see if the key is already there, abort if we - // run out of probing room, or we find a non-filled entry - int8_t j; - for( j = 0; - isFilled( nCur ) && j < 32; - nCur = (nCur + (1<( nCapacity ); - - // Allocate new memory + prep - bFilled = ca.allocate( nKeysSize ); - bDeleted = ca.allocate( nKeysSize ); - clearBits(); - - aHashCodes = ca.allocate( nCapacity ); - aKeys = ka.allocate( nCapacity ); - aValues = va.allocate( nCapacity ); - - nDeleted = nFilled = 0; - - // Re-insert all of the old data (except deleted items) - for( uint32_t j = 0; j < nOldCapacity; j++ ) - { - if( (bOldFilled[j/32]&(1<<(j%32)))!=0 && - (bOldDeleted[j/32]&(1<<(j%32)))==0 ) - { - insert( aOldKeys[j], aOldValues[j] ); - } - } - - // Delete all of the old data - for( uint32_t j = 0; j < nOldCapacity; j++ ) - { - if( (bOldFilled[j/32]&(1<<(j%32)))!=0 && - (bOldDeleted[j/32]&(1<<(j%32)))==0 ) - { - va.destroy( &aOldValues[j] ); - ka.destroy( &aOldKeys[j] ); - } - } - va.deallocate( aOldValues, nOldCapacity ); - ka.deallocate( aOldKeys, nOldCapacity ); - ca.deallocate( bOldFilled, nOldKeysSize ); - ca.deallocate( bOldDeleted, nOldKeysSize ); - ca.deallocate( aOldHashCodes, nOldCapacity ); - } - - bool isFilled( uint32_t loc ) const - { - if( loc >= nCapacity ) - throw HashException("Referenced position invalid."); - return (bFilled[loc/32]&(1<<(loc%32)))!=0; - } - - bool isDeleted( uint32_t loc ) const - { - if( loc >= nCapacity ) - throw HashException("Referenced position invalid."); - return (bDeleted[loc/32]&(1<<(loc%32)))!=0; - } - - void clear() - { - for( uint32_t j = 0; j < nCapacity; j++ ) - { - if( isFilled( j ) ) - if( !isDeleted( j ) ) - { - va.destroy( &aValues[j] ); - ka.destroy( &aKeys[j] ); - } - } - va.deallocate( aValues, nCapacity ); - ka.deallocate( aKeys, nCapacity ); - ca.deallocate( bFilled, nKeysSize ); - ca.deallocate( bDeleted, nKeysSize ); - ca.deallocate( aHashCodes, nCapacity ); - - bFilled = NULL; - bDeleted = NULL; - aKeys = NULL; - aValues = NULL; - aHashCodes = NULL; - - nCapacity = 0; - nFilled = 0; - nDeleted = 0; - } - - uint32_t nCapacity; - uint32_t nFilled; - uint32_t nDeleted; - uint32_t *bFilled; - uint32_t *bDeleted; - uint32_t nKeysSize; - key *aKeys; - value *aValues; - uint32_t *aHashCodes; - valuealloc va; - keyalloc ka; - challoc ca; - sizecalc szCalc; - }; - /** @endcond */ - - /** - * Libbu++ Template Hash Table. This is your average hash table, that uses - * template functions in order to do fast, efficient, generalized hashing. - * It's pretty easy to use, and works well with all other libbu++ types so - * far. - * - * In order to use it, I recommend the following for all basic usage: - *@code - // Define a Hash typedef with strings as keys and ints as values. - typedef Bu::Hash StrIntHash; - - // Create one - StrIntHash hInts; - - // Insert some integers - hInts["one"] = 1; - hInts["forty-two"] = 42; - hInts.insert("forty two", 42 ); - - // Get values out of the hash, the last two options are the most explicit, - // and must be used if the hash's value type does not match what you're - // comparing to exactly. - if( hInts["one"] == 1 ) doSomething(); - if( hInts["forty-two"].value() == 42 ) doSomething(); - if( hInts.get("forty two") == 42 ) doSomething(); - - // Iterate through the Hash - for( StrIntHash::iterator i = hInts.begin(); i != hInts.end(); i++ ) - { - // i.getValue() works too - print("'%s' = %d\n", i.getKey().getStr(), (*i) ); - } - - @endcode - *@param key (typename) The datatype of the hashtable keys - *@param value (typename) The datatype of the hashtable data - *@param sizecalc (typename) Functor to compute new table size on rehash - *@param keyalloc (typename) Memory allocator for hashtable keys - *@param valuealloc (typename) Memory allocator for hashtable values - *@param challoc (typename) Byte allocator for bitflags - *@ingroup Containers - */ - template, - typename valuealloc = std::allocator, - typename challoc = std::allocator - > - class Hash : public SharedCore< - Hash, - HashCore - > - { - private: - typedef class HashCore Core; - typedef class Hash MyType; - protected: - using SharedCore::core; - using SharedCore::_hardCopy; - using SharedCore::_resetCore; - using SharedCore::_allocateCore; - - public: - Hash() - { - } - - Hash( const MyType &src ) : - SharedCore( src ) - { - } - - virtual ~Hash() - { - } - - /** - * Get the current hash table capacity. (Changes at re-hash) - *@returns (uint32_t) The current capacity. - */ - uint32_t getCapacity() const - { - return core->nCapacity; - } - - /** - * Get the number of hash locations spoken for. (Including - * not-yet-cleaned-up deleted items.) - *@returns (uint32_t) The current fill state. - */ - uint32_t getFill() const - { - return core->nFilled; - } - - /** - * Get the number of items stored in the hash table. - *@returns (uint32_t) The number of items stored in the hash table. - */ - uint32_t getSize() const - { - return core->nFilled-core->nDeleted; - } - - bool isEmpty() const - { - return (core->nFilled-core->nDeleted) == 0; - } - - /** - * Get the number of items which have been deleted, but not yet - * cleaned up. - *@returns (uint32_t) The number of deleted items. - */ - uint32_t getDeleted() const - { - return core->nDeleted; - } - - struct HashProxy - { - friend class Hash; - private: - HashProxy( MyType &h, const key *k, uint32_t nPos, uint32_t hash ) : - hsh( h ), - pKey( k ), - nPos( nPos ), - hash( hash ), - bFilled( false ) - { - } - - HashProxy( MyType &h, uint32_t nPos, value *pValue ) : - hsh( h ), - nPos( nPos ), - pValue( pValue ), - bFilled( true ) - { - } - - MyType &hsh; - const key *pKey; - uint32_t nPos; - value *pValue; - uint32_t hash; - bool bFilled; - - public: - /** - * Cast operator for HashProxy. - *@returns (value_type &) The value the HashProxy is pointing to. - */ - operator value &() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - /** - * Direct function for retrieving a value out of the HashProxy. - *@returns (value_type &) The value pointed to by this HashProxy. - */ - value &getValue() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return *pValue; - } - - /** - * Whether this HashProxy points to something real or not. - */ - bool isFilled() - { - return bFilled; - } - - /** - * Erase the data pointed to by this HashProxy. - */ - void erase() - { - if( bFilled ) - { - hsh.core->_erase( nPos ); - } - } - - /** - * Assign data to this point in the hash table. - *@param nval (value_type) the data to assign. - */ - value operator=( value nval ) - { - if( bFilled ) - { - hsh.core->va.destroy( &hsh.core->aValues[nPos] ); - hsh.core->va.construct( &hsh.core->aValues[nPos], nval ); - } - else - { - hsh.core->fill( nPos, *pKey, nval, hash ); - } - - return nval; - } - - /** - * Pointer extraction operator. Access to members of data pointed to - * by HashProxy. - *@returns (value_type *) - */ - value *operator->() - { - if( bFilled == false ) - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - return pValue; - } - }; - - /** - * Hash table index operator - *@param k (key_type) Key of data to be retrieved. - *@returns (HashProxy) Proxy pointing to the data. - */ - HashProxy operator[]( const key &k ) - { - _hardCopy(); - - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = core->probe( hash, k, bFill ); - - if( bFill ) - { - return HashProxy( *this, nPos, &core->aValues[nPos] ); - } - else - { - return HashProxy( *this, &k, nPos, hash ); - } - } - - /** - * Insert a value (v) under key (k) into the hash table - *@param k (key_type) Key to list the value under. - *@param v (value_type) Value to store in the hash table. - */ - void insert( const key &k, const value &v ) - { - _hardCopy(); - - core->insert( k, v ); - } - - /** - * Remove a value from the hash table. - *@param k (key_type) The data under this key will be erased. - */ - void erase( const key &k ) - { - _hardCopy(); - - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = core->probe( hash, k, bFill ); - - if( bFill ) - { - core->_erase( nPos ); - } - } - - struct iterator; - - /** - * Remove a value from the hash pointed to from an iterator. - *@param i (iterator &) The data to be erased. - */ - void erase( struct iterator &i ) - { - if( this != i.hsh ) - throw HashException("This iterator didn't come from this Hash."); - - _hardCopy(); - - if( core->isFilled( i.nPos ) && !core->isDeleted( i.nPos ) ) - { - core->_erase( i.nPos ); - } - } - - /** - * Remove all data from the hash table. - */ - virtual void clear() - { - _resetCore(); - } - - /** - * Get an item of data from the hash table. - *@param k (key_type) Key pointing to the data to be retrieved. - *@returns (value_type &) The data pointed to by (k). - */ - value &get( const key &k ) - { - _hardCopy(); - - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = core->probe( hash, k, bFill, false ); - - if( bFill ) - { - return core->aValues[nPos]; - } - else - { - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - } - } - - /** - * Get a const item of data from the hash table. - *@param k (key_type) Key pointing to the data to be retrieved. - *@returns (const value_type &) A const version of the data pointed - * to by (k). - */ - const value &get( const key &k ) const - { - uint32_t hash = __calcHashCode( k ); - bool bFill; - uint32_t nPos = core->probe( hash, k, bFill ); - - if( bFill ) - { - return core->aValues[nPos]; - } - else - { - throw HashException( - excodeNotFilled, - "No data assosiated with that key." - ); - } - } - - /** - * Does the hash table contain an item under key (k). - *@param k (key_type) The key to check. - *@returns (bool) Whether there was an item in the hash under key (k). - */ - bool has( const key &k ) const - { - bool bFill; - core->probe( __calcHashCode( k ), k, bFill ); - - return bFill; - } - - /** - * Iteration structure for iterating through the hash. - */ - typedef struct iterator - { - friend class Hash; - private: - iterator( MyType *hsh ) : - hsh( hsh ), - nPos( 0 ), - bFinished( false ) - { - nPos = hsh->core->getFirstPos( bFinished ); - } - - iterator( MyType *hsh, bool bDone ) : - hsh( hsh ), - nPos( 0 ), - bFinished( bDone ) - { - } - - MyType *hsh; - uint32_t nPos; - bool bFinished; - - public: - iterator( const iterator &i ) : - hsh( i.hsh ), - nPos( i.nPos ), - bFinished( i.bFinished ) - { - } - - iterator() : - hsh( NULL ), - nPos( NULL ), - bFinished( true ) - { - } - - bool isValid() const - { - return !bFinished; - } - - operator bool() const - { - return !bFinished; - } - - /** - * Iterator incrementation operator. Move the iterator forward. - */ - iterator operator++( int ) - { - if( bFinished == false ) - nPos = hsh->core->getNextPos( nPos, bFinished ); - - return *this; - } - - /** - * Iterator incrementation operator. Move the iterator forward. - */ - iterator operator++() - { - if( bFinished == false ) - nPos = hsh->core->getNextPos( nPos, bFinished ); - - return *this; - } - - /** - * Iterator equality comparison operator. Iterators the same? - */ - bool operator==( const iterator &oth ) const - { - if( bFinished != oth.bFinished ) - return false; - if( bFinished == true ) - { - return true; - } - else - { - if( oth.nPos == nPos ) - return true; - return false; - } - } - - /** - * Iterator not equality comparison operator. Not the same? - */ - bool operator!=( const iterator &oth ) const - { - return !(*this == oth ); - } - - /** - * Iterator assignment operator. - */ - iterator operator=( const iterator &oth ) - { - hsh = oth.hsh; - nPos = oth.nPos; - bFinished = oth.bFinished; - return *this; - } - - /** - * Iterator dereference operator... err.. get the value - *@returns (value_type &) The value behind this iterator. - */ - value &operator *() - { - hsh->_hardCopy(); - return hsh->core->getValueAtPos( nPos ); - } - - const value &operator *() const - { - return hsh->core->getValueAtPos( nPos ); - } - - /** - * Get the key behind this iterator. - *@returns (key_type &) The key behind this iterator. - */ - const key &getKey() const - { - return hsh->core->getKeyAtPos( nPos ); - } - - /** - * Get the value behind this iterator. - *@returns (value_type &) The value behind this iterator. - */ - value &getValue() - { - hsh->_hardCopy(); - return hsh->core->getValueAtPos( nPos ); - } - - /** - * Get the value behind this iterator. - *@returns (value_type &) The value behind this iterator. - */ - const value &getValue() const - { - return hsh->core->getValueAtPos( nPos ); - } - } iterator; - - /** - * Iteration structure for iterating through the hash (const). - */ - typedef struct const_iterator - { - friend class Hash; - private: - const_iterator( const MyType *hsh ) : - hsh( hsh ), - nPos( 0 ), - bFinished( false ) - { - nPos = hsh->core->getFirstPos( bFinished ); - } - - const_iterator( const MyType *hsh, bool bDone ) : - hsh( hsh ), - nPos( 0 ), - bFinished( bDone ) - { - } - - const MyType *hsh; - uint32_t nPos; - bool bFinished; - - public: - const_iterator() : - hsh( NULL ), - nPos( 0 ), - bFinished( true ) - { - } - - const_iterator( const const_iterator &src ) : - hsh( src.hsh ), - nPos( src.nPos ), - bFinished( src.bFinished ) - { - } - - const_iterator( const iterator &src ) : - hsh( src.hsh ), - nPos( src.nPos ), - bFinished( src.bFinished ) - { - } - - bool isValid() const - { - return !bFinished; - } - - operator bool() const - { - return !bFinished; - } - - /** - * Iterator incrementation operator. Move the iterator forward. - */ - const_iterator operator++( int ) - { - if( bFinished == false ) - nPos = hsh->core->getNextPos( nPos, bFinished ); - - return *this; - } - - /** - * Iterator incrementation operator. Move the iterator forward. - */ - const_iterator operator++() - { - if( bFinished == false ) - nPos = hsh->core->getNextPos( nPos, bFinished ); - - return *this; - } - - /** - * Iterator equality comparison operator. Iterators the same? - */ - bool operator==( const const_iterator &oth ) const - { - if( bFinished != oth.bFinished ) - return false; - if( bFinished == true ) - { - return true; - } - else - { - if( oth.nPos == nPos ) - return true; - return false; - } - } - - /** - * Iterator not equality comparison operator. Not the same? - */ - bool operator!=( const const_iterator &oth ) const - { - return !(*this == oth ); - } - - /** - * Iterator assignment operator. - */ - const_iterator operator=( const const_iterator &oth ) - { - hsh = oth.hsh; - nPos = oth.nPos; - bFinished = oth.bFinished; - return *this; - } - - /** - * Iterator dereference operator... err.. get the value - *@returns (value_type &) The value behind this iterator. - */ - const value &operator *() const - { - return hsh->core->getValueAtPos( nPos ); - } - - /** - * Get the key behind this iterator. - *@returns (key_type &) The key behind this iterator. - */ - const key &getKey() const - { - return hsh->core->getKeyAtPos( nPos ); - } - - /** - * Get the value behind this iterator. - *@returns (value_type &) The value behind this iterator. - */ - const value &getValue() const - { - return hsh->core->getValueAtPos( nPos ); - } - } const_iterator; - - /** - * Get an iterator pointing to the first item in the hash table. - *@returns (iterator) An iterator pointing to the first item in the - * hash table. - */ - iterator begin() - { - return iterator( this ); - } - - const_iterator begin() const - { - return const_iterator( this ); - } - - /** - * Get an iterator pointing to a point just past the last item in the - * hash table. - *@returns (iterator) An iterator pointing to a point just past the - * last item in the hash table. - */ - iterator end() - { - return iterator( this, true ); - } - - const_iterator end() const - { - return const_iterator( this, true ); - } - - /** - * Get a list of all the keys in the hash table. - *@returns (std::list) The list of keys in the hash table. - */ - Bu::List getKeys() const - { - Bu::List lKeys; - - for( uint32_t j = 0; j < core->nCapacity; j++ ) - { - if( core->isFilled( j ) ) - { - if( !core->isDeleted( j ) ) - { - lKeys.append( core->aKeys[j] ); - } - } - } - - return lKeys; - } - - Bu::List getValues() const - { - Bu::List lValues; - - for( uint32_t j = 0; j < core->nCapacity; j++ ) - { - if( core->isFilled( j ) ) - { - if( !core->isDeleted( j ) ) - { - lValues.append( core->aValues[j] ); - } - } - } - - return lValues; - } - - bool operator==( const MyType &rhs ) const - { - if( this == &rhs ) - return true; - if( core == rhs.core ) - return true; - if( core == NULL || rhs.core == NULL ) - return false; - if( getSize() != rhs.getSize() ) - return false; - - for( uint32_t j = 0; j < core->nCapacity; j++ ) - { - if( core->isFilled( j ) ) - { - if( !core->isDeleted( j ) ) - { - // Check to see if this key is in the other hash - if( rhs.has( core->aKeys[j] ) ) - { - if( !(core->aValues[j] == rhs.get( core->aKeys[j]) ) ) - { - return false; - } - } - else - { - return false; - } - } - } - } - - return true; - } - - bool operator!=( const MyType &rhs ) const - { - return !(*this == rhs); - } - - protected: - virtual Core *_copyCore( Core *src ) - { - Core *pRet = _allocateCore(); - - pRet->nFilled = 0; - pRet->nDeleted = 0; - pRet->nCapacity = src->nCapacity; - pRet->nKeysSize = bitsTo( pRet->nCapacity ); - pRet->bFilled = pRet->ca.allocate( pRet->nKeysSize ); - pRet->bDeleted = pRet->ca.allocate( pRet->nKeysSize ); - pRet->clearBits(); - - pRet->aHashCodes = pRet->ca.allocate( pRet->nCapacity ); - pRet->aKeys = pRet->ka.allocate( pRet->nCapacity ); - pRet->aValues = pRet->va.allocate( pRet->nCapacity ); - - for( uint32_t j = 0; j < src->nCapacity; j++ ) - { - if( src->isFilled( j ) && !src->isDeleted( j ) ) - { - pRet->insert( src->aKeys[j], src->aValues[j] ); - } - } - - return pRet; - } - }; - - template uint32_t __calcHashCode( const T &k ) - { - return static_cast( k ); - } - - template bool __cmpHashKeys( const T &a, const T &b ) - { - return (a == b); - } - - template<> uint32_t __calcHashCode( const char * const &k ); - template<> bool __cmpHashKeys( const char * const &a, const char * const &b ); - - template<> uint32_t __calcHashCode( char * const &k ); - template<> bool __cmpHashKeys( char * const &a, char * const &b ); - - class Formatter; - Formatter &operator<<( Formatter &rOut, char *sStr ); - Formatter &operator<<( Formatter &rOut, signed char c ); - template - Formatter &operator<<( Formatter &f, const Bu::Hash &l ) - { - f << '{'; - for( typename Bu::Hash::const_iterator i = l.begin(); i; i++ ) - { - if( i != l.begin() ) - f << ", "; - f << i.getKey() << ": " << i.getValue(); - } - f << '}'; - - return f; - } - - template - ArchiveBase &operator<<( ArchiveBase &ar, const Hash &h ) - { - long iSize = h.getSize(); - ar << iSize; - for( typename Hash::const_iterator i = h.begin(); i != h.end(); i++ ) - { - ar << (i.getKey()); - ar << (i.getValue()); - } - - return ar; - } - - template - ArchiveBase &operator>>( ArchiveBase &ar, Hash &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - for( long j = 0; j < nSize; j++ ) - { - key k; value v; - ar >> k >> v; - h.insert( k, v ); - } - - return ar; - } -} - -#endif diff --git a/src/heap.cpp b/src/heap.cpp deleted file mode 100644 index a2ffac2..0000000 --- a/src/heap.cpp +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/heap.h" - -namespace Bu { subExceptionDef( HeapException ) } diff --git a/src/heap.h b/src/heap.h deleted file mode 100644 index afe8be6..0000000 --- a/src/heap.h +++ /dev/null @@ -1,612 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_HEAP_H -#define BU_HEAP_H - -#include -#include -#include "bu/exceptionbase.h" -#include "bu/util.h" -#include "bu/queue.h" -#include "bu/sharedcore.h" - -namespace Bu -{ - subExceptionDecl( HeapException ); - - template - class Heap; - - /** @cond DEVEL */ - template - class HeapCore - { - friend class Heap; - friend class SharedCore< - Heap, HeapCore - >; - private: - HeapCore() : - iSize( 0 ), - iFill( 0 ), - aItem( NULL ) - { - } - - virtual ~HeapCore() - { - clear(); - } - - void init() - { - if( iSize > 0 ) - return; - - iSize = 7; - iFill = 0; - aItem = ia.allocate( iSize ); - } - - void init( int iCap ) - { - if( iSize > 0 ) - return; - - for( iSize = 1; iSize < iCap; iSize=iSize*2+1 ) { } - iFill = 0; - aItem = ia.allocate( iSize ); - } - - void clear() - { - if( iSize == 0 ) - return; - - for( int j = 0; j < iFill; j++ ) - ia.destroy( &aItem[j] ); - ia.deallocate( aItem, iSize ); - aItem = NULL; - iSize = 0; - iFill = 0; - } - - void upSize() - { - if( iSize == 0 ) - { - init(); - return; - } - - item *aNewItems = ia.allocate( iSize*2+1 ); - // - // We cannot use a memcopy here because we don't know what kind - // of datastructures are being used, we have to copy them one at - // a time. - // - for( int j = 0; j < iFill; j++ ) - { - ia.construct( &aNewItems[j], aItem[j] ); - ia.destroy( &aItem[j] ); - } - ia.deallocate( aItem, iSize ); - aItem = aNewItems; - iSize = iSize*2+1; - } - - virtual void enqueue( const item &it ) - { - item i = it; // TODO: This is a silly workaround, put the i item - // at the end. - if( iFill+1 >= iSize ) - upSize(); - - for( int j = 0; j < iFill; ) - { - if( cmp( i, aItem[j] ) ) - { - Bu::swap( i, aItem[j] ); - } - - if( j*2+1 >= iFill ) - break; - if( cmp( i, aItem[j*2+1] ) ) - { - j = j*2+1; - } - else - { - j = j*2+2; - } - } - ia.construct( &aItem[iFill], i ); - if( iFill > 0 ) - { - for( int j = iFill; j >= 0; ) - { - int k = (j-1)/2; - if( j == k ) - break; - if( cmp( aItem[k], aItem[j] ) ) - break; - - Bu::swap( aItem[k], aItem[j] ); - j = k; - } - } - iFill++; - } - - virtual item dequeue() - { - if( iFill == 0 ) - throw HeapException("Heap empty."); - item iRet = aItem[0]; - int j; - for( j = 0; j < iFill; ) - { - int k = j*2+1; - if( k+1 < iFill && cmp( aItem[k+1], aItem[k] ) ) - { - if( k+1 < iFill-1 && cmp( aItem[iFill-1], aItem[k+1] ) ) - break; - aItem[j] = aItem[k+1]; - j = k+1; - } - else if( k < iFill ) - { - if( k < iFill-1 && cmp( aItem[iFill-1], aItem[k] ) ) - break; - aItem[j] = aItem[k]; - j = k; - } - else - break; - } - if( j < iFill-1 ) - aItem[j] = aItem[iFill-1]; - ia.destroy( &aItem[iFill-1] ); - iFill--; - - return iRet; - } - - private: - int iSize; - int iFill; - item *aItem; - cmpfunc cmp; - itemalloc ia; - }; - /** @endcond */ - - /** - * A priority queue that allows for an unlimited number of priorities. All - * objects enqueued must support less-than-comparison. Then every time an - * item is dequeued it is always the least item in the heap. The heap - * operates using a binary tree for storage, which allows most operations - * to be very fast. Enqueueing and dequeueing are both O(log(N)) operatoins - * whereas peeking is constant time. - * - * This heap implementation allows iterating, however please note that any - * enqueue or dequeue operation will invalidate the iterator and make it - * unusable (if it still works, you shouldn't trust the results). Also, - * the items are not stored in memory in order, they are optomized into a - * tree. This means that the items will be in effectively random order - * while iterating through them, and the order cannot be trusted. Also, - * modifying an item in the heap will not cause that item to be re-sorted. - * If you want to change the position of an item in the heap you will have - * to dequeue every item before it, dequeue that item, change it, and - * re-enqueue all of the items removed. - */ - template, typename itemalloc=std::allocator > - class Heap : public Queue, public SharedCore< - Heap, - HeapCore - > - { - private: - typedef class Heap MyType; - typedef class HeapCore Core; - - protected: - using SharedCore::core; - using SharedCore::_hardCopy; - using SharedCore::_resetCore; - using SharedCore::_allocateCore; - - public: - Heap() - { - } - - Heap( cmpfunc cmpin ) - { - core->cmp = cmpin; - } - - Heap( int iInitialCapacity ) - { - core->init( iInitialCapacity ); - } - - Heap( cmpfunc cmpin, int iInitialCapacity ) - { - core->cmp = cmpin; - core->init( iInitialCapacity ); - } - - Heap( const MyType &rSrc ) : - SharedCore( rSrc ) - { - } - - virtual ~Heap() - { - } - - virtual void enqueue( const item &it ) - { - _hardCopy(); - - core->enqueue( it ); - } - - virtual item &peek() - { - _hardCopy(); - - if( core->iFill == 0 ) - throw HeapException("Heap empty."); - return core->aItem[0]; - } - - virtual const item &peek() const - { - if( core->iFill == 0 ) - throw HeapException("Heap empty."); - return core->aItem[0]; - } - - virtual item dequeue() - { - _hardCopy(); - - return core->dequeue(); - } - - virtual bool isEmpty() const - { - return (core->iFill==0); - } - - virtual int getSize() const - { - return core->iFill; - } - - class iterator - { - friend class const_iterator; - friend class Heap; - private: - Heap *pHeap; - int iIndex; - - iterator( Heap *pHeap, int iIndex ) : - pHeap( pHeap ), iIndex( iIndex ) - { - } - - void checkValid() - { - if( pHeap == NULL ) - throw Bu::ExceptionBase("Iterator not initialized."); - if( iIndex < 0 || iIndex >= pHeap->core->iFill ) - throw Bu::ExceptionBase("Iterator out of bounds."); - } - - public: - iterator() : - pHeap( NULL ), - iIndex( -1 ) - { - } - - iterator( const iterator &i ) : - pHeap( i.pHeap ), - iIndex( i.iIndex ) - { - } - - bool operator==( const iterator &oth ) const - { - return (oth.pHeap == pHeap) && (oth.iIndex == iIndex); - } - - bool operator!=( const iterator &oth ) const - { - return (oth.pHeap != pHeap) || (oth.iIndex != iIndex); - } - - item &operator*() - { - pHeap->_hardCopy(); - - return pHeap->core->aItem[iIndex]; - } - - item *operator->() - { - pHeap->_hardCopy(); - - return &(pHeap->core->aItem[iIndex]); - } - - iterator &operator++() - { - checkValid(); - iIndex++; - if( iIndex >= pHeap->iFill ) - iIndex = -1; - - return *this; - } - - iterator &operator--() - { - checkValid(); - iIndex--; - - return *this; - } - - iterator &operator++( int ) - { - checkValid(); - iIndex++; - if( iIndex >= pHeap->core->iFill ) - iIndex = -1; - - return *this; - } - - iterator &operator--( int ) - { - checkValid(); - iIndex--; - - return *this; - } - - iterator operator+( int iDelta ) - { - checkValid(); - iterator ret( *this ); - ret.iIndex += iDelta; - if( ret.iIndex >= pHeap->core->iFill ) - ret.iIndex = -1; - return ret; - } - - iterator operator-( int iDelta ) - { - checkValid(); - iterator ret( *this ); - ret.iIndex -= iDelta; - if( ret.iIndex < 0 ) - ret.iIndex = -1; - return ret; - } - - operator bool() const - { - return iIndex != -1; - } - - bool isValid() const - { - return iIndex != -1; - } - - iterator &operator=( const iterator &oth ) - { - pHeap = oth.pHeap; - iIndex = oth.iIndex; - } - }; - - class const_iterator - { - friend class Heap; - private: - Heap *pHeap; - int iIndex; - - const_iterator( Heap *pHeap, - int iIndex ) : - pHeap( pHeap ), iIndex( iIndex ) - { - } - - void checkValid() - { - if( pHeap == NULL ) - throw Bu::ExceptionBase("Iterator not initialized."); - if( iIndex < 0 || iIndex >= pHeap->core->iFill ) - throw Bu::ExceptionBase("Iterator out of bounds."); - } - - public: - const_iterator() : - pHeap( NULL ), - iIndex( -1 ) - { - } - - const_iterator( const const_iterator &i ) : - pHeap( i.pHeap ), - iIndex( i.iIndex ) - { - } - - const_iterator( const iterator &i ) : - pHeap( i.pHeap ), - iIndex( i.iIndex ) - { - } - - bool operator==( const const_iterator &oth ) const - { - return (oth.pHeap == pHeap) && (oth.iIndex == iIndex); - } - - bool operator!=( const const_iterator &oth ) const - { - return (oth.pHeap != pHeap) || (oth.iIndex != iIndex); - } - - const item &operator*() - { - pHeap->_hardCopy(); - - return pHeap->core->aItem[iIndex]; - } - - const item *operator->() - { - pHeap->_hardCopy(); - - return &(pHeap->core->aItem[iIndex]); - } - - const_iterator &operator++() - { - checkValid(); - iIndex++; - if( iIndex >= pHeap->core->iFill ) - iIndex = -1; - - return *this; - } - - const_iterator &operator--() - { - checkValid(); - iIndex--; - - return *this; - } - - const_iterator &operator++( int ) - { - checkValid(); - iIndex++; - if( iIndex >= pHeap->core->iFill ) - iIndex = -1; - - return *this; - } - - const_iterator &operator--( int ) - { - checkValid(); - iIndex--; - - return *this; - } - - const_iterator operator+( int iDelta ) - { - checkValid(); - const_iterator ret( *this ); - ret.iIndex += iDelta; - if( ret.iIndex >= pHeap->iFill ) - ret.iIndex = -1; - return ret; - } - - const_iterator operator-( int iDelta ) - { - checkValid(); - const_iterator ret( *this ); - ret.iIndex -= iDelta; - if( ret.iIndex < 0 ) - ret.iIndex = -1; - return ret; - } - - operator bool() const - { - return iIndex != -1; - } - - bool isValid() const - { - return iIndex != -1; - } - - const_iterator &operator=( const const_iterator &oth ) - { - pHeap = oth.pHeap; - iIndex = oth.iIndex; - } - - const_iterator &operator=( const iterator &oth ) - { - pHeap = oth.pHeap; - iIndex = oth.iIndex; - } - }; - - iterator begin() - { - if( core->iFill == 0 ) - return end(); - return iterator( this, 0 ); - } - - const_iterator begin() const - { - if( core->iFill == 0 ) - return end(); - return const_iterator( this, 0 ); - } - - iterator end() - { - return iterator( this, -1 ); - } - - const_iterator end() const - { - return const_iterator( this, -1 ); - } - - - protected: - virtual Core *_copyCore( Core *src ) - { - Core *pRet = _allocateCore(); - - pRet->iSize = src->iSize; - pRet->iFill = src->iFill; - pRet->cmp = src->cmp; - pRet->aItem = pRet->ia.allocate( pRet->iSize ); - for( int j = 0; j < pRet->iFill; j++ ) - { - pRet->ia.construct( &pRet->aItem[j], src->aItem[j] ); - } - - return pRet; - } - }; -}; - -#endif diff --git a/src/hex.cpp b/src/hex.cpp deleted file mode 100644 index 2a04c6f..0000000 --- a/src/hex.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/hex.h" - -Bu::Hex::Hex( Bu::Stream &rNext, bool bUpperCase, int iChunk ) : - Bu::Filter( rNext ), - iChunk( iChunk ), - iPos( 0 ), - iIn( 0 ), - sChrs(bUpperCase?"0123456789ABCDEF":"0123456789abcdef") -{ -} - -Bu::Hex::~Hex() -{ -} - -void Bu::Hex::start() -{ - iPos = iIn = 0; -} - -Bu::size Bu::Hex::stop() -{ - return iPos; -} - -Bu::size Bu::Hex::read( void *pBuf, Bu::size iBytes ) -{ - Bu::size j; - uint8_t *puBuf = (uint8_t *)pBuf; - for( j = 0; j < iBytes; j++ ) - { - for(; iIn < 2; iIn++ ) - { - if( rNext.read( &cIn[iIn], 1 ) == 0 ) - return j; - if( cIn[iIn] == ' ' || cIn[iIn] == '\t' || - cIn[iIn] == '\n' || cIn[iIn] == '\r' ) - iIn--; - } -#define chr2nibble( c ) ((c>='0'&&c<='9')?(c-'0'):((c|0x60)-'a'+10)) - puBuf[j] = ((chr2nibble(cIn[0])<<4)|chr2nibble(cIn[1])); - iIn = 0; - } - return j; -} - -Bu::size Bu::Hex::write( const void *pBuf, Bu::size iBytes ) -{ - char cOut[2]; - uint8_t *puBuf = (uint8_t *)pBuf; - for( Bu::size j = 0; j < iBytes; j++ ) - { - cOut[0] = sChrs[(puBuf[j]&0xf0)>>4]; - cOut[1] = sChrs[(puBuf[j]&0x0f)]; - if( iChunk > 0 && iPos%iChunk == 0 && iPos>0 ) - rNext.write(" ", 1 ); - rNext.write( cOut, 2 ); - iPos++; - } - return iBytes; -} - diff --git a/src/hex.h b/src/hex.h deleted file mode 100644 index 3595fae..0000000 --- a/src/hex.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_HEX_H -#define BU_HEX_H - -#include "bu/filter.h" - -namespace Bu -{ - /** - * This very simple filter encodes to/decodes from hex encoded string data. - * The primary use of this filter is in debugging, use it with - * Bu::encodeStr to easily create hex dumps of string data, even other raw - * structures. - * - *@code - Bu::println("Hexdump: " + Bu::encodeStr("Test data ;)") ); - @endcode - * Or... - *@code - complex_struct data; - ... - Bu::println("Hexdump: " + - Bu::encodeStr( - Bu::String( &data, sizeof(data) ) - ) - ); - @endcode - **/ - class Hex : public Bu::Filter - { - public: - Hex( Bu::Stream &rNext, bool bUpperCase=false, int iChunk=-1 ); - virtual ~Hex(); - - virtual void start(); - virtual Bu::size stop(); - - virtual Bu::size read( void *pBuf, Bu::size iBytes ); - virtual Bu::size write( const void *pBuf, Bu::size iBytes ); - using Bu::Stream::write; - - private: - int iChunk; - Bu::size iPos; - char cIn[2]; - int iIn; - const char *sChrs; - }; -}; - -#endif diff --git a/src/httpget.cpp b/src/httpget.cpp deleted file mode 100644 index 99492a2..0000000 --- a/src/httpget.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/httpget.h" - -Bu::HttpGet::HttpGet( const Bu::Url &uSrc, const Bu::String &sMethod ) : - uSrc( uSrc ), - sMethod( sMethod ), - sSrv( uSrc.getHost(), uSrc.getPort() ) -{ - sSrv.write( sMethod + " " + uSrc.getFullPath() + " HTTP/1.1\r\n" ); -} - -Bu::HttpGet::~HttpGet() -{ -} - -void Bu::HttpGet::close() -{ -} - -void Bu::HttpGet::get() -{ - for( MimeHash::iterator i = hMimeOut.begin(); i; i++ ) - { - sSrv.write( i.getKey() + ": " + i.getValue() + "\r\n" ); - } - sSrv.write("\r\n", 2 ); - -// sSrv.read( -} - -Bu::size Bu::HttpGet::read( void * /*pBuf*/, Bu::size /*nBytes*/ ) -{ - return 0; -} - -Bu::size Bu::HttpGet::write( const void * /*pBuf*/, Bu::size /*nBytes*/ ) -{ - return 0; -} - -Bu::size Bu::HttpGet::tell() -{ - return 0; -} - -void Bu::HttpGet::seek( Bu::size ) -{ -} - -void Bu::HttpGet::setPos( Bu::size ) -{ -} - -void Bu::HttpGet::setPosEnd( Bu::size ) -{ -} - -bool Bu::HttpGet::isEos() -{ - return false; -} - -bool Bu::HttpGet::isOpen() -{ - return true; -} - -void Bu::HttpGet::flush() -{ -} - -bool Bu::HttpGet::canRead() -{ - return true; -} - -bool Bu::HttpGet::canWrite() -{ - return false; -} - -bool Bu::HttpGet::isReadable() -{ - return true; -} - -bool Bu::HttpGet::isWritable() -{ - return false; -} - -bool Bu::HttpGet::isSeekable() -{ - return false; -} - -bool Bu::HttpGet::isBlocking() -{ - return true; -} - -void Bu::HttpGet::setBlocking( bool /*bBlocking*/ ) -{ -} - -Bu::size Bu::HttpGet::getSize() const -{ - return 0; -} - -Bu::size Bu::HttpGet::getBlockSize() const -{ - return 0; -} - -Bu::String Bu::HttpGet::getLocation() const -{ - return uSrc.getUrl(); -} - diff --git a/src/httpget.h b/src/httpget.h deleted file mode 100644 index a58e8ac..0000000 --- a/src/httpget.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_HTTP_GET_H -#define BU_HTTP_GET_H - -#include "bu/stream.h" -#include "bu/string.h" -#include "bu/url.h" -#include "bu/tcpsocket.h" -#include "bu/hash.h" - -namespace Bu -{ - class HttpGet : public Bu::Stream - { - public: - HttpGet( const Bu::Url &uSrc, const Bu::String &sMethod="GET" ); - virtual ~HttpGet(); - - void get(); - - // From Bu::Stream - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Stream::write; - - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - - virtual void flush(); - - virtual bool canRead(); - virtual bool canWrite(); - - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - private: - Bu::Url uSrc; - Bu::String sMethod; - Bu::TcpSocket sSrv; - typedef Bu::Hash MimeHash; - MimeHash hMimeIn; - MimeHash hMimeOut; - }; -}; - -#endif diff --git a/src/itoserver.cpp b/src/itoserver.cpp deleted file mode 100644 index c7165e2..0000000 --- a/src/itoserver.cpp +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/itoserver.h" -#include -#include "bu/tcpserversocket.h" -#include "bu/client.h" -#include "bu/tcpsocket.h" - -#include "bu/config.h" - -Bu::ItoServer::ItoServer() : - nTimeoutSec( 1 ), - nTimeoutUSec( 0 ) -{ - FD_ZERO( &fdActive ); -} - -Bu::ItoServer::~ItoServer() -{ - while( !qClientCleanup.isEmpty() ) - { - ItoClient *pCli = qClientCleanup.dequeue(); - pCli->join(); - delete pCli; - } - // TODO: Make sure here that each client has shutdown it's socket, and - // maybe even written any extra data, we could put a timelimit on this... - // anyway, it's not as clean as it could be right now. - for( ClientHash::iterator i = hClients.begin(); i != hClients.end(); i++ ) - { - ItoClient *pCli = (*i); - pCli->join(); - delete pCli; - } -} - -void Bu::ItoServer::addPort( int nPort, int nPoolSize ) -{ - TcpServerSocket *s = new TcpServerSocket( nPort, nPoolSize ); - int nSocket = s->getSocket(); - FD_SET( nSocket, &fdActive ); - hServers.insert( nSocket, s ); -} - -void Bu::ItoServer::addPort( const String &sAddr, int nPort, int nPoolSize ) -{ - TcpServerSocket *s = new TcpServerSocket( sAddr, nPort, nPoolSize ); - int nSocket = s->getSocket(); - FD_SET( nSocket, &fdActive ); - hServers.insert( nSocket, s ); -} - -void Bu::ItoServer::setTimeout( int nTimeoutSec, int nTimeoutUSec ) -{ - this->nTimeoutSec = nTimeoutSec; - this->nTimeoutUSec = nTimeoutUSec; -} - -void Bu::ItoServer::addClient( int nSocket, int nPort ) -{ - ItoClient *pC = new ItoClient( *this, nSocket, nPort, nTimeoutSec, - nTimeoutUSec ); - - imClients.lock(); - hClients.insert( nSocket, pC ); - imClients.unlock(); - - pC->start(); -} - -void Bu::ItoServer::run() -{ - for(;;) - { - struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; - - fd_set fdRead = fdActive; - //fd_set fdWrite = fdActive; - fd_set fdException = fdActive; - - if( TEMP_FAILURE_RETRY( select( FD_SETSIZE, &fdRead, NULL, &fdException, &xTimeout ) ) < 0 ) - { - throw ExceptionBase("Error attempting to scan open connections."); - } - - for( ServerHash::iterator i = hServers.begin(); i != hServers.end(); i++ ) - { - if( FD_ISSET( i.getKey(), &fdRead ) ) - { - TcpServerSocket *pSrv = i.getValue(); - addClient( pSrv->accept(), pSrv->getPort() ); - } - } - - while( !qClientCleanup.isEmpty() ) - { - ItoClient *pCli = qClientCleanup.dequeue(); - pCli->join(); - delete pCli; - } - } -} - -void Bu::ItoServer::clientCleanup( int iSocket ) -{ - imClients.lock(); - ItoClient *pCli = hClients.get( iSocket ); - imClients.unlock(); - qClientCleanup.enqueue( pCli ); -} - -Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, - int nTimeoutSec, int nTimeoutUSec ) : - rSrv( rSrv ), - iSocket( iSocket ), - iPort( iPort ), - nTimeoutSec( nTimeoutSec ), - nTimeoutUSec( nTimeoutUSec ) -{ - FD_ZERO( &fdActive ); - FD_SET( iSocket, &fdActive ); - - pClient = new Client( - new Bu::TcpSocket( iSocket ), - new SrvClientLinkFactory( rSrv ) - ); -} - -Bu::ItoServer::ItoClient::~ItoClient() -{ -} - -void Bu::ItoServer::ItoClient::run() -{ - imProto.lock(); - rSrv.onNewConnection( pClient, iPort ); - pClient->processOutput(); - imProto.unlock(); - - for(;;) - { - struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; - - fd_set fdRead = fdActive; - fd_set fdWrite; - fd_set fdException = fdActive; - - FD_ZERO( &fdWrite ); - if( pClient->hasOutput() ) - FD_SET( iSocket, &fdWrite ); - - if( TEMP_FAILURE_RETRY( select( FD_SETSIZE, - &fdRead, &fdWrite, &fdException, &xTimeout ) ) < 0 ) - { - throw ExceptionBase("Error attempting to scan open connections."); - } - - while( !qMsg.isEmpty() ) - { - imProto.lock(); - Bu::String *pMsg = qMsg.dequeue(); - pClient->onMessage( *pMsg ); - delete pMsg; - pClient->processOutput(); - imProto.unlock(); - } - - if( FD_ISSET( iSocket, &fdRead ) ) - { - imProto.lock(); - pClient->processInput(); - imProto.unlock(); - if( !pClient->isOpen() ) - { - imProto.lock(); - rSrv.onClosedConnection( pClient ); - imProto.unlock(); - - rSrv.clientCleanup( iSocket ); - - return; - } - } - - if( FD_ISSET( iSocket, &fdWrite ) ) - { - imProto.lock(); - pClient->processOutput(); - imProto.unlock(); - } - } -} - -Bu::ItoServer::SrvClientLink::SrvClientLink( ItoClient *pClient ) : - pClient( pClient ) -{ -} - -Bu::ItoServer::SrvClientLink::~SrvClientLink() -{ -} - -void Bu::ItoServer::SrvClientLink::sendMessage( const Bu::String &sMsg ) -{ - if( !pClient->imProto.trylock() ) - { - pClient->pClient->onMessage( sMsg ); - pClient->pClient->processOutput(); - pClient->imProto.unlock(); - } - else - { - Bu::String *pMsg = new Bu::String( sMsg ); - pClient->qMsg.enqueue( pMsg ); - } -} - -Bu::ItoServer::SrvClientLinkFactory::SrvClientLinkFactory( - Bu::ItoServer &rSrv ) : - rSrv( rSrv ) -{ -} - -Bu::ItoServer::SrvClientLinkFactory::~SrvClientLinkFactory() -{ -} - -Bu::ClientLink *Bu::ItoServer::SrvClientLinkFactory::createLink( - Bu::Client *pClient ) -{ - rSrv.imClients.lock(); - ItoClient *pCli = rSrv.hClients.get( *pClient->getSocket() ); - rSrv.imClients.unlock(); - - return new SrvClientLink( pCli ); -} - diff --git a/src/itoserver.h b/src/itoserver.h deleted file mode 100644 index b1f5479..0000000 --- a/src/itoserver.h +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ITO_SERVER_H -#define BU_ITO_SERVER_H - -#include - -#ifndef WIN32 - #include -#endif - -#include "bu/string.h" -#include "bu/list.h" -#include "bu/thread.h" -#include "bu/mutex.h" -#include "bu/synchroqueue.h" -#include "bu/set.h" - -#include "bu/clientlink.h" -#include "bu/clientlinkfactory.h" - -namespace Bu -{ - class TcpServerSocket; - class TcpSocket; - class Client; - - /** - * Core of a network server. This class is distinct from a ServerSocket in - * that a ServerSocket is one listening socket, nothing more. Socket will - * manage a pool of both ServerSockets and connected Sockets along with - * their protocols and buffers. - * - * To start serving on a new port, use the addPort functions. Each call to - * addPort creates a new ServerSocket, starts it listening, and adds it to - * the server pool. - * - * All of the real work is done by scan, which will wait for up - * to the timeout set by setTimeout before returning if there is no data - * pending. scan should probably be called in some sort of tight - * loop, possibly in it's own thread, or in the main control loop. - * - * In order to use a Server you must subclass it and implement the pure - * virtual functions. These allow you to receive notification of events - * happening within the server itself, and actually makes it useful. - *@ingroup Threading Serving - */ - class ItoServer : public Thread - { - friend class ItoClient; - friend class SrvClientLinkFactory; - public: - ItoServer(); - virtual ~ItoServer(); - - void addPort( int nPort, int nPoolSize=40 ); - void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); - - //void scan(); - void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); - - void addClient( int nSocket, int nPort ); - - virtual void onNewConnection( Client *pClient, int nPort )=0; - virtual void onClosedConnection( Client *pClient )=0; - - protected: - virtual void run(); - - private: - class SrvClientLink; - class ItoClient : public Thread - { - friend class Bu::ItoServer::SrvClientLink; - public: - ItoClient( ItoServer &rSrv, int nSocket, int nPort, - int nTimeoutSec, int nTimeoutUSec ); - virtual ~ItoClient(); - - typedef SynchroQueue StringQueue; - StringQueue qMsg; - - protected: - virtual void run(); - - private: - ItoServer &rSrv; - Client *pClient; - fd_set fdActive; - int iSocket; - int iPort; - int nTimeoutSec; - int nTimeoutUSec; - Mutex imProto; - }; - - class SrvClientLink : public Bu::ClientLink - { - public: - SrvClientLink( ItoClient *pClient ); - virtual ~SrvClientLink(); - - virtual void sendMessage( const Bu::String &sMsg ); - - private: - ItoClient *pClient; - }; - - class SrvClientLinkFactory : public Bu::ClientLinkFactory - { - public: - SrvClientLinkFactory( ItoServer &rSrv ); - virtual ~SrvClientLinkFactory(); - - virtual Bu::ClientLink *createLink( Bu::Client *pClient ); - - private: - ItoServer &rSrv; - }; - - int nTimeoutSec; - int nTimeoutUSec; - fd_set fdActive; - typedef Hash ServerHash; - ServerHash hServers; - typedef Hash ClientHash; - typedef SynchroQueue ClientQueue; - ClientHash hClients; - ClientQueue qClientCleanup; - Mutex imClients; - - void clientCleanup( int iSocket ); - }; -} - -#endif diff --git a/src/lexer.cpp b/src/lexer.cpp deleted file mode 100644 index 185456a..0000000 --- a/src/lexer.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/lexer.h" -#include "bu/membuf.h" -#include "bu/formatter.h" - -Bu::Lexer::Lexer() -{ -} - -Bu::Lexer::~Lexer() -{ -} - -Bu::Lexer::Token::Token() : - iToken( -1 ) -{ -} - -Bu::Lexer::Token::Token( Bu::Lexer::TokenType iToken ) : - iToken( iToken ) -{ -} - -Bu::String Bu::Lexer::tokenToString( const Bu::Lexer::Token &t ) -{ - Bu::MemBuf mb; - Bu::Formatter f( mb ); - f << "<" << t.iToken << ">"; - if( t.vExtra.isSet() ) - f << " (" << t.vExtra << ")"; - - return mb.getString(); -} - diff --git a/src/lexer.h b/src/lexer.h deleted file mode 100644 index b057692..0000000 --- a/src/lexer.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_LEXER_H -#define BU_LEXER_H - -#include "bu/variant.h" - -namespace Bu -{ - class Stream; - - /** - * The base class for creating a lexical analyzer. This is designed to work - * in tandem with the Bu::Parser class, which uses this to tokenize textual - * input. It can be used by just about anything that cares about tokens - * more than raw input, though. - */ - class Lexer - { - public: - Lexer(); - virtual ~Lexer(); - - typedef int TokenType; - - class Token - { - public: - Token(); - Token( TokenType iToken ); - - template - Token( TokenType iToken, const t &v ) : - iToken( iToken )//, -// vExtra( v ) - { - vExtra = v; - } - TokenType iToken; - Bu::Variant vExtra; - int iStartCol; - int iStartRow; - int iEndCol; - int iEndRow; - }; - - virtual Token *nextToken()=0; - - virtual Bu::String tokenToString( const Token &t ); - }; -}; - -#endif diff --git a/src/linkmessage.cpp b/src/linkmessage.cpp deleted file mode 100644 index cd024c0..0000000 --- a/src/linkmessage.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/linkmessage.h" - -Bu::LinkMessage::LinkMessage( int nNewMsg ) -{ - nMsg = nNewMsg; -} - -Bu::LinkMessage::~LinkMessage() -{ -} - -/* -void LinkMessage::setBroadcast( bool bOn ) -{ - bBroadcast = bOn; -} - -bool LinkMessage::isBroadcast() -{ - return bBroadcast; -} - - -void LinkMessage::setFromID( int id ) -{ - nFromLinkID = id; -} - -int LinkMessage::getFromID() -{ - return nFromLinkID; -} - -void LinkMessage::setToID( int id ) -{ - nTargetLinkID = id; -} - -int LinkMessage::getToID() -{ - return nTargetLinkID; -} -*/ diff --git a/src/linkmessage.h b/src/linkmessage.h deleted file mode 100644 index 69423b3..0000000 --- a/src/linkmessage.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -/**\file linkmessage.h - */ - -#ifndef BU_LINKMESSAGE_H -#define BU_LINKMESSAGE_H - -namespace Bu -{ - /** - * A message to be broadcast accross ProgramLinks in a ProgramChain. Generally - * one would make a subclass of this in order to transmit more useful - * information, but sometimes it isn't necesarry. - */ - class LinkMessage - { - public: - /** - * Construct a blank LinkMessage. - */ - LinkMessage() {}; - - /** - * Deconstruct a LinkMessage. - */ - virtual ~LinkMessage(); - - /** - * Create a LinkMessage object with a specific message assosiated with it - * to start with. - *@param nNewMsg The message to use in the Message object. - */ - LinkMessage( int nNewMsg ); - - /** - * The message contained in the Message object. - */ - int nMsg; - }; -} - -#endif diff --git a/src/list.cpp b/src/list.cpp deleted file mode 100644 index e05765e..0000000 --- a/src/list.cpp +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/list.h" - diff --git a/src/list.h b/src/list.h deleted file mode 100644 index 21ba0b5..0000000 --- a/src/list.h +++ /dev/null @@ -1,1014 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_LIST_H -#define BU_LIST_H - -#include -#include "bu/exceptionbase.h" -#include "bu/sharedcore.h" -#include "bu/archivebase.h" -#include "bu/heap.h" - -namespace Bu -{ - /** @cond DEVEL */ - template - struct ListLink - { - value *pValue; - ListLink *pNext; - ListLink *pPrev; - }; - - template - class List; - - template - struct ListCore - { - friend class List; - friend class SharedCore< - List, - ListCore - >; - private: - typedef struct ListLink Link; - ListCore() : - pFirst( NULL ), - pLast( NULL ), - nSize( 0 ) - { } - - virtual ~ListCore() - { - clear(); - } - - Link *pFirst; - Link *pLast; - long nSize; - linkalloc la; - valuealloc va; - - /** - * Append a value to the list. - *@param v (const value_type &) The value to append. - */ - Link *append( const value &v ) - { - Link *pNew = la.allocate( 1 ); - pNew->pValue = va.allocate( 1 ); - va.construct( pNew->pValue, v ); - nSize++; - if( pFirst == NULL ) - { - // Empty list - pFirst = pLast = pNew; - pNew->pNext = pNew->pPrev = NULL; - } - else - { - pNew->pNext = NULL; - pNew->pPrev = pLast; - pLast->pNext = pNew; - pLast = pNew; - } - return pNew; - } - - /** - * Prepend a value to the list. - *@param v (const value_type &) The value to prepend. - */ - Link *prepend( const value &v ) - { - Link *pNew = la.allocate( 1 ); - pNew->pValue = va.allocate( 1 ); - va.construct( pNew->pValue, v ); - nSize++; - if( pFirst == NULL ) - { - // Empty list - pFirst = pLast = pNew; - pNew->pNext = pNew->pPrev = NULL; - } - else - { - pNew->pNext = pFirst; - pNew->pPrev = NULL; - pFirst->pPrev = pNew; - pFirst = pNew; - } - return pNew; - } - - void clear() - { - Link *pCur = pFirst; - for(;;) - { - if( pCur == NULL ) break; - va.destroy( pCur->pValue ); - va.deallocate( pCur->pValue, 1 ); - Link *pTmp = pCur->pNext; - la.destroy( pCur ); - la.deallocate( pCur, 1 ); - pCur = pTmp; - } - pFirst = pLast = NULL; - nSize = 0; - } - - Link *insert( Link *pLink, const value &v ) - { - Link *pAfter = pLink; - if( pAfter == NULL ) - { - return append( v ); - } - Link *pPrev = pAfter->pPrev; - if( pPrev == NULL ) - { - return prepend( v ); - } - - Link *pNew = la.allocate( 1 ); - pNew->pValue = va.allocate( 1 ); - va.construct( pNew->pValue, v ); - nSize++; - - pNew->pNext = pAfter; - pNew->pPrev = pPrev; - pAfter->pPrev = pNew; - pPrev->pNext = pNew; - - return pNew; - } - - /** - * Erase an item from the list. - *@param i (iterator) The item to erase. - */ - void erase( Link *pLink ) - { - Link *pCur = pLink; - if( pCur == NULL ) return; - Link *pPrev = pCur->pPrev; - if( pPrev == NULL ) - { - va.destroy( pCur->pValue ); - va.deallocate( pCur->pValue, 1 ); - pFirst = pCur->pNext; - la.destroy( pCur ); - la.deallocate( pCur, 1 ); - if( pFirst == NULL ) - pLast = NULL; - else - pFirst->pPrev = NULL; - nSize--; - } - else - { - va.destroy( pCur->pValue ); - va.deallocate( pCur->pValue, 1 ); - Link *pTmp = pCur->pNext; - la.destroy( pCur ); - la.deallocate( pCur, 1 ); - pPrev->pNext = pTmp; - if( pTmp != NULL ) - pTmp->pPrev = pPrev; - else - pLast = pPrev; - nSize--; - } - } - }; - /** @endcond */ - - /** - * Linked list template container. This class is similar to the stl list - * class except for a few minor changes. First, when const, all - * members are only accessable const. Second, erasing a location does not - * invalidate the iterator used, it simply points to the next valid - * location, or end() if there are no more. Other iterators pointing to - * the deleted record will, of course, no longer be valid. - * - *@param value (typename) The type of data to store in your list - *@param valuealloc (typename) Memory Allocator for your value type - *@param linkalloc (typename) Memory Allocator for the list links. - *@extends SharedCore - *@ingroup Containers - */ - template, - typename linkalloc=std::allocator > > - class List /** @cond */ : public SharedCore< - List, - ListCore - > /** @endcond */ - { - private: - typedef struct ListLink Link; - typedef class List MyType; - typedef struct ListCore Core; - - protected: - using SharedCore::core; - using SharedCore::_hardCopy; - using SharedCore::_allocateCore; - - public: - struct const_iterator; - struct iterator; - - List() - { - } - - List( const MyType &src ) : - SharedCore( src ) - { - } - - List( const value &v ) - { - append( v ); - } - - ~List() - { - } - - MyType &operator+=( const value &v ) - { - _hardCopy(); - append( v ); - return *this; - } - - MyType &operator+=( const MyType &src ) - { - _hardCopy(); - append( src ); - return *this; - } - - MyType operator+( const MyType &src ) - { - MyType lNew( *this ); - lNew += src; - return lNew; - } - - bool operator==( const MyType &rhs ) const - { - if( getSize() != rhs.getSize() ) - return false; - - for( typename MyType::const_iterator a = begin(), b = rhs.begin(); - a; a++, b++ ) - { - if( *a != *b ) - return false; - } - - return true; - } - - bool operator!=( const MyType &rhs ) const - { - return !(*this == rhs); - } - - /** - * Clear the data from the list. - */ - void clear() - { - _hardCopy(); - core->clear(); - } - - MyType &enqueue( const value &v ) - { - _hardCopy(); - append( v ); - - return *this; - } - - value dequeue() - { - // _hardCopy(); erase will call this for me - value v = *core->pFirst->pValue; - - erase( begin() ); - - return v; - } - - MyType &push( const value &v ) - { - _hardCopy(); - prepend( v ); - - return *this; - } - - MyType &pop() - { - _hardCopy(); - erase( begin() ); - - return *this; - } - - value peekPop() - { - value v = first(); - pop(); - return v; - } - - value &peek() - { - return first(); - } - - /** - * Append a value to the list. - *@param v (const value_type &) The value to append. - */ - MyType &append( const value &v ) - { - _hardCopy(); - core->append( v ); - - return *this; - } - - MyType &append( const MyType &rSrc ) - { - _hardCopy(); - for( typename MyType::const_iterator i = rSrc.begin(); - i != rSrc.end(); i++ ) - { - core->append( *i ); - } - - return *this; - } - - /** - * Prepend a value to the list. - *@param v (const value_type &) The value to prepend. - */ - MyType &prepend( const value &v ) - { - _hardCopy(); - core->prepend( v ); - - return *this; - } - - /** - * Prepend another list to the front of this one. This will prepend - * the rSrc list in reverse order...I may fix that later. - */ - MyType &prepend( const MyType &rSrc ) - { - _hardCopy(); - for( typename MyType::const_iterator i = rSrc.begin(); - i != rSrc.end(); i++ ) - { - core->prepend( *i ); - } - - return *this; - } - - MyType &insert( MyType::iterator &i, const value &v ) - { - _hardCopy(); - - core->insert( i.pLink, v ); - - return *this; - } - - template - void sort( cmptype cmp ) - { - Heap hSort( cmp, getSize() ); - for( typename MyType::iterator i = begin(); i; i++ ) - { - hSort.enqueue( *i ); - } - clear(); - while( !hSort.isEmpty() ) - { - append( hSort.dequeue() ); - } - } - - void sort() - { - sort<__basicLTCmp >(); - } - - template - void sort() - { - Heap hSort( getSize() ); - for( typename MyType::iterator i = begin(); i; i++ ) - { - hSort.enqueue( *i ); - } - clear(); - while( !hSort.isEmpty() ) - { - append( hSort.dequeue() ); - } - } - - /** - * Insert a new item in sort order by searching for the first item that - * is larger and inserting this before it, or at the end if none are - * larger. If this is the only function used to insert data in the - * List all items will be sorted. To use this, the value type must - * support the > operator. - */ - template - iterator insertSorted( cmptype cmp, const value &v ) - { - _hardCopy(); - if( core->pFirst == NULL ) - { - // Empty list - return iterator( core->append( v ) ); - } - else - { - Link *pCur = core->pFirst; - for(;;) - { - if( cmp( v, *(pCur->pValue)) ) - { - return iterator( core->insert( pCur, v ) ); - } - pCur = pCur->pNext; - if( pCur == NULL ) - { - return iterator( core->append( v ) ); - } - } - } - } - - iterator insertSorted( const value &v ) - { - return insertSorted<__basicLTCmp >( v ); - } - - template - iterator insertSorted( const value &v ) - { - cmptype cmp; - return insertSorted( cmp, v ); - } - - /** - * An iterator to iterate through your list. - */ - typedef struct iterator - { - friend struct const_iterator; - friend class List; - private: - Link *pLink; - - iterator( Link *pLink ) : - pLink( pLink ) - { - } - - public: - iterator() : - pLink( NULL ) - { - } - - iterator( const iterator &i ) : - pLink( i.pLink ) - { - } - - /** - * Equals comparison operator. - *@param oth (const iterator &) The iterator to compare to. - *@returns (bool) Are they equal? - */ - bool operator==( const iterator &oth ) const - { - return ( pLink == oth.pLink ); - } - - /** - * Equals comparison operator. - *@param pOth (const Link *) The link to compare to. - *@returns (bool) Are they equal? - */ - bool operator==( const Link *pOth ) const - { - return ( pLink == pOth ); - } - - /** - * Not equals comparison operator. - *@param oth (const iterator &) The iterator to compare to. - *@returns (bool) Are they not equal? - */ - bool operator!=( const iterator &oth ) const - { - return ( pLink != oth.pLink ); - } - - /** - * Not equals comparison operator. - *@param pOth (const Link *) The link to compare to. - *@returns (bool) Are they not equal? - */ - bool operator!=( const Link *pOth ) const - { - return ( pLink != pOth ); - } - - /** - * Dereference operator. - *@returns (value_type &) The value. - */ - value &operator*() - { - return *(pLink->pValue); - } - - /** - * Pointer access operator. - *@returns (value_type *) A pointer to the value. - */ - value *operator->() - { - return pLink->pValue; - } - - iterator &operator++() - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past end of list."); - pLink = pLink->pNext; - return *this; - } - - iterator &operator--() - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - pLink = pLink->pPrev; - return *this; - } - - iterator &operator++( int ) - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past end of list."); - pLink = pLink->pNext; - return *this; - } - - iterator &operator--( int ) - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - pLink = pLink->pPrev; - return *this; - } - - iterator operator+( int iDelta ) - { - iterator ret( *this ); - for( int j = 0; j < iDelta; j++ ) - { - if( ret.pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - ret.pLink = ret.pLink->pNext; - } - return ret; - } - - iterator operator-( int iDelta ) - { - iterator ret( *this ); - for( int j = 0; j < iDelta; j++ ) - { - if( ret.pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - ret.pLink = ret.pLink->pPrev; - } - return ret; - } - - operator bool() - { - return pLink != NULL; - } - - bool isValid() - { - return pLink != NULL; - } - - /** - * Assignment operator. - *@param oth (const iterator &) The other iterator to set this - * one to. - */ - iterator &operator=( const iterator &oth ) - { - pLink = oth.pLink; - return *this; - } - } iterator; - - /** - *@see iterator - */ - typedef struct const_iterator - { - friend class List; - private: - Link *pLink; - - const_iterator( Link *pLink ) : - pLink( pLink ) - { - } - - public: - const_iterator() : - pLink( NULL ) - { - } - - const_iterator( const iterator &i ) : - pLink( i.pLink ) - { - } - - bool operator==( const const_iterator &oth ) const - { - return ( pLink == oth.pLink ); - } - - bool operator==( const Link *pOth ) const - { - return ( pLink == pOth ); - } - - bool operator!=( const const_iterator &oth ) const - { - return ( pLink != oth.pLink ); - } - - bool operator!=( const Link *pOth ) const - { - return ( pLink != pOth ); - } - - const value &operator*() - { - return *(pLink->pValue); - } - - const value *operator->() - { - return pLink->pValue; - } - - const_iterator &operator++() - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past end of list."); - pLink = pLink->pNext; - return *this; - } - - const_iterator &operator--() - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - pLink = pLink->pPrev; - return *this; - } - - const_iterator &operator++( int ) - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past end of list."); - pLink = pLink->pNext; - return *this; - } - - const_iterator &operator--( int ) - { - if( pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - pLink = pLink->pPrev; - return *this; - } - - const_iterator operator+( int iDelta ) - { - const_iterator ret( *this ); - for( int j = 0; j < iDelta; j++ ) - { - if( ret.pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - ret.pLink = ret.pLink->pNext; - } - return ret; - } - - const_iterator operator-( int iDelta ) - { - const_iterator ret( *this ); - for( int j = 0; j < iDelta; j++ ) - { - if( ret.pLink == NULL ) - throw Bu::ExceptionBase( - "Attempt to iterate past begining of list."); - ret.pLink = ret.pLink->pPrev; - } - return ret; - } - - const_iterator &operator=( const iterator &oth ) - { - pLink = oth.pLink; - return *this; - } - - const_iterator &operator=( const const_iterator &oth ) - { - pLink = oth.pLink; - return *this; - } - - operator bool() - { - return pLink != NULL; - } - - bool isValid() - { - return pLink != NULL; - } - } const_iterator; - - /** - * Get an iterator pointing to the first item in the list. - *@returns (iterator) - */ - iterator begin() - { - _hardCopy(); - return iterator( core->pFirst ); - } - - /** - * Get a const iterator pointing to the first item in the list. - *@returns (const const_iterator) - */ - const_iterator begin() const - { - return const_iterator( core->pFirst ); - } - - /** - * Get an iterator pointing to a place just past the last item in - * the list. - *@returns (const Link *) - */ - const iterator end() - { - return iterator( NULL ); - } - - /** - * Get an iterator pointing to a place just past the last item in - * the list. - *@returns (const Link *) - */ - const const_iterator end() const - { - return const_iterator( NULL ); - } - - /** - * Erase an item from the list. - *@param i (iterator) The item to erase. - */ - MyType &erase( iterator i ) - { - _hardCopy(); - core->erase( i.pLink ); - - return *this; - } - - /** - * Erase an item from the list. - *@param i (iterator) The item to erase. - */ - MyType &erase( const_iterator i ) - { - _hardCopy(); - core->erase( i.pLink ); - - return *this; - } - - /** - * Erase an item from the list if you already know the item. - *@param v The item to find and erase. - */ - MyType &erase( const value &v ) - { - for( const_iterator i = begin(); i != end(); i++ ) - { - if( (*i) == v ) - { - erase( i ); - return *this; - } - } - - return *this; - } - - iterator find( const value &v ) - { - for( iterator i = begin(); i; i++ ) - { - if( (*i) == v ) - return i; - } - - return end(); - } - - const_iterator find( const value &v ) const - { - for( const_iterator i = begin(); i; i++ ) - { - if( (*i) == v ) - return i; - } - - return end(); - } - - /** - * Get the current size of the list. - *@returns (int) The current size of the list. - */ - long getSize() const - { - return core->nSize; - } - - /** - * Get the first item in the list. - *@returns (value_type &) The first item in the list. - */ - value &first() - { - if( core->pFirst->pValue == NULL ) - throw Bu::ExceptionBase("Attempt to read first element from empty list."); - _hardCopy(); - return *core->pFirst->pValue; - } - - /** - * Get the first item in the list. - *@returns (const value_type &) The first item in the list. - */ - const value &first() const - { - if( core->pFirst->pValue == NULL ) - throw Bu::ExceptionBase("Attempt to read first element from empty list."); - return *core->pFirst->pValue; - } - - /** - * Get the last item in the list. - *@returns (value_type &) The last item in the list. - */ - value &last() - { - _hardCopy(); - return *core->pLast->pValue; - } - - /** - * Get the last item in the list. - *@returns (const value_type &) The last item in the list. - */ - const value &last() const - { - return *core->pLast->pValue; - } - - bool isEmpty() const - { - return (core->nSize == 0); - } - - protected: - virtual Core *_copyCore( Core *src ) - { - Core *pRet = _allocateCore(); - for( Link *pCur = src->pFirst; pCur; pCur = pCur->pNext ) - { - pRet->append( *pCur->pValue ); - } - return pRet; - } - - private: - }; - - class Formatter; - Formatter &operator<<( Formatter &rOut, char *sStr ); - Formatter &operator<<( Formatter &rOut, signed char c ); - template - Formatter &operator<<( Formatter &f, const Bu::List &l ) - { - f << '['; - for( typename Bu::List::const_iterator i = l.begin(); i; i++ ) - { - if( i != l.begin() ) - f << ", "; - f << *i; - } - f << ']'; - - return f; - } - - template - ArchiveBase &operator<<( ArchiveBase &ar, const List &h ) - { - ar << h.getSize(); - for( typename List::const_iterator i = h.begin(); i != h.end(); i++ ) - { - ar << (*i); - } - - return ar; - } - - template - ArchiveBase &operator>>( ArchiveBase &ar, List &h ) - { - h.clear(); - long nSize; - ar >> nSize; - - for( long j = 0; j < nSize; j++ ) - { - value v; - ar >> v; - h.append( v ); - } - - return ar; - } - -} - -#endif diff --git a/src/logger.cpp b/src/logger.cpp deleted file mode 100644 index 8e46390..0000000 --- a/src/logger.cpp +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/logger.h" -#include -#include -#include -#include -#include - -Bu::Logger::Logger() -{ - setFormat("%t"); -} - -Bu::Logger::~Logger() -{ -} - -void Bu::Logger::log( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...) -{ -#ifndef WIN32 - if( (nLevel&nLevelMask) == 0 ) - return; - - va_list ap; - va_start( ap, sFormat ); - char *text; - if( vasprintf( &text, sFormat, ap ) < 0 ) - { - printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WTF?\n"); - return; - } - va_end(ap); - - time_t t = time(NULL); - - char *line = NULL; - struct tm *pTime; - pTime = localtime( &t ); - if ( asprintf( - &line, - sLogFormat.getStr(), - pTime->tm_year+1900, - pTime->tm_mon+1, - pTime->tm_mday, - pTime->tm_hour, - pTime->tm_min, - pTime->tm_sec, - nLevel, - sFile, - nLine, - text, - sFunction - ) < 0 ) - { - //printf("LOGGER: ERROR ALLOCATING STRING: %s\n", strerror( errno ) ); - return; - } - write( fileno(stdout), line, strlen(line) ); - free( text ); - free( line ); -#else - #warning Bu::Logger::log IS A STUB for WIN32!!!! -#endif -} - -void Bu::Logger::setFormat( const Bu::String &str ) -{ - sLogFormat = ""; - - static char fmts[][4]={ - {'y', 'd', '0', '1'}, - {'m', 'd', '0', '2'}, - {'d', 'd', '0', '3'}, - {'h', 'd', '0', '4'}, - {'M', 'd', '0', '5'}, - {'s', 'd', '0', '6'}, - {'L', 'd', '0', '7'}, - {'f', 's', '0', '8'}, - {'l', 'd', '0', '9'}, - {'t', 's', '1', '0'}, - {'F', 's', '1', '1'}, - {'\0', '\0', '\0', '\0'}, - }; - - for( const char *s = str.getStr(); *s; s++ ) - { - if( *s == '%' ) - { - sLogFormat += '%'; - Bu::String sBuf; - for(;;) - { - s++; - int l; - for( l = 0;; l++ ) - { - if( fmts[l][0] == '\0' ) - { - sBuf += *s; - break; - } - else if( *s == fmts[l][0] ) - { - sLogFormat += fmts[l][2]; - sLogFormat += fmts[l][3]; - sLogFormat += '$'; - sLogFormat += sBuf; - sLogFormat += fmts[l][1]; - break; - } - } - if( fmts[l][0] != '\0' ) - break; - } - } - else - { - sLogFormat += *s; - } - } - sLogFormat += '\n'; - - //write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); -} - -void Bu::Logger::setMask( uint32_t n ) -{ - nLevelMask = n; -} - -uint32_t Bu::Logger::getMask() -{ - return nLevelMask; -} - -void Bu::Logger::setLevel( uint32_t n ) -{ - int j; - for( j = 31; j > 0; j-- ) - { - if( (n&(1<= 0; j-- ) - { - n |= (1<32 && pData[j+k]<=128)?(pData[j+k]):('.') ); - sLine += buf; - } - log( nLevel, sFile, sFunction, nLine, sLine.getStr() ); - sLine = ""; - j += kmax; - if( j >= nDataLen ) break; - } - log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); -} - diff --git a/src/logger.h b/src/logger.h deleted file mode 100644 index 5c1352b..0000000 --- a/src/logger.h +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_LOGGER_H -#define BU_LOGGER_H - -#include "bu/singleton.h" -#include "bu/string.h" - -namespace Bu -{ - /** - * Simple logging facility. All output goes straight to stdout, unlike the - * old multi-log system. Generally we expect any program complex enough to - * want to use this will have other facilities for processing the logging - * output, but if we need it we can add other output methods. - * - * Currently implemented as a singleton to avoid clutter with globals, you - * generally never want to use the logging system directly, it's annoying. - * Instead use the handy macros lineLog, setLogMask, setLogFormat, and - * setLogLevel. They do all the real work for you. - * - * In the log format, you can specify extra information that will be written - * to the log with every message, and extras in printf style. Use %X flags - * where X is one of the following: - * - L - Logging level of the log message (not the current mask) - * - y - Full year - * - m - Month - * - d - Day of month - * - h - Hour (24-hour format) - * - M - Minutes - * - s - Seconds - * - f - Source file - * - l - Line number - * - F - function name - * - t - Text of message (usually important) - * - * You can include anything extra that you would like, a newline will always - * be added automatically, so no need to worry about that. You can also - * include any extra printf style formatting that you would like, for - * example: "%h:%02M:%02s" for the time 4:02:09 instead of 4:2:9. - * - * It's generally handy to create an enum of values you use as levels during - * program execution (such as error, warning, info, debug, etc). These - * levels should be treated as bitflags, and the most desirable messages, - * i.e. serious errors and the like should be low order (0x01), and the much - * less desirable messages, like debugging info, should be higher order - * (0xF0). During operation you can then set either an explicit mask, - * selecting just the levels that you would like to see printed, or set the - * mask using the setLevel helper function, which simulates verbosity - * levels, enabling every flag lower order than the highest order set bit - * passed. I.E. if you had the following enumerated levels: - * - *@code - enum { - logError = 0x01, - logWarning = 0x02, - logInfo = 0x04, - logDebug = 0x08 - }; - @endcode - * And you set the mask with setMask( logInfo ) the only messages you would - * see are the ones catagorized logInfo. However, if you used - * setLevel( logInfo ) then you would see logInfo, logWarning, and logError - * type messages, since they are lower order. - */ - class Logger : public Bu::Singleton - { - friend class Bu::Singleton; - private: - Logger(); - virtual ~Logger(); - - public: - void log( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...); - - void setFormat( const Bu::String &str ); - void setMask( uint32_t n ); - void setLevel( uint32_t n ); - uint32_t getMask(); - - void hexDump( uint32_t nLevel, const char *sFile, const char *sFunction, int nLine, const void *pData, long nDataLen, const char *lpName ); - - private: - Bu::String sLogFormat; - uint32_t nLevelMask; - }; -} - -/** - * Use Bu::Logger to log a message at the given level and with the given message - * using printf style formatting, and include extra data such as the current - * file, line number, and function. - */ -#define lineLog( nLevel, sFrmt, ...) \ - Bu::Logger::getInstance().log( nLevel, __FILE__, __PRETTY_FUNCTION__, __LINE__, sFrmt, ##__VA_ARGS__ ) - -#define logHexDump( nLevel, pData, iSize, sName ) \ - Bu::Logger::getInstance().hexDump( nLevel, __FILE__, __PRETTY_FUNCTION__, __LINE__, pData, iSize, sName ) - -/** - * Set the Bu::Logger logging mask directly. See Bu::Logger::setMask for - * details. - */ -#define setLogMask( nLevel ) \ - Bu::Logger::getInstance().setMask( nLevel ) - -/** - * Set the Bu::Logger format. See Bu::Logger::setFormat for details. - */ -#define setLogFormat( sFrmt ) \ - Bu::Logger::getInstance().setFormat( sFrmt ) - -/** - * Set the Bu::Logger logging mask simulating levels. See Bu::Logger::setLevel - * for details. - */ -#define setLogLevel( nLevel ) \ - Bu::Logger::getInstance().setLevel( nLevel ) - -#endif diff --git a/src/lzma.cpp b/src/lzma.cpp deleted file mode 100644 index 6ed0806..0000000 --- a/src/lzma.cpp +++ /dev/null @@ -1,248 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/lzma.h" -#include "bu/trace.h" - -#include - -#define pState ((lzma_stream *)prState) - -using namespace Bu; - -Bu::Lzma::Lzma( Bu::Stream &rNext, int nCompression, Format eFmt ) : - Bu::Filter( rNext ), - prState( NULL ), - nCompression( nCompression ), - sTotalOut( 0 ), - eFmt( eFmt ), - bEos( false ) -{ - TRACE( nCompression ); - start(); -} - -Bu::Lzma::~Lzma() -{ - TRACE(); - stop(); -} - -void Bu::Lzma::start() -{ - TRACE(); - nBufSize = 64*1024; - pBuf = new char[nBufSize]; -} - -Bu::size Bu::Lzma::stop() -{ - TRACE(); - if( pState ) - { - if( bReading ) - { - lzma_end( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return 0; - } - else - { - for(;;) - { - pState->next_in = NULL; - pState->avail_in = 0; - pState->avail_out = nBufSize; - pState->next_out = (uint8_t *)pBuf; - int res = lzma_code( pState, LZMA_FINISH ); - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( res == LZMA_STREAM_END ) - break; - } - lzma_end( pState ); - delete[] pBuf; - pBuf = NULL; - delete pState; - prState = NULL; - return sTotalOut; - } - } - return 0; -} - -void Bu::Lzma::lzmaError( int code ) -{ - TRACE( code ); - switch( code ) - { - case LZMA_OK: - case LZMA_STREAM_END: - case LZMA_NO_CHECK: - case LZMA_UNSUPPORTED_CHECK: - break; - - case LZMA_MEM_ERROR: - throw ExceptionBase("Lzma: Memory allocation error."); - - case LZMA_MEMLIMIT_ERROR: - throw ExceptionBase("Lzma: Memory usage limit was reached."); - - case LZMA_FORMAT_ERROR: - throw ExceptionBase("Lzma: File format not recognized."); - - case LZMA_OPTIONS_ERROR: - throw ExceptionBase("Lzma: Invalid or unsupported options."); - - case LZMA_DATA_ERROR: - throw ExceptionBase("Lzma: Data is corrupt."); - - case LZMA_BUF_ERROR: - throw ExceptionBase("Lzma: No progress is possible."); - - case LZMA_PROG_ERROR: - throw ExceptionBase("Lzma: Programming error."); - - default: - throw ExceptionBase("Lzma: Unknown error encountered." ); - } -} - -Bu::size Bu::Lzma::read( void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( !pState ) - { - prState = new ::lzma_stream; - lzma_stream zEmpty = LZMA_STREAM_INIT; - Bu::memcpy( prState, &zEmpty, sizeof(lzma_stream) ); - - bReading = true; - lzmaError( lzma_auto_decoder( pState, UINT64_MAX, 0 ) ); - pState->next_in = (uint8_t *)pBuf; - pState->avail_in = 0; - } - if( bReading == false ) - throw ExceptionBase("This lzma filter is in writing mode, you can't read."); - - int nRead = 0; - int nReadTotal = pState->total_out; - pState->next_out = (uint8_t *)pData; - pState->avail_out = nBytes; - for(;;) - { - int ret = lzma_code( pState, LZMA_RUN ); - printf("inflate returned %d; avail in=%d, out=%d\n", ret, - pState->avail_in, pState->avail_out ); - - nReadTotal += nRead-pState->avail_out; - - if( ret == LZMA_STREAM_END ) - { - bEos = true; - if( pState->avail_in > 0 ) - { - if( rNext.isSeekable() ) - { - rNext.seek( -pState->avail_in ); - } - } - return nBytes-pState->avail_out; - } -// if( ret != LZMA_BUF_ERROR ) - lzmaError( ret ); - - if( pState->avail_out ) - { - if( pState->avail_in == 0 ) - { - nRead = rNext.read( pBuf, nBufSize ); - if( nRead == 0 && rNext.isEos() ) - { - throw Bu::ExceptionBase("Premature end of underlying " - "stream found reading deflate stream."); - } - pState->next_in = (uint8_t *)pBuf; - pState->avail_in = nRead; - } - } - else - { - return nBytes-pState->avail_out; - } - } - return 0; -} - -Bu::size Bu::Lzma::write( const void *pData, Bu::size nBytes ) -{ - TRACE( pData, nBytes ); - if( !pState ) - { - prState = new ::lzma_stream; - lzma_stream zEmpty = LZMA_STREAM_INIT; - Bu::memcpy( prState, &zEmpty, sizeof(lzma_stream) ); - - bReading = false; - if( eFmt == Xz ) - lzmaError( - lzma_easy_encoder( pState, nCompression, LZMA_CHECK_CRC64 ) - ); - else if( eFmt == LzmaAlone ) - { - lzma_options_lzma opt; - lzma_lzma_preset( &opt, nCompression ); - lzmaError( lzma_alone_encoder( pState, &opt ) ); - } - else - throw Bu::ExceptionBase("Invalid format for lzma."); - } - if( bReading == true ) - throw ExceptionBase("This lzma filter is in reading mode, you can't write."); - - pState->next_in = (uint8_t *)pData; - pState->avail_in = nBytes; - for(;;) - { - pState->avail_out = nBufSize; - pState->next_out = (uint8_t *)pBuf; - - lzmaError( lzma_code( pState, LZMA_RUN ) ); - - if( pState->avail_out < nBufSize ) - { - sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); - } - if( pState->avail_in == 0 ) - break; - } - - return nBytes; -} - -bool Bu::Lzma::isOpen() -{ - TRACE(); - return (pState != NULL); -} - -bool Bu::Lzma::isEos() -{ - TRACE(); - return bEos; -} - -Bu::size Bu::Lzma::getCompressedSize() -{ - return sTotalOut; -} - diff --git a/src/lzma.h b/src/lzma.h deleted file mode 100644 index 090da8d..0000000 --- a/src/lzma.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_LZMA_H -#define BU_LZMA_H - -#include - -#include "bu/filter.h" - -namespace Bu -{ - /** - * Provides XZ compression and decompression, both LZMA1 (LzmaAlone) as - * well as the newer LZMA2 (xz) format. This uses .xz by default. - * - *@ingroup Streams - *@ingroup Compression - */ - class Lzma : public Bu::Filter - { - public: - enum Format - { - Xz = 0x01, - LzmaAlone = 0x02, - }; - - Lzma( Bu::Stream &rNext, int nCompression=6, Format eFmt=Xz ); - virtual ~Lzma(); - - virtual void start(); - virtual Bu::size stop(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - - virtual bool isOpen(); - virtual bool isEos(); - - Bu::size getCompressedSize(); - - private: - void lzmaError( int code ); - void *prState; - bool bReading; - int nCompression; - char *pBuf; - uint32_t nBufSize; - Bu::size sTotalOut; - Format eFmt; - bool bEos; - }; -} - -#endif diff --git a/src/md5.cpp b/src/md5.cpp deleted file mode 100644 index 15cba17..0000000 --- a/src/md5.cpp +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include -#include -#include -#include "bu/md5.h" -#include "bu/stream.h" - -#ifdef SYSTEM_BIG_ENDIAN -# define toLittleEndian( a, b ) _toLittleEndian( a, b ) -#else -# define toLittleEndian( a, b ) (void)0 -#endif - -Bu::Md5::Md5() -{ - reset(); -} - -Bu::Md5::~Md5() -{ -} - -void Bu::Md5::reset() -{ - // These are the magic seed numbers... - - sum[0] = 0x67452301U; - sum[1] = 0xEFCDAB89U; - sum[2] = 0x98BADCFEU; - sum[3] = 0x10325476U; - - uBits[0] = 0; - uBits[1] = 0; -} - -void Bu::Md5::setSalt( const Bu::String & /*sSalt*/ ) -{ -} - -void Bu::Md5::addData( const void *sVData, int iSize ) -{ - const char *sData = (const char *)sVData; - uint32_t t; - - t = uBits[0]; - if( (uBits[0] = t + ((uint32_t)iSize << 3)) < t ) - uBits[1]++; - uBits[1] += iSize >> 29; - - t = (t >> 3) & 0x3f; /* How many bytes we have buffered */ - - /* Handle any leading odd-sized chunks */ - if( t ) - { - unsigned char *p = (unsigned char *) inbuf + t; - - t = 64 - t; - if( (uint32_t)iSize < t ) { - memcpy( p, sData, iSize ); - return; - } - memcpy( p, sData, t ); - toLittleEndian( inbuf, 16 ); - compBlock( sum, (uint32_t *)inbuf ); - sData += t; - iSize -= t; - } - - /* Process data in 64-byte chunks */ - while( iSize >= 64 ) - { - memcpy( inbuf, sData, 64 ); - toLittleEndian( inbuf, 16 ); - compBlock( sum, (uint32_t *)inbuf ); - sData += 64; - iSize -= 64; - } - - /* Handle any remaining bytes of data. */ - memcpy( inbuf, sData, iSize ); -} - -Bu::String Bu::Md5::getResult() -{ - uint32_t lsum[4]; - compCap( lsum ); - return Bu::String( (const char *)lsum, 4*4 ); -} - -void Bu::Md5::writeResult( Bu::Stream &sOut ) -{ - uint32_t lsum[4]; - compCap( lsum ); - sOut.write( lsum, 4*4 ); -} - -void Bu::Md5::compCap( uint32_t *sumout ) -{ - uint8_t tmpbuf[64]; - memcpy( sumout, sum, 4*4 ); - memcpy( tmpbuf, inbuf, 64 ); - - uint32_t count; - uint8_t *p; - - /* Compute number of bytes mod 64 */ - count = (uBits[0] >> 3) & 0x3F; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = tmpbuf + count; - *p++ = 0x80; - - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; - - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset( p, 0, count ); - toLittleEndian( tmpbuf, 16 ); - compBlock( sumout, (uint32_t *)tmpbuf ); - - /* Now fill the next block with 56 bytes */ - memset( tmpbuf, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset( p, 0, count - 8); - } - toLittleEndian( tmpbuf, 14 ); - - /* Append length in bits and transform */ - ((uint32_t *) tmpbuf)[14] = uBits[0]; - ((uint32_t *) tmpbuf)[15] = uBits[1]; - - compBlock( sumout, (uint32_t *)tmpbuf ); - toLittleEndian((unsigned char *)sumout, 4); -} - -#define F1(x, y, z) (z ^ (x & (y ^ z))) -#define F2(x, y, z) F1(z, x, y) -#define F3(x, y, z) (x ^ y ^ z) -#define F4(x, y, z) (y ^ (x | ~z)) - -/* This is the central step in the MD5 algorithm. */ -#define MD5STEP(f, w, x, y, z, data, s) \ - ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) - -void Bu::Md5::compBlock( uint32_t *lsum, uint32_t *x ) -{ - register uint32_t a, b, c, d; - a = lsum[0]; - b = lsum[1]; - c = lsum[2]; - d = lsum[3]; - - MD5STEP(F1, a, b, c, d, x[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, x[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, x[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, x[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, x[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, x[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, x[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, x[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, x[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, x[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, x[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, x[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, x[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, x[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, x[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, x[15] + 0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, x[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, x[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, x[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, x[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, x[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, x[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, x[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, x[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, x[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, x[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, x[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, x[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, x[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, x[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, x[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, x[12] + 0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, x[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, x[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, x[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, x[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, x[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, x[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, x[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, x[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, x[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, x[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, x[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, x[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, x[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, x[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, x[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, x[2] + 0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, x[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, x[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, x[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, x[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, x[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, x[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, x[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, x[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, x[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, x[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, x[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, x[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, x[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, x[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, x[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, x[9] + 0xeb86d391, 21); - - lsum[0] += a; - lsum[1] += b; - lsum[2] += c; - lsum[3] += d; -} - -void Bu::Md5::_toLittleEndian( uint8_t *buf, uint32_t count ) -{ - uint32_t t; - do { - t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | - ((unsigned) buf[1] << 8 | buf[0]); - *(uint32_t *) buf = t; - buf += 4; - } while( --count ); -} diff --git a/src/md5.h b/src/md5.h deleted file mode 100644 index b7597fd..0000000 --- a/src/md5.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MD5_H -#define BU_MD5_H - -#include "bu/cryptohash.h" - -namespace Bu -{ - /** - * Class for easily calculating MD5 sums of just about any data. - * This code is based on some public domain code written by Colin Plumb in - * 1993. - *@author Mike Buland - */ - class Md5 : public Bu::CryptoHash - { - public: - /** Build an MD5 sum builder. */ - Md5(); - - /** Deconstruct */ - virtual ~Md5(); - - virtual void reset(); - virtual void setSalt( const Bu::String &sSalt ); - virtual void addData( const void *sData, int iSize ); - using Bu::CryptoHash::addData; - virtual String getResult(); - virtual void writeResult( Bu::Stream &sOut ); - - private: - /** - * Compute one block of input data. - */ - void compBlock( uint32_t *lsum, uint32_t *x ); - void compCap( uint32_t *sumout ); - - void _addData( uint8_t *target, int &iCurFill, const void *sData, - int iSize ); - void _toLittleEndian( uint8_t *buf, uint32_t count ); - - uint8_t inbuf[64]; - uint32_t sum[4]; - uint32_t uBits[2]; - }; -}; - -#endif diff --git a/src/membuf.cpp b/src/membuf.cpp deleted file mode 100644 index 14d0d58..0000000 --- a/src/membuf.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/membuf.h" - -using namespace Bu; - -Bu::MemBuf::MemBuf() : - nPos( 0 ) -{ -} - -Bu::MemBuf::MemBuf( const Bu::String &str ) : - sBuf( str ), - nPos( 0 ) -{ -} - -Bu::MemBuf::~MemBuf() -{ -} - -void Bu::MemBuf::close() -{ -} - -size Bu::MemBuf::read( void *pBuf, size nBytes ) -{ - if( (size)sBuf.getSize()-(size)nPos < nBytes ) - nBytes = sBuf.getSize()-nPos; - - memcpy( pBuf, sBuf.getStr()+nPos, nBytes ); - nPos += nBytes; - - return nBytes; -} - -size Bu::MemBuf::write( const void *pBuf, size nBytes ) -{ - if( nPos == sBuf.getSize() ) - { - // Easiest, just append the data. - sBuf.append( (const char *)pBuf, nBytes ); - nPos += nBytes; - return nBytes; - } - else - { - // Trickier, we must do this in two parts, overwrite, then append - // Frist, overwrite. - size iOver = sBuf.getSize() - nPos; - if( iOver > nBytes ) - iOver = nBytes; - memcpy( sBuf.getStr()+nPos, pBuf, iOver ); - // Then append - if( iOver < nBytes ) - { - sBuf.append( ((const char *)pBuf)+iOver, nBytes-iOver ); - } - nPos += nBytes; - return nBytes; - } -} - -size Bu::MemBuf::tell() -{ - return nPos; -} - -void Bu::MemBuf::seek( size offset ) -{ - nPos += offset; - if( nPos < 0 ) nPos = 0; - else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); -} - -void Bu::MemBuf::setPos( size pos ) -{ - nPos = pos; - if( nPos < 0 ) nPos = 0; - else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); -} - -void Bu::MemBuf::setPosEnd( size pos ) -{ - nPos = sBuf.getSize()-pos; - if( nPos < 0 ) nPos = 0; - else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize(); -} - -bool Bu::MemBuf::isEos() -{ - return (nPos == sBuf.getSize()); -} - -bool Bu::MemBuf::isOpen() -{ - return true; -} - -void Bu::MemBuf::flush() -{ -} - -bool Bu::MemBuf::canRead() -{ - return !isEos(); -} - -bool Bu::MemBuf::canWrite() -{ - return true; -} - -bool Bu::MemBuf::isReadable() -{ - return true; -} - -bool Bu::MemBuf::isWritable() -{ - return true; -} - -bool Bu::MemBuf::isSeekable() -{ - return true; -} - -bool Bu::MemBuf::isBlocking() -{ - return true; -} - -void Bu::MemBuf::setBlocking( bool ) -{ -} - -void Bu::MemBuf::setSize( size iSize ) -{ - if( iSize < 0 ) - iSize = 0; - sBuf.setSize( iSize ); - if( nPos > iSize ) - nPos = iSize; -} - -Bu::size Bu::MemBuf::getSize() const -{ - return sBuf.getSize(); -} - -Bu::size Bu::MemBuf::getBlockSize() const -{ - return sBuf.getSize(); -} - -Bu::String Bu::MemBuf::getLocation() const -{ - return ""; -} - -Bu::String &Bu::MemBuf::getString() -{ - return sBuf; -} - -void Bu::MemBuf::setString( const Bu::String &sNewData ) -{ - sBuf = sNewData; - nPos = 0; -} - diff --git a/src/membuf.h b/src/membuf.h deleted file mode 100644 index 544dc83..0000000 --- a/src/membuf.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MEM_BUF_H -#define BU_MEM_BUF_H - -#include - -#include "bu/config.h" -#include "bu/stream.h" -#include "bu/string.h" - -namespace Bu -{ - /** - * A memory buffer stream. This provides a read/write stream in memory that - * works exactly like a file stream...only in memory. You can seed the - * memory buffer with a Bu::String of your own, or start with an empty one. - * Due to Bu::String using Bu::SharedCore starting with a string will not - * necesarilly cause the MemBuf to make a copy of your memory, but if you're - * sure you're not going to need to change the stream then use StaticMemBuf. - *@ingroup Streams - */ - class MemBuf : public Stream - { - public: - MemBuf(); - MemBuf( const Bu::String &str ); - virtual ~MemBuf(); - - virtual void close(); - virtual size read( void *pBuf, size iBytes ); - - virtual size write( const void *pBuf, size iBytes ); - using Stream::write; - virtual size tell(); - virtual void seek( size offset ); - virtual void setPos( size pos ); - virtual void setPosEnd( size pos ); - virtual bool isEos(); - virtual bool isOpen(); - virtual void flush(); - virtual bool canRead(); - virtual bool canWrite(); - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( size iSize ); - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - Bu::String &getString(); - void setString( const Bu::String &sNewData ); - - private: - Bu::String sBuf; - size nPos; - }; -} - -#endif diff --git a/src/minicron.cpp b/src/minicron.cpp deleted file mode 100644 index 95cf66b..0000000 --- a/src/minicron.cpp +++ /dev/null @@ -1,477 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/minicron.h" - -#include -#include - -Bu::MiniCron::MiniCron() : - jidNext( 1 ) -{ -} - -Bu::MiniCron::~MiniCron() -{ - while( !hJobs.isEmpty() ) - { - delete hJobs.dequeue(); - } -} - -bool Bu::MiniCron::hasJobs() -{ - return !hJobs.isEmpty(); -} - -time_t Bu::MiniCron::getNextRun() -{ - if( hasJobs() ) - return hJobs.peek()->getNextRun(); - return -1; -} - -time_t Bu::MiniCron::getNextRun( Bu::MiniCron::JobId jid ) -{ - for( JobHeap::iterator i = hJobs.begin(); i; i++ ) - { - if( (*i)->getId() == jid ) - { - return (*i)->getNextRunTime(); - } - } - return -1; -} - -void Bu::MiniCron::poll() -{ - time_t tNow = time( NULL ); - - while( !hJobs.isEmpty() ) - { - if( hJobs.peek()->getNextRun() <= tNow ) - { - Job *pJob = hJobs.dequeue(); - pJob->run(); - if( pJob->bContinue ) - { - hJobs.enqueue( pJob ); - } - else - { - delete pJob; - } - } - else - { - break; - } - } -} - -Bu::MiniCron::JobId Bu::MiniCron::addJob( const Bu::String &sName, - Bu::MiniCron::CronSignal sigJob, const Bu::MiniCron::Timer &t ) -{ - JobId jid = jidNext++; - Job *pJob = new Job( sName, jid ); - pJob->sigJob = sigJob; - pJob->pTimer = t.clone(); - pJob->tNextRun = pJob->pTimer->nextTime(); - hJobs.enqueue( pJob ); - - return jid; -} - -Bu::MiniCron::JobId Bu::MiniCron::addJobOnce( const Bu::String &sName, - Bu::MiniCron::CronSignal sigJob, const Bu::MiniCron::Timer &t ) -{ - JobId jid = jidNext++; - Job *pJob = new Job( sName, jid, false ); - pJob->sigJob = sigJob; - pJob->pTimer = t.clone(); - pJob->tNextRun = pJob->pTimer->nextTime(); - hJobs.enqueue( pJob ); - - return jid; -} - -void Bu::MiniCron::removeJob( JobId jid ) -{ - Bu::List lJobs; - while( !hJobs.isEmpty() ) - { - Job *pJob = hJobs.dequeue(); - if( pJob->getId() == jid ) - { - delete pJob; - } - else - lJobs.append( pJob ); - } - - for( Bu::List::iterator i = lJobs.begin(); i; i++ ) - { - hJobs.enqueue( *i ); - } -} - -void Bu::MiniCron::runJob( JobId jid, bool bReschedule ) -{ - Bu::List lJobs; - while( !hJobs.isEmpty() ) - { - Job *pJob = hJobs.dequeue(); - if( pJob->getId() == jid ) - { - pJob->run( bReschedule ); - if( !pJob->bContinue ) - { - delete pJob; - break; - } - lJobs.append( pJob ); - break; - } - lJobs.append( pJob ); - } - - for( Bu::List::iterator i = lJobs.begin(); i; i++ ) - { - hJobs.enqueue( *i ); - } -} - -void Bu::MiniCron::runJob( const Bu::String &sName, bool bReschedule ) -{ - Bu::List lJobs; - while( !hJobs.isEmpty() ) - { - Job *pJob = hJobs.dequeue(); - if( pJob->getName() == sName ) - { - pJob->run( bReschedule ); - if( !pJob->bContinue ) - { - delete pJob; - break; - } - lJobs.append( pJob ); - break; - } - lJobs.append( pJob ); - } - - for( Bu::List::iterator i = lJobs.begin(); i; i++ ) - { - hJobs.enqueue( *i ); - } -} - -Bu::MiniCron::JobInfoList Bu::MiniCron::getJobInfo() -{ - JobInfoList lRet; - for( JobHeap::iterator i = hJobs.begin(); i; i++ ) - { - lRet.append( - JobInfo( (*i)->getName(), (*i)->getId(), (*i)->getNextRun() ) - ); - } - lRet.sort(); - return lRet; -} - -Bu::MiniCron::Job::Job( const Bu::String &sName, JobId jid, bool bRepeat ) : - sName( sName ), - pTimer( NULL ), - bContinue( bRepeat ), - jid( jid ), - tAdded( time( NULL ) ), - iRunCount( 0 ) -{ -} - -Bu::MiniCron::Job::~Job() -{ - delete pTimer; - pTimer = NULL; -} - -void Bu::MiniCron::Job::run( bool bReschedule ) -{ - iRunCount++; - if( bReschedule ) - tNextRun = pTimer->nextTime(); - sigJob( *this ); -} - -time_t Bu::MiniCron::Job::getNextRun() const -{ - return tNextRun; -} - -void Bu::MiniCron::Job::calcNextRun() -{ - if( pTimer ) - tNextRun = pTimer->nextTime(); -} - -void Bu::MiniCron::Job::setTimer( const Timer &t ) -{ - delete pTimer; - pTimer = t.clone(); -} - -void Bu::MiniCron::Job::stop() -{ - bContinue = false; -} - -void Bu::MiniCron::Job::resume() -{ - bContinue = true; -} - -Bu::MiniCron::JobId Bu::MiniCron::Job::getId() const -{ - return jid; -} - -time_t Bu::MiniCron::Job::getTimeCreated() const -{ - return tAdded; -} - -int Bu::MiniCron::Job::getRunCount() const -{ - return iRunCount; -} - -time_t Bu::MiniCron::Job::getNextRunTime() const -{ - return tNextRun; -} - -Bu::String Bu::MiniCron::Job::getName() const -{ - return sName; -} - -Bu::MiniCron::JobInfo::JobInfo( const Bu::String &sName, JobId jid, - time_t tNext ) : - sName( sName ), - jid( jid ), - tNext( tNext ) -{ -} - -Bu::MiniCron::JobInfo::~JobInfo() -{ -} - -bool Bu::MiniCron::JobInfo::operator<( const JobInfo &rhs ) const -{ - return jid < rhs.jid; -} - -Bu::MiniCron::Timer::Timer() -{ -} - -Bu::MiniCron::Timer::~Timer() -{ -} - -Bu::MiniCron::TimerInterval::TimerInterval( time_t tFirst, time_t tInterval ) : - tNext( tFirst ), - tInterval( tInterval ) -{ -} - -Bu::MiniCron::TimerInterval::~TimerInterval() -{ -} - -time_t Bu::MiniCron::TimerInterval::nextTime() -{ - time_t tRet = tNext; - tNext += tInterval; - return tRet; -} - -Bu::MiniCron::TimerBasic::TimerBasic( const Bu::String &s ) : - tLast( -1 ), - sSpec( s ) -{ -} - -Bu::MiniCron::TimerBasic::~TimerBasic() -{ -} - -time_t Bu::MiniCron::TimerBasic::nextTime() -{ - if( tLast == -1 ) - tLast = time( NULL ); - - Bu::String::const_iterator i = sSpec.begin(); - switch( lex( i ) ) - { - case tokDaily: - { - int iHour = lexInt( i ); - int iMin = lexInt( i ); - - struct tm t; - ::memcpy( &t, localtime( &tLast ), sizeof(struct tm) ); - if( iHour < t.tm_hour || - (iHour == t.tm_hour && iMin <= t.tm_min) ) - { - t.tm_mday++; - } - t.tm_hour = iHour; - t.tm_min = iMin; - t.tm_sec = 0; - tLast = mktime( &t ); - } - break; - - case tokHourly: - { - int iMin = lexInt( i ); - - struct tm t; - ::memcpy( &t, localtime( &tLast ), sizeof(struct tm) ); - if( iMin <= t.tm_min ) - t.tm_hour++; - t.tm_min = iMin; - t.tm_sec = 0; - tLast = mktime( &t ); - } - break; - - case tokWeekly: - { - int iDay = lexInt( i ); - int iHour = lexInt( i ); - int iMin = lexInt( i ); - - struct tm t; - ::memcpy( &t, localtime( &tLast ), sizeof(struct tm) ); - if( iDay < t.tm_wday || - (iDay == t.tm_wday && iHour < t.tm_hour) || - (iDay == t.tm_wday && iHour == t.tm_hour - && iMin <= t.tm_min) ) - { - if( iDay <= t.tm_wday ) - t.tm_mday += 7 - (t.tm_wday-iDay); - else - t.tm_mday += 7 - (iDay-t.tm_wday); - } - else - { - t.tm_mday += (iDay-t.tm_wday); - } - t.tm_hour = iHour; - t.tm_min = iMin; - t.tm_sec = 0; - tLast = mktime( &t ); - } - break; - - case tokMonthly: - break; - - case tokYearly: - break; - - default: - break; - } - - return tLast; -} - -Bu::MiniCron::TimerBasic::Token Bu::MiniCron::TimerBasic::lex( - Bu::String::const_iterator &i ) -{ - if( !i ) - { - return tokEos; - } - - Bu::String::const_iterator b = i; - - for(; b && (*b == ' ' || *b == '\t'); b++ ) { i = b+1; } - for(; b && *b != ' ' && *b != '\t'; b++ ) { } - - Bu::String sTok( i, b ); - i = b; - - if( sTok == "daily" ) - return tokDaily; - else if( sTok == "hourly" ) - return tokHourly; - else if( sTok == "weekly" ) - return tokWeekly; - else if( sTok == "monthly" ) - return tokMonthly; - else if( sTok == "yearly" ) - return tokYearly; - else if( sTok == "sun" ) - { - iVal = 0; - return valInt; - } - else if( sTok == "mon" ) - { - iVal = 1; - return valInt; - } - else if( sTok == "tue" ) - { - iVal = 2; - return valInt; - } - else if( sTok == "wed" ) - { - iVal = 3; - return valInt; - } - else if( sTok == "thu" ) - { - iVal = 4; - return valInt; - } - else if( sTok == "fri" ) - { - iVal = 5; - return valInt; - } - else if( sTok == "sat" ) - { - iVal = 6; - return valInt; - } - else if( sTok[0] >= '0' && sTok[0] <= '9' ) - { - iVal = strtol( sTok.getStr(), NULL, 0 ); - return valInt; - } - - return tokErr; -} - -int Bu::MiniCron::TimerBasic::lexInt( Bu::String::const_iterator &i ) -{ - Token t = lex( i ); - if( t == tokEos ) - return 0; - if( t != valInt ) - throw Bu::ExceptionBase("Expected int, got something else."); - return iVal; -} - diff --git a/src/minicron.h b/src/minicron.h deleted file mode 100644 index 0d1cb62..0000000 --- a/src/minicron.h +++ /dev/null @@ -1,336 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MINICRON_H -#define BU_MINICRON_H - -#include "bu/signals.h" -#include "bu/heap.h" -#include "bu/string.h" - -#include - -namespace Bu -{ - /** - * A simple cron like system designed to be embedded in any program. This - * class creates a simple cron system that can run any number of jobs at - * customizable intervals or schedules. It does not support some of the - * more complex scheduling that some cron systems can do such as load - * balancing directly, but this could be done on the job side. - * - * This system is synchronous, it does not use any threads on it's own, but - * it is threadsafe, so a cron thread could be created if desired. - * - * The operation is fairly simple, jobs can be added at any time, and use - * any timer they would like, even custom timers. When it is time for a - * job to be run it signals the slot provided when the job was added. Every - * job slot recieves a handle to the job object so that it may control it's - * own lifetime and get information about itself. In addition, every job - * is assigned a unique ID that can be used to control it's operation - * at any time. - * - * By default a job will continually reschedule itself after being run - * unless it calls stop() on it's job object, it is removed using - * removeJob() on the cron object, or it is added with addJobOnce. - * - *@todo A minor change to the job execution system could allow a Timer to - * defer or reschedule execution instead of the job executing. This would, - * in effect, allow us to do every type of interesting scheduling that - * systems like fcron offer, including time constrained load-balanced - * execution. - */ - class MiniCron - { - public: - class Job; - class Timer; - typedef Bu::Signal1 CronSignal; - typedef int JobId; - - MiniCron(); - virtual ~MiniCron(); - - /** - * Tells you if there are jobs registered in the MiniCron. - *@returns true if there are jobs, false otherwise. - */ - virtual bool hasJobs(); - - /** - * If there are jobs, tells you the time the next one will execute. - *@returns The timestamp that the next job will execute at. - */ - virtual time_t getNextRun(); - - /** - * Tells you the time the job matching jid will run next. - *@returns The timestamp that the job jid will next run. - */ - virtual time_t getNextRun( JobId jid ); - - /** - * Call this regularly to execute all jobs that should be executed. - * This will loop until all jobs who's run time match the current time - * or are below the current time (we've missed them). - * If there is nothing to run, the runtime of this funcion is constant, - * it is very fast. Otherwise it executes at log(N) per job run, - * O(N*log(N)). - */ - virtual void poll(); - - /** - * Add a job for repeated scheduling. Pass in a slot to signal, and a - * Timer object to use to do the scheduling. This function returns a - * JobId which can be used at a later time to control the execution of - * the job. - */ - virtual JobId addJob( const Bu::String &sName, CronSignal sigJob, - const Timer &t ); - - /** - * Add a job for one time scheduling. Pass in a slot to signal, and a - * Timer object to use to schodule the one run of this job. This - * function returns a JobId which can be used at a later time to control - * the execution of the job. - */ - virtual JobId addJobOnce( const Bu::String &sName, CronSignal sigJob, - const Timer &t ); - - /** - * Remove a job, preventing all future runs of the job. If there is no - * job matching the given JobId then nothing will happen. However, this - * function is relatively expensive compared to the others in this class - * and has a worse case runtime of 2*N*log(N), still not that bad, and - * a O(N*log(N)). - */ - virtual void removeJob( JobId jid ); - - /** - * Executes the job specified right now. If bReschedule is true then - * the job is then removed from the queue and rescheduled as though - * it's time had come naturally to be run. Otherwise, it's run without - * interrupting the normal schedule. - */ - virtual void runJob( JobId jid, bool bReschedule=false ); - - /** - * Executes the job specified right now. If bReschedule is true then - * the job is then removed from the queue and rescheduled as though - * it's time had come naturally to be run. Otherwise, it's run without - * interrupting the normal schedule. - */ - virtual void runJob( const Bu::String &sName, bool bReschedule=false ); - - class JobInfo - { - public: - JobInfo( const Bu::String &sName, JobId jid, time_t tNext ); - virtual ~JobInfo(); - - bool operator<( const JobInfo &rhs ) const; - - Bu::String sName; - JobId jid; - time_t tNext; - }; - typedef Bu::List JobInfoList; - - JobInfoList getJobInfo(); - - /** - * The baseclass for timer/schedulers for MiniCron jobs. Classes that - * inherit from this are used to determine when jobs will run and at - * what interval. - */ - class Timer - { - public: - Timer(); - virtual ~Timer(); - - /** - * Called by MiniCron when each job is run to determine the next - * time that a job should be run. When a job is run, this function - * is actually called before the job is executed again so that the - * job can tell when the next time it will be run will be. - */ - virtual time_t nextTime()=0; - - /** - * This function should return a copy of the child class. - */ - virtual Timer *clone() const = 0; - }; - - /** - * Execute the job every tInterval seconds, also you can delay the - * first run by a different amount of time from the job's creation. - */ - class TimerInterval : public Timer - { - public: - TimerInterval( time_t tFirst, time_t tInterval ); - virtual ~TimerInterval(); - - virtual time_t nextTime(); - virtual Timer *clone() const - { return new TimerInterval( *this ); } - private: - time_t tNext; - time_t tInterval; - }; - - /** - * A much more general timer class that can be used for much more - * "cron-like" functionality. The constructor takes a string that - * describes the times that the job should be run. At the moment the - * following schemes are understood: - * - * "daily [hour] [minute]" - * "hourly [minute]" - * "weekly [day] [hour] [minute]" - * - * In these examples each word in [brackets] represents a number that - * matches the data type in the brackets. [day] is the number of days - * since sunday, 0-6. You can also use lowercase three character - * abbreviations for the day names. - * - * Many more forms follow. - */ - class TimerBasic : public Timer - { - public: - TimerBasic( const Bu::String &s ); - virtual ~TimerBasic(); - - virtual time_t nextTime(); - virtual Timer *clone() const - { return new TimerBasic( *this ); } - - private: - enum Token - { - tokDaily, - tokHourly, - tokWeekly, - tokMonthly, - tokYearly, - valInt, - tokErr, - tokEos - }; - Token lex( Bu::String::const_iterator &i ); - int lexInt( Bu::String::const_iterator &i ); - int iVal; //< A temp variable for parsing. - time_t tLast; - Bu::String sSpec; - }; - - /** - * Represents a MiniCron Job. This class is used for both internal - * job management as well as job slot interaction and control. Objects - * of this class are passed into the slots that are signaled when a job - * is executed. - */ - class Job - { - friend class Bu::MiniCron; - private: - Job( const Bu::String &sName, JobId jid, bool bRepeat=true ); - virtual ~Job(); - - public: - - /** - * Execute this job once, increment the runcount and schedule the - * next occurance of it. - */ - void run( bool bReschedule=true ); - - /** - * Get the time this job will next run. - */ - time_t getNextRun() const; - - /** - * Compute the time this job will next run. - */ - void calcNextRun(); - - /** - * Replace the current job timer with a new one, this will trigger - * a re-schedule. - */ - void setTimer( const Timer &t ); - - /** - * Stop execution of this job, never execute this job again. - */ - void stop(); - - /** - * Undo a previous stop. This will cause a job that has been - * stopped or even added with addJobOnce to be set for repeated - * scheduling. - */ - void resume(); - - /** - * Get the unique ID of this job. - */ - JobId getId() const; - - /** - * Get the timestamp this job was created. - */ - time_t getTimeCreated() const; - - /** - * Get the current run count of this job, how many times it has been - * executed. This is incremented before the slot is signaled. - */ - int getRunCount() const; - - /** - * Get the next time that this job will be run. Certain timers may - * have the ability to delay job executions, so this is the earliest - * time that the job may run. - */ - time_t getNextRunTime() const; - - /** - * Gets the name that was set when the job was created. - */ - Bu::String getName() const; - - private: - Bu::String sName; - CronSignal sigJob; - time_t tNextRun; - Timer *pTimer; - bool bContinue; - JobId jid; - time_t tAdded; - int iRunCount; - }; - - private: - struct JobPtrCmp - { - bool operator()( const Job *pLeft, const Job *pRight ) - { - return pLeft->tNextRun < pRight->tNextRun; - } - }; - typedef Bu::Heap JobHeap; - JobHeap hJobs; - JobId jidNext; - }; -}; - -#endif diff --git a/src/minimacro.cpp b/src/minimacro.cpp deleted file mode 100644 index b6fd6a8..0000000 --- a/src/minimacro.cpp +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/minimacro.h" - -Bu::MiniMacro::MiniMacro() -{ - hFuncs.insert("toupper", new FuncToUpper() ); - hFuncs.insert("tolower", new FuncToLower() ); -} - -Bu::MiniMacro::MiniMacro( const StrHash &sVarSrc ) -{ - for( StrHash::const_iterator i = sVarSrc.begin(); i != sVarSrc.end(); i++ ) - { - addVar( i.getKey(), i.getValue() ); - } -} - -Bu::MiniMacro::~MiniMacro() -{ -} - -Bu::String Bu::MiniMacro::parse( const Bu::String &sIn ) -{ - bContinue = true; - Bu::String sOut; - for( sCur = sIn.getStr(); *sCur && bContinue; sCur++ ) - { - if( *sCur == '{' ) - { - switch( sCur[1] ) - { - case '=': - sCur += 2; - sOut += parseRepl(); - break; - - case '?': - sCur += 2; - sOut += parseCond(); - break; - - case ':': - sCur += 2; - sOut += parseCmd(); - break; - - default: - sOut += *sCur; - continue; - } - } - else - { - sOut += *sCur; - } - } - - iLastPos = (ptrdiff_t)sCur - (ptrdiff_t)sIn.getStr(); - - return sOut; -} - -Bu::String Bu::MiniMacro::parseRepl() -{ - Bu::String sOut; - bool bIsFirst = true; - for( const char *sNext = sCur;;) - { - for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ ) { } - if( *sNext == '\0' ) - break; - Bu::String sName( sCur, (ptrdiff_t)sNext-(ptrdiff_t)sCur ); - if( bIsFirst ) - { - sOut = hVars[sName]; - bIsFirst = false; - //printf("Variable: \"%s\"\n", sName.getStr() ); - } - else - { - sOut = callFunc( sOut, sName ); - //printf("Filter: \"%s\"\n", sName.getStr() ); - } - if( *sNext == '}' ) - { - sCur = sNext; - break; - } - else if( *sNext == ':' ) - { - } - sNext++; - sCur = sNext; - } - return sOut; -} - -Bu::String Bu::MiniMacro::parseCond() -{ - Bu::String sOut; - //printf("%20s\n", sCur ); - return sOut; -} - -Bu::String Bu::MiniMacro::parseCmd() -{ - Bu::String sOut; - const char *sNext = sCur; - for(; *sNext != ':' && *sNext != '}' && *sNext != '\0'; sNext++ ) { } - if( *sNext != '\0' ) - { - Bu::String sName( sCur, (ptrdiff_t)sNext-(ptrdiff_t)sCur ); - if( sName == "end" ) - { - sCur = sNext; - bContinue = false; - return sOut; - } - else - { - throw Bu::ExceptionBase("Unknown command '%s'.", - sName.getStr() - ); - } - } - else - { - //printf("Uh...?\n"); - } - - //printf("%20s\n", sCur ); - return sOut; -} - -Bu::String Bu::MiniMacro::callFunc( - const Bu::String &sIn, const Bu::String &sFunc ) -{ - int i = sFunc.findIdx('('); - if( i < 0 ) - throw Bu::ExceptionBase("That doesn't look like a function call"); - Bu::String sName( sFunc.getStr(), i ); - StrList lsParams; - for( const char *s = sFunc.getStr()+i+1; *s && *s != ')'; s++ ) - { - for(; *s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'; s++ ) { } - const char *sNext; - for( sNext = s; *sNext && *sNext != ')' && *sNext != ','; sNext++ ) { } - Bu::String p( s, (ptrdiff_t)sNext-(ptrdiff_t)s ); - lsParams.append( p ); - sNext++; - s = sNext; - } - return hFuncs.get( sName )->call( sIn, lsParams ); -} - -void Bu::MiniMacro::addVar( - const Bu::String &sName, const Bu::String &sValue ) -{ - hVars.insert( sName, sValue ); -} - -bool Bu::MiniMacro::hasVar( const Bu::String &sName ) -{ - return hVars.has( sName ); -} - -const Bu::String &Bu::MiniMacro::getVar( const Bu::String &sName ) -{ - return hVars.get( sName ); -} - -const Bu::StrHash &Bu::MiniMacro::getVars() -{ - return hVars; -} - -int Bu::MiniMacro::getPosition() -{ - return iLastPos; -} - diff --git a/src/minimacro.h b/src/minimacro.h deleted file mode 100644 index b6c7c13..0000000 --- a/src/minimacro.h +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MINI_MACRO_H -#define BU_MINI_MACRO_H - -#include "bu/hash.h" -#include "bu/string.h" - -namespace Bu -{ - typedef Bu::Hash StrHash; - /** - * A processor for Libbu++ brand Mini Macros. These are really simple, but - * still fairly flexible. It's mainly text replacement, but with a few - * extras like filter functions and conditional text segments. So far we - * don't support loops or anything, I'm not sure we ever will. - * - * Anatomy of a mini macro: - * - Every macro begins with a two character code, the first character is - * always '{', the second character determines the operation to perform. - * - If the '{' is followed by a character that is not valid it is not - * considered for expansion and the characters are copied to the output. - * - Every macro ends with a closing '}' - * - Macro types: - * - '=': variable replacement. The '=' is immediatley followed by the - * name of the variable to replace, then any number of optional filter - * segments. - * - '?': conditional text. The '?' is immediately followed by the - * variable to test. This works two ways, the variable can be alone, in - * which case it's existance is tested, or it can be followed by a "=" - * and a string to compare to. This is then followed by a text segment - * that will be used if the test is true, and an optional text segment - * to be used if the test is false. - * - ':': command. The ':' is immediately followed by a command string, - * of which there's only one right now, but that's ok. These are not - * put into the output stream, but instead mark something for the - * parser. Currently supported: - * - {:end}: end of parsing, stop here, also make note of how many input - * characters were used. - * - Segments: - * - Each segment is seperated by a colon. - * - Filter segments give the name of the filter, followed by - * parenthesies. Parameters may be provided within the parenthesies. - * - Text segments should always be quoted, but may contain any characters - * within the quotes, backslash is used as per C/ANSI/ISO standard. - * You can also quote any text using [' '] instead of quotes, which - * allows for nested strings. The [' token is only recognised within - * a macro. - * - *@verbatim - {=name:tolower()} - {=name:ccsplit("_"):toupper()} - {?name:"name exists and is {=name}"} - {?name:"{=name}":"no name!"} - {?name="bob":"You're named bob!":"Who are you? I only know bob..."} - @endverbatim - */ - class MiniMacro - { - public: - MiniMacro(); - MiniMacro( const StrHash &sVarSrc ); - virtual ~MiniMacro(); - - Bu::String parse( const Bu::String &sIn ); - void addVar( const Bu::String &sName, const Bu::String &sValue ); - bool hasVar( const Bu::String &sName ); - const Bu::String &getVar( const Bu::String &sName ); - const StrHash &getVars(); - int getPosition(); - - private: - const char *sCur; - Bu::String parseRepl(); - Bu::String parseCond(); - Bu::String parseCmd(); - Bu::String callFunc( - const Bu::String &sIn, const Bu::String &sFunc ); - - StrHash hVars; - bool bContinue; - int iLastPos; - - public: - typedef Bu::List StrList; - class Func - { - public: - Func(){} - virtual ~Func(){} - virtual Bu::String call( - const Bu::String &sIn, StrList &lsParam )=0; - }; - - class FuncToUpper : public Func - { - public: - FuncToUpper(){} - virtual ~FuncToUpper(){} - virtual Bu::String call( - const Bu::String &sIn, StrList & ) - { - return sIn.toUpper(); - } - }; - - class FuncToLower : public Func - { - public: - FuncToLower(){} - virtual ~FuncToLower(){} - virtual Bu::String call( - const Bu::String &sIn, StrList & ) - { - return sIn.toLower(); - } - }; - - private: - typedef Bu::Hash FuncHash; - FuncHash hFuncs; - }; -}; - -#endif diff --git a/src/multiserver.cpp b/src/multiserver.cpp deleted file mode 100644 index bd598ed..0000000 --- a/src/multiserver.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/multiserver.h" -#include "bu/protocol.h" -#include "bu/client.h" - -#include "bu/config.h" - -Bu::MultiServer::MultiServer() -{ -} - -Bu::MultiServer::~MultiServer() -{ -} - -void Bu::MultiServer::addProtocol( Bu::Protocol *(*proc)(), int iPort, - int nPoolSize ) -{ - hProtos[iPort] = proc; - addPort( iPort, nPoolSize ); -} - -void Bu::MultiServer::addProtocol( Protocol *(*proc)(), const String &sAddr, - int iPort, int nPoolSize ) -{ - hProtos[iPort] = proc; - addPort( sAddr, iPort, nPoolSize ); -} - -void Bu::MultiServer::onNewConnection( Bu::Client *pClient, int nPort ) -{ - pClient->setProtocol( hProtos.get( nPort )() ); -} - -void Bu::MultiServer::onClosedConnection( Bu::Client *pClient ) -{ - delete pClient->getProtocol(); -} - -void Bu::MultiServer::shutdown() -{ - Bu::Server::shutdown(); -} - -void Bu::MultiServer::tick() -{ - Bu::Server::tick(); -} - diff --git a/src/multiserver.h b/src/multiserver.h deleted file mode 100644 index e3b3ec3..0000000 --- a/src/multiserver.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MULTI_SERVER_H -#define BU_MULTI_SERVER_H - -#include "bu/server.h" -#include "bu/hash.h" - -namespace Bu -{ - class Protocol; - class Client; - - template - Protocol *genProtocol() - { - return new T; - } - - class MultiServer : protected Server - { - public: - MultiServer(); - virtual ~MultiServer(); - - void addProtocol( Protocol *(*proc)(), int iPort, int nPoolSize=40 ); - void addProtocol( Protocol *(*proc)(), const String &sAddr, int iPort, - int nPoolSize=40 ); - - void scan() - { - Server::scan(); - } - - void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ) - { - Server::setTimeout( nTimeoutSec, nTimeoutUSec ); - } - - virtual void onNewConnection( Client *pClient, int nPort ); - virtual void onClosedConnection( Client *pClient ); - - void shutdown(); - - void tick(); - - private: - Bu::Hash hProtos; - }; -} - -#endif diff --git a/src/mutex.cpp b/src/mutex.cpp deleted file mode 100644 index dbaaece..0000000 --- a/src/mutex.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/mutex.h" - -Bu::Mutex::Mutex() -{ - pthread_mutex_init( &mutex, NULL ); -} - -Bu::Mutex::~Mutex() -{ - pthread_mutex_destroy( &mutex ); -} - -int Bu::Mutex::lock() -{ - return pthread_mutex_lock( &mutex ); -} - -int Bu::Mutex::unlock() -{ - return pthread_mutex_unlock( &mutex ); -} - -int Bu::Mutex::trylock() -{ - return pthread_mutex_trylock( &mutex ); -} - diff --git a/src/mutex.h b/src/mutex.h deleted file mode 100644 index b5c8b7a..0000000 --- a/src/mutex.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MUTEX_H -#define BU_MUTEX_H - -#include - -namespace Bu -{ - /** - * Simple mutex wrapper. Currently this doesn't do anything extra for you - * except keep all of the functionality together in an OO sorta' way and - * keep you from having to worry about cleaning up your mutexes properly, - * or initing them. - *@ingroup Threading - */ - class Mutex - { - public: - /** - * Create an unlocked mutex. - */ - Mutex(); - - /** - * Destroy a mutex. This can only be done when a mutex is unlocked. - * Failure to unlock before destroying a mutex object could cause it to - * wait for the mutex to unlock, the odds of which are usually farily - * low at deconstruction time. - */ - ~Mutex(); - - /** - * Lock the mutex. This causes all future calls to lock on this - * instance of mutex to block until the first thread that called mutex - * unlocks it. At that point the next thread that called lock will get - * a chance to go to work. Because of the nature of a mutex lock it is - * a very bad idea to do any kind of serious or rather time consuming - * computation within a locked section. This can cause thread-deadlock - * and your program may hang. - */ - int lock(); - - /** - * Unlock the mutex. This allows the next thread that asked for a lock - * to lock the mutex and continue with execution. - */ - int unlock(); - - /** - * Try to lock the mutex. This is the option to go with if you cannot - * avoid putting lengthy operations within a locked section. trylock - * will attempt to lock the mutex, if the mutex is already locked this - * function returns immediately with an error code. - */ - int trylock(); - - protected: - pthread_mutex_t mutex; /**< The internal mutex reference. */ - }; -} - -#endif diff --git a/src/mutexlocker.cpp b/src/mutexlocker.cpp deleted file mode 100644 index 90b730e..0000000 --- a/src/mutexlocker.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "bu/mutexlocker.h" -#include "bu/mutex.h" - -Bu::MutexLocker::MutexLocker( Bu::Mutex &mu ) : - mu( mu ) -{ - mu.lock(); -} - -Bu::MutexLocker::~MutexLocker() -{ - mu.unlock(); -} - -void Bu::MutexLocker::unlock() -{ - mu.unlock(); -} - -void Bu::MutexLocker::relock() -{ - mu.lock(); -} - diff --git a/src/mutexlocker.h b/src/mutexlocker.h deleted file mode 100644 index 7c3c97e..0000000 --- a/src/mutexlocker.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef BU_MUTEX_LOCKER_H -#define BU_MUTEX_LOCKER_H - -namespace Bu -{ - class Mutex; - class MutexLocker - { - public: - MutexLocker( Mutex &mu ); - virtual ~MutexLocker(); - - void unlock(); - void relock(); - - private: - Mutex μ - }; -}; - -#endif diff --git a/src/myriad.cpp b/src/myriad.cpp deleted file mode 100644 index de44930..0000000 --- a/src/myriad.cpp +++ /dev/null @@ -1,663 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/myriad.h" -#include "bu/stream.h" -#include "bu/myriadstream.h" -#include - -#include "bu/sio.h" -using Bu::sio; -using Bu::Fmt; - -#define Myriad_MAGIC_CODE ((unsigned char *)"\x0a\xd3\xfa\x84") - -namespace Bu -{ - subExceptionDef( MyriadException ) - template t blkDiv( t total, t block ) { - return (total/block)+((total%block==0)?(0):(1)); - } -} - -Bu::Myriad::Myriad( Bu::Stream &sStore, int iBlockSize, int iPreallocate ) : - sStore( sStore ), - iBlockSize( iBlockSize ), - iBlocks( 0 ), - iUsed( 0 ), - bHeaderChanged( false ) -{ - try - { - initialize(); - } - catch( Bu::MyriadException &e ) - { - if( e.getErrorCode() == MyriadException::emptyStream ) - { - initialize( iBlockSize, iPreallocate ); - } - else - { - throw; - } - } -} - -Bu::Myriad::~Myriad() -{ - if( !hActiveBlocks.isEmpty() ) - { - sio << "Bu::Myriad::~Myriad(): Error: There are " - << hActiveBlocks.getSize() << " unsynced blocks!" << sio.nl; - } - sync(); - - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - delete *i; - } -} - -void Bu::Myriad::sync() -{ - updateHeader(); - - for( BlockHash::iterator i = hActiveBlocks.begin(); i; i++ ) - { - if( (*i)->bChanged ) - { - syncBlock( *i ); - } - } -} - -void Bu::Myriad::initialize() -{ - sStore.setPosEnd( 0 ); - int iSize = sStore.tell(); - sStore.setPos( 0 ); - - unsigned char buf[4]; - if( sStore.read( buf, 4 ) < 4 ) - throw MyriadException( MyriadException::emptyStream, - "Input stream appears to be empty."); - if( memcmp( buf, Myriad_MAGIC_CODE, 4 ) ) - { - throw MyriadException( MyriadException::invalidFormat, - "Stream does not appear to be a valid Myriad format."); - } - sStore.read( buf, 2 ); - if( buf[0] != 1 ) - throw MyriadException( MyriadException::badVersion, - "We can only handle version 1 for now."); - if( buf[1] != 32 ) - throw MyriadException( MyriadException::invalidWordSize, - "We can only handle 32-bit words at the moment."); - sStore.read( &iBlockSize, 4 ); - int iStreams; - sStore.read( &iStreams, 4 ); - - iBlocks = iSize/iBlockSize; - //sio << "Myriad: iSize=" << iSize << ", iBlockSize=" << iBlockSize - // << ", iBlocks=" << iBlocks << ", iStreams=" << iStreams << sio.nl; - - int iHeaderSize = 14 + 8 + 4; - int iHeaderBlocks = 0; //blkDiv( iHeaderSize+4, iBlockSize ); - - while( iHeaderSize > iHeaderBlocks*iBlockSize ) - { - iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize ); - iHeaderSize = 14 + 8 + 4*iHeaderBlocks; - } - - //sio << "Myriad: iHeaderSize=" << iHeaderSize - // << ", iHeaderBlocks=" << iHeaderBlocks << sio.nl; - - Stream *pFakeHdr = new Stream; - pFakeHdr->iId = 0; - pFakeHdr->iSize = iHeaderSize; - for( int j = 0; j < iHeaderBlocks; j++ ) - { - pFakeHdr->aBlocks.append( j ); - } - - bsBlockUsed.setSize( iBlocks, true ); - -// bool bCanSkip = false; // Can skip around, post initial header stream i/o - MyriadStream *pIn = new MyriadStream( *this, pFakeHdr ); - pIn->setPos( sStore.tell() ); - for( int j = 0; j < iStreams; j++ ) - { - aStreams.append( new Stream() ); - Stream &s = *aStreams[j]; - pIn->read( &s.iId, 4 ); - pIn->read( &s.iSize, 4 ); - int iSBlocks = blkDiv(s.iSize, iBlockSize); - // sio << "Myriad: - Stream::iId=" << s.iId - // << ", Stream::iSize=" << s.iSize - // << ", Stream::aBlocks=" << iSBlocks - // << ", pIn->tell()=" << pIn->tell() << sio.nl; - for( int k = 0; k < iSBlocks; k++ ) - { - int iBId; - pIn->read( &iBId, 4 ); - // sio << "Myriad: - iBId=" << iBId - // << ", iStartPos=" << iBId*iBlockSize - // << ", pIn->tell()=" << pIn->tell() << sio.nl; - s.aBlocks.append( iBId ); - bsBlockUsed.setBit( iBId ); - iUsed++; - if( (j == 0 && k == iHeaderBlocks-1) ) - { - // sio << "Myriad: - End of prepartition, unlocking skipping." - // << sio.nl; -// bCanSkip = true; - MyriadStream *pTmp = new MyriadStream( *this, aStreams[0] ); - // sio << "Myriad - Position = " << pIn->tell() << sio.nl; - pTmp->setPos( pIn->tell() ); - delete pIn; - delete pFakeHdr; - pIn = pTmp; - } - } - } - delete pIn; - - //sio << "Myriad: Blocks used: " << bsBlockUsed.toString() << sio.nl; -} - -void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate ) -{ - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - delete *i; - } - aStreams.clear(); - iUsed = 0; - - int iHeaderSize = 14 + 8 + 4; - int iHeaderBlocks = 0; //blkDiv( iHeaderSize+4, iBlockSize ); - char cBuf = 1; - int iBuf = 0; - - Stream *pStr = new Stream; - pStr->iId = 0; - - while( iHeaderSize > iHeaderBlocks*iBlockSize ) - { - iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize ); - iHeaderSize = 14 + 8 + 4*iHeaderBlocks; - } - - iPreAllocate += iHeaderBlocks; - - //sio << "Myriad: iHeaderSize=" << iHeaderSize << ", iBlockSize=" - // << iBlockSize << ", iHeaderBlocks=" << iHeaderBlocks << sio.nl; - - bsBlockUsed.setSize( iPreAllocate, true ); - iUsed++; - - char *pBlock = new char[iBlockSize]; - memset( pBlock, 0, iBlockSize ); - for( int j = 0; j < iPreAllocate; j++ ) - { - sStore.write( pBlock, iBlockSize ); - } - delete[] (char *)pBlock; - - sStore.setPos( 0 ); - - // Magic number - sStore.write( Myriad_MAGIC_CODE, 4 ); - - // Version (0) - sStore.write( &cBuf, 1 ); - - // Bits per int - cBuf = 32; - sStore.write( &cBuf, 1 ); - - // The size of each block - sStore.write( &iBlockSize, 4 ); - - iBuf = 1; - // The number of streams - sStore.write( &iBuf, 4 ); - - // Stream header - iBuf = 0; - sStore.write( &iBuf, 4 ); - sStore.write( &iHeaderSize, 4 ); - for( iBuf = 0; iBuf < iHeaderBlocks; iBuf++ ) - { - sStore.write( &iBuf, 4 ); - } - - this->iBlockSize = iBlockSize; - this->iBlocks = iPreAllocate; - - pStr->iSize = sStore.tell(); -// sio << "Myriad: Actual end of header stream = " << pStr->iSize << sio.nl; - - pStr->iSize = iHeaderSize; - for( int j = 0; j < iHeaderBlocks; j++ ) - { - pStr->aBlocks.append( j ); - bsBlockUsed.setBit( j ); - iUsed++; - } - - aStreams.append( pStr ); - - //sio << bsBlockUsed.toString() << " - " << pStr->aBlocks << sio.nl; - - bHeaderChanged = true; - //hStreams.insert( 0, BlockArray( 0 ) ); -} - -void Bu::Myriad::updateHeader() -{ - if( bHeaderChanged == false ) - return; - if( !sStore.canWrite() ) - return; - - char cBuf; - int iBuf; - - //for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - //{ - // sio << "Myriad: Stream " << Fmt(4) << (*i)->iId << ": " << (*i)->aBlocks << sio.nl; - //} - - // Compute the new size of the header. - int iHeaderSize = 14 + 8*aStreams.getSize(); -// sio << "Myriad: updateHeader: aStreams.getSize() = " << aStreams.getSize() -// << sio.nl; - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - iHeaderSize += 4*(*i)->aBlocks.getSize(); -// sio << "Myriad: updateHeader: (*i)->aBlocks.getSize() = " -// << (*i)->aBlocks.getSize() << sio.nl; - } - int iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); - while( iNewBlocks > aStreams[0]->aBlocks.getSize() ) - { - int iBlock = findEmptyBlock(); -// sio << "Myriad: updateHeader: Appending block " << iBlock -// << " to header." << sio.nl; - aStreams[0]->aBlocks.append( iBlock ); - bsBlockUsed.setBit( iBlock ); - iUsed++; - iHeaderSize += 4; - iNewBlocks = blkDiv( iHeaderSize, iBlockSize ); - } - aStreams[0]->iSize = iHeaderSize; -// sio << "Myriad: updateHeader: iHeaderSize=" << iHeaderSize -// << ", iNewBlocks=" << iNewBlocks << ", curBlocks=" -// << aStreams[0]->aBlocks.getSize() << sio.nl; - - MyriadStream sHdr( *this, aStreams[0] ); - sHdr.write( Myriad_MAGIC_CODE, 4 ); - - // Version (1) - cBuf = 1; - sHdr.write( &cBuf, 1 ); - - // Bits per int - cBuf = 32; - sHdr.write( &cBuf, 1 ); - - // The size of each block - sHdr.write( &iBlockSize, 4 ); - - iBuf = aStreams.getSize(); - // The number of streams - sHdr.write( &iBuf, 4 ); - - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - sHdr.write( &(*i)->iId, 4 ); - sHdr.write( &(*i)->iSize, 4 ); - int iUsedBlocks = blkDiv( (*i)->iSize, iBlockSize ); -// for( BlockArray::iterator j = (*i)->aBlocks.begin(); j; j++ ) - for( int j = 0; j < iUsedBlocks; j++ ) - { - sHdr.write( &(*i)->aBlocks[j], 4 ); - } - } - - bHeaderChanged = false; -} - -int Bu::Myriad::createStream( int iPreAllocate ) -{ - Stream *pStr = new Stream(); - pStr->iId = aStreams.last()->iId+1; - //sio << "Myriad: New stream id=" << pStr->iId << ", iPreAllocate=" - // << iPreAllocate << sio.nl; - pStr->iSize = 0; - aStreams.append( pStr ); - - for( int j = 0; j < iPreAllocate; j++ ) - { - int iFreeBlock = findEmptyBlock(); -// sio << "Myriad: Adding block " << iFreeBlock << sio.nl; - pStr->aBlocks.append( iFreeBlock ); - bsBlockUsed.setBit( iFreeBlock ); - iUsed++; - } - - bHeaderChanged = true; - - return pStr->iId; -} - -int Bu::Myriad::createStreamWithId( int iId, int iPreAllocate ) -{ - try - { - findStream( iId ); - throw MyriadException( MyriadException::streamExists, - "There is already a stream with the given id."); - } - catch( MyriadException &e ) - { - Stream *pStr = new Stream(); - pStr->iId = iId; - //sio << "Myriad: New stream id=" << pStr->iId << ", iPreAllocate=" - // << iPreAllocate << sio.nl; - pStr->iSize = 0; - if( aStreams.last()->iId < iId ) - { - aStreams.append( pStr ); - } - else - { - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - if( (*i)->iId > iId ) - { - aStreams.insert( i, pStr ); - break; - } - } - } - - for( int j = 0; j < iPreAllocate; j++ ) - { - int iFreeBlock = findEmptyBlock(); - // sio << "Myriad: Adding block " << iFreeBlock << sio.nl; - pStr->aBlocks.append( iFreeBlock ); - bsBlockUsed.setBit( iFreeBlock ); - iUsed++; - } - - bHeaderChanged = true; - - return pStr->iId; - } -} - -int Bu::Myriad::findEmptyBlock() -{ - bHeaderChanged = true; - - for( int j = 0; j < bsBlockUsed.getSize(); j++ ) - { - if( bsBlockUsed.getBit( j ) == false ) - return j; - } -// sio << "Myriad: findEmptyBlock(): No empty blocks, adding new one." << sio.nl; - - bsBlockUsed.setSize( bsBlockUsed.getSize()+1, false ); - /* - sStore.setPos( iBlockSize*iBlocks ); - - char *pBlock = new char[iBlockSize]; - memset( pBlock, 0, iBlockSize ); - sStore.write( pBlock, iBlockSize ); - delete[] pBlock; - */ - - sStore.setSize( (iBlocks+1)*iBlockSize ); - - return iBlocks++; -} - -void Bu::Myriad::deleteStream( int iId ) -{ - if( iId < 0 ) - throw MyriadException( MyriadException::invalidStreamId, - "Invalid stream id."); - if( iId == 0 ) - throw MyriadException( MyriadException::protectedStream, - "You cannot delete stream zero, it is protected."); - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - if( (*i)->iId == iId ) - { - Stream *pStream = *i; - for( BlockArray::iterator j = pStream->aBlocks.begin(); j; j++ ) - { - bsBlockUsed.setBit( *j, false ); - iUsed--; - } - aStreams.erase( i ); - bHeaderChanged = true; - delete pStream; - return; - } - } -} - -Bu::Array Bu::Myriad::getStreamIds() -{ - Bu::Array aRet( aStreams.getSize() ); - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - aRet.append( (*i)->iId ); - } - - return aRet; -} - -int Bu::Myriad::getStreamSize( int iId ) -{ - return findStream( iId )->iSize; -} - -bool Bu::Myriad::hasStream( int iId ) -{ - try - { - findStream( iId ); - return true; - }catch(...) - { - return false; - } -} - -Bu::MyriadStream Bu::Myriad::openStream( int iId ) -{ - //sio << "Myriad: Request to open stream: " << iId << sio.nl; - return MyriadStream( *this, findStream( iId ) ); -} - -int Bu::Myriad::getNumStreams() -{ - return aStreams.getSize(); -} - -int Bu::Myriad::getBlockSize() -{ - return iBlockSize; -} - -int Bu::Myriad::getNumBlocks() -{ - return iBlocks; -} - -int Bu::Myriad::getNumUsedBlocks() -{ - return iUsed; -} - -int Bu::Myriad::getTotalUsedBytes() -{ - int iTotalSize = 0; - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - iTotalSize += (*i)->iSize; - } - return iTotalSize; -} - -int Bu::Myriad::getTotalUnusedBytes() -{ - int iTotalSize = (iBlocks-iUsed)*iBlockSize; - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - iTotalSize += iBlockSize - ((*i)->iSize%iBlockSize); - } - return iTotalSize; -} - -int Bu::Myriad::getTotalUnusedBytes( int iFakeBlockSize ) -{ - int iTotalSize = (iBlocks-iUsed)*iFakeBlockSize; - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - iTotalSize += iFakeBlockSize - ((*i)->iSize%iFakeBlockSize); - } - return iTotalSize; -} - -Bu::Myriad::Stream *Bu::Myriad::findStream( int iId ) -{ - for( StreamArray::iterator i = aStreams.begin(); i; i++ ) - { - if( (*i)->iId == iId ) - return *i; - } - - throw MyriadException( MyriadException::noSuchStream, - "The requested stream doesn't exist and cannot be opened." ); - - return NULL; -} - -Bu::Myriad::Block *Bu::Myriad::getBlock( int iBlock ) -{ -// sio << "Myriad: Reading block " << iBlock << ", bytes " -// << iBlockSize*iBlock << "-" << iBlockSize*(iBlock+1) << sio.nl; - Block *pBlock = new Block; - pBlock->pData = new char[iBlockSize]; - sStore.setPos( iBlockSize * iBlock ); - sStore.read( pBlock->pData, iBlockSize ); - pBlock->bChanged = false; - pBlock->iBlockIndex = iBlock; - - hActiveBlocks.insert( iBlock, pBlock ); - - return pBlock; -} - -void Bu::Myriad::releaseBlock( Bu::Myriad::Block *pBlock ) -{ - if( pBlock == NULL ) - return; -// sio << "Myriad: Releasing block " << pBlock->iBlockIndex << sio.nl; - syncBlock( pBlock ); - hActiveBlocks.erase( pBlock->iBlockIndex ); - delete[] pBlock->pData; - delete pBlock; -} - -void Bu::Myriad::syncBlock( Block *pBlock ) -{ - if( pBlock->bChanged ) - { -// sio << "Myriad: - Block changed, writing back to stream." << sio.nl; - sStore.setPos( iBlockSize * pBlock->iBlockIndex ); - sStore.write( pBlock->pData, iBlockSize ); - pBlock->bChanged = false; - } -} - -int Bu::Myriad::streamAddBlock( Stream *pStream ) -{ - int iBlock = findEmptyBlock(); - pStream->aBlocks.append( iBlock ); - bsBlockUsed.setBit( iBlock ); - bHeaderChanged = true; - iUsed++; - return iBlock; -} - -void Bu::Myriad::setStreamSize( Stream *pStream, long iSize ) -{ - if( pStream->iSize == iSize ) - { - return; - } - else if( pStream->iSize > iSize ) - { - // Shrink - for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize; - iNewSize-iBlockSize > iSize; iNewSize -= iBlockSize ) - { - if( bsBlockUsed.getBit( pStream->aBlocks.last() ) ) - iUsed--; - bsBlockUsed.setBit( pStream->aBlocks.last(), false ); - pStream->aBlocks.eraseLast(); - } - pStream->iSize = iSize; - bHeaderChanged = true; - } - else - { - // Grow - for( int iNewSize = pStream->aBlocks.getSize()*iBlockSize; - iNewSize < iSize; iNewSize += iBlockSize ) - { - streamAddBlock( pStream ); - } - pStream->iSize = iSize; - bHeaderChanged = true; - } -} - -void Bu::Myriad::headerChanged() -{ - bHeaderChanged = true; -} - -bool Bu::Myriad::isMyriad( Bu::Stream &sStore ) -{ - sStore.setPos( 0 ); - - unsigned char buf[4]; - if( sStore.read( buf, 4 ) < 4 ) - throw MyriadException( MyriadException::emptyStream, - "Input stream appears to be empty."); - sStore.setPos( 0 ); - if( memcmp( buf, Myriad_MAGIC_CODE, 4 ) ) - { - return false; - } - return true; -} - -const Bu::BitString &Bu::Myriad::getBlocksUsed() const -{ - return bsBlockUsed; -} - diff --git a/src/myriad.h b/src/myriad.h deleted file mode 100644 index 3382ab5..0000000 --- a/src/myriad.h +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MYRIAD_H -#define BU_MYRIAD_H - -#include -#include "bu/bitstring.h" -#include "bu/exceptionbase.h" -#include "bu/array.h" -#include "bu/hash.h" - -namespace Bu -{ - class Stream; - class MyriadStream; - - subExceptionDeclBegin( MyriadException ) - enum - { - emptyStream, - invalidFormat, - badVersion, - invalidWordSize, - noSuchStream, - streamExists, - invalidStreamId, - protectedStream - }; - subExceptionDeclEnd(); - - /** - * Myriad block-allocated stream multiplexing system. This is a system for - * creating streams that contain other streams in a flexible and lightweight - * manner. Basically, you can create a file (or any other stream) that can - * store any number of flexible, growing streams. The streams within the - * Myriad stream are automatically numbered, not named. This works more - * or less like a filesystem, but without the extra layer for managing - * file and directory links. This would actually be very easy to add - * on top of Myriad, but is not required. - * - * Header format is as follows: - * - * MMMMvBssssSSSS* - * M = Magic number (0AD3FA84) - * v = version number - * B = Bits per int - * s = Blocksize in bytes - * S = Number of Streams - * - * The * represents the Stream headers, one per stream, as follows: - * IIIIssss$ - * I = Id number of the stream - * s = size of stream in bytes - * - * The $ represents the Block headers, one per used block, as follows: - * IIII - * I = Index of the block - * - * The stream/block data is interleaved in the header, so all blocks stored - * with one stream are together. The block headers are in order, and the - * data in them is required to be "solid" you cannot fill partial blocks - * mid-way through a stream. - * - * The initial block starts with the nids header, and is both the zero block - * and the zero stream. For now, the minimum block size is the size needed - * to store the base header, the zero stream header, and the first two - * blocks of the zero stream, so 30 bytes. Since it's reccomended to use - * a size that will fit evenly into filesystem blocks, then a size of 32 is - * probably the smallest reccomended size because all powers of two equal - * to or greater than 32 are evenly divisible by 32. - * - * I have had a thought that if the block size were smaller than 42 bytes - * the header would consume the first N blocks where N * block size is - * enough space to house the initial header, the first stream header, and - * the first N block headers. This, of course, causes you to hit an - * infinite header if the block size is small enough. - */ - class Myriad - { - friend class MyriadStream; - public: - /** - * Create a Myriad object that uses the given stream to store data. - * This stream must be random access. The block size and preallocate - * values passed in are values that will be used if the given stream - * is empty. In that case the stream will be "formatted" for myriad - * with the specified block size. If there is already a viable Myriad - * format present in the stream, then the blocksize and preallocate - * values will be ignored and the values from the stream will be used - * instead. If the stream doesn't appear to be Myriad formatted an - * exception will be thrown. - */ - Myriad( Bu::Stream &sStore, int iBlockSize=512, int iPreallocate=8 ); - virtual ~Myriad(); - - /** - * Destroy whatever data may be in the base stream and create a new - * Myriad system there with the given blocksize. Use this with care, - * it will destroy anything that was already in the stream, and - * generally, should not ever have to be used. - */ - void initialize( int iBlockSize, int iPreAllocate=1 ); - - /** - * Create a new stream within the Myriad system. The ID of the new - * stream is returned. - */ - int createStream( int iPreAllocate=1 ); - - /** - * Create a new stream within the Myriad system with a given id. The - * id that you provide will be the new id of the stream unless it's - * already used, in which case an error is thrown. This is primarilly - * useful when copying an old Myriad file into a new one. - */ - int createStreamWithId( int iId, int iPreAllocate=1 ); - - /** - * Delete a stream that's already within the Myriad. - */ - void deleteStream( int iId ); - - /** - * Return a new Stream object assosiated with the given stream ID. - */ - MyriadStream openStream( int iId ); - - Bu::Array getStreamIds(); - int getStreamSize( int iId ); - bool hasStream( int iId ); - - int getNumStreams(); - int getBlockSize(); - int getNumBlocks(); - int getNumUsedBlocks(); - int getTotalUsedBytes(); - int getTotalUnusedBytes(); - int getTotalUnusedBytes( int iFakeBlockSize ); - - /** - * Syncronize the header data, etc. with the storage stream. It's not - * a bad idea to call this periodically. - */ - void sync(); - - /** - * Read the first few bytes from the given stream and return true/false - * depending on weather or not it's a Myriad stream. This will throw - * an exception if the stream is empty, or is not random access. - */ - static bool isMyriad( Bu::Stream &sStore ); - - const Bu::BitString &getBlocksUsed() const; - - private: - /** - * Initialize this object based on the data already in the assosiated - * stream. This will be called automatically for you if you forget, - * but if you want to pre-initialize for some reason, just call this - * once before you actually start doing anything with your Myriad. - */ - void initialize(); - - enum - { - blockUnused = 0xFFFFFFFFUL - }; - - typedef Bu::Array BlockArray; - class Stream - { - public: - int iId; - int iSize; - BlockArray aBlocks; - }; - typedef Bu::Array StreamArray; - - class Block - { - public: - char *pData; - bool bChanged; - int iBlockIndex; - }; - - void updateHeader(); - int findEmptyBlock(); - - /** - *@todo Change this to use a binary search, it's nicer. - */ - Stream *findStream( int iId ); - - Block *getBlock( int iBlock ); - void releaseBlock( Block *pBlock ); - void syncBlock( Block *pBlock ); - - int streamAddBlock( Stream *pStream ); - void setStreamSize( Stream *pStream, long iSize ); - - void headerChanged(); - - private: - Bu::Stream &sStore; - int iBlockSize; - int iBlocks; - int iUsed; - Bu::BitString bsBlockUsed; - StreamArray aStreams; - typedef Bu::Hash BlockHash; - BlockHash hActiveBlocks; - bool bHeaderChanged; - }; -}; - -#endif diff --git a/src/myriadfs.cpp b/src/myriadfs.cpp deleted file mode 100644 index 6884a31..0000000 --- a/src/myriadfs.cpp +++ /dev/null @@ -1,703 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/myriadfs.h" -#include "bu/myriadstream.h" - -#include -#include -#include - -#include "bu/sio.h" -using Bu::sio; -using Bu::Fmt; - -namespace Bu { subExceptionDef( MyriadFsException ) } - -#define Myriad_Fs_MAGIC_CODE ((char *)"\xa7\x18\x8b\x39") - -Bu::MyriadFs::MyriadFs( Bu::Stream &rStore, int iBlockSize ) : - rStore( rStore ), - mStore( rStore, iBlockSize ), - iUser( 0 ), - iGroup( 0 ) -{ -#ifndef WIN32 - iUser = getuid(); - iGroup = getgid(); -#endif - - if( mStore.hasStream( 1 ) ) - { - // Check to see if this is a MyriadFs stream. - Bu::MyriadStream ms = mStore.openStream( 1 ); - char sMagic[4]; - if( ms.read( sMagic, 4 ) < 4 ) - throw MyriadFsException("The provided stream does not appear to be " - "a MyriadFs stream."); - if( ::strncmp( sMagic, Myriad_Fs_MAGIC_CODE, 4 ) ) - throw MyriadFsException("The provided stream does not appear to be " - "a MyriadFs stream."); - - int8_t iVer; - ms.read( &iVer, 1 ); - - int32_t iNumNodes; - ms.read( &iNumNodes, 4 ); - for( int32_t j = 0; j < iNumNodes; j++ ) - { - int32_t iNode; - int32_t iPos; - ms.read( &iNode, 4 ); - ms.read( &iPos, 4 ); - hNodeIndex.insert( iNode, iPos ); - } - } - else - { - // Create initial header stream - { - mStore.createStream( 1 ); - Bu::MyriadStream ms = mStore.openStream( 1 ); - ms.write( Myriad_Fs_MAGIC_CODE, 4 ); - int8_t iVer = 1; - int32_t iTmp = 1; - ms.write( &iVer, 1 ); - ms.write( &iTmp, 4 ); // iNumNodes - iTmp = 0; - ms.write( &iTmp, 4 ); // iInode - ms.write( &iTmp, 4 ); // iPosition - hNodeIndex.insert( 0, 0 ); - } - - // Create initial inode stream, with one root node. - { - mStore.createStream( 2 ); - Bu::MyriadStream ms = mStore.openStream( 2 ); - RawStat rs; - rs.iNode = 0; - rs.iUser = iUser; - rs.iGroup = iGroup; - rs.uPerms = 0755|typeDir; - rs.iLinks = 1; - rs.uStreamIndex = 3; - rs.iCTime = rs.iMTime = rs.iATime = time(NULL); - ms.write( &rs, sizeof(RawStat) ); - } - - // Create inode 0's storage stream. - { - mStore.createStream( 3 ); - Bu::MyriadStream ms = mStore.openStream( 3 ); - int32_t iTmp32 = 0; - ms.write( &iTmp32, 4 ); // iChildCount - } - } -} - -Bu::MyriadFs::~MyriadFs() -{ - writeHeader(); -} - -void Bu::MyriadFs::stat( const Bu::String &sPath, Bu::MyriadFs::Stat &rBuf ) -{ - int32_t iParent; - int32_t iNode = lookupInode( sPath, iParent ); - Bu::MyriadStream is = mStore.openStream( 2 ); - stat( iNode, rBuf, is ); -} - -Bu::MyriadStream Bu::MyriadFs::open( const Bu::String &sPath, int /*iMode*/, - uint16_t uPerms ) -{ - int32_t iParent = -1; - int32_t iNode; - try - { - iNode = lookupInode( sPath, iParent ); - sio << "File found." << sio.nl; - // The file was found - return openByInode( iNode ); - } - catch( Bu::MyriadFsException &e ) - { - if( iParent < 0 ) - throw; - - // This means that an intermediate path component couldn't be found - if( e.getErrorCode() == 1 ) - throw; - - // The file wasn't found, but the path leading up to it was. - // first, figure out the final path element... - Bu::String sName = filePart( sPath ); - sio << "End filename: " << sName << sio.nl; - sio << "Parent inode: " << iParent << sio.nl; - iNode = create( iParent, sName, (uPerms&permMask)|typeRegFile, 0 ); - sio << "New iNode: " << iNode << sio.nl; - return openByInode( iNode ); - } -} - -void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms ) -{ - create( sPath, iPerms, 0 ); -} - -void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms, - uint16_t iDevHi, uint16_t iDevLo ) -{ - create( sPath, iPerms, ((uint32_t)iDevHi<<16)|(uint32_t)iDevLo ); -} - -void Bu::MyriadFs::create( const Bu::String &sPath, uint16_t iPerms, - uint32_t uSpecial ) -{ - int32_t iParent = -1; - int32_t iNode; - try - { - iNode = lookupInode( sPath, iParent ); - sio << "File found." << sio.nl; - // The file was found - throw Bu::MyriadFsException("Path already exists."); - } - catch( Bu::MyriadFsException &e ) - { - if( iParent < 0 ) - throw; - - // This means that an intermediate path component couldn't be found - if( e.getErrorCode() == 1 ) - throw; - - // The file wasn't found, but the path leading up to it was. - // first, figure out the final path element... - Bu::String sName = filePart( sPath ); - sio << "End filename: " << sName << sio.nl; - sio << "Parent inode: " << iParent << sio.nl; - iNode = create( iParent, sName, iPerms, uSpecial ); - sio << "New iNode: " << iNode << sio.nl; - } -} - -void Bu::MyriadFs::mkDir( const Bu::String &sPath, uint16_t iPerms ) -{ - create( sPath, (iPerms&permMask)|typeDir, 0 ); -} - -void Bu::MyriadFs::mkSymLink( const Bu::String &sTarget, - const Bu::String &sPath ) -{ - int32_t iParent = -1; - int32_t iNode; - try - { - iNode = lookupInode( sPath, iParent ); - throw Bu::MyriadFsException("Path already exists."); - } - catch( Bu::MyriadFsException &e ) - { - if( iParent < 0 ) - throw; - - // This means that an intermediate path component couldn't be found - if( e.getErrorCode() == 1 ) - throw; - - // The file wasn't found, but the path leading up to it was. - // first, figure out the final path element... - Bu::String sName = filePart( sPath ); - sio << "End filename: " << sName << sio.nl; - sio << "Parent inode: " << iParent << sio.nl; - iNode = create( iParent, sName, 0777|typeSymLink, 0 ); - sio << "New iNode: " << iNode << sio.nl; - MyriadStream ms = openByInode( iNode ); - ms.write( sTarget ); - } -} - -void Bu::MyriadFs::mkHardLink( const Bu::String &sTarget, - const Bu::String &sPath ) -{ - int32_t iParent = -1; - int32_t iNode; - - iNode = lookupInode( sTarget, iParent ); - - try - { - lookupInode( sPath, iParent ); - throw Bu::MyriadFsException("Path already exists."); - } - catch( Bu::MyriadFsException &e ) - { - if( iParent < 0 ) - throw; - - // This means that an intermediate path component couldn't be found - if( e.getErrorCode() == 1 ) - throw; - - // The file wasn't found, but the path leading up to it was. - // first, figure out the final path element... - Bu::String sName = filePart( sPath ); - sio << "End filename: " << sName << sio.nl; - sio << "Parent inode: " << iParent << sio.nl; - addToDir( iParent, iNode, sName ); - MyriadStream is = mStore.openStream( 2 ); - RawStat rs; - readInode( iNode, rs, is ); - rs.iLinks++; - writeInode( rs, is ); - } -} - -Bu::String Bu::MyriadFs::readSymLink( const Bu::String &sPath ) -{ - int32_t iParent = -1; - int32_t iNode; - iNode = lookupInode( sPath, iParent ); - MyriadStream ms = openByInode( iNode ); - Bu::String sRet; - sRet.setSize( ms.getSize() ); - ms.read( sRet.getStr(), ms.getSize() ); - return sRet; -} - -Bu::MyriadFs::Dir Bu::MyriadFs::readDir( const Bu::String &sPath ) -{ - int32_t iParent = -1; - int32_t iNode = lookupInode( sPath, iParent ); - return readDir( iNode ); -} - -void Bu::MyriadFs::setTimes( const Bu::String &sPath, int64_t iATime, - int64_t iMTime ) -{ - int32_t iParent = -1; - int32_t iNode; - - iNode = lookupInode( sPath, iParent ); - - setTimes( iNode, iATime, iMTime ); -} - -void Bu::MyriadFs::unlink( const Bu::String &sPath ) -{ - int32_t iParent = -1; -// int32_t iNode; - - /*iNode =*/ lookupInode( sPath, iParent ); - - Dir lDir = readDir( iParent ); - - Bu::String sName = filePart( sPath ); - - for( Dir::iterator i = lDir.begin(); i; i++ ) - { - if( sName == (*i).sName ) - { - RawStat rs; - readInode( (*i).iNode, rs ); - if( (rs.uPerms&typeMask) == typeDir ) - { - MyriadStream msDir = mStore.openStream( rs.uStreamIndex ); - int32_t iCount; - msDir.read( &iCount, 4 ); - if( iCount > 0 ) - { - throw Bu::MyriadFsException("Directory not empty."); - } - } - if( --rs.iLinks == 0 ) - { - destroyNode( (*i).iNode ); - } - else - { - writeInode( rs ); - } - lDir.erase( i ); - break; - } - } - - Bu::MyriadStream ms = openByInode( iParent ); - int32_t iNumChildren = lDir.getSize(); - ms.write( &iNumChildren, 4 ); - for( Dir::iterator i = lDir.begin(); i; i++ ) - { - ms.write( &(*i).iNode, 4 ); - uint8_t iSize = (*i).sName.getSize(); - ms.write( &iSize, 1 ); - ms.write( (*i).sName.getStr(), iSize ); - } - ms.setSize( ms.tell() ); -} - -void Bu::MyriadFs::setFileSize( const Bu::String &sPath, int32_t iSize ) -{ - int32_t iParent = -1; - int32_t iNode; - iNode = lookupInode( sPath, iParent ); - MyriadStream ms = openByInode( iNode ); - ms.setSize( iSize ); -} - -void Bu::MyriadFs::rename( const Bu::String &sFrom, const Bu::String &sTo ) -{ - mkHardLink( sFrom, sTo ); - unlink( sFrom ); -} - -dev_t Bu::MyriadFs::devToSys( uint32_t uDev ) -{ - return (((uDev&0xFFFF0000)>>8)&0xFF00) | ((uDev&0xFF)); -} - -uint32_t Bu::MyriadFs::sysToDev( dev_t uDev ) -{ - return (((uint32_t)uDev&0xFF00)<<8) | ((uint32_t)uDev&0xFF); -} - -int32_t Bu::MyriadFs::lookupInode( const Bu::String &sPath, int32_t &iParent ) -{ - if( sPath == "/" ) - { - return 0; - } - if( sPath[0] == '/' ) - { - // Absolute lookup - return lookupInode( sPath.begin()+1, 0, iParent ); - } - else - { - // Relative lookup - throw Bu::ExceptionBase( - "Relative lookups in MyriadFs are not working yet."); - } -} - -int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart, - int32_t iNode, int32_t &iParent ) -{ - iParent = iNode; - - Bu::String::const_iterator iEnd = iStart.find('/'); - Bu::String sTok( iStart, iEnd ); - -// sio << "Direcotry component: " << sTok << sio.nl; - - Dir lDir = readDir( iNode ); - - for( Dir::iterator i = lDir.begin(); i; i++ ) - { - if( (*i).sName == sTok ) - { - // We found an item - if( !iEnd ) - { - // It's the last one in the requested path, return it - return (*i).iNode; - } - else - { - // Not the last one in our path, double check it's a dir - if( ((*i).uPerms&typeMask) == typeDir ) - { - return lookupInode( iEnd+1, (*i).iNode, iParent ); - } - else - { - iParent = -1; - throw Bu::MyriadFsException( - "Element '%s' in given path is not a directory.", - sTok.getStr() ); - } - } - } - } - - if( iEnd ) - throw Bu::MyriadFsException( 1, "Path not found"); - else - throw Bu::MyriadFsException( 2, "Path not found"); -} - -void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ) -{ - rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) ); - if( rIs.read( &rs, sizeof(RawStat) ) < sizeof(RawStat) ) - throw Bu::MyriadFsException("Filesystem corruption detected."); - if( rs.iNode != iNode ) - throw Bu::MyriadFsException("Filesystem corruption detected."); -} - -void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs ) -{ - MyriadStream ms = mStore.openStream( 2 ); - readInode( iNode, rs, ms ); -} - -void Bu::MyriadFs::writeInode( const RawStat &rs, - MyriadStream &rOs ) -{ - rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) ); - rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) ); - if( rOs.write( &rs, sizeof(RawStat) ) < sizeof(RawStat) ) - throw Bu::MyriadFsException("Error writing inode to header stream."); -} - -void Bu::MyriadFs::writeInode( const RawStat &rs ) -{ - MyriadStream ms = mStore.openStream( 2 ); - writeInode( rs, ms ); -} - -Bu::MyriadFs::Dir Bu::MyriadFs::readDir( int32_t iNode ) -{ - Bu::MyriadStream ms = openByInode( iNode ); - int32_t iNumChildren = 0; - ms.read( &iNumChildren, 4 ); - - Bu::MyriadStream is = mStore.openStream( 2 ); - Dir lDir; -// sio << "Reading dir, " << iNumChildren << " entries:" << sio.nl; - for( int32_t j = 0; j < iNumChildren; j++ ) - { - int32_t iChildNode; - ms.read( &iChildNode, 4 ); - Stat s; - stat( iChildNode, s, is ); - uint8_t uLen; - ms.read( &uLen, 1 ); - s.sName.setSize( uLen ); - ms.read( s.sName.getStr(), uLen ); - lDir.append( s ); - -// sio << " " << s.sName << sio.nl; - } - - return lDir; -} - -Bu::MyriadStream Bu::MyriadFs::openByInode( int32_t iNode ) -{ - RawStat rs; - readInode( iNode, rs ); - switch( (rs.uPerms&typeMask) ) - { - case typeDir: - case typeSymLink: - case typeRegFile: - return mStore.openStream( rs.uStreamIndex ); - - default: - throw Bu::MyriadFsException( - "inode incorrect type for low-level openByInode."); - } -} - -void Bu::MyriadFs::addToDir( int32_t iDir, int32_t iNode, - const Bu::String &sName ) -{ - if( sName.getSize() > 255 ) - { - throw Bu::MyriadFsException("Filename too long, max is 255 bytes."); - } - Bu::MyriadStream ms = openByInode( iDir ); - int32_t iNumChildren = 0; - ms.read( &iNumChildren, 4 ); - iNumChildren++; - ms.setPos( 0 ); - ms.write( &iNumChildren, 4 ); - ms.setPosEnd( 0 ); - ms.write( &iNode, 4 ); - uint8_t uLen = sName.getSize(); - ms.write( &uLen, 1 ); - ms.write( sName.getStr(), uLen ); -} - -int32_t Bu::MyriadFs::create( int32_t iParent, const Bu::String &sName, - uint16_t uPerms, uint32_t uSpecial ) -{ - int32_t iNode = allocInode( uPerms, uSpecial ); - addToDir( iParent, iNode, sName ); - return iNode; -} - -int32_t Bu::MyriadFs::allocInode( uint16_t uPerms, uint32_t uSpecial ) -{ - int32_t iNode = 0; - for(; iNode < 0xfffffff; iNode++ ) - { - if( !hNodeIndex.has( iNode ) ) - { - hNodeIndex.insert( iNode, hNodeIndex.getSize() ); - RawStat rs; - rs.iNode = iNode; - rs.iUser = iUser; - rs.iGroup = iGroup; - rs.uPerms = uPerms; - rs.iLinks = 1; - switch( (uPerms&typeMask) ) - { - case typeRegFile: - case typeSymLink: - rs.uStreamIndex = mStore.createStream(); - break; - - case typeDir: - rs.uStreamIndex = mStore.createStream(); - sio << "Creating directory node, storage: " - << rs.uStreamIndex << sio.nl; - { - Bu::MyriadStream msDir = mStore.openStream( - rs.uStreamIndex - ); - uint32_t uSize = 0; - msDir.write( &uSize, 4 ); - } - break; - - case typeChrDev: - case typeBlkDev: - rs.uStreamIndex = uSpecial; - break; - - default: - rs.uStreamIndex = 0; - break; - } - rs.iATime = time(NULL); - rs.iMTime = time(NULL); - rs.iCTime = time(NULL); - writeInode( rs ); - - return iNode; - } - } - - throw Bu::MyriadFsException( - "No inode could be allocated. You've run out!"); -} - -void Bu::MyriadFs::stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ) -{ - RawStat rs; - readInode( iNode, rs, rIs ); - rBuf.iNode = iNode; - rBuf.iUser = rs.iUser; - rBuf.iGroup = rs.iGroup; - rBuf.uPerms = rs.uPerms; - rBuf.iLinks = rs.iLinks; - rBuf.iATime = rs.iATime; - rBuf.iMTime = rs.iMTime; - rBuf.iCTime = rs.iCTime; - rBuf.uDev = 0; - rBuf.iSize = 0; - switch( (rBuf.uPerms&typeMask) ) - { - case typeRegFile: - case typeSymLink: - rBuf.iSize = mStore.getStreamSize( rs.uStreamIndex ); - break; - - case typeChrDev: - case typeBlkDev: - rBuf.uDev = rs.uStreamIndex; - break; - - default: - rBuf.iSize = 0; - break; - } -} - -void Bu::MyriadFs::writeHeader() -{ - Bu::MyriadStream ms = mStore.openStream( 1 ); - ms.write( Myriad_Fs_MAGIC_CODE, 4 ); - int8_t iVer = 1; - int32_t iNumNodes = hNodeIndex.getSize(); - ms.write( &iVer, 1 ); - ms.write( &iNumNodes, 4 ); // iNumNodes - for( NodeIndex::iterator i = hNodeIndex.begin(); i; i++ ) - { - int32_t iNode = i.getKey(); - int32_t iPosition = i.getValue(); - ms.write( &iNode, 4 ); - ms.write( &iPosition, 4 ); - } - - // Truncate the stream afterwards so we don't use up too much space. - ms.setSize( ms.tell() ); -} - -void Bu::MyriadFs::setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ) -{ - RawStat rs; - Bu::MyriadStream is = mStore.openStream( 2 ); - - readInode( iNode, rs, is ); - rs.iATime = iATime; - rs.iMTime = iMTime; - writeInode( rs, is ); -} - -void Bu::MyriadFs::destroyNode( int32_t iNode ) -{ - if( iNode == 0 ) - throw Bu::MyriadFsException("You cannot destroy the root."); - - Bu::MyriadStream is = mStore.openStream( 2 ); - - // This will be overwritten with the last node - uint32_t iPosition = hNodeIndex.get( iNode ); - RawStat rsOld; - readInode( iNode, rsOld, is ); - switch( (rsOld.uPerms&typeMask) ) - { - case typeRegFile: - case typeDir: - case typeSymLink: - mStore.deleteStream( rsOld.uStreamIndex ); - break; - } - - hNodeIndex.erase( iNode ); - - // Read the last node, can't use the helpers, because we don't know the - // iNode yet. - if( iPosition != hNodeIndex.getSize() ) - { - // If this is the last node, then we don't need to do anything, but - // this case handles what to do if we aren't on the last node - RawStat rs; - is.setPos( (hNodeIndex.getSize())*sizeof(RawStat) ); - is.read( &rs, sizeof(RawStat) ); - - hNodeIndex.get( rs.iNode ) = iPosition; - writeInode( rs, is ); - } - - is.setSize( hNodeIndex.getSize() * sizeof(RawStat) ); -} - -Bu::String Bu::MyriadFs::filePart( const Bu::String &sPath ) -{ - Bu::String::const_iterator iStart = sPath.begin(); - if( *iStart == '/' ) - iStart++; - for( Bu::String::const_iterator iEnd = iStart.find('/'); iEnd; - iStart = iEnd+1, iEnd = iStart.find('/') ) { } - return Bu::String( iStart, sPath.end() ); -} - diff --git a/src/myriadfs.h b/src/myriadfs.h deleted file mode 100644 index cc9961a..0000000 --- a/src/myriadfs.h +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef MYRIAD_FS_H -#define MYRIAD_FS_H - -#include - -#include "bu/myriad.h" - -namespace Bu -{ - class Stream; - - subExceptionDecl( MyriadFsException ); - - /** - * A POSIX compliant, node based filesystem built on top of Myriad. - * - * A header is placed into stream 1. - * Header format: - * int32_t iMagicHeader (A7188B39) - * int8_t iVersion (1) - * int32_t iNumNodes - * NodeLookup[iNumNodes] nNode - * - * Node lookup: - * int32_t iInode - * int32_t iPosition - * - * The node headers or inode structures have a base size of 44 bytes. - * The name is stored in the directory format. - * Basic node header format: - * int32_t iNode - * int32_t iUser - * int32_t iGroup - * uint16_t uPerms - * int16_t iLinks - * uint32_t uStreamIndex - * int64_t iATime - * int64_t iMTime - * int64_t iCTime - * - * Some types get special formats for their assosiated data stream, or - * other special considerations, here's a list: - * - * - typeFifo: No stream, uStreamIndex unused (probably) - * - typeChrDev: No stream, uStreamIndex is device hi/lo - * - typeDir: The stream contains a directory contents listing, described - * below - * - typeBlkDev: No stream, uStreamIndex is device hi/lo - * - typeRegFile: The stream is the file data - * - typeSymLink: The stream is the destination of the symlink - * - typeSocket: No steram, uStreamIndex unused (probably) - * - * Directory streams have this simple listing format. They contain a list - * of all child elements, with no particular order at the moment. The . and - * .. entries are not listed, they are implicit: - * int32_t iNumNodes - * NodeTable[iNumNodes] nChildren - * - * NodeTable: - * int32_t iInode - * uint8_t uNameSize - * char[uNameSize] sName - */ - class MyriadFs - { - public: - MyriadFs( Bu::Stream &rStore, int iBlockSize=512 ); - virtual ~MyriadFs(); - - enum - { - permOthX = 0000001, - permOthW = 0000002, - permOthR = 0000004, - permGrpX = 0000010, - permGrpW = 0000020, - permGrpR = 0000040, - permUsrX = 0000100, - permUsrW = 0000200, - permUsrR = 0000400, - permSticky = 0001000, - permSetGid = 0002000, - permSetUid = 0004000, - permMask = 0007777, - typeFifo = 0010000, - typeChrDev = 0020000, - typeDir = 0040000, - typeBlkDev = 0060000, - typeRegFile = 0100000, - typeSymLink = 0120000, - typeSocket = 0140000, - typeMask = 0170000 - }; - - enum - { - Read = 0x01, ///< Open file for reading - Write = 0x02, ///< Open file for writing - Create = 0x04, ///< Create file if it doesn't exist - Truncate = 0x08, ///< Truncate file if it does exist - Append = 0x10, ///< Always append on every write - NonBlock = 0x20, ///< Open file in non-blocking mode - Exclusive = 0x44, ///< Create file, if it exists then fail - - // Helpful mixes - ReadWrite = 0x03, ///< Open for reading and writing - WriteNew = 0x0E ///< Create a file (or truncate) for writing. - /// Same as Write|Create|Truncate - }; - - class Stat - { - public: - int32_t iNode; - int32_t iUser; - int32_t iGroup; - uint16_t uPerms; - int16_t iLinks; - int64_t iATime; - int64_t iMTime; - int64_t iCTime; - int32_t iSize; - uint32_t uDev; - Bu::String sName; - }; - typedef Bu::List Dir; - - void stat( const Bu::String &sPath, Stat &rBuf ); - MyriadStream open( const Bu::String &sPath, int iMode, - uint16_t uPerms=0664 ); - void create( const Bu::String &sPath, uint16_t iPerms ); - void create( const Bu::String &sPath, uint16_t iPerms, - uint16_t iDevHi, uint16_t iDevLo ); - void create( const Bu::String &sPath, uint16_t iPerms, - uint32_t uSpecial ); - void mkDir( const Bu::String &sPath, uint16_t iPerms ); - void mkSymLink( const Bu::String &sTarget, const Bu::String &sPath ); - void mkHardLink( const Bu::String &sTarget, const Bu::String &sPath ); - Bu::String readSymLink( const Bu::String &sPath ); - Dir readDir( const Bu::String &sPath ); - void setTimes( const Bu::String &sPath, int64_t iATime, - int64_t iMTime ); - void unlink( const Bu::String &sPath ); - void setFileSize( const Bu::String &sPath, int32_t iSize ); - void rename( const Bu::String &sFrom, const Bu::String &sTo ); - - static dev_t devToSys( uint32_t uDev ); - static uint32_t sysToDev( dev_t uDev ); - - private: - class RawStat - { - public: - int32_t iNode; - int32_t iUser; - int32_t iGroup; - uint16_t uPerms; - int16_t iLinks; - uint32_t uStreamIndex; - int64_t iATime; - int64_t iMTime; - int64_t iCTime; - }; - typedef Bu::Hash NodeIndex; - - private: - int32_t lookupInode( const Bu::String &sPath, int32_t &iParent ); - int32_t lookupInode( Bu::String::const_iterator iStart, - int32_t iNode, int32_t &iParent ); - void readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ); - void readInode( int32_t iNode, RawStat &rs ); - void writeInode( const RawStat &rs ); - void writeInode( const RawStat &rs, MyriadStream &rOs ); - Dir readDir( int32_t iNode ); - MyriadStream openByInode( int32_t iNode ); - void addToDir( int32_t iDir, int32_t iNode, const Bu::String &sName ); - int32_t create( int32_t iParent, const Bu::String &sName, - uint16_t uPerms, uint32_t uSpecial ); - int32_t allocInode( uint16_t uPerms, uint32_t uSpecial ); - void stat( int32_t iNode, Stat &rBuf, MyriadStream &rIs ); - void writeHeader(); - void setTimes( int32_t iNode, int64_t iATime, int64_t iMTime ); - void destroyNode( int32_t iNode ); - - Bu::String filePart( const Bu::String &sPath ); - - private: - Bu::Stream &rStore; - Bu::Myriad mStore; - NodeIndex hNodeIndex; - int32_t iUser; - int32_t iGroup; - }; -}; - -#endif diff --git a/src/myriadstream.cpp b/src/myriadstream.cpp deleted file mode 100644 index e6e94bc..0000000 --- a/src/myriadstream.cpp +++ /dev/null @@ -1,309 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/myriadstream.h" - -#include - -// #define MYRIAD_STREAM_DEBUG 1 - -#ifdef MYRIAD_STREAM_DEBUG -#include "bu/sio.h" - -using Bu::sio; -using Bu::Fmt; -#endif - -Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad, - Bu::Myriad::Stream *pStream ) : - rMyriad( rMyriad ), - pStream( pStream ), - pCurBlock( NULL ), - iPos( 0 ) -{ -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: " << __LINE__ << ": Created, iId=" << pStream->iId << ", iSize=" - << pStream->iSize << sio.nl; -#endif - //pCurBlock = rMyriad.newBlock(); - //rMyriad.getBlock( uStream, pCurBlock ); - //uSize = pCurBlock->uBytesUsed; -} - -Bu::MyriadStream::~MyriadStream() -{ - if( pCurBlock ) - rMyriad.releaseBlock( pCurBlock ); - //rMyriad.updateStreamSize( uStream, uSize ); - //rMyriad.deleteBlock( pCurBlock ); -} - -void Bu::MyriadStream::close() -{ -} - -Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes ) -{ -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b." - << sio.nl; -#endif - if( nBytes > (Bu::size)pStream->iSize-iPos ) - nBytes = pStream->iSize-iPos; - if( nBytes <= 0 ) - return 0; - int iLeft = nBytes; -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Started, going to read " << nBytes << "b." - << sio.nl; -#endif - if( pCurBlock == NULL ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": No block loaded, loading initial block." - << sio.nl; -#endif - pCurBlock = rMyriad.getBlock( - pStream->aBlocks[iPos/rMyriad.iBlockSize] - ); - } - while( iLeft > 0 ) - { - int iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize]; - if( pCurBlock->iBlockIndex != iCurBlock ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Loading new block " << iCurBlock << "." - << sio.nl; -#endif - rMyriad.releaseBlock( pCurBlock ); - pCurBlock = rMyriad.getBlock( iCurBlock ); - } - - int iAmnt = Bu::min( - Bu::min( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ), - pStream->iSize-iPos - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: " - << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pBuf, - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - iAmnt - ); - iPos += iAmnt; - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - return nBytes; -} - -Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) -{ - if( nBytes <= 0 ) - return 0; - -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": Started, asked to write " << nBytes << "b." - << sio.nl; -#endif - if( nBytes <= 0 ) - return 0; - int iLeft = nBytes; - /* - if( pCurBlock == NULL ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: No block loaded, loading initial block." - << sio.nl; -#endif - pCurBlock = rMyriad.getBlock( - pStream->aBlocks[iPos/rMyriad.iBlockSize] - ); - }*/ - - while( iLeft > 0 ) - { - int iCurBlock; - if( iPos/rMyriad.iBlockSize < pStream->aBlocks.getSize() ) - { - iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize]; - } - else - { - iCurBlock = rMyriad.streamAddBlock( pStream ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: " - << iCurBlock << "." << sio.nl; - -#endif - } - if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": Loading new block " << iCurBlock << "." - << sio.nl; -#endif - rMyriad.releaseBlock( pCurBlock ); - pCurBlock = rMyriad.getBlock( iCurBlock ); - } - pCurBlock->bChanged = true; - - // There are two main writing modes when it comes down to it. - // Overwrite mode and append mode. Append is what pretty much always - // happens when creating a new stream. - if( iPos < pStream->iSize ) - { - int iAmnt = Bu::min( - Bu::min( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ), - pStream->iSize-iPos - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: " - << (iPos%rMyriad.iBlockSize) << "+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - pBuf, - iAmnt - ); - iPos += iAmnt; - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - else - { - int iAmnt = Bu::min( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (app): " << __LINE__ << ": Copying in bytes: " - << (iPos%rMyriad.iBlockSize) << "+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - pBuf, - iAmnt - ); - iPos += iAmnt; - pStream->iSize += iAmnt; - rMyriad.headerChanged(); - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - } - - return nBytes; -} - -Bu::size Bu::MyriadStream::tell() -{ - return iPos; -} - -void Bu::MyriadStream::seek( Bu::size offset ) -{ - iPos += offset; -} - -void Bu::MyriadStream::setPos( Bu::size pos ) -{ - iPos = pos; -} - -void Bu::MyriadStream::setPosEnd( Bu::size pos ) -{ - iPos = pStream->iSize-pos; -} - -bool Bu::MyriadStream::isEos() -{ - return iPos >= pStream->iSize; -} - -bool Bu::MyriadStream::isOpen() -{ - return true; -} - -void Bu::MyriadStream::flush() -{ -} - -bool Bu::MyriadStream::canRead() -{ - return true; -} - -bool Bu::MyriadStream::canWrite() -{ - return true; -} - -bool Bu::MyriadStream::isReadable() -{ - return true; -} - -bool Bu::MyriadStream::isWritable() -{ - return true; -} - -bool Bu::MyriadStream::isSeekable() -{ - return true; -} - -bool Bu::MyriadStream::isBlocking() -{ - return true; -} - -void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ ) -{ -} - -void Bu::MyriadStream::setSize( Bu::size iSize ) -{ - if( iSize < 0 ) - iSize = 0; - rMyriad.setStreamSize( pStream, iSize ); - if( iPos > iSize ) - iPos = iSize; -} - -Bu::size Bu::MyriadStream::getSize() const -{ - return pStream->iSize; -} - -Bu::size Bu::MyriadStream::getBlockSize() const -{ - return rMyriad.getBlockSize(); -} - -Bu::String Bu::MyriadStream::getLocation() const -{ - return Bu::String("%1").arg( pStream->iId ); -} - diff --git a/src/myriadstream.h b/src/myriadstream.h deleted file mode 100644 index fdad669..0000000 --- a/src/myriadstream.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_MYRIAD_STREAM_H -#define BU_MYRIAD_STREAM_H - -#include "bu/stream.h" -#include "bu/myriad.h" - -namespace Bu -{ - class MyriadStream : public Bu::Stream - { - friend class Myriad; - private: - /** - * These can only be created by the Myriad class. - */ - MyriadStream( Myriad &rMyriad, Myriad::Stream *pStream ); - - public: - virtual ~MyriadStream(); - - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Stream::write; - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - virtual void flush(); - virtual bool canRead(); - virtual bool canWrite(); - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( Bu::size iSize ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - private: - Myriad &rMyriad; - Myriad::Stream *pStream; - Myriad::Block *pCurBlock; - int iBlockSize; - int iPos; - }; -}; - -#endif diff --git a/src/newline.cpp b/src/newline.cpp deleted file mode 100644 index ffc9eb0..0000000 --- a/src/newline.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/newline.h" - -Bu::NewLine::NewLine( Bu::Stream &rNext ) : - Bu::Filter( rNext ), - bExChar( false ) -{ -} - -Bu::NewLine::~NewLine() -{ -} - -void Bu::NewLine::start() -{ -} - -Bu::size Bu::NewLine::stop() -{ - return 0; -} - -Bu::size Bu::NewLine::read( void *pBufV, Bu::size iAmnt ) -{ - Bu::size iTotal = 0; - Bu::size iOffset = 0; - Bu::size iRead = rNext.read( pBufV, iAmnt ); - char *pBuf = (char *)pBufV; - - for( Bu::size i = 0; i < iRead; i++ ) - { - if( pBuf[i] == '\r' ) - { - pBuf[i+iOffset] = '\n'; - if( pBuf[i+1] == '\n' ) - { - iOffset--; - } - } - else if( pBuf[i] == '\n' ) - { - pBuf[i+iOffset] = '\n'; - if( pBuf[i+1] == '\r' ) - { - iOffset--; - } - } - else if( iOffset ) - { - pBuf[i+iOffset] = pBuf[i]; - } - } - - iTotal += iRead + iOffset; - return iTotal; -} - -Bu::size Bu::NewLine::write( const void *, Bu::size ) -{ - return 0; -} - diff --git a/src/newline.h b/src/newline.h deleted file mode 100644 index afe0a84..0000000 --- a/src/newline.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_NEW_LINE_H -#define BU_NEW_LINE_H - -#include "bu/filter.h" - -namespace Bu -{ - /** - * Converts new-line characters from any standard convention into linefeeds - * (\\n) on reading, and converts them to either your OS's standard or a - * specified standard, depending on how you construct the class. - * - * If you're reading in a text file, then this filter is practically - * required. - */ - class NewLine : public Bu::Filter - { - public: - NewLine( Bu::Stream &rNext ); - virtual ~NewLine(); - - virtual void start(); - virtual Bu::size stop(); - - virtual Bu::size read( void *pBuf, Bu::size iAmnt ); - virtual Bu::size write( const void *pBuf, Bu::size iAmnt ); - - private: - bool bExChar; - char cExChar; - }; -}; - -#endif diff --git a/src/nullstream.cpp b/src/nullstream.cpp deleted file mode 100644 index 9552cc5..0000000 --- a/src/nullstream.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/nullstream.h" - -Bu::NullStream::NullStream() : - sRead( 0 ), - sWrote( 0 ) -{ -} - -Bu::NullStream::~NullStream() -{ -} - -void Bu::NullStream::close() -{ - sRead = sWrote = 0; -} - -Bu::size Bu::NullStream::read( void *pBuf, Bu::size nBytes ) -{ - memset( pBuf, 0, nBytes ); - sRead += nBytes; - return nBytes; -} - -Bu::String Bu::NullStream::readLine() -{ - sRead++; - return Bu::String("\0", 1 ); -} - -Bu::size Bu::NullStream::write( const void *, Bu::size nBytes ) -{ - sWrote += nBytes; - return nBytes; -} - -Bu::size Bu::NullStream::tell() -{ - return sRead + sWrote; -} - -void Bu::NullStream::seek( Bu::size ) -{ -} - -void Bu::NullStream::setPos( Bu::size ) -{ -} - -void Bu::NullStream::setPosEnd( Bu::size ) -{ -} - -bool Bu::NullStream::isEos() -{ - return false; -} - -bool Bu::NullStream::isOpen() -{ - return true; -} - -void Bu::NullStream::flush() -{ -} - -bool Bu::NullStream::canRead() -{ - return true; -} - -bool Bu::NullStream::canWrite() -{ - return true; -} - -bool Bu::NullStream::isReadable() -{ - return true; -} - -bool Bu::NullStream::isWritable() -{ - return true; -} - -bool Bu::NullStream::isSeekable() -{ - return false; -} - -bool Bu::NullStream::isBlocking() -{ - return true; -} - -void Bu::NullStream::setBlocking( bool ) -{ -} - -void Bu::NullStream::setSize( Bu::size ) -{ -} - -Bu::size Bu::NullStream::getSize() const -{ - return 0; -} - -Bu::size Bu::NullStream::getBlockSize() const -{ - return 0; -} - -Bu::String Bu::NullStream::getLocation() const -{ - return ""; -} - diff --git a/src/nullstream.h b/src/nullstream.h deleted file mode 100644 index 9b75332..0000000 --- a/src/nullstream.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_NULL_STREAM_H -#define BU_NULL_STREAM_H - -#include "bu/stream.h" - -namespace Bu -{ - /** - * Works a lot like /dev/null on *nix style systems. This class allows - * infinite reading and writing. All operatorns "succeed" even if they - * don't seem to do anything. This is great for testing writing code or - * doing dry runs. When reading, it will produce NULL bytes, so any - * application that would like the ability to produce null streams as a - * snap-in replacement for any other Bu::Stream, this is the right option. - * - * As an added feature, the NullStream will track how many bytes it was - * asked to read and write, allowing you to use it to determine how many - * bytes a write opretion would use without actually writing anything. - */ - class NullStream : public Bu::Stream - { - public: - NullStream(); - virtual ~NullStream(); - - virtual void close(); - virtual Bu::size read( void *pBuf, Bu::size nBytes ); - virtual Bu::String readLine(); - virtual Bu::size write( const void *pBuf, Bu::size nBytes ); - using Bu::Stream::write; - virtual Bu::size tell(); - virtual void seek( Bu::size offset ); - virtual void setPos( Bu::size pos ); - virtual void setPosEnd( Bu::size pos ); - virtual bool isEos(); - virtual bool isOpen(); - virtual void flush(); - virtual bool canRead(); - virtual bool canWrite(); - virtual bool isReadable(); - virtual bool isWritable(); - virtual bool isSeekable(); - virtual bool isBlocking(); - virtual void setBlocking( bool bBlocking=true ); - virtual void setSize( Bu::size iSize ); - - virtual size getSize() const; - virtual size getBlockSize() const; - virtual Bu::String getLocation() const; - - Bu::size getBytesRead() { return sRead; } - Bu::size getByetsWritten() { return sWrote; } - - private: - Bu::size sRead; - Bu::size sWrote; - }; -}; - -#endif diff --git a/src/optparser.cpp b/src/optparser.cpp deleted file mode 100644 index 050232c..0000000 --- a/src/optparser.cpp +++ /dev/null @@ -1,492 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/optparser.h" -#include "bu/sio.h" -using namespace Bu; - -#include - -Bu::OptParser::OptParser() -{ -} - -Bu::OptParser::~OptParser() -{ -} - -void Bu::OptParser::parse( int argc, char **argv ) -{ - for( int j = 1; j < argc; j++ ) - { - if( argv[j][0] == '-' ) - { - // Now we're on to something, which kind is it? - if( argv[j][1] == '-' ) - { - int iEPos; - for( iEPos = 2; argv[j][iEPos] != '\0' && - argv[j][iEPos] != '='; iEPos++ ) { } - - Bu::String sOpt; - int iCount = argc-j; - Bu::String sExtraParam; - if( argv[j][iEPos] == '=' ) - { - sOpt.set( argv[j]+2, iEPos-2 ); - iCount++; - sExtraParam.set( argv[j]+iEPos+1 ); - } - else - { - sOpt.set( argv[j]+2 ); - } - if( !hlOption.has( sOpt ) ) - { - optionError( "--" + sOpt ); - } - else - { - // Long param, cool, that's easy, first search for = - Option *pOpt = hlOption.get( sOpt ); - if( pOpt->sUsed ) - { - Bu::StrArray aParams( iCount ); - aParams.append( sOpt ); - if( sExtraParam.isSet() ) - { - aParams.append( argv[j]+iEPos+1 ); - } - for( int k = j+1; k < argc; k++ ) - { - aParams.append( argv[k] ); - } - j += pOpt->sUsed( aParams ); - } - else if( pOpt->pProxy ) - { - if( pOpt->sOverride.isSet() ) - { - pOpt->pProxy->setValue( pOpt->sOverride ); - } - else if( sExtraParam.isSet() ) - { - pOpt->pProxy->setValueFromStr( sExtraParam ); - } - else if( argv[j+1] != '\0' ) - { - pOpt->pProxy->setValueFromStr( argv[j+1] ); - j++; - } - } - } - } - else - { - int iCPos; - for( iCPos = 1; argv[j][iCPos] != '\0'; iCPos++ ) - { - if( !hsOption.has( argv[j][iCPos] ) ) - { - Bu::String sOpt("-"); - sOpt += argv[j][iCPos]; - optionError( sOpt ); - } - else - { - Option *pOpt = hsOption.get( argv[j][iCPos] ); - char buf[2] = {argv[j][iCPos], '\0'}; - if( pOpt->sUsed ) - { - Bu::StrArray aParams( argc-j+1 ); - aParams.append( buf ); - int iMod = 0; - if( argv[j][iCPos+1] != '\0' ) - { - aParams.append( argv[j]+iCPos+1 ); - iMod = -1; - } - for( int k = j+1; k < argc; k++ ) - { - aParams.append( argv[k] ); - } - int iUsed = pOpt->sUsed( aParams ); - if( iUsed > 0 ) - { - j += iUsed + iMod; - break; - } - } - else if( pOpt->pProxy ) - { - if( pOpt->sOverride.isSet() ) - { - pOpt->pProxy->setValue( pOpt->sOverride ); - } - else if( argv[j][iCPos+1] != '\0' ) - { - pOpt->pProxy->setValueFromStr( - argv[j]+iCPos+1 - ); - break; - } - else if( argv[j+1] ) - { - pOpt->pProxy->setValueFromStr( - argv[j+1] - ); - j++; - break; - } - } - } - } - } - } - else - { - if( !sNonOption ) - { - optionError( argv[j] ); - } - else - { - int iCount = argc-j; - Bu::StrArray aParams( iCount ); - for( int k = j; k < argc; k++ ) - { - aParams.append( argv[k] ); - } - j += sNonOption( aParams ); - } - } - } -} - -void Bu::OptParser::parse( const Bu::String &sLine ) -{ - Bu::String sCmd = sLine.clone(); - int iParams = 0; - bool bInGap = true; - bool bInQuote = false; - for( Bu::String::iterator i = sCmd.begin(); i; i++ ) - { - if( bInQuote == false && (*i == ' ' || *i == '\t') ) - { - if( bInGap == false ) - { - bInGap = true; - } - } - else if( *i == '"' ) - { - bInQuote = !bInQuote; - } - else - { - if( bInGap ) - { - iParams++; - bInGap = false; - } - } - } - - bInQuote = false; - bInGap = true; - char **asParam = new char*[iParams]; - iParams = 0; - for( char *i = sCmd.getStr(); *i; i++ ) - { - if( bInQuote == false && (*i == ' ' || *i == '\t') ) - { - if( bInGap == false ) - { - bInGap = true; - *i = '\0'; - } - } - else if( *i == '"' ) - { - bInQuote = !bInQuote; - } - else - { - if( bInGap ) - { - asParam[iParams++] = i; - bInGap = false; - } - } - } - - parse( iParams, asParam ); - - delete[] asParam; -} - -void Bu::OptParser::addOption( const Option &opt ) -{ - lOption.append( opt ); - if( opt.cOpt != '\0' ) - hsOption.insert( opt.cOpt, &lOption.last() ); - if( opt.sOpt.isSet() ) - hlOption.insert( opt.sOpt, &lOption.last() ); -} - -void Bu::OptParser::setOverride( char cOpt, const Bu::Variant &sOverride ) -{ - hsOption.get( cOpt )->sOverride = sOverride; -} - -void Bu::OptParser::setOverride( const Bu::String &sOpt, const Bu::Variant &sOverride ) -{ - hlOption.get( sOpt )->sOverride = sOverride; -} - -void Bu::OptParser::setHelpDefault( const Bu::String &sOpt, const Bu::String &sTxt ) -{ - hlOption.get( sOpt )->sHelpDefault = sTxt; -} - -void Bu::OptParser::addHelpOption( char c, const Bu::String &s, const Bu::String &sHelp ) -{ - Option o; - o.sUsed = slot( this, &OptParser::optHelp ); - o.cOpt = c; - o.sOpt = s; - o.sHelp = sHelp; - addOption( o ); -} - -void Bu::OptParser::addHelpBanner( const Bu::String &sText, bool bFormatted ) -{ - Banner b; - b.sText = sText; - b.bFormatted = bFormatted; - if( lOption.getSize() > 0 ) - { - for( b.iAfter = lOption.begin(); b.iAfter+1; b.iAfter++ ) { } - } - lBanner.append( b ); -} - -int Bu::OptParser::optHelp( StrArray /*aParams*/ ) -{ - bool bHasShort = false; - int iMaxWidth = 0; - int iScrWidth = 80; - char *env = getenv("COLUMNS"); - if( env ) - iScrWidth = strtol( env, NULL, 10 ); - for( OptionList::iterator i = lOption.begin(); i; i++ ) - { - if( (*i).cOpt != '\0' ) - bHasShort = true; - int lOptSize = (*i).sOpt.getSize() + (*i).sHelpDefault.getSize(); - if( (*i).sOpt.isSet() && iMaxWidth < lOptSize ) - iMaxWidth = lOptSize; - } - int iIndent = 4; - if( bHasShort ) - iIndent += 4; - if( iMaxWidth > 0 ) - iIndent += 4 + iMaxWidth; - - BannerList::iterator iBanner; - for( iBanner = lBanner.begin(); iBanner; iBanner++ ) - { - if( (*iBanner).iAfter ) - break; - - if( (*iBanner).bFormatted ) - sio << format( (*iBanner).sText, iScrWidth-1, 0 ); - else - sio << (*iBanner).sText; - sio << sio.nl; - } - for( OptionList::iterator i = lOption.begin(); i; i++ ) - { - sio << " "; - if( bHasShort ) - { - if( (*i).cOpt == '\0' ) - sio << " "; - else - sio << "-" << (*i).cOpt; - sio << " "; - } - if( iMaxWidth > 0 ) - { - if( (*i).sOpt.isSet() ) - { - sio << "--" << Fmt(iMaxWidth, Fmt::Left) - << (*i).sOpt + (*i).sHelpDefault; - } - else - { - sio << " " << Fmt(iMaxWidth) << ""; - } - sio << " "; - } - sio << format( (*i).sHelp, iScrWidth-iIndent-1, iIndent ); - sio << sio.nl; - - for( ; iBanner; iBanner++ ) - { - if( (*iBanner).iAfter != i ) - break; - - if( (*iBanner).bFormatted ) - sio << format( (*iBanner).sText, iScrWidth-1, 0 ); - else - sio << (*iBanner).sText; - sio << sio.nl; - } - } - exit( 0 ); - return 0; -} - -void Bu::OptParser::optionError( const Bu::String &sOption ) -{ - sio << "Unregcognized option discovered: " << sOption << sio.nl << sio.nl; - exit( 1 ); -} - -void Bu::OptParser::setNonOption( OptionSignal sSignal ) -{ - sNonOption = sSignal; -} - -Bu::String Bu::OptParser::format( const Bu::String &sIn, int iWidth, - int iIndent ) -{ - Bu::String sOut; - Bu::String sIndent; - for( int j = 0; j < iIndent; j++ ) - sIndent.append(" ", 1); - bool bFirst = true; - int iSpaceCount = 0; - bool bSpace = false; - int iPrevLineLen; - int iLineLen = 0; - Bu::String::const_iterator iLastSpace, iStart; - for( Bu::String::const_iterator i = iLastSpace = iStart = sIn.begin(); i; i++ ) - { - if( *i == ' ' ) - { - if( bSpace == false ) - { - iLastSpace = i; - iSpaceCount++; - bSpace = true; - iPrevLineLen = iLineLen; - } - } - else - { - bSpace = false; - } - iLineLen++; - - if( iLineLen >= iWidth ) - { - iSpaceCount--; - if( bFirst == true ) - bFirst = false; - else - sOut += sIndent; - int iExtraSpaces = iWidth-iPrevLineLen; - bSpace = false; - float fFill = 0.0; - int iSubSpaceCount = 0; - float fAdd = ((float)iExtraSpaces/(float)iSpaceCount); - for( Bu::String::const_iterator k = iStart; k != iLastSpace; k++ ) - { - sOut += *k; - if( *k == ' ' ) - { - if( bSpace == false && iExtraSpaces > 0 ) - { - bSpace = true; - fFill += fAdd; - iSubSpaceCount++; - for( int sp = 0; sp < (int)(fFill); sp++ ) - { - sOut += ' '; - iExtraSpaces--; - } - fFill -= (int)fFill; - if( iSubSpaceCount == iSpaceCount && iExtraSpaces > 0 ) - { - for(; iExtraSpaces > 0; iExtraSpaces-- ) - { - sOut += ' '; - } - } - } - } - else - bSpace = false; - } - //sOut.append( iStart, iLastSpace ); - sOut.append("\n"); - for(; iLastSpace && *iLastSpace == ' '; iLastSpace++ ) { } - iStart = i = iLastSpace; - bSpace = false; - iLineLen = 1; - iSpaceCount = 0; - } - } - if( !bFirst ) - sOut += sIndent; - sOut.append( iStart ); - return sOut; -} - - -// -// Code for Bu::OptParser::_ValueProxy -// - -Bu::OptParser::_ValueProxy::_ValueProxy() -{ -} - -Bu::OptParser::_ValueProxy::~_ValueProxy() -{ -} - -// -// Code for Bu::OptParser::Option -// - -Bu::OptParser::Option::Option() : - cOpt( '\0' ), - pProxy( NULL ) -{ -} - -Bu::OptParser::Option::Option( const Option &rSrc ) : - cOpt( rSrc.cOpt ), - sOpt( rSrc.sOpt ), - sHelp( rSrc.sHelp ), - sUsed( rSrc.sUsed ), - pProxy( NULL ), - sOverride( rSrc.sOverride ) -{ - if( rSrc.pProxy ) - pProxy = rSrc.pProxy->clone(); -} - -Bu::OptParser::Option::~Option() -{ - delete pProxy; - pProxy = NULL; -} - diff --git a/src/optparser.h b/src/optparser.h deleted file mode 100644 index f2fe531..0000000 --- a/src/optparser.h +++ /dev/null @@ -1,223 +0,0 @@ -/* - * Copyright (C) 2007-2011 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_OPT_PARSER_H -#define BU_OPT_PARSER_H - -#include "bu/string.h" -#include "bu/list.h" -#include "bu/hash.h" -#include "bu/signals.h" -#include "bu/array.h" -#include "bu/membuf.h" -#include "bu/formatter.h" -#include "bu/variant.h" - -namespace Bu -{ - typedef Bu::Array StrArray; - - /** - * POSIX/Gnu style command line parser. Handles long and short options in - * a variety of fun and useful ways, along with singal based callbacks and - * automatic variable setting. It's pretty easy to use, and very flexible. - * - * OptParser supports it's own builtin help mechanism which automatically - * enumerates the available options and their help in a well formatted and - * easy to read way, automatically formatting your help text per option and - * allows for addition "help banners" which can be placed wherever you - * would like. - */ - class OptParser - { - private: - class _ValueProxy - { - public: - _ValueProxy(); - virtual ~_ValueProxy(); - - virtual void setValueFromStr( const Bu::String & )=0; - virtual void setValue( const Bu::Variant &vVar )=0; - virtual _ValueProxy *clone()=0; - }; - - template - class ValueProxy : public _ValueProxy - { - public: - ValueProxy( ptype &v ) : - v( v ) - { - } - - virtual ~ValueProxy() - { - } - - virtual void setValueFromStr( const Bu::String &sVal ) - { - Bu::MemBuf mb( sVal ); - Bu::Formatter f( mb ); - f << Bu::Fmt().tokenize( false ); - f >> v; - } - - virtual void setValue( const Bu::Variant &vVar ) - { - if( vVar.getType() == typeid(ptype) ) - { - v = vVar.get(); - } - else if( vVar.getType() == typeid(Bu::String) ) - { - setValueFromStr( vVar.get() ); - } - else - { - Bu::MemBuf mb; - Bu::Formatter f( mb ); -// f << vVar; - setValueFromStr( mb.getString() ); - } - } - - virtual _ValueProxy *clone() - { - return new ValueProxy( v ); - } - - private: - ptype &v; - }; - - public: - typedef Signal1 OptionSignal; - class Option - { - public: - Option(); - Option( const Option &rSrc ); - virtual ~Option(); - - char cOpt; - Bu::String sOpt; - Bu::String sHelp; - OptionSignal sUsed; - _ValueProxy *pProxy; - Bu::Variant sOverride; - Bu::String sHelpDefault; - }; - - private: - typedef Bu::List