Commit | Line | Data |
---|---|---|
d44e3c4f | 1 | /****************************************************************************** |
2 | * Copyright (c) 2000-2016 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 | * Contributors: | |
9 | * Balasko, Jeno | |
10 | * Beres, Szabolcs | |
11 | * | |
12 | ******************************************************************************/ | |
970ed795 EL |
13 | #include "Path2.hh" |
14 | ||
15 | #include "path.h" | |
16 | //#include "dbgnew.hh" | |
17 | ||
18 | using std::string; | |
19 | ||
20 | const char Path::SEPARATOR = '/'; | |
21 | ||
22 | std::string Path::normalize(const std::string& original) { | |
23 | std::string result; | |
24 | bool last_slash = false; | |
25 | for (size_t i = 0; i < original.size(); ++i) { | |
26 | if (original[i] != SEPARATOR) { | |
27 | result += original[i]; | |
28 | last_slash = false; | |
29 | continue; | |
30 | } | |
31 | ||
32 | if (!last_slash) { | |
33 | last_slash = true; | |
34 | result += original[i]; | |
35 | } | |
36 | } | |
37 | return result; | |
38 | } | |
39 | ||
40 | std::string Path::get_abs_path(const std::string& fname) { | |
41 | if (fname.empty()) { | |
42 | return std::string(1, SEPARATOR); | |
43 | } | |
44 | ||
45 | if (fname[0] == SEPARATOR) { | |
46 | return normalize(fname); | |
47 | } | |
48 | ||
49 | expstring_t working_dir = get_working_dir(); | |
50 | std::string work_dir(working_dir); | |
51 | Free(working_dir); | |
52 | work_dir += SEPARATOR; | |
53 | work_dir.append(fname); | |
54 | return normalize(work_dir); | |
55 | } | |
56 | ||
57 | std::string Path::get_file(const std::string& path) { | |
58 | size_t idx = path.rfind(SEPARATOR); | |
59 | ||
60 | if (idx == string::npos) { | |
61 | return path; | |
62 | } | |
63 | if (idx == path.size() - 1) { | |
64 | return string(); | |
65 | } | |
66 | ||
67 | return path.substr(idx + 1); | |
68 | } | |
69 | ||
70 | string Path::get_dir(const string& path) { | |
71 | size_t idx = path.rfind(SEPARATOR); | |
72 | ||
73 | if (idx == string::npos) { | |
74 | return string(); | |
75 | } | |
76 | return path.substr(0, idx + 1); | |
77 | } | |
78 | ||
79 | string Path::compose(const string& path1, const string& path2) { | |
80 | if (path1.empty()) { | |
81 | return path2; | |
82 | } | |
83 | ||
84 | if (path2.empty()) { | |
85 | return path1; | |
86 | } | |
87 | ||
88 | string result = path1; | |
89 | if (result[result.size() - 1] != SEPARATOR | |
90 | && path2[0] != SEPARATOR) { | |
91 | result += SEPARATOR; | |
92 | } | |
93 | ||
94 | result.append(path2); | |
95 | ||
96 | return result; | |
97 | } | |
98 | ||
99 | bool Path::is_absolute(const string& path) { | |
100 | return (!path.empty() && path[0] == SEPARATOR); | |
101 | } | |
102 |