pingus-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Compression support


From: Erik Søe Sørensen
Subject: Compression support
Date: Tue, 13 Apr 2004 15:22:16 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040121

Jonas Bähr wrote:
> Am Sonntag, 11. April 2004 11:32 schrieb David Philippi:
>>Do you really believe that it hurts to have the uncompressed on disk?
> storing on disk is not the problem...

Disk space may be cheap these days - but for individual users it may still be a problem. Right now the level may only constitute 16% of the installed size, but when the planned levels are there this will rise to a significant part of the space (50%+).

David Philippi wrote:
Am Mo, den 12.04.2004 schrieb Jonas Bähr um 23:04:

An easy alternative could be to write a little shell-script (ex: 'pingusgz' or so) which pipes the recived file throgh gzip and calls pingus afterwards. So you only need to tell your browser to open .pingusgz with this script instead of pingus itself....

Yes, that's a viable solution. I didn't think of .pingusgz and
.pingus.gz doesn't help you very much. We surely could even add support
for something like detecting wheter a .pingus level is gzipped or plain
text but I don't have the time to code such a thing at the moment. Or to
put it into other words, I don't see it as important enough as to take
the time.

I did (and this is my last vacation day :)
And it so happens that zlib already has that detection ("if this file has as GZ header, then treat it as a gzipped file, otherwise treat it as a raw file"). No shell stuff needed (an even easier alternative might have been to create a pipe and run system('gzip -c <name-of-file>') so that the main program could read the output. I don't understand why the libxml interface doesn't have a parse-from-FILE* function?).

My 40 lines (new file) and a patch attached - C++-code, that is.
Enjoy - and proof-read, bitte!

/Erik

Index: src/Makefile.am
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/Makefile.am,v
retrieving revision 1.153
diff -r1.153 Makefile.am
300a301,302
> xmlgz_reader.hxx \
> xmlgz_reader.cxx \
Index: src/xml_plf.cxx
===================================================================
RCS file: /var/lib/cvs/Games/Pingus/src/xml_plf.cxx,v
retrieving revision 1.38
diff -r1.38 xml_plf.cxx
21a22
> #include "xmlgz_reader.hxx"
40c41
<   doc = xmlParseFile(filename.c_str());
---
>   doc = xmlgzParseFile(filename.c_str());
/*  $Id: xml_plf.cxx,v 1.38 2003/04/19 10:23:17 torangan Exp $
 *
 *  Pingus - A free Lemmings clone
 *  Copyright (C) 2000 Ingo Ruhnke <address@hidden>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "xmlgz_reader.hxx"
#include <iostream>
#include <stdio.h>
#include <zlib.h>

using namespace std;

/** This file implements parsing of gzipped XML files.
 *  The main function for this is  xmlgzParseFile(),
 *  which should behave like libxml's  xmlParseFile() except that
 *  it is capable of handling both gzipped and raw XML.
 */

xmlDocPtr xmlgzParseFile(const char * filename) {
    gzFile in = NULL;
    char* buf = NULL; 
    xmlDocPtr doc = NULL;
    try {
        // 1: Open file.
        in = gzopen(filename, "rb"); // Works for both gzipped and raw files.
        
        if (!in) throw "Cannot open file.";
    
        // 2: Read file:
        int length;
        {
            int bufsize = 8192; // For starters.
            buf = (char*) malloc(bufsize);
            if (!buf) throw "Allocation of buffer memory failed.";
            int pos=0, status;
            while (1) {
                status = gzread(in, buf+pos, bufsize-pos);
                if (status <= 0) break; // Error or EOF
                pos += status;
                if (pos==bufsize) {
                    int newbufsize = bufsize*2;
                    buf = (char*) realloc(buf, newbufsize);     
                    if (!buf) throw "Expansion of buffer memory failed.";
                    bufsize = newbufsize;
                }
            }
            if (status < 0) throw "Uncompression error.";
            length = pos;
        }

        // 3: Parse:
        doc = xmlParseMemory(buf, length);
    } catch (const char* str) {
        cout << "Reading the file " << filename << " failed: " << str << endl;
    }

    // Finally, clean up:
    if (buf) free(buf);
    if (in) gzclose(in);
    return doc;
}

/*  $Id: xml_plf.cxx,v 1.38 2003/04/19 10:23:17 torangan Exp $
 *
 *  Pingus - A free Lemmings clone
 *  Copyright (C) 2000 Ingo Ruhnke <address@hidden>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifndef HEADER_PINGUS_XMLGZ_READER_HXX
#define HEADER_PINGUS_XMLGZ_READER_HXX

#include "xml_helper.hxx"

xmlDocPtr xmlgzParseFile(const char * filename);

#endif // Double-inclusion protection

/* EOF */


reply via email to

[Prev in Thread] Current Thread [Next in Thread]