Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ZStrimW_InsertSeparator, and refactor ZStreamW_HexStrim to use it.

  • Loading branch information...
commit 2173b16568fa9ab4a38b460180ef459f1e40155d 1 parent 68a6824
@ElectricMagic ElectricMagic authored
View
105 zoolib/ZStreamW_HexStrim.cpp
@@ -19,52 +19,17 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------------------------- */
#include "zoolib/ZStreamW_HexStrim.h"
-#include "zoolib/ZStrim.h"
namespace ZooLib {
-// =================================================================================================
-// MARK: - ZStreamW_HexStrim
-
static const UTF8 spHexDigits[] = "0123456789ABCDEF";
static const UTF8 spHexDigitsWithUnderscore[] = "_123456789ABCDEF";
-/**
-\param iByteSeparator A UTF-8 string to be inserted between each byte that is output.
-\param iChunkSeparator A UTF-8 string to be inserted between each chunk that is output.
-\param iChunkSize The number of bytes to treat as a chunk.
-\param iStrimSink The strim to which characters should be written.
-*/
-ZStreamW_HexStrim::ZStreamW_HexStrim(const std::string& iByteSeparator,
- const std::string& iChunkSeparator, size_t iChunkSize, const ZStrimW& iStrimSink)
-: fStrimSink(iStrimSink),
- fByteSeparator(iByteSeparator),
- fChunkSeparator(iChunkSeparator),
- fChunkSize(iChunkSize),
- fCurrentChunkLength(0),
- fWrittenAny(false),
- fHexDigits(spHexDigits)
- {}
+// =================================================================================================
+// MARK: - ZStreamW_HexStrim_Real
-/**
-\param iByteSeparator A UTF-8 string to be inserted between each byte that is output.
-\param iChunkSeparator A UTF-8 string to be inserted between each chunk that is output.
-\param iChunkSize The number of bytes to treat as a chunk.
-\param iUseUnderscore If true then an underscore should be output in place of zero, if
-false then a zero is output. This is useful when what's being output is the hex representation
-of a 4 bit per pixel grayscale pixmap, in which case underscores are output for black pixels,
-and visually drop out somewhat.
-\param iStrimSink The strim to which characters should be written.
-*/
-ZStreamW_HexStrim::ZStreamW_HexStrim(const std::string& iByteSeparator,
- const std::string& iChunkSeparator, size_t iChunkSize,
- bool iUseUnderscore, const ZStrimW& iStrimSink)
-: fStrimSink(iStrimSink),
- fByteSeparator(iByteSeparator),
- fChunkSeparator(iChunkSeparator),
- fChunkSize(iChunkSize),
- fCurrentChunkLength(0),
- fWrittenAny(false)
+ZStreamW_HexStrim_Real::ZStreamW_HexStrim_Real(bool iUseUnderscore, const ZStrimW& iStrimSink)
+: fStrimSink(iStrimSink)
{
if (iUseUnderscore)
fHexDigits = spHexDigitsWithUnderscore;
@@ -72,41 +37,49 @@ ZStreamW_HexStrim::ZStreamW_HexStrim(const std::string& iByteSeparator,
fHexDigits = spHexDigits;
}
-void ZStreamW_HexStrim::Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten)
+void ZStreamW_HexStrim_Real::Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten)
{
- if (oCountWritten)
- *oCountWritten = 0;
const uint8* localSource = reinterpret_cast<const uint8*>(iSource);
while (iCount)
{
- if (fWrittenAny)
- {
- if (fCurrentChunkLength)
- fStrimSink.Write(fByteSeparator);
- else if (fChunkSize)
- fStrimSink.Write(fChunkSeparator);
- }
- fWrittenAny = true;
-
- UTF8 sHex[2];
- sHex[0] = fHexDigits[((*localSource) >> 4) & 0x0F];
- sHex[1] = fHexDigits[(*localSource) & 0x0F];
- fStrimSink.Write(sHex, 2);
-
- fCurrentChunkLength += 1;
- if (fCurrentChunkLength == fChunkSize)
- fCurrentChunkLength = 0;
- iCount -= 1;
- localSource += 1;
- if (oCountWritten)
- *oCountWritten += 1;
+ UTF8 theHex[2];
+ theHex[0] = fHexDigits[((*localSource) >> 4) & 0x0F];
+ theHex[1] = fHexDigits[(*localSource) & 0x0F];
+ fStrimSink.Write(theHex, 2);
+ --iCount;
+ ++localSource;
}
+
+ if (oCountWritten)
+ *oCountWritten = localSource - reinterpret_cast<const uint8*>(iSource);
}
+void ZStreamW_HexStrim_Real::Imp_Flush()
+ { fStrimSink.Flush(); }
+
+// =================================================================================================
+// MARK: - ZStreamW_HexStrim
+
+ZStreamW_HexStrim::ZStreamW_HexStrim(const std::string& iByteSeparator,
+ const std::string& iChunkSeparator, size_t iChunkSize, const ZStrimW& iStrimSink)
+: fStrim_Chunks((2 + iByteSeparator.size()) * iChunkSize, iChunkSeparator, iStrimSink)
+, fStrim_Bytes(2, iByteSeparator, fStrim_Chunks)
+, fStream(false, fStrim_Bytes)
+ {}
+
+#if 0
+ZStreamW_HexStrim::ZStreamW_HexStrim(const std::string& iByteSeparator,
+ const std::string& iChunkSeparator, size_t iChunkSize,
+ bool iUseUnderscore, const ZStrimW& iStrimSink);
+#endif
+
+void ZStreamW_HexStrim::Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten)
+ { fStream.Write(iSource, iCount, oCountWritten); }
+
void ZStreamW_HexStrim::Imp_Flush()
- {
- fStrimSink.Flush();
- }
+ { fStream.Flush(); }
+
+
} // namespace ZooLib
View
30 zoolib/ZStreamW_HexStrim.h
@@ -23,15 +23,29 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "zconfig.h"
#include "zoolib/ZStream.h"
+#include "zoolib/ZStrimW_InsertSeparator.h"
namespace ZooLib {
// =================================================================================================
-// MARK: - ZStreamW_HexStrim
+// MARK: - ZStreamW_HexStrim_Real
+
+class ZStreamW_HexStrim_Real : public ZStreamW
+ {
+public:
+ ZStreamW_HexStrim_Real(bool iUseUnderscore, const ZStrimW& iStrimSink);
+
+// From ZStreamW
+ virtual void Imp_Write(const void* iSource, size_t iCount, size_t* oCountWritten);
+ virtual void Imp_Flush();
-class ZStrimW;
+protected:
+ const ZStrimW& fStrimSink;
+ const char* fHexDigits;
+ };
-/// A write filter stream that takes binary data and writes hex characters to its sink strim.
+// =================================================================================================
+// MARK: - ZStreamW_HexStrim
class ZStreamW_HexStrim : public ZStreamW
{
@@ -48,13 +62,9 @@ class ZStreamW_HexStrim : public ZStreamW
virtual void Imp_Flush();
protected:
- const ZStrimW& fStrimSink;
- std::string fByteSeparator;
- std::string fChunkSeparator;
- size_t fChunkSize;
- size_t fCurrentChunkLength;
- size_t fWrittenAny;
- const char* fHexDigits;
+ ZStrimW_InsertSeparator fStrim_Chunks;
+ ZStrimW_InsertSeparator fStrim_Bytes;
+ ZStreamW_HexStrim_Real fStream;
};
} // namespace ZooLib
View
68 zoolib/ZStrimW_InsertSeparator.cpp
@@ -0,0 +1,68 @@
+/* -------------------------------------------------------------------------------------------------
+Copyright (c) 2012 Andrew Green
+http://www.zoolib.org
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES
+OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+------------------------------------------------------------------------------------------------- */
+
+#include "zoolib/ZCompat_algorithm.h"
+#include "zoolib/ZStrimW_InsertSeparator.h"
+
+namespace ZooLib {
+
+// =================================================================================================
+// MARK: - ZStrimW_CRLFRemove
+
+ZStrimW_InsertSeparator::ZStrimW_InsertSeparator
+ (size_t iSpacing, const string8& iSeparator, const ZStrimW& iStrimSink)
+: fSpacing(iSpacing)
+, fSeparator(iSeparator)
+, fStrimSink(iStrimSink)
+, fCount(0)
+ {}
+
+void ZStrimW_InsertSeparator::Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU)
+ {
+ const UTF32* localSource = iSource;
+ size_t countRemaining = iCountCU;
+ while (countRemaining)
+ {
+ size_t countToWrite = countRemaining;
+ if (fSpacing)
+ {
+ if (fCount == fSpacing)
+ {
+ fStrimSink.Write(fSeparator);
+ fCount = 0;
+ }
+ countToWrite = std::min(countToWrite, fSpacing - fCount);
+ }
+
+ size_t countWritten;
+ fStrimSink.Write(localSource, countToWrite, &countWritten);
+ if (countWritten == 0)
+ break;
+
+ countRemaining -= countWritten;
+ localSource += countWritten;
+ fCount += countWritten;
+ }
+
+ if (oCountCU)
+ *oCountCU = localSource - iSource;
+ }
+
+} // namespace ZooLib
View
50 zoolib/ZStrimW_InsertSeparator.h
@@ -0,0 +1,50 @@
+/* -------------------------------------------------------------------------------------------------
+Copyright (c) 2012 Andrew Green
+http://www.zoolib.org
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES
+OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+------------------------------------------------------------------------------------------------- */
+
+#ifndef __ZStrimW_InsertSeparator_h__
+#define __ZStrimW_InsertSeparator_h__ 1
+#include "zconfig.h"
+
+#include "zoolib/ZStrim.h"
+
+namespace ZooLib {
+
+// =================================================================================================
+// MARK: - ZStrimW_InsertSeparator
+
+class ZStrimW_InsertSeparator
+: public ZStrimW_NativeUTF32
+ {
+public:
+ ZStrimW_InsertSeparator(size_t iSpacing, const string8& iSeparator, const ZStrimW& iStrimSink);
+
+ virtual void Imp_WriteUTF32(const UTF32* iSource, size_t iCountCU, size_t* oCountCU);
+
+private:
+ const ZStrimW& fStrimSink;
+ const size_t fSpacing;
+ const string8 fSeparator;
+
+ size_t fCount;
+ };
+
+} // namespace ZooLib
+
+#endif // __ZStrimW_InsertSeparator_h__
Please sign in to comment.
Something went wrong with that request. Please try again.