Merge pull request #19 from nspaseski/master
[deliverable/titan.core.git] / common / Path2.cc
CommitLineData
970ed795 1///////////////////////////////////////////////////////////////////////////////
3abe9331 2// Copyright (c) 2000-2015 Ericsson Telecom AB
970ed795
EL
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
13using std::string;
14
15const char Path::SEPARATOR = '/';
16
17std::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
35std::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
52std::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
65string 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
74string 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
94bool Path::is_absolute(const string& path) {
95 return (!path.empty() && path[0] == SEPARATOR);
96}
97
This page took 0.027308 seconds and 5 git commands to generate.