aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp
new file mode 100644
index 0000000..f72ef38
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CWriteFile.cpp
@@ -0,0 +1,123 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "CWriteFile.h"
6#include <stdio.h>
7
8namespace irr
9{
10namespace io
11{
12
13
14CWriteFile::CWriteFile(const io::path& fileName, bool append)
15: FileSize(0)
16{
17 #ifdef _DEBUG
18 setDebugName("CWriteFile");
19 #endif
20
21 Filename = fileName;
22 openFile(append);
23}
24
25
26
27CWriteFile::~CWriteFile()
28{
29 if (File)
30 fclose(File);
31}
32
33
34
35//! returns if file is open
36inline bool CWriteFile::isOpen() const
37{
38 return File != 0;
39}
40
41
42
43//! returns how much was read
44s32 CWriteFile::write(const void* buffer, u32 sizeToWrite)
45{
46 if (!isOpen())
47 return 0;
48
49 return (s32)fwrite(buffer, 1, sizeToWrite, File);
50}
51
52
53
54//! changes position in file, returns true if successful
55//! if relativeMovement==true, the pos is changed relative to current pos,
56//! otherwise from begin of file
57bool CWriteFile::seek(long finalPos, bool relativeMovement)
58{
59 if (!isOpen())
60 return false;
61
62 return fseek(File, finalPos, relativeMovement ? SEEK_CUR : SEEK_SET) == 0;
63}
64
65
66
67//! returns where in the file we are.
68long CWriteFile::getPos() const
69{
70 return ftell(File);
71}
72
73
74
75//! opens the file
76void CWriteFile::openFile(bool append)
77{
78 if (Filename.size() == 0)
79 {
80 File = 0;
81 return;
82 }
83
84#if defined(_IRR_WCHAR_FILESYSTEM)
85 File = _wfopen(Filename.c_str(), append ? L"ab" : L"wb");
86#else
87 File = fopen(Filename.c_str(), append ? "ab" : "wb");
88#endif
89
90 if (File)
91 {
92 // get FileSize
93
94 fseek(File, 0, SEEK_END);
95 FileSize = ftell(File);
96 fseek(File, 0, SEEK_SET);
97 }
98}
99
100
101
102//! returns name of file
103const io::path& CWriteFile::getFileName() const
104{
105 return Filename;
106}
107
108
109
110IWriteFile* createWriteFile(const io::path& fileName, bool append)
111{
112 CWriteFile* file = new CWriteFile(fileName, append);
113 if (file->isOpen())
114 return file;
115
116 file->drop();
117 return 0;
118}
119
120
121} // end namespace io
122} // end namespace irr
123