FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / utils / zenutils / source / shared / utils.cpp
blobfbb223b42b42ad073850af1d3c8875dcbba06ec7
1 /* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "utils.h"
20 #include <fstream>
21 #include <zlib/zlib.h>
24 std::string shared::replace_extension(const std::string& filename, const std::string& extension)
26 std::string newname;
27 const char* name = filename.c_str();
28 const char* ext = strrchr(name, '.');
29 if (ext)
31 // If an extension was found, replace it.
32 newname.assign(name, ext-name);
33 newname += extension;
35 else
37 // If an extension was not found, append it.
38 newname = name;
39 newname += extension;
41 return newname;
44 std::string shared::remove_extension(const std::string& filename)
46 std::string newname;
47 const char* name = filename.c_str();
48 const char* ext = strrchr(name, '.');
49 if (ext)
51 newname.assign(name, ext-name);
53 else
55 newname = name;
57 return newname;
60 std::string shared::double_quote(const std::string& str)
62 std::string out;
63 for (int i = 0, j = str.length(); i < j; i++)
65 if (str[i] == '\\')
66 out += "\\\\";
67 else
68 out += str[i];
70 return out;
73 bool shared::inflate_to_file(const bytes& buffer, const char* filename)
75 // Open output file.
76 std::ofstream ofs;
77 ofs.open(filename, std::ios::binary);
78 if (!ofs)
80 return false;
83 // Initialize zlib.
84 z_stream d_stream; // decompression stream
86 d_stream.zalloc = Z_NULL;
87 d_stream.zfree = Z_NULL;
88 d_stream.opaque = Z_NULL;
90 d_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]);
91 d_stream.avail_in = static_cast<uInt>(buffer.size());
93 int ret = inflateInit(&d_stream);
94 if (ret != Z_OK)
95 return false;
97 // Allocate buffer to hold the inflated data.
98 const size_t BUFSIZE = 1048576;
99 Bytef* infbuf = new Bytef[BUFSIZE];
100 if (!infbuf)
101 return false;
103 // Decompress untill the end of the input buffer.
104 uLong totalout = 0;
105 bool bLoop = true;
106 while (bLoop)
108 d_stream.next_out = infbuf;
109 d_stream.avail_out = BUFSIZE;
111 ret = inflate(&d_stream, Z_NO_FLUSH);
112 if (ret == Z_STREAM_END)
114 bLoop = false;
116 else if (ret != Z_OK)
118 inflateEnd(&d_stream);
119 delete [] infbuf;
120 return false;
123 // Write the inflated data to the output file.
124 if (!ofs.write((const char*)infbuf, d_stream.total_out-totalout))
126 inflateEnd(&d_stream);
127 delete [] infbuf;
128 return false;
130 totalout = d_stream.total_out;
133 // Cleanup and return.
134 inflateEnd(&d_stream);
135 delete [] infbuf;
137 return true;
140 bool shared::deflate_to_file(const bytes& buffer, const char* filename)
142 // Open output file.
143 std::ofstream ofs;
144 ofs.open(filename, std::ios::binary);
145 if (!ofs)
147 return false;
150 // Initialize zlib.
151 z_stream c_stream; // compression stream.
153 c_stream.zalloc = Z_NULL;
154 c_stream.zfree = Z_NULL;
155 c_stream.opaque = Z_NULL;
157 int ret = deflateInit(&c_stream, Z_BEST_COMPRESSION);
158 if (ret != Z_OK)
159 return false;
161 // Allocate buffer to hold the deflated data.
162 const size_t BUFSIZE = 1048576;
163 Bytef* defbuf = new Bytef[BUFSIZE];
164 if (!defbuf)
165 return false;
167 c_stream.avail_in = static_cast<uInt>(buffer.size());
168 c_stream.next_in = const_cast<bytes::value_type*>(&buffer[0]);
170 // Compress until end of the buffer.
171 uLong totalout = 0;
172 bool bLoop = true;
173 while (bLoop)
175 c_stream.avail_out = BUFSIZE;
176 c_stream.next_out = defbuf;
178 ret = deflate(&c_stream, Z_NO_FLUSH); // no bad return value
179 if (ret == Z_STREAM_END)
181 bLoop = false;
183 else if (ret == Z_BUF_ERROR && !c_stream.avail_in)
185 ret = deflate(&c_stream, Z_FINISH); // no bad return value
186 bLoop = false;
188 else if (ret != Z_OK)
190 deflateEnd(&c_stream);
191 delete [] defbuf;
192 return false;
195 // Write the inflated data to the output file.
196 if (!ofs.write((const char*)defbuf, c_stream.total_out-totalout))
198 deflateEnd(&c_stream);
199 delete [] defbuf;
200 return false;
203 totalout = c_stream.total_out;
206 // Clean up and return.
207 deflateEnd(&c_stream);
208 delete [] defbuf;
210 return true;