|
| 1 | +//****************************************************************************** |
| 2 | +/// |
| 3 | +/// @file platform/windows/syspovpath.cpp |
| 4 | +/// |
| 5 | +/// Windows-specific partial implementation of the @ref Path class. |
| 6 | +/// |
| 7 | +/// @copyright |
| 8 | +/// @parblock |
| 9 | +/// |
| 10 | +/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.7. |
| 11 | +/// Copyright 1991-2016 Persistence of Vision Raytracer Pty. Ltd. |
| 12 | +/// |
| 13 | +/// POV-Ray is free software: you can redistribute it and/or modify |
| 14 | +/// it under the terms of the GNU Affero General Public License as |
| 15 | +/// published by the Free Software Foundation, either version 3 of the |
| 16 | +/// License, or (at your option) any later version. |
| 17 | +/// |
| 18 | +/// POV-Ray is distributed in the hope that it will be useful, |
| 19 | +/// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +/// GNU Affero General Public License for more details. |
| 22 | +/// |
| 23 | +/// You should have received a copy of the GNU Affero General Public License |
| 24 | +/// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | +/// |
| 26 | +/// ---------------------------------------------------------------------------- |
| 27 | +/// |
| 28 | +/// POV-Ray is based on the popular DKB raytracer version 2.12. |
| 29 | +/// DKBTrace was originally written by David K. Buck. |
| 30 | +/// DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins. |
| 31 | +/// |
| 32 | +/// @endparblock |
| 33 | +/// |
| 34 | +//****************************************************************************** |
| 35 | + |
| 36 | +#include "syspovpath.h" |
| 37 | + |
| 38 | +// this must be the last file included |
| 39 | +#include "base/povdebug.h" |
| 40 | + |
| 41 | +namespace pov_base |
| 42 | +{ |
| 43 | + |
| 44 | +//****************************************************************************** |
| 45 | + |
| 46 | +#if !POV_USE_DEFAULT_PATH_PARSER |
| 47 | + |
| 48 | +/// Test for the special directory name denoting the current directory (`.`). |
| 49 | +static inline bool IsCurrentDir(const UCS2String& s) |
| 50 | +{ |
| 51 | + return (s.length() == 1) && (s[0] == '.'); |
| 52 | +} |
| 53 | + |
| 54 | +/// Test for the special directory name denoting the parent directory (`..`). |
| 55 | +static inline bool IsParentDir(const UCS2String& s) |
| 56 | +{ |
| 57 | + return (s.length() == 2) && (s[0] == '.') && (s[1] == '.'); |
| 58 | +} |
| 59 | + |
| 60 | +bool Path::ParsePathString (UCS2String& volume, vector<UCS2String>& dirnames, UCS2String& filename, const UCS2String& path) |
| 61 | +{ |
| 62 | + UCS2String stash; |
| 63 | + |
| 64 | + // Unless noted otherwise, all components are considered empty. |
| 65 | + volume.clear(); |
| 66 | + dirnames.clear(); |
| 67 | + filename.clear(); |
| 68 | + |
| 69 | + // Empty strings are considered valid path names, too. |
| 70 | + if (path.empty() == true) |
| 71 | + return true; |
| 72 | + |
| 73 | + UCS2String::const_iterator i = path.begin(); |
| 74 | + |
| 75 | + // Check for a volume identifier: |
| 76 | + // - If the second character is a colon, we presume the first two characters to identify the drive. In that case |
| 77 | + // we'll also check whether the following character is a path separator, indicating an absolute path on that |
| 78 | + // drive, in which case we'll also include a trailing separator to the drive letter. |
| 79 | + // - If the path starts with two identical path separator characters, we presume the string to be a UNC path, in |
| 80 | + // which case we set the volume identifier to the network share, including two leading and a trailing separator |
| 81 | + // characters. |
| 82 | + // - Otherwise, if the first character is a path separator, this indicates an absolute path on the current drive, |
| 83 | + // in which case we set the volume identifier to a single path separator character. |
| 84 | + // - In any other case, we presume the string to be a relative path, and set the volume identifier to an empty |
| 85 | + // string. |
| 86 | + |
| 87 | + if ((*i == POV_PATH_SEPARATOR) || (*i == POV_PATH_SEPARATOR_2)) |
| 88 | + { |
| 89 | + // String starts with a path separator; may be an absolute path on the current drive or a UNC path. |
| 90 | + |
| 91 | + // Stash the separator (use the canonical one, not the one actually used). |
| 92 | + stash += POV_PATH_SEPARATOR; |
| 93 | + ++i; |
| 94 | + |
| 95 | + if ((i != path.end()) && (*i == stash[0])) |
| 96 | + { |
| 97 | + // The second character is also an (identical) separator character, indicating a UNC path. |
| 98 | + |
| 99 | + // Stash another path separators (use the canonical one, not the one actually used). |
| 100 | + stash += POV_PATH_SEPARATOR; |
| 101 | + ++i; |
| 102 | + |
| 103 | + // Stash everything that follows, up to the next path separator. |
| 104 | + for (; (i != path.end()) && (*i != POV_PATH_SEPARATOR) && (*i != POV_PATH_SEPARATOR_2); ++i) |
| 105 | + stash += *i; |
| 106 | + |
| 107 | + // Currently, we don't support bare UNC share names without trailing separator character, |
| 108 | + // even though allegedly they are technically valid. |
| 109 | + if (i == path.end()) |
| 110 | + return false; |
| 111 | + |
| 112 | + // Stash another path separator (use the canonical one, not the one actually used) |
| 113 | + // to indicate an absolute path. |
| 114 | + stash += POV_PATH_SEPARATOR; |
| 115 | + ++i; |
| 116 | + } |
| 117 | + // If it's not a UNC path, at this point our stash contains a single path separator, |
| 118 | + // which is exactly what we intend to emit as the volume identifier in that case. |
| 119 | + |
| 120 | + // Emit whatever string we have stashed as the volume identifier. |
| 121 | + volume = stash; |
| 122 | + stash.clear(); |
| 123 | + } |
| 124 | + else if (isalpha (*i)) |
| 125 | + { |
| 126 | + // String starts with an ASCII letter; may be a relative path or a drive letter. |
| 127 | + |
| 128 | + // Stash the character, then go on to test what's next. |
| 129 | + stash += *i; |
| 130 | + ++i; |
| 131 | + |
| 132 | + if ((i != path.end()) && (*i == ':')) |
| 133 | + { |
| 134 | + // Yup, that's a drive letter. Add the colon to the stashed letter. |
| 135 | + stash += ':'; |
| 136 | + ++i; |
| 137 | + |
| 138 | + // Currently, we don't support relative paths if a volume is specified. |
| 139 | + if ((i == path.end()) || ((*i != POV_PATH_SEPARATOR) && (*i != POV_PATH_SEPARATOR_2))) |
| 140 | + return false; |
| 141 | + |
| 142 | + // Stash another path separator (use the canonical one, not the one actually used) |
| 143 | + // to indicate an absolute path. |
| 144 | + stash += POV_PATH_SEPARATOR; |
| 145 | + ++i; |
| 146 | + |
| 147 | + // Emit the stashed string as the volume identifier. |
| 148 | + volume = stash; |
| 149 | + stash.clear(); |
| 150 | + } |
| 151 | + // If it's not a drive letter, at this point we have only stashed the first letter, but |
| 152 | + // our index still points to the second one so the following algorithm will take care of it. |
| 153 | + } |
| 154 | + |
| 155 | + // Walk through the path string, stashing any non-separator characters. Whenever we hit a separator |
| 156 | + // character, emit the stashed characters (if any) as a directory name and clear the stash. |
| 157 | + // Also, as we go along, resolve the special directory names `.` and `..` if possible. |
| 158 | + |
| 159 | + // NB since we do not emit "empty" directory names, any sequence of consecutive separator |
| 160 | + // characters is effectively treated as a single separator character. |
| 161 | + |
| 162 | + for(; i != path.end(); ++i) |
| 163 | + { |
| 164 | + if ((*i == POV_PATH_SEPARATOR) || (*i == POV_PATH_SEPARATOR_2)) |
| 165 | + { |
| 166 | + if (!stash.empty() && !IsCurrentDir(stash)) |
| 167 | + { |
| 168 | + if (!dirnames.empty() && IsParentDir(stash)) |
| 169 | + dirnames.pop_back(); |
| 170 | + else |
| 171 | + dirnames.push_back (stash); |
| 172 | + stash.clear(); |
| 173 | + } |
| 174 | + } |
| 175 | + else |
| 176 | + stash += *i; |
| 177 | + } |
| 178 | + |
| 179 | + // Whatever is left in the stash is presumably the actual file name. |
| 180 | + |
| 181 | + // NB as a consequence of the algorithm chosen, any path name ending in a path separator |
| 182 | + // character will be emitted as a list of directories only, with the file name left empty. |
| 183 | + |
| 184 | + filename = stash; |
| 185 | + |
| 186 | + return true; |
| 187 | +} |
| 188 | + |
| 189 | +#endif // !POV_USE_DEFAULT_PATH_PARSER |
| 190 | + |
| 191 | +//****************************************************************************** |
| 192 | + |
| 193 | +} |
0 commit comments