Mercurial > audlegacy-plugins
comparison src/console/Gzip_File.cxx @ 12:3da1b8942b8b trunk
[svn] - remove src/Input src/Output src/Effect src/General src/Visualization src/Container
author | nenolod |
---|---|
date | Mon, 18 Sep 2006 03:14:20 -0700 |
parents | src/Input/console/Gzip_File.cxx@13389e613d67 |
children |
comparison
equal
deleted
inserted
replaced
11:cff1d04026ae | 12:3da1b8942b8b |
---|---|
1 | |
2 #include "Gzip_File.h" | |
3 | |
4 #include "zlib.h" | |
5 | |
6 /* Copyright (C) 2005 Shay Green. Permission is hereby granted, free of | |
7 charge, to any person obtaining a copy of this software module and associated | |
8 documentation files (the "Software"), to deal in the Software without | |
9 restriction, including without limitation the rights to use, copy, modify, | |
10 merge, publish, distribute, sublicense, and/or sell copies of the Software, and | |
11 to permit persons to whom the Software is furnished to do so, subject to the | |
12 following conditions: The above copyright notice and this permission notice | |
13 shall be included in all copies or substantial portions of the Software. THE | |
14 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
17 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
18 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | |
20 | |
21 static const char* get_gzip_eof( FILE* file, long* eof ) | |
22 { | |
23 unsigned char buf [4]; | |
24 if ( !fread( buf, 2, 1, file ) ) | |
25 return "Couldn't read from file"; | |
26 | |
27 if ( buf [0] == 0x1F && buf [1] == 0x8B ) | |
28 { | |
29 if ( fseek( file, -4, SEEK_END ) ) | |
30 return "Couldn't seek in file"; | |
31 | |
32 if ( !fread( buf, 4, 1, file ) ) | |
33 return "Couldn't read from file"; | |
34 | |
35 *eof = buf [3] * 0x1000000L + buf [2] * 0x10000L + buf [1] * 0x100L + buf [0]; | |
36 } | |
37 else | |
38 { | |
39 if ( fseek( file, 0, SEEK_END ) ) | |
40 return "Couldn't seek in file"; | |
41 | |
42 *eof = ftell( file ); | |
43 } | |
44 | |
45 return NULL; | |
46 } | |
47 | |
48 const char* get_gzip_eof( const char* path, long* eof ) | |
49 { | |
50 FILE* file = fopen( path, "rb" ); | |
51 if ( !file ) | |
52 return "Couldn't open file"; | |
53 const char* error = get_gzip_eof( file, eof ); | |
54 fclose( file ); | |
55 return error; | |
56 } | |
57 | |
58 // Gzip_File_Reader | |
59 | |
60 Gzip_File_Reader::Gzip_File_Reader() : file_( NULL ) | |
61 { | |
62 } | |
63 | |
64 Gzip_File_Reader::~Gzip_File_Reader() | |
65 { | |
66 close(); | |
67 } | |
68 | |
69 Gzip_File_Reader::error_t Gzip_File_Reader::open( const char* path ) | |
70 { | |
71 error_t error = get_gzip_eof( path, &size_ ); | |
72 if ( error ) | |
73 return error; | |
74 | |
75 file_ = gzopen( path, "rb" ); | |
76 if ( !file_ ) | |
77 return "Couldn't open file"; | |
78 | |
79 return NULL; | |
80 } | |
81 | |
82 long Gzip_File_Reader::size() const | |
83 { | |
84 return size_; | |
85 } | |
86 | |
87 long Gzip_File_Reader::read_avail( void* p, long s ) | |
88 { | |
89 return (long) gzread( file_, p, s ); | |
90 } | |
91 | |
92 long Gzip_File_Reader::tell() const | |
93 { | |
94 return gztell( file_ ); | |
95 } | |
96 | |
97 Gzip_File_Reader::error_t Gzip_File_Reader::seek( long n ) | |
98 { | |
99 if ( gzseek( file_, n, SEEK_SET ) < 0 ) | |
100 return "Error seeking in file"; | |
101 return NULL; | |
102 } | |
103 | |
104 void Gzip_File_Reader::close() | |
105 { | |
106 if ( file_ ) | |
107 { | |
108 gzclose( file_ ); | |
109 file_ = NULL; | |
110 } | |
111 } | |
112 | |
113 // Gzip_File_Writer | |
114 | |
115 Gzip_File_Writer::Gzip_File_Writer() : file_( NULL ) | |
116 { | |
117 } | |
118 | |
119 Gzip_File_Writer::~Gzip_File_Writer() | |
120 { | |
121 close(); | |
122 } | |
123 | |
124 Gzip_File_Writer::error_t Gzip_File_Writer::open( const char* path ) | |
125 { | |
126 file_ = gzopen( path, "wb9" ); | |
127 if ( !file_ ) | |
128 return "Couldn't open file for writing"; | |
129 | |
130 return NULL; | |
131 } | |
132 | |
133 Gzip_File_Writer::error_t Gzip_File_Writer::write( const void* p, long s ) | |
134 { | |
135 long result = (long) gzwrite( file_ , (void*) p, s ); | |
136 if ( result != s ) | |
137 return "Couldn't write to file"; | |
138 return NULL; | |
139 } | |
140 | |
141 void Gzip_File_Writer::close() | |
142 { | |
143 if ( file_ ) | |
144 { | |
145 gzclose( file_ ); | |
146 file_ = NULL; | |
147 } | |
148 } |