wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / Path.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18  #include <memory>
19 #include <algorithm>
20 #include <iterator>
21 #include <stdlib.h>
22 #include <limits.h>
23 #include <dpl/assert.h>
24 #include <Commons/Exception.h>
25 #include <Commons/StringUtils.h>
26 #include "Path.h"
27 #include <Logger.h>
28
29 using namespace WrtDeviceApis;
30 using namespace WrtDeviceApis::Commons;
31
32 namespace DeviceAPI {
33 namespace Filesystem {
34 const Path::SeparatorType Path::m_pathSeparator = '/';
35
36 IPathPtr Path::create(const std::string& path)
37 {
38     DPL::SharedPtr<Path> result(new Path());
39     result->reset(path);
40     return DPL::StaticPointerCast<IPath>(result);
41 }
42
43 std::string Path::getFullPath() const
44 {
45     return m_fullPath;
46 }
47
48 std::string Path::getPath() const
49 {
50     return m_path;
51 }
52
53 std::string Path::getName() const
54 {
55     return m_name;
56 }
57
58 IPathPtr Path::append(const std::string& path)
59 {
60     reset(m_fullPath + m_pathSeparator + path);
61     return DPL::StaticPointerCast<IPath>(SharedFromThis());
62 }
63
64 IPathPtr Path::append(const IPathPtr& path)
65 {
66     reset(m_fullPath + m_pathSeparator + path->getFullPath());
67     return DPL::StaticPointerCast<IPath>(SharedFromThis());
68 }
69
70 bool Path::isAbsolute() const
71 {
72     return (!m_fullPath.empty() && (m_fullPath[0] == m_pathSeparator));
73 }
74
75 IPath::SeparatorType Path::getSeparator()
76 {
77     return m_pathSeparator;
78 }
79
80 bool Path::isValid(const std::string& str)
81 {
82     return !str.empty();
83 }
84
85 IPathPtr Path::clone() const
86 {
87     return Path::create(m_fullPath);
88 }
89
90 Path::Path()
91 {
92 }
93
94 void Path::reset(const std::string& str)
95 {
96     if (!isValid(str)) {
97         LoggerD(str << "empty string");
98         ThrowMsg(Commons::NotFoundException,
99                  "Not a valid path: " + str + ".");
100     }
101
102     std::string tmp = Commons::String::unique(Commons::String::trim(
103                                                   str), m_pathSeparator);
104     std::string::size_type pos = tmp.find_last_of(m_pathSeparator);
105     if (pos == std::string::npos) {
106         m_fullPath = m_name = tmp;
107         m_path.clear();
108     } else {
109         if (0 == pos) {
110             m_fullPath = m_path = m_pathSeparator;
111         } else {
112             m_fullPath = m_path = tmp.substr(0, pos);
113             m_fullPath += m_pathSeparator;
114         }
115         m_name = tmp.substr(pos + 1);
116         m_fullPath += m_name;
117     }
118 }
119 } // Filesystem
120 } // TizenApis