Commit | Line | Data |
---|---|---|
970ed795 EL |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (c) 2000-2014 Ericsson Telecom AB | |
3 | // All rights reserved. This program and the accompanying materials | |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | #include "Path2.hh" | |
9 | ||
10 | #include "path.h" | |
11 | //#include "dbgnew.hh" | |
12 | ||
13 | using std::string; | |
14 | ||
15 | const char Path::SEPARATOR = '/'; | |
16 | ||
17 | std::string Path::normalize(const std::string& original) { | |
18 | std::string result; | |
19 | bool last_slash = false; | |
20 | for (size_t i = 0; i < original.size(); ++i) { | |
21 | if (original[i] != SEPARATOR) { | |
22 | result += original[i]; | |
23 | last_slash = false; | |
24 | continue; | |
25 | } | |
26 | ||
27 | if (!last_slash) { | |
28 | last_slash = true; | |
29 | result += original[i]; | |
30 | } | |
31 | } | |
32 | return result; | |
33 | } | |
34 | ||
35 | std::string Path::get_abs_path(const std::string& fname) { | |
36 | if (fname.empty()) { | |
37 | return std::string(1, SEPARATOR); | |
38 | } | |
39 | ||
40 | if (fname[0] == SEPARATOR) { | |
41 | return normalize(fname); | |
42 | } | |
43 | ||
44 | expstring_t working_dir = get_working_dir(); | |
45 | std::string work_dir(working_dir); | |
46 | Free(working_dir); | |
47 | work_dir += SEPARATOR; | |
48 | work_dir.append(fname); | |
49 | return normalize(work_dir); | |
50 | } | |
51 | ||
52 | std::string Path::get_file(const std::string& path) { | |
53 | size_t idx = path.rfind(SEPARATOR); | |
54 | ||
55 | if (idx == string::npos) { | |
56 | return path; | |
57 | } | |
58 | if (idx == path.size() - 1) { | |
59 | return string(); | |
60 | } | |
61 | ||
62 | return path.substr(idx + 1); | |
63 | } | |
64 | ||
65 | string Path::get_dir(const string& path) { | |
66 | size_t idx = path.rfind(SEPARATOR); | |
67 | ||
68 | if (idx == string::npos) { | |
69 | return string(); | |
70 | } | |
71 | return path.substr(0, idx + 1); | |
72 | } | |
73 | ||
74 | string Path::compose(const string& path1, const string& path2) { | |
75 | if (path1.empty()) { | |
76 | return path2; | |
77 | } | |
78 | ||
79 | if (path2.empty()) { | |
80 | return path1; | |
81 | } | |
82 | ||
83 | string result = path1; | |
84 | if (result[result.size() - 1] != SEPARATOR | |
85 | && path2[0] != SEPARATOR) { | |
86 | result += SEPARATOR; | |
87 | } | |
88 | ||
89 | result.append(path2); | |
90 | ||
91 | return result; | |
92 | } | |
93 | ||
94 | bool Path::is_absolute(const string& path) { | |
95 | return (!path.empty() && path[0] == SEPARATOR); | |
96 | } | |
97 |