FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / utils / zenutils / source / shared / file.cpp
blobb1b1093170b1a8bc4d830f446f9cd58fd68335f7
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 "file.h"
20 #include <fstream>
23 bool shared::read_file(const std::string& filename, bytes& buffer,
24 std::streampos offset, std::streamsize count)
26 std::ifstream ifs;
27 ifs.open(filename.c_str(), std::ios::binary);
28 if (!ifs)
30 return false;
33 std::ifstream::pos_type startpos = offset;
34 ifs.seekg(offset, std::ios::beg);
35 if (count == -1)
36 ifs.seekg(0, std::ios::end);
37 else
38 ifs.seekg(count, std::ios::cur);
39 std::ifstream::pos_type endpos = ifs.tellg();
41 buffer.resize(endpos-startpos);
42 ifs.seekg(offset, std::ios::beg);
44 ifs.read((char*)&buffer[0], endpos-startpos);
46 ifs.close();
47 return ifs.good();
51 bool shared::write_file(const std::string& filename, bytes& buffer,
52 bool truncate, std::streampos offset,
53 std::streamsize count)
55 std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary;
56 if (truncate)
57 mode |= std::ios::trunc;
59 std::fstream ofs;
60 ofs.open(filename.c_str(), mode);
61 if (!ofs)
63 return false;
66 if (count == -1)
67 count = buffer.size();
68 else if (count > buffer.size())
69 return false;
71 ofs.seekg(offset, std::ios::beg);
73 ofs.write((char*)&buffer[0], count);
75 ofs.close();
76 return ofs.good();
79 bool shared::file_exists(const std::string& filename)
81 std::ifstream ifs;
82 ifs.open(filename.c_str(), std::ios::in);
83 if (ifs.is_open())
85 ifs.close();
86 return true;
88 return false;
91 bool shared::copy_file(const std::string& srcname, const std::string& dstname)
93 bytes buffer;
94 if (!read_file(srcname, buffer))
95 return false;
96 return write_file(dstname, buffer, true);
99 bool shared::backup_file(const std::string& filename, bool force)
101 std::string backupname = filename + ".bak";
102 if (!force)
103 if (file_exists(backupname))
104 return true;
105 return copy_file(filename, backupname);