]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CReadFile.cpp
SpriteBank: error check on non existing textureNumber
[irrlicht.git] / source / Irrlicht / CReadFile.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CReadFile.h"\r
6 \r
7 namespace irr\r
8 {\r
9 namespace io\r
10 {\r
11 \r
12 \r
13 CReadFile::CReadFile(const io::path& fileName)\r
14 : File(0), FileSize(0), Filename(fileName)\r
15 {\r
16         #ifdef _DEBUG\r
17         setDebugName("CReadFile");\r
18         #endif\r
19 \r
20         openFile();\r
21 }\r
22 \r
23 \r
24 CReadFile::~CReadFile()\r
25 {\r
26         if (File)\r
27                 fclose(File);\r
28 }\r
29 \r
30 \r
31 //! returns how much was read\r
32 size_t CReadFile::read(void* buffer, size_t sizeToRead)\r
33 {\r
34         if (!isOpen())\r
35                 return 0;\r
36 \r
37         return fread(buffer, 1, sizeToRead, File);\r
38 }\r
39 \r
40 \r
41 //! changes position in file, returns true if successful\r
42 //! if relativeMovement==true, the pos is changed relative to current pos,\r
43 //! otherwise from begin of file\r
44 bool CReadFile::seek(long finalPos, bool relativeMovement)\r
45 {\r
46         if (!isOpen())\r
47                 return false;\r
48 \r
49         return fseek(File, finalPos, relativeMovement ? SEEK_CUR : SEEK_SET) == 0;\r
50 }\r
51 \r
52 \r
53 //! returns size of file\r
54 long CReadFile::getSize() const\r
55 {\r
56         return FileSize;\r
57 }\r
58 \r
59 \r
60 //! returns where in the file we are.\r
61 long CReadFile::getPos() const\r
62 {\r
63         return ftell(File);\r
64 }\r
65 \r
66 \r
67 //! opens the file\r
68 void CReadFile::openFile()\r
69 {\r
70         if (Filename.size() == 0) // bugfix posted by rt\r
71         {\r
72                 File = 0;\r
73                 return;\r
74         }\r
75 \r
76         File = fopen(Filename.c_str(), "rb");\r
77 \r
78         if (File)\r
79         {\r
80                 // get FileSize\r
81 \r
82                 fseek(File, 0, SEEK_END);\r
83                 FileSize = getPos();\r
84                 fseek(File, 0, SEEK_SET);\r
85         }\r
86 }\r
87 \r
88 \r
89 //! returns name of file\r
90 const io::path& CReadFile::getFileName() const\r
91 {\r
92         return Filename;\r
93 }\r
94 \r
95 \r
96 IReadFile* CReadFile::createReadFile(const io::path& fileName)\r
97 {\r
98         CReadFile* file = new CReadFile(fileName);\r
99         if (file->isOpen())\r
100                 return file;\r
101 \r
102         file->drop();\r
103         return 0;\r
104 }\r
105 \r
106 \r
107 } // end namespace io\r
108 } // end namespace irr\r
109 \r