xoreos  0.0.5
writefile.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/writefile.h"
28 #include "src/common/error.h"
29 #include "src/common/ustring.h"
30 #include "src/common/platform.h"
31 #include "src/common/filepath.h"
32 
33 namespace Common {
34 
35 WriteFile::WriteFile() : _handle(0), _size(0) {
36 }
37 
38 WriteFile::WriteFile(const UString &fileName) : _handle(0), _size(0) {
39  if (!open(fileName))
40  throw Exception("Can't open file \"%s\" for writing", fileName.c_str());
41 }
42 
44  try {
45  close();
46  } catch (...) {
47  }
48 }
49 
50 bool WriteFile::open(const UString &fileName) {
51  close();
52 
53  UString path = FilePath::normalize(fileName);
54  if (path.empty())
55  return false;
56 
57  try {
59  } catch (...) {
60  return false;
61  }
62 
64  return false;
65 
66  return true;
67 }
68 
70  flush();
71 
72  if (_handle)
73  std::fclose(_handle);
74 
75  _handle = 0;
76  _size = 0;
77 }
78 
79 bool WriteFile::isOpen() const {
80  return _handle != 0;
81 }
82 
84  if (!_handle)
85  return;
86 
87  if (std::fflush(_handle) != 0)
88  throw Exception(kWriteError);
89 }
90 
91 size_t WriteFile::write(const void *dataPtr, size_t dataSize) {
92  if (!_handle)
93  return 0;
94 
95  assert(dataPtr);
96 
97  const ptrdiff_t oldPos = pos();
98  const ptrdiff_t written = std::fwrite(dataPtr, 1, dataSize, _handle);
99  _size += MAX<ptrdiff_t>(0, -static_cast<ptrdiff_t>(size()) + oldPos + written);
100 
101  return written;
102 }
103 
104 size_t WriteFile::size() const {
105  return _size;
106 }
107 
108 size_t WriteFile::pos() const {
109  return std::ftell(_handle);
110 }
111 
112 size_t WriteFile::seek(ptrdiff_t offset, SeekableWriteStream::Origin whence) {
113  const size_t oldPos = pos();
114  const size_t newPos = evalSeek(offset, whence, pos(), 0, size());
115 
116  if (newPos > _size)
117  throw Exception(kSeekError);
118 
119  if (std::fseek(_handle, newPos, SEEK_SET))
120  throw Exception(kSeekError);
121 
122  return oldPos;
123 }
124 
125 } // End of namespace Common
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
Definition: writefile.cpp:91
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
static size_t evalSeek(ptrdiff_t offset, Origin whence, size_t pos, size_t begin, size_t size)
Evaluate the seek offset relative to whence into a position from the beginning.
std::FILE * _handle
The actual file handle.
Definition: writefile.h:76
Platform-dependant functions, mostly for internal use in the Common namespace.
Basic exceptions to throw.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
bool open(const UString &fileName)
Try to open the file with the given fileName.
Definition: writefile.cpp:50
bool isOpen() const
Checks if the object opened a file successfully.
Definition: writefile.cpp:79
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
static std::FILE * openFile(const UString &fileName, FileMode mode)
Open a file with an UTF-8 encoded name.
Definition: platform.cpp:102
const Exception kWriteError("Write error")
Exception when writing to a stream failed.
Definition: error.h:64
StackException Exception
Definition: error.h:59
static UString getDirectory(const UString &p)
Return a path&#39;s directory.
Definition: filepath.cpp:107
size_t seek(ptrdiff_t offset, Origin whence=SeekableWriteStream::kOriginBegin)
Seek to the speciied offset from the specified origin.
Definition: writefile.cpp:112
void flush()
Commit any buffered data to the underlying channel or storage medium; unbuffered streams can use the ...
Definition: writefile.cpp:83
Unicode string handling.
void close()
Close the file, if open.
Definition: writefile.cpp:69
const Exception kSeekError("Seek error")
Exception when seeking a stream failed.
Definition: error.h:63
static bool createDirectories(const UString &path)
Create all directories in this path.
Definition: filepath.cpp:342
static UString normalize(const UString &p, bool resolveSymLinks=true)
Normalize a path.
Definition: filepath.cpp:154
Origin
The position a seeking offset takes as a base.
Definition: writestream.h:218
Implementing the stream writing interfaces for files.
size_t size() const
Return the number of bytes written to the current file in total.
Definition: writefile.cpp:104
Utility class for manipulating file paths.
size_t pos() const
Return the current position ot the stream in the file.
Definition: writefile.cpp:108