/src/ogre/OgreMain/src/OgreFileSystemUtil.h
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef __OgreFileSystemUtil_H__ |
29 | | #define __OgreFileSystemUtil_H__ |
30 | | |
31 | | #include "OgrePrerequisites.h" |
32 | | #include <ctime> |
33 | | #include <fstream> |
34 | | |
35 | | // This header is internal to OgreMain. It is deliberately not installed and |
36 | | // none of its functions are exported, so that the Windows path encoding |
37 | | // conversion stays contained in OgreMain. |
38 | | // |
39 | | // On Windows, Ogre::String is expected to hold UTF-8 encoded paths (see |
40 | | // OGRE_CONFIG_FILESYSTEM_UNICODE). The narrow CRT and std::fstream interfaces |
41 | | // instead interpret their argument using the ANSI code page (CP_ACP), which |
42 | | // mangles any path that is not representable in it. Everything in OgreMain |
43 | | // that touches the filesystem must therefore go through this header (or |
44 | | // through the public Root::openFileStream / Root::createFileStream / |
45 | | // FileSystemLayer wrappers), never through the narrow APIs directly. |
46 | | |
47 | | #if (OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT) && \ |
48 | | OGRE_CONFIG_FILESYSTEM_UNICODE |
49 | | # define OGRE_FILESYSTEM_USE_WIDECHAR 1 |
50 | | #else |
51 | | # define OGRE_FILESYSTEM_USE_WIDECHAR 0 |
52 | | #endif |
53 | | |
54 | | namespace Ogre { |
55 | | namespace FileSystemUtil { |
56 | | |
57 | | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT |
58 | | /** Convert a path to the wide representation used by the Windows |
59 | | filesystem APIs. If OGRE_CONFIG_FILESYSTEM_UNICODE is enabled the input |
60 | | is decoded as UTF-8, otherwise the ANSI code page is used (matching the |
61 | | behaviour of the narrow CRT). |
62 | | */ |
63 | | std::wstring toWide(const String& path); |
64 | | |
65 | | /** Convert a wide path returned by the Windows filesystem APIs to the |
66 | | encoding used by Ogre::String. |
67 | | */ |
68 | | String fromWide(const std::wstring& wide); |
69 | | #endif |
70 | | |
71 | | /** Get the size of a file in bytes. |
72 | | @return 0 if the file does not exist or can not be accessed. |
73 | | */ |
74 | | size_t getFileSize(const String& path); |
75 | | |
76 | | /** Get the time the given file was last modified. |
77 | | @return 0 if the file does not exist or the time can not be read. |
78 | | */ |
79 | | time_t getFileModifiedTime(const String& path); |
80 | | |
81 | | /** Open a std::ofstream with a correctly decoded path. */ |
82 | | inline void openOutputFile(std::ofstream& stream, const String& path, |
83 | | std::ios::openmode mode = std::ios::out) |
84 | 1 | { |
85 | | #if OGRE_FILESYSTEM_USE_WIDECHAR |
86 | | stream.open(toWide(path).c_str(), mode); |
87 | | #else |
88 | 1 | stream.open(path.c_str(), mode); |
89 | 1 | #endif |
90 | 1 | } |
91 | | |
92 | | /** Open a std::ifstream with a correctly decoded path. */ |
93 | | inline void openInputFile(std::ifstream& stream, const String& path, |
94 | | std::ios::openmode mode = std::ios::in) |
95 | 0 | { |
96 | | #if OGRE_FILESYSTEM_USE_WIDECHAR |
97 | | stream.open(toWide(path).c_str(), mode); |
98 | | #else |
99 | 0 | stream.open(path.c_str(), mode); |
100 | 0 | #endif |
101 | 0 | } |
102 | | |
103 | | /** Open a std::fstream with a correctly decoded path. */ |
104 | | inline void openInputOutputFile(std::fstream& stream, const String& path, |
105 | | std::ios::openmode mode) |
106 | 0 | { |
107 | | #if OGRE_FILESYSTEM_USE_WIDECHAR |
108 | | stream.open(toWide(path).c_str(), mode); |
109 | | #else |
110 | 0 | stream.open(path.c_str(), mode); |
111 | 0 | #endif |
112 | 0 | } |
113 | | |
114 | | } // namespace FileSystemUtil |
115 | | } // namespace Ogre |
116 | | |
117 | | #endif |