1 /* Copyright (C) 2007 John Whitney
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * Author: John Whitney <jjw@deltup.org>
13 */
14
15 #include <stdlib.h>
16 #include <zlib.h>
17 #include <bzlib.h>
18 #include <string>
19 using namespace std;
20 #include <list>
21 #include "file.h"
22
GZ_IFStream(string fname)23 GZ_IFStream::GZ_IFStream(string fname) {file = gzopen(fname.c_str(), "rb");}
~GZ_IFStream()24 GZ_IFStream::~GZ_IFStream() {if (!bad()) gzclose(file);}
read(void * data,unsigned num)25 unsigned GZ_IFStream::read(void *data, unsigned num) {return gzread(file, data, num);}
26
GZ_OFStream(string fname)27 GZ_OFStream::GZ_OFStream(string fname) {file = gzopen(fname.c_str(), "wb");}
~GZ_OFStream()28 GZ_OFStream::~GZ_OFStream() {gzclose(file);}
write(const void * data,unsigned num)29 unsigned GZ_OFStream::write(const void *data, unsigned num) {return gzwrite(file, (voidp)data, num);}
30
BZ_IFStream(string fname)31 BZ_IFStream::BZ_IFStream(string fname) {file = BZ2_bzopen(fname.c_str(), "rb");}
~BZ_IFStream()32 BZ_IFStream::~BZ_IFStream() {if (!bad()) BZ2_bzclose(file);}
read(void * data,unsigned num)33 unsigned BZ_IFStream::read(void *data, unsigned num) {return BZ2_bzread(file, data, num);}
34
getLenOfFile(string fname)35 unsigned getLenOfFile(string fname) {
36 FILE *f = fopen(fname.c_str(), "rb");
37 fseek(f, 0, SEEK_END);
38 unsigned len = ftell(f);
39 fclose(f);
40 return len;
41 }
42
fileExists(string fname)43 bool fileExists(string fname) {
44 FILE *f = fopen(fname.c_str(), "rb");
45 bool exists = (f!=NULL);
46 if (exists) fclose(f);
47 return exists;
48 }
49
read_word(IStream & f)50 unsigned read_word(IStream &f) {
51 unsigned char b, b2;
52 f.get(b);
53 f.get(b2);
54 return (b2<<8)+b;
55 }
read_dword(IStream & f)56 unsigned read_dword(IStream &f) {
57 unsigned low = read_word(f);
58 return (read_word(f)<<16)+low;
59 }
60
write_word(OStream & f,unsigned number)61 void write_word(OStream &f, unsigned number) {
62 unsigned char b = number&255;
63 f.put(b);
64 b = number>>8;
65 f.put(b);
66 }
67
write_dword(OStream & f,unsigned number)68 void write_dword(OStream &f, unsigned number) {
69 write_word(f, number&65535);
70 write_word(f, number>>16);
71 }
72
73 //returns true if all bytes were successfully copied
copy_bytes_to_file(IStream & infile,OStream & outfile,unsigned numleft)74 bool copy_bytes_to_file(IStream &infile, OStream &outfile, unsigned numleft) {
75 unsigned numread;
76 do {
77 char buf[1024];
78 numread = infile.read(buf, numleft>1024?1024:numleft);
79 if (outfile.write(buf, numread) != numread)
80 return false;
81 //error("Could not write temporary data. Possibly out of space");
82 numleft-=numread;
83 } while (numleft && !(numread < 1024 && numleft));
84 return (numleft==0);
85 }
86
read_filename(IStream & f)87 char *read_filename(IStream &f) {
88 unsigned fnamelen = read_word(f);
89 char *f1 = (char*)malloc(fnamelen+1);
90 f.read(f1, fnamelen);
91 f1[fnamelen] = 0;
92 return f1;
93 }
94
write_filename(OFStream & f,string fname)95 void write_filename(OFStream &f, string fname) {
96 unsigned lenName = fname.size();
97 write_word(f, lenName);
98 f.write(fname.c_str(), lenName);
99 }
100