summaryrefslogtreecommitdiff
path: root/utils/zenutils/source/shared/file.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/zenutils/source/shared/file.cpp')
-rw-r--r--[-rwxr-xr-x]utils/zenutils/source/shared/file.cpp212
1 files changed, 106 insertions, 106 deletions
diff --git a/utils/zenutils/source/shared/file.cpp b/utils/zenutils/source/shared/file.cpp
index 2c31498972..b1b1093170 100755..100644
--- a/utils/zenutils/source/shared/file.cpp
+++ b/utils/zenutils/source/shared/file.cpp
@@ -1,106 +1,106 @@
1/* zenutils - Utilities for working with creative firmwares. 1/* zenutils - Utilities for working with creative firmwares.
2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> 2 * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com>
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 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 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 6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. 7 * (at your option) any later version.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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 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 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */ 17 */
18 18
19#include "file.h" 19#include "file.h"
20#include <fstream> 20#include <fstream>
21 21
22 22
23bool shared::read_file(const std::string& filename, bytes& buffer, 23bool shared::read_file(const std::string& filename, bytes& buffer,
24 std::streampos offset, std::streamsize count) 24 std::streampos offset, std::streamsize count)
25{ 25{
26 std::ifstream ifs; 26 std::ifstream ifs;
27 ifs.open(filename.c_str(), std::ios::binary); 27 ifs.open(filename.c_str(), std::ios::binary);
28 if (!ifs) 28 if (!ifs)
29 { 29 {
30 return false; 30 return false;
31 } 31 }
32 32
33 std::ifstream::pos_type startpos = offset; 33 std::ifstream::pos_type startpos = offset;
34 ifs.seekg(offset, std::ios::beg); 34 ifs.seekg(offset, std::ios::beg);
35 if (count == -1) 35 if (count == -1)
36 ifs.seekg(0, std::ios::end); 36 ifs.seekg(0, std::ios::end);
37 else 37 else
38 ifs.seekg(count, std::ios::cur); 38 ifs.seekg(count, std::ios::cur);
39 std::ifstream::pos_type endpos = ifs.tellg(); 39 std::ifstream::pos_type endpos = ifs.tellg();
40 40
41 buffer.resize(endpos-startpos); 41 buffer.resize(endpos-startpos);
42 ifs.seekg(offset, std::ios::beg); 42 ifs.seekg(offset, std::ios::beg);
43 43
44 ifs.read((char*)&buffer[0], endpos-startpos); 44 ifs.read((char*)&buffer[0], endpos-startpos);
45 45
46 ifs.close(); 46 ifs.close();
47 return ifs.good(); 47 return ifs.good();
48} 48}
49 49
50 50
51bool shared::write_file(const std::string& filename, bytes& buffer, 51bool shared::write_file(const std::string& filename, bytes& buffer,
52 bool truncate, std::streampos offset, 52 bool truncate, std::streampos offset,
53 std::streamsize count) 53 std::streamsize count)
54{ 54{
55 std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary; 55 std::ios::openmode mode = std::ios::in|std::ios::out|std::ios::binary;
56 if (truncate) 56 if (truncate)
57 mode |= std::ios::trunc; 57 mode |= std::ios::trunc;
58 58
59 std::fstream ofs; 59 std::fstream ofs;
60 ofs.open(filename.c_str(), mode); 60 ofs.open(filename.c_str(), mode);
61 if (!ofs) 61 if (!ofs)
62 { 62 {
63 return false; 63 return false;
64 } 64 }
65 65
66 if (count == -1) 66 if (count == -1)
67 count = buffer.size(); 67 count = buffer.size();
68 else if (count > buffer.size()) 68 else if (count > buffer.size())
69 return false; 69 return false;
70 70
71 ofs.seekg(offset, std::ios::beg); 71 ofs.seekg(offset, std::ios::beg);
72 72
73 ofs.write((char*)&buffer[0], count); 73 ofs.write((char*)&buffer[0], count);
74 74
75 ofs.close(); 75 ofs.close();
76 return ofs.good(); 76 return ofs.good();
77} 77}
78 78
79bool shared::file_exists(const std::string& filename) 79bool shared::file_exists(const std::string& filename)
80{ 80{
81 std::ifstream ifs; 81 std::ifstream ifs;
82 ifs.open(filename.c_str(), std::ios::in); 82 ifs.open(filename.c_str(), std::ios::in);
83 if (ifs.is_open()) 83 if (ifs.is_open())
84 { 84 {
85 ifs.close(); 85 ifs.close();
86 return true; 86 return true;
87 } 87 }
88 return false; 88 return false;
89} 89}
90 90
91bool shared::copy_file(const std::string& srcname, const std::string& dstname) 91bool shared::copy_file(const std::string& srcname, const std::string& dstname)
92{ 92{
93 bytes buffer; 93 bytes buffer;
94 if (!read_file(srcname, buffer)) 94 if (!read_file(srcname, buffer))
95 return false; 95 return false;
96 return write_file(dstname, buffer, true); 96 return write_file(dstname, buffer, true);
97} 97}
98 98
99bool shared::backup_file(const std::string& filename, bool force) 99bool shared::backup_file(const std::string& filename, bool force)
100{ 100{
101 std::string backupname = filename + ".bak"; 101 std::string backupname = filename + ".bak";
102 if (!force) 102 if (!force)
103 if (file_exists(backupname)) 103 if (file_exists(backupname))
104 return true; 104 return true;
105 return copy_file(filename, backupname); 105 return copy_file(filename, backupname);
106} 106}