1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 =============================================================================*/
20
21 /* header include */
22 #include "File.h"
23
24 /* includes */
25 #include <cassert>
26
27 #include <physfs.h>
28
29 #include "Global.h"
30 #include "FileSystem.h"
31
32 /* implementation */
33
34
35
File()36 File::File() : mHandle(0)
37 {
38
39 }
40
File(const std::string & filename,OpenMode mode,bool no_override)41 File::File(const std::string& filename, OpenMode mode, bool no_override) : mHandle(0), mFileName("")
42 {
43 open(filename, mode, no_override);
44 }
45
~File()46 File::~File()
47 {
48 // make sure we close this!
49 close();
50 }
51
open(const std::string & filename,OpenMode mode,bool no_override)52 void File::open(const std::string& filename, OpenMode mode, bool no_override)
53 {
54 // check that we don't have anything opened!
55 /// \todo maybe we could just close the old file here... but
56 /// then, this could also lead to errors...
57 assert(mHandle == 0);
58
59 // open depending on mode
60 if( mode == OPEN_WRITE )
61 {
62 if(no_override && FileSystem::getSingleton().exists(filename))
63 {
64 BOOST_THROW_EXCEPTION(FileAlreadyExistsException(filename));
65 }
66
67 mHandle = PHYSFS_openWrite(filename.c_str());
68 }
69 else
70 {
71 mHandle = PHYSFS_openRead(filename.c_str());
72 }
73
74 if (!mHandle)
75 {
76 BOOST_THROW_EXCEPTION(FileLoadException(filename));
77 }
78
79 mFileName = filename;
80 }
81
close()82 void File::close()
83 {
84 // if handle is 0, no file is currently opened, so close does not do anything
85 // maybe we could assert this, but i'm not sure that that is necessary.
86 // we cannot assert this, because this function is run in the destrucor!
87 if(mHandle)
88 {
89 if (PHYSFS_close( reinterpret_cast<PHYSFS_file*> (mHandle) ) )
90 {
91 /// we can't throw an error here, as this function gets called
92 /// in the destructor and therefore might be called while another
93 /// excpetion is active so we cant throw.
94 };
95 mHandle = 0;
96 mFileName = "";
97 }
98 }
99
getPHYSFS_file()100 void* File::getPHYSFS_file()
101 {
102 return mHandle;
103 }
104
is_open() const105 bool File::is_open() const
106 {
107 return mHandle;
108 }
109
length() const110 uint32_t File::length() const
111 {
112 check_file_open();
113
114 PHYSFS_sint64 len = PHYSFS_fileLength( reinterpret_cast<PHYSFS_file*> (mHandle) );
115 if( len == -1 )
116 {
117 BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) );
118 }
119
120 return len;
121 }
122
tell() const123 uint32_t File::tell() const
124 {
125 check_file_open();
126
127 PHYSFS_sint64 tp = PHYSFS_tell( reinterpret_cast<PHYSFS_file*> (mHandle) );
128
129 if(tp == -1)
130 BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) );
131
132 return tp;
133 }
134
getFileName() const135 std::string File::getFileName() const
136 {
137 return mFileName;
138 }
139
seek(uint32_t target)140 void File::seek(uint32_t target)
141 {
142 check_file_open();
143
144 if(!PHYSFS_seek( reinterpret_cast<PHYSFS_file*>(mHandle), target))
145 {
146 BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) );
147 }
148 }
149
150
check_file_open() const151 void File::check_file_open() const
152 {
153 // check that we have a handle
154 if( !mHandle )
155 {
156 BOOST_THROW_EXCEPTION( NoFileOpenedException() );
157 }
158 }
159
160
161