Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rewrite ISerializable::strlen

  • Loading branch information...
commit f498c6b01bb04e6ba144cc430ed84607810ab05e 1 parent 3cadf82
@apreiml authored
View
18 include/genie/file/Compressor.h
@@ -33,10 +33,15 @@ struct zlib_params;
namespace genie
{
+//------------------------------------------------------------------------------
+/// Utility to compress and decompress streams handled in ISerializeable
+/// objects.
+//
class Compressor : public boost::noncopyable
{
public:
//----------------------------------------------------------------------------
+ /// Object that calls Compressors methods needs to register itself here.
///
/// @param obj object that calls compressors methods
//
@@ -45,8 +50,10 @@ class Compressor : public boost::noncopyable
//----------------------------------------------------------------------------
virtual ~Compressor();
+ //----------------------------------------------------------------------------
void beginCompression(void);
+ //----------------------------------------------------------------------------
void endCompression(void);
private:
@@ -56,6 +63,9 @@ class Compressor : public boost::noncopyable
std::istream *istream_;
boost::shared_ptr<std::istream> uncompressedIstream_;
+ std::ostream *ostream_;
+ boost::shared_ptr<std::iostream> bufferedStream_;
+
//----------------------------------------------------------------------------
/// Get zlib parameters necessary for (de)compressing genie archives.
///
@@ -69,17 +79,15 @@ class Compressor : public boost::noncopyable
void startDecompression(void);
//----------------------------------------------------------------------------
- /// Closes decompressed stream. Returns compressed istream given at start.
- ///
- /// @return input stream given at startDecompression
+ /// Closes decompressed stream.
//
void stopDecompression(void);
//----------------------------------------------------------------------------
- std::iostream *startCompression(std::ostream *ostr);
+ void startCompression(void);
//----------------------------------------------------------------------------
- std::ostream *stopCompression(std::istream *ostr);
+ void stopCompression(void);
};
View
37 main.cpp
@@ -122,6 +122,35 @@ void testScn()
std::cout << "Original filename: " << scn.originalFileName << std::endl;
+ scn.saveAs("xxxx.scx");
+
+ genie::ScnFile scn2;
+ scn2.load("xxxx.scx");
+
+
+ std::cout << "Original filename: " << scn2.originalFileName << std::endl;
+
+}
+
+size_t xxstrnlen(const char *str, size_t maxLen)
+{
+// const char *ptr = 0;
+
+// for (ptr = str; *ptr != '\0' && maxLen-- != 0; ++ptr);
+
+// return (ptr - str);
+
+ size_t len = 0;
+
+ for (unsigned int i=0; i < maxLen; i++)
+ {
+ if (str[i] == '\0')
+ return len;
+
+ len ++;
+ }
+
+ return len;
}
int main(int argc, char **argv) {
@@ -131,6 +160,14 @@ int main(int argc, char **argv) {
// testDrs();
testScn();
+
+
+// char x[1];
+//
+// x[0] = 'c';
+//
+// std::cout << xxstrnlen(x, 1) << std::endl;
+
/*
std::ifstream ifs;
std::ofstream ofs;
View
52 src/file/Compressor.cpp
@@ -52,15 +52,13 @@ void Compressor::beginCompression(void)
switch(obj_->getOperation())
{
case ISerializable::OP_READ:
-
- istream_ = obj_->getIStream();
-
startDecompression();
-
- obj_->setIStream(*uncompressedIstream_);
-
break;
-
+
+ case ISerializable::OP_WRITE:
+ startCompression();
+ break;
+
default:
break;
@@ -73,11 +71,11 @@ void Compressor::endCompression(void)
switch(obj_->getOperation())
{
case ISerializable::OP_READ:
-
- obj_->setIStream(*istream_);
-
stopDecompression();
+ break;
+ case ISerializable::OP_WRITE:
+ stopCompression();
break;
default:
@@ -106,6 +104,8 @@ boost::iostreams::zlib_params Compressor::getZlibParams(void ) const
//------------------------------------------------------------------------------
void Compressor::startDecompression(void)
{
+ istream_ = obj_->getIStream();
+
try
{
filtering_istreambuf in;
@@ -122,6 +122,8 @@ void Compressor::startDecompression(void)
copy(in, b_ins);
uncompressedIstream_ = shared_ptr<std::istream>(new v_stream(file_buf));
+
+ obj_->setIStream(*uncompressedIstream_);
}
catch ( const zlib_error &z_err)
{
@@ -135,32 +137,42 @@ void Compressor::startDecompression(void)
//------------------------------------------------------------------------------
void Compressor::stopDecompression(void)
{
- uncompressedIstream_.reset();
-
+ obj_->setIStream(*istream_);
istream_ = 0;
+
+ uncompressedIstream_.reset();
}
//------------------------------------------------------------------------------
-std::iostream *Compressor::startCompression(std::ostream *ostr)
+void Compressor::startCompression(void)
{
+ // create buffer
std::vector<char> file_buf;
- //return std::auto_ptr<std::iostream> (new v_stream(file_buf));
- return new v_stream(file_buf);
+ bufferedStream_ = boost::shared_ptr<std::iostream> (new v_stream(file_buf));
+
+ ostream_ = obj_->getOStream();
+
+ obj_->setOStream(*bufferedStream_);
}
//------------------------------------------------------------------------------
-std::ostream *Compressor::stopCompression(std::istream *istr)
-{/*
+void Compressor::stopCompression(void)
+{
try
{
filtering_ostreambuf out;
out.push(zlib_compressor(getZlibParams()));
- out.push(*istream_);
+ out.push(*ostream_);
+
+ copy (*bufferedStream_, out);
- copy (istr, out);
+ obj_->setOStream(*ostream_);
+ ostream_ = 0;
+
+ bufferedStream_.reset();
}
catch (const zlib_error &z_err)
@@ -168,7 +180,7 @@ std::ostream *Compressor::stopCompression(std::istream *istr)
std::cerr << "Zlib compression failed with error code: "
<< z_err.error() << std::endl;
throw z_err;
- }*/
+ }
}
}
View
16 src/file/ISerializable.cpp
@@ -152,13 +152,19 @@ std::ostream * ISerializable::getOStream(void)
//------------------------------------------------------------------------------
-size_t ISerializable::strnlen(const char *str, size_t max_size)
-{
- const char *ptr = 0;
+size_t ISerializable::strnlen(const char *str, size_t maxLen)
+{
+ size_t len = 0;
- for (ptr = str; *ptr != '\0' && max_size-- != 0; ++ptr);
+ for (unsigned int i=0; i < maxLen; i++)
+ {
+ if (str[i] == '\0')
+ return len;
+
+ len ++;
+ }
- return (ptr - str);
+ return maxLen;
}
//------------------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.