/src/proj/src/filemanager.hpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * Project: PROJ |
3 | | * Purpose: File manager |
4 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
5 | | * |
6 | | ****************************************************************************** |
7 | | * Copyright (c) 2019, Even Rouault, <even.rouault at spatialys.com> |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | | * copy of this software and associated documentation files (the "Software"), |
11 | | * to deal in the Software without restriction, including without limitation |
12 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
13 | | * and/or sell copies of the Software, and to permit persons to whom the |
14 | | * Software is furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included |
17 | | * in all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
20 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
25 | | * DEALINGS IN THE SOFTWARE. |
26 | | *****************************************************************************/ |
27 | | |
28 | | #ifndef FILEMANAGER_HPP_INCLUDED |
29 | | #define FILEMANAGER_HPP_INCLUDED |
30 | | |
31 | | #include <memory> |
32 | | #include <string> |
33 | | #include <vector> |
34 | | |
35 | | #include "proj.h" |
36 | | #include "proj/util.hpp" |
37 | | |
38 | | //! @cond Doxygen_Suppress |
39 | | |
40 | | NS_PROJ_START |
41 | | |
42 | | class File; |
43 | | |
44 | | enum class FileAccess { |
45 | | READ_ONLY, // "rb" |
46 | | READ_UPDATE, // "r+b" |
47 | | CREATE, // "w+b" |
48 | | }; |
49 | | |
50 | | class FileManager { |
51 | | private: |
52 | | FileManager() = delete; |
53 | | |
54 | | public: |
55 | | // "Low-level" interface. |
56 | | static PROJ_DLL std::unique_ptr<File> |
57 | | open(PJ_CONTEXT *ctx, const char *filename, FileAccess access); |
58 | | static PROJ_DLL bool exists(PJ_CONTEXT *ctx, const char *filename); |
59 | | static bool mkdir(PJ_CONTEXT *ctx, const char *filename); |
60 | | static bool unlink(PJ_CONTEXT *ctx, const char *filename); |
61 | | static bool rename(PJ_CONTEXT *ctx, const char *oldPath, |
62 | | const char *newPath); |
63 | | static std::string getProjDataEnvVar(PJ_CONTEXT *ctx); |
64 | | |
65 | | // "High-level" interface, honoring PROJ_DATA and the like. |
66 | | static std::unique_ptr<File> |
67 | | open_resource_file(PJ_CONTEXT *ctx, const char *name, |
68 | | char *out_full_filename = nullptr, |
69 | | size_t out_full_filename_size = 0); |
70 | | |
71 | | static void fillDefaultNetworkInterface(PJ_CONTEXT *ctx); |
72 | | |
73 | | static void clearMemoryCache(); |
74 | | }; |
75 | | |
76 | | // --------------------------------------------------------------------------- |
77 | | |
78 | | class File { |
79 | | protected: |
80 | | std::string name_; |
81 | | std::string readLineBuffer_{}; |
82 | | bool eofReadLine_ = false; |
83 | | explicit File(const std::string &filename); |
84 | | |
85 | | public: |
86 | | virtual PROJ_DLL ~File(); |
87 | | virtual size_t read(void *buffer, size_t sizeBytes) = 0; |
88 | | virtual size_t write(const void *buffer, size_t sizeBytes) = 0; |
89 | | virtual bool seek(unsigned long long offset, int whence = SEEK_SET) = 0; |
90 | | virtual unsigned long long tell() = 0; |
91 | | virtual void reassign_context(PJ_CONTEXT *ctx) = 0; |
92 | | virtual bool hasChanged() const = 0; |
93 | | std::string PROJ_DLL read_line(size_t maxLen, bool &maxLenReached, |
94 | | bool &eofReached); |
95 | | |
96 | 4.35k | const std::string &name() const { return name_; } |
97 | | }; |
98 | | |
99 | | // --------------------------------------------------------------------------- |
100 | | |
101 | | std::unique_ptr<File> pj_network_file_open(PJ_CONTEXT *ctx, |
102 | | const char *filename); |
103 | | NS_PROJ_END |
104 | | |
105 | | // Exported for projsync |
106 | | std::vector<std::string> PROJ_DLL pj_get_default_searchpaths(PJ_CONTEXT *ctx); |
107 | | |
108 | | //! @endcond Doxygen_Suppress |
109 | | |
110 | | #endif // FILEMANAGER_HPP_INCLUDED |