llvm.org GIT mirror llvm / cf55c8e
Added method Path::getDirname(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49352 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 12 years ago
4 changed file(s) with 57 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
268268 /// @returns std::string containing the basename of the path
269269 /// @brief Get the base name of the path
270270 std::string getBasename() const;
271
272 /// This function strips off the suffix of the path beginning with the
273 /// path separator ('/' on Unix, '\' on Windows) and returns the result.
274 std::string getDirname() const;
271275
272276 /// This function strips off the path and basename(up to and
273277 /// including the last dot) of the file or directory name and
566570 /// MemoryBuffer::getFile instead.
567571 static void UnMapFilePages(const char *Base, uint64_t FileSize);
568572
573
574 /// @}
575 /// @name Internal methods.
576 /// @{
577 protected:
578 std::string getDirnameCharSep(char Sep) const;
579
569580 /// @}
570581 /// @name Data
571582 /// @{
572583 protected:
573584 mutable std::string path; ///< Storage for the path name.
585
574586
575587 /// @}
576588 };
195195 Paths.push_back(tmpPath);
196196 }
197197
198 std::string Path::getDirnameCharSep(char Sep) const {
199
200 if (path.empty())
201 return ".";
202
203 // If the path is all slashes, return a single slash.
204 // Otherwise, remove all trailing slashes.
205
206 signed pos = path.size() - 1;
207
208 while (pos >= 0 && path[pos] == Sep)
209 --pos;
210
211 if (pos < 0)
212 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
213
214 // Any slashes left?
215 signed i = 0;
216
217 while (i < pos && path[i] != Sep)
218 ++i;
219
220 if (i == pos) // No slashes? Return "."
221 return ".";
222
223 // There is at least one slash left. Remove all trailing non-slashes.
224 while (pos >= 0 && path[pos] != Sep)
225 --pos;
226
227 // Remove any trailing slashes.
228 while (pos >= 0 && path[pos] == Sep)
229 --pos;
230
231 if (pos < 0)
232 return path[0] == Sep ? std::string(1, Sep) : std::string(".");
233
234 return path.substr(0, pos+1);
235 }
236
198237 // Include the truly platform-specific parts of this class.
199238 #if defined(LLVM_ON_UNIX)
200239 #include "Unix/Path.inc"
276276 }
277277
278278
279 std::string Path::getDirname() const { return getDirnameCharSep('/'); }
280
279281 std::string
280282 Path::getBasename() const {
281283 // Find the last slash
282 size_t slash = path.rfind('/');
284 std::string::size_type slash = path.rfind('/');
283285 if (slash == std::string::npos)
284286 slash = 0;
285287 else
286288 slash++;
287289
288 size_t dot = path.rfind('.');
290 std::string::size_type dot = path.rfind('.');
289291 if (dot == std::string::npos || dot < slash)
290292 return path.substr(slash);
291293 else
228228 return len > 0 && path[len-1] == '/';
229229 }
230230
231 std::string Path::getDirname() const { return getDirnameCharSep('\\'); }
232
231233 std::string
232234 Path::getBasename() const {
233235 // Find the last slash