SWCompress Class Reference

#include <swcomprs.h>

Inheritance diagram for SWCompress:
Inheritance graph
[legend]
Collaboration diagram for SWCompress:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual char * Buf (const char *buf=0, unsigned long *len=0)
virtual void Decode (void)
virtual void Encode (void)
virtual unsigned long GetChars (char *buf, unsigned long len)
virtual unsigned long SendChars (char *buf, unsigned long len)
 SWCompress ()
virtual char * zBuf (unsigned long *len, char *buf=0)
virtual ~SWCompress ()

Protected Attributes

char * buf
char direct
unsigned long pos
unsigned long slen
char * zbuf
unsigned long zlen
unsigned long zpos

Private Member Functions

void cycleStream ()
void Init ()

Detailed Description

Definition at line 29 of file swcomprs.h.


Constructor & Destructor Documentation

SWORD_NAMESPACE_START SWCompress::SWCompress (  ) 

Definition at line 34 of file swcomprs.cpp.

00035 {
00036     buf = zbuf = 0;
00037     Init();
00038 }

SWCompress::~SWCompress (  )  [virtual]

Definition at line 45 of file swcomprs.cpp.

00046 {
00047     if (zbuf)
00048         free(zbuf);
00049 
00050     if (buf)
00051         free(buf);
00052 }


Member Function Documentation

char * SWCompress::Buf ( const char *  buf = 0,
unsigned long *  len = 0 
) [virtual]

Definition at line 73 of file swcomprs.cpp.

00073                                                           {
00074     // setting an uncompressed buffer
00075     if (ibuf) {
00076         Init();
00077         slen = (len) ? *len : strlen(ibuf);
00078         buf = (char *) calloc(slen + 1, 1);
00079         memcpy(buf, ibuf, slen);
00080     }
00081 
00082     // getting an uncompressed buffer
00083     if (!buf) {
00084         buf = (char *)calloc(1,1); // be sure we at least allocate an empty buf for return;
00085         direct = 1;
00086         Decode();
00087 //      slen = strlen(buf);
00088         if (len)
00089             *len = slen;
00090     }
00091     return buf;
00092 }

void SWCompress::cycleStream (  )  [private]

Definition at line 197 of file swcomprs.cpp.

00197                              {
00198     char buf[1024];
00199     unsigned long len, totlen = 0;
00200 
00201     do {
00202         len = GetChars(buf, 1024);
00203         if (len)
00204             totlen += SendChars(buf, len);
00205     } while (len == 1024);
00206 
00207     zlen = slen = totlen;
00208 }

void SWCompress::Decode ( void   )  [virtual]

Reimplemented in LZSSCompress, and ZipCompress.

Definition at line 191 of file swcomprs.cpp.

00192 {
00193     cycleStream();
00194 }

void SWCompress::Encode ( void   )  [virtual]

Reimplemented in LZSSCompress, and ZipCompress.

Definition at line 177 of file swcomprs.cpp.

00178 {
00179     cycleStream();
00180 }

unsigned long SWCompress::GetChars ( char *  buf,
unsigned long  len 
) [virtual]

Definition at line 116 of file swcomprs.cpp.

00117 {
00118     if (direct) {
00119         len = (((zlen - zpos) > (unsigned)len) ? len : zlen - zpos);
00120         if (len > 0) {
00121             memmove(ibuf, &zbuf[zpos], len);
00122             zpos += len;
00123         }
00124     }
00125     else {
00126 //      slen = strlen(buf);
00127         len = (((slen - pos) > (unsigned)len) ? len : slen - pos);
00128         if (len > 0) {
00129             memmove(ibuf, &buf[pos], len);
00130             pos += len;
00131         }
00132     }
00133     return len;
00134 }

void SWCompress::Init (  )  [private]

Definition at line 55 of file swcomprs.cpp.

00056 {
00057         if (buf)
00058             free(buf);
00059 
00060         if (zbuf)
00061             free(zbuf);
00062 
00063         buf    = 0;
00064         zbuf   = 0;
00065         direct  = 0;
00066         zlen    = 0;
00067         slen    = 0;
00068         zpos    = 0;
00069         pos     = 0;
00070 }

unsigned long SWCompress::SendChars ( char *  buf,
unsigned long  len 
) [virtual]

Definition at line 137 of file swcomprs.cpp.

00138 {
00139     if (direct) {
00140         if (buf) {
00141 //          slen = strlen(buf);
00142             if ((pos + len) > (unsigned)slen) {
00143                 buf = (char *) realloc(buf, pos + len + 1024);
00144                 memset(&buf[pos], 0, len + 1024);
00145             }
00146         }
00147         else    buf = (char *)calloc(1, len + 1024);
00148         memmove(&buf[pos], ibuf, len);
00149         pos += len;
00150     }
00151     else {
00152         if (zbuf) {
00153             if ((zpos + len) > zlen) {
00154                 zbuf = (char *) realloc(zbuf, zpos + len + 1024);
00155                 zlen = zpos + len + 1024;
00156             }
00157         }
00158         else {
00159             zbuf = (char *)calloc(1, len + 1024);
00160             zlen = len + 1024;
00161         }
00162         memmove(&zbuf[zpos], ibuf, len);
00163         zpos += len;
00164     }
00165     return len;
00166 }

char * SWCompress::zBuf ( unsigned long *  len,
char *  buf = 0 
) [virtual]

Definition at line 95 of file swcomprs.cpp.

00096 {
00097     // setting a compressed buffer
00098     if (ibuf) {
00099         Init();
00100         zbuf = (char *) malloc(*len);
00101         memcpy(zbuf, ibuf, *len);
00102         zlen = *len;
00103     }
00104 
00105     // getting a compressed buffer
00106     if (!zbuf) {
00107         direct = 0;
00108         Encode();
00109     }
00110 
00111     *len = zlen;
00112     return zbuf;
00113 }


Member Data Documentation

char* SWCompress::buf [protected]

Definition at line 34 of file swcomprs.h.

char SWCompress::direct [protected]

Definition at line 34 of file swcomprs.h.

unsigned long SWCompress::pos [protected]

Definition at line 35 of file swcomprs.h.

unsigned long SWCompress::slen [protected]

Definition at line 35 of file swcomprs.h.

char * SWCompress::zbuf [protected]

Definition at line 34 of file swcomprs.h.

unsigned long SWCompress::zlen [protected]

Definition at line 35 of file swcomprs.h.

unsigned long SWCompress::zpos [protected]

Definition at line 35 of file swcomprs.h.


The documentation for this class was generated from the following files:

Generated on 18 Mar 2013 for The SWORD Project by  doxygen 1.6.1