Coverage Report

Created: 2026-10-28 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreFileSystemLayer.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 __FileSystemLayer_H__
29
#define __FileSystemLayer_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreStringVector.h"
33
#include "OgreHeaderPrefix.h"
34
35
namespace Ogre
36
{
37
    /** Provides methods to find out where the Ogre config files are stored
38
        and where logs and settings files should be written to.
39
40
        In modern multi-user OS, a standard user account will often not
41
        have write access to the path where the application is stored.
42
        In order to still be able to store graphics settings and log
43
        output and for the user to overwrite the default Ogre config files,
44
        this class tries to create a folder inside the user's home directory.
45
46
        @note All paths returned by this class are encoded as UTF-8, even on
47
        Windows, and can be passed to Root::openFileStream,
48
        Root::createFileStream and the static file operations below.
49
    */
50
    class _OgreExport FileSystemLayer : public FileSystemLayerAlloc
51
    {
52
    public:
53
        /** Creates a concrete platform-dependent implementation of FileSystemLayer.
54
         @param subdir
55
         A subdirectory inside the user's path to distinguish between
56
         different Ogre applications.
57
         */
58
        FileSystemLayer(const Ogre::String& subdir)
59
0
        {
60
0
            // determine directories to search for config files
61
0
            getConfigPaths();
62
0
            // prepare write location in user directory
63
0
            prepareUserHome(subdir);
64
0
        }
65
        
66
        /** Search for the given config file in a set of predefined locations
67
68
         The search order is
69
         1. Subdirectory in user Home (see @ref getWritablePath)
70
         2. OS dependent config-paths
71
         3. Current working directory
72
73
         @param filename The config file name (without path)
74
         @return The full path to the config file
75
         */
76
        Ogre::String getConfigFilePath(Ogre::String filename) const
77
0
        {
78
0
            // look for the requested file in several locations:
79
0
            
80
0
            // 1. in the writable path (so user can provide custom files)
81
0
            Ogre::String path = getWritablePath(filename);
82
0
            if (fileExists(path))
83
0
                return path;
84
0
            
85
0
            // 2. in the config file search paths
86
0
            for (const String& cpath : mConfigPaths)
87
0
            {
88
0
                path = cpath + filename;
89
0
                if (fileExists(path))
90
0
                    return path;
91
0
            }
92
0
            
93
0
            // 3. fallback to current working dir
94
0
            return filename;
95
0
        }
96
97
        /** Find a path where the given filename can be written to. This path
98
         will usually be a subdirectory in the user's home directory.
99
         This function should be used for any output like logs and graphics settings.
100
101
         | Platform         | Location |
102
         |------------------|----------|
103
         | Windows          | Documents/$subdir/ |
104
         | Linux            | ~/.cache/$subdir/ |
105
         | OSX              | ~/Library/Application Support/$subdir/ |
106
         | iOS              | NSDocumentDirectory |
107
         | Android / Emscripten | n/a |
108
109
         @param filename Name of the file.
110
         @return The full path to a writable location for the given filename.
111
         */
112
        Ogre::String getWritablePath(const Ogre::String& filename) const
113
0
        {
114
0
            return mHomePath + filename;
115
0
        }
116
        
117
0
        void setConfigPaths(const Ogre::StringVector &paths){
118
0
            mConfigPaths = paths;
119
0
        }
120
        
121
0
        void setHomePath(const Ogre::String &path){
122
0
            mHomePath = path;
123
0
        }
124
        
125
        /** Resolve path inside the application bundle
126
         * on some platforms Ogre is delivered as an application bundle
127
         * this function resolves the given path such that it points inside that bundle
128
         * @param path
129
         * @return path inside the bundle
130
         */
131
        static String resolveBundlePath(String path);
132
133
        /** Create a directory. */
134
        static bool createDirectory(const Ogre::String& name);
135
        /** Delete a directory. Should be empty */
136
        static bool removeDirectory(const Ogre::String& name);
137
        /** Test if the given file exists. */
138
        static bool fileExists(const Ogre::String& path);
139
        /** Delete a file. */
140
        static bool removeFile(const Ogre::String& path);
141
        /** Rename a file. */
142
        static bool renameFile(const Ogre::String& oldpath, const Ogre::String& newpath);
143
144
    private:
145
        Ogre::StringVector mConfigPaths;
146
        Ogre::String mHomePath;
147
        
148
        /** Determine config search paths. */
149
        void getConfigPaths();
150
        
151
        /** Create an Ogre directory and the given subdir in the user's home. */
152
        void prepareUserHome(const Ogre::String& subdir);
153
    };
154
155
}
156
157
#include "OgreHeaderSuffix.h"
158
159
#endif