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
23 bool shared::read_file(const std::string
& filename
, bytes
& buffer
,
24 std::streampos offset
, std::streamsize count
)
27 ifs
.open(filename
.c_str(), std::ios::binary
);
33 std::ifstream::pos_type startpos
= offset
;
34 ifs
.seekg(offset
, std::ios::beg
);
36 ifs
.seekg(0, std::ios::end
);
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
);
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
;
57 mode
|= std::ios::trunc
;
60 ofs
.open(filename
.c_str(), mode
);
67 count
= buffer
.size();
68 else if (count
> buffer
.size())
71 ofs
.seekg(offset
, std::ios::beg
);
73 ofs
.write((char*)&buffer
[0], count
);
79 bool shared::file_exists(const std::string
& filename
)
82 ifs
.open(filename
.c_str(), std::ios::in
);
91 bool shared::copy_file(const std::string
& srcname
, const std::string
& dstname
)
94 if (!read_file(srcname
, buffer
))
96 return write_file(dstname
, buffer
, true);
99 bool shared::backup_file(const std::string
& filename
, bool force
)
101 std::string backupname
= filename
+ ".bak";
103 if (file_exists(backupname
))
105 return copy_file(filename
, backupname
);