~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to source/Irrlicht/CWriteFile.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2002-2011 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
 
 
8
namespace irr
 
9
{
 
10
namespace io
 
11
{
 
12
 
 
13
 
 
14
CWriteFile::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
 
 
27
CWriteFile::~CWriteFile()
 
28
{
 
29
        if (File)
 
30
                fclose(File);
 
31
}
 
32
 
 
33
 
 
34
 
 
35
//! returns if file is open
 
36
inline bool CWriteFile::isOpen() const
 
37
{
 
38
        return File != 0;
 
39
}
 
40
 
 
41
 
 
42
 
 
43
//! returns how much was read
 
44
s32 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
 
57
bool 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.
 
68
long CWriteFile::getPos() const
 
69
{
 
70
        return ftell(File);
 
71
}
 
72
 
 
73
 
 
74
 
 
75
//! opens the file
 
76
void 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
 
103
const io::path& CWriteFile::getFileName() const
 
104
{
 
105
        return Filename;
 
106
}
 
107
 
 
108
 
 
109
 
 
110
IWriteFile* 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