1 | /* |
2 | * PROGRAM: Client/Server Common Code |
3 | * MODULE: dir_list.h |
4 | * DESCRIPTION: Directory listing config file operation |
5 | * |
6 | * The contents of this file are subject to the Interbase Public |
7 | * License Version 1.0 (the "License"); you may not use this file |
8 | * except in compliance with the License. You may obtain a copy |
9 | * of the License at http://www.Inprise.com/IPL.html |
10 | * |
11 | * Software distributed under the License is distributed on an |
12 | * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express |
13 | * or implied. See the License for the specific language governing |
14 | * rights and limitations under the License. |
15 | * |
16 | * Created by: Alex Peshkov <AlexPeshkov@users.sourceforge.net> |
17 | * |
18 | * All Rights Reserved. |
19 | * Contributor(s): ______________________________________. |
20 | */ |
21 | |
22 | #ifndef CONFIG_DIR_LIST_H |
23 | #define CONFIG_DIR_LIST_H |
24 | |
25 | #include "fb_types.h" |
26 | #include "../common/classes/fb_string.h" |
27 | #include "../common/config/config_file.h" |
28 | |
29 | namespace Firebird { |
30 | |
31 | // |
32 | // This class is used internally by DirectoryList |
33 | // to store single path in format of pre-parsed |
34 | // elements of that path & perform basic operations |
35 | // with this path representation. |
36 | // Because of it's internal nature it has only |
37 | // internally required subset of possible operators. |
38 | // |
39 | class ParsedPath : public ObjectsArray<PathName> |
40 | { |
41 | typedef ObjectsArray<PathName> inherited; |
42 | public: |
43 | explicit ParsedPath(MemoryPool& p) : ObjectsArray<PathName>(p) { } |
44 | ParsedPath(MemoryPool& p, const PathName& path) |
45 | : ObjectsArray<PathName>(p) |
46 | { |
47 | parse(path); |
48 | } |
49 | ParsedPath() : ObjectsArray<PathName>() { } |
50 | explicit ParsedPath(const PathName& path) |
51 | : ObjectsArray<PathName>() |
52 | { |
53 | parse(path); |
54 | } |
55 | // Take new path inside |
56 | void parse(const PathName& path); |
57 | // Convert internal representation to traditional one |
58 | operator PathName() const; |
59 | // Compare with path given by constant |
60 | bool operator==(const char* path) const |
61 | { |
62 | return PathName(*this) == path; |
63 | } |
64 | // Check, whether pPath lies inside directory tree, |
65 | // specified by *this ParsedPath. Also checks against |
66 | // possible symbolic links. |
67 | bool contains(const ParsedPath& pPath) const; |
68 | // Returns path, containing elements from 0 to n-1 |
69 | PathName subPath(size_t n) const; |
70 | }; |
71 | |
72 | |
73 | class DirectoryList : public ObjectsArray<ParsedPath> |
74 | { |
75 | private: |
76 | typedef ObjectsArray<ParsedPath> inherited; |
77 | enum ListMode {NotInitialized = -1, None = 0, Restrict = 1, Full = 2, SimpleList = 3}; |
78 | ListMode mode; |
79 | // Check, whether Value begins with Key, |
80 | // followed by any character from Next. |
81 | // If Next is empty, Value shoult exactly match Key. |
82 | // If Key found, sets Mode to KeyMode and returns true. |
83 | bool keyword(const ListMode keyMode, PathName& value, PathName key, PathName next); |
84 | protected: |
85 | // Clear allocated memory and reinitialize |
86 | void clear() |
87 | { |
88 | ((inherited*) this)->clear(); |
89 | mode = NotInitialized; |
90 | } |
91 | // Used for various configuration parameters - |
92 | // returns parameter PathName from Config Manager. |
93 | virtual const PathName getConfigString() const = 0; |
94 | // Initialize loads data from Config Manager. |
95 | // With simple mutex add-on may be easily used to |
96 | // load them dynamically. Now called locally |
97 | // when IsPathInList() invoked first time. |
98 | void initialize(bool simple_mode = false); |
99 | public: |
100 | explicit DirectoryList(MemoryPool& p) |
101 | : ObjectsArray<ParsedPath>(p), mode(NotInitialized) |
102 | { } |
103 | |
104 | DirectoryList() |
105 | : ObjectsArray<ParsedPath>(), mode(NotInitialized) |
106 | { } |
107 | |
108 | virtual ~DirectoryList() {clear();} |
109 | |
110 | // Check, whether Path is inside this DirectoryList |
111 | bool isPathInList(const PathName& path) const; |
112 | |
113 | // Search for file Name in all directories of DirectoryList. |
114 | // If found, return full path to it in Path. |
115 | // Otherwise Path = Name. |
116 | bool expandFileName(PathName& path, const PathName& name) const; |
117 | |
118 | // Use first directory in this directory list |
119 | // to build default full name for a file |
120 | bool defaultName(PathName& path, const PathName& name) const; |
121 | }; |
122 | |
123 | class TempDirectoryList : public DirectoryList { |
124 | public: |
125 | explicit TempDirectoryList(MemoryPool& p) |
126 | : DirectoryList(p) |
127 | { |
128 | initialize(true); |
129 | } |
130 | TempDirectoryList() |
131 | { |
132 | initialize(true); |
133 | } |
134 | |
135 | private: |
136 | // implementation of the inherited function |
137 | const PathName getConfigString() const; |
138 | }; |
139 | |
140 | } //namespace Firebird |
141 | |
142 | #endif // CONFIG_DIR_LIST_H |
143 | |