Skip to content

Commit

Permalink
merge dreamcast-experimental, add fastlz compression to datatree
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcliffe committed Jan 2, 2013
1 parent fb84678 commit 2f833c6
Show file tree
Hide file tree
Showing 5 changed files with 1,037 additions and 156 deletions.
67 changes: 60 additions & 7 deletions cubicvr/include/CubicVR/datatree/DataTree.h
Expand Up @@ -37,6 +37,7 @@
#include <CubicVR/XYZ.h>
#include <CubicVR/map_string.h>
#include <CubicVR/tinyxml/tinyxml.h>
#include <CubicVR/fastlz/fastlz.h>


using namespace std;
Expand Down Expand Up @@ -90,8 +91,8 @@ class DataInvalidChildException : public DataException
class DataElement
{
private:
short int data_type;
long data_size;
unsigned char data_type;
unsigned int data_size;

char *data_val;

Expand Down Expand Up @@ -165,6 +166,7 @@ class DataElement
class DataNode
{
private:
DataNode *parentNode;
vector<DataNode *> children;
map<string, vector<DataNode *>, string_less> childmap;
map<string, unsigned int, string_less> childmap_ptr;
Expand All @@ -182,7 +184,10 @@ class DataNode

void setName(const char *name_in);
string &getName() { return node_name; }


DataNode *getParentNode() { return parentNode; };
void setParentNode(DataNode &parentNode_in) { parentNode = &parentNode_in; };

int numChildren(); /* Number of children */
int numChildren(const char *name_in); /* Number of children named 'name_in' */

Expand All @@ -192,15 +197,58 @@ class DataNode
DataNode &child(const char *name_in, int index = 0) throw (DataInvalidChildException);
DataNode &child(int index) throw (DataInvalidChildException);


bool hasAnother(const char *name_in); /* useful for while() loops in conjunction with getNext() */
bool hasAnother();
DataNode &getNext(const char *name_in) throw (DataInvalidChildException); /* get next of specified name */
DataNode &getNext() throw (DataInvalidChildException); /* get next child */
void rewind(const char *name_in); /* rewind specific */
void rewind(); /* rewind generic */

void findAll(const char *name_in, vector<DataNode *> &node_list_out);

operator string () { string s; element().get(s); return s; }
operator int () { int v; element().get(v); return v; }
operator long () { long v; element().get(v); return v; }
operator float () { float v; element().get(v); return v; }
operator double () { double v; element().get(v); return v; }

operator vector<int> () { vector<int> v; element().get(v); return v; }
operator vector<long> () { vector<long> v; element().get(v); return v; }
operator vector<float> () { vector<float> v; element().get(v); return v; }
operator vector<double> () { vector<double> v; element().get(v); return v; }

const string &operator= (const string &s) { element().set(s); return s; }

int operator= (int i) { element().set(i); return i; }
long operator= (long i) { element().set(i); return i; }
float operator= (float i) { element().set(i); return i; }
double operator= (double i) { element().set(i); return i; }


vector<int> &operator= (vector<int> &v) { element().set(v); return v; }
vector<long> &operator= (vector<long> &v) { element().set(v); return v; }
vector<float> &operator= (vector<float> &v) { element().set(v); return v; }
vector<double> &operator= (vector<double> &v) { element().set(v); return v; }

DataNode &operator[] (const char *name_in) { return getNext(name_in); }
DataNode &operator[] (int idx) { return child(idx); }

bool operator() (const char *name_in) { return hasAnother(name_in); }
bool operator() () { return hasAnother(); }

DataNode &operator ^(const char *name_in) { return newChild(name_in); }

};


typedef vector<DataNode *> DataNodeList;

enum DT_FloatingPointPolicy {
USE_FLOAT,
USE_DOUBLE
};

class DataTree
{
private:
Expand All @@ -209,21 +257,26 @@ class DataTree
public:
DataTree(const char *name_in);
DataTree();
~DataTree();
~DataTree();

DataNode &rootNode();

void nodeToXML(DataNode *elem, TiXmlElement *elxml);
void setFromXML(DataNode *elem, TiXmlNode *elxml, bool root_node=true);
void setFromXML(DataNode *elem, TiXmlNode *elxml, bool root_node=true, DT_FloatingPointPolicy fpp=USE_FLOAT);
void decodeXMLText(DataNode *elem, const char *in_text, DT_FloatingPointPolicy fpp);

void printXML(); /* print datatree as XML */
long getSerializedSize(DataElement &de_node_names, bool debug=false); /* get serialized size + return node names header */
long getSerialized(char **ser_str, bool debug=false);
void setSerialized(char *ser_str, bool debug=false);

bool LoadFromFileXML(const std::string& filename);
bool LoadFromFileXML(const std::string& filename, DT_FloatingPointPolicy fpp=USE_FLOAT);
bool SaveToFileXML(const std::string& filename);
bool SaveToFile(const std::string& filename);

// bool SaveToFile(const std::string& filename);
// bool LoadFromFile(const std::string& filename);

bool SaveToFile(const std::string& filename, bool compress = true, int compress_level = 2);
bool LoadFromFile(const std::string& filename);
};

Expand Down
100 changes: 100 additions & 0 deletions cubicvr/include/CubicVR/fastlz/fastlz.h
@@ -0,0 +1,100 @@
/*
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.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
AUTHORS OR COPYRIGHT HOLDERS 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 FASTLZ_H
#define FASTLZ_H

#define FASTLZ_VERSION 0x000100

#define FASTLZ_VERSION_MAJOR 0
#define FASTLZ_VERSION_MINOR 0
#define FASTLZ_VERSION_REVISION 0

#define FASTLZ_VERSION_STRING "0.1.0"

#if defined (__cplusplus)
extern "C" {
#endif

/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
*/

int fastlz_compress(const void* input, int length, void* output);

/**
Decompress a block of compressed data and returns the size of the
decompressed block. If error occurs, e.g. the compressed data is
corrupted or the output buffer is not large enough, then 0 (zero)
will be returned instead.
The input buffer and the output buffer can not overlap.
Decompression is memory safe and guaranteed not to write the output buffer
more than what is specified in maxout.
*/

int fastlz_decompress(const void* input, int length, void* output, int maxout);

/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
Compression level can be specified in parameter level. At the moment,
only level 1 and level 2 are supported.
Level 1 is the fastest compression and generally useful for short data.
Level 2 is slightly slower but it gives better compression ratio.
Note that the compressed data, regardless of the level, can always be
decompressed using the function fastlz_decompress above.
*/

int fastlz_compress_level(int level, const void* input, int length, void* output);

#if defined (__cplusplus)
}
#endif

#endif /* FASTLZ_H */

0 comments on commit 2f833c6

Please sign in to comment.