2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <dpl/log/log.h>
22 #include <dpl/assert.h>
23 #include <Commons/Exception.h>
24 #include <Commons/StringUtils.h>
27 namespace WrtDeviceApis {
28 namespace Filesystem {
31 const Path::SeparatorType Path::m_pathSeparator = '/';
33 IPathPtr Path::create(const std::string& path)
35 DPL::SharedPtr<Path> result(new Path());
37 return DPL::StaticPointerCast<IPath>(result);
40 std::string Path::getFullPath() const
45 std::string Path::getPath() const
50 std::string Path::getName() const
55 IPathPtr Path::append(const std::string& path)
57 reset(m_fullPath + m_pathSeparator + path);
58 return DPL::StaticPointerCast<IPath>(SharedFromThis());
61 IPathPtr Path::append(const IPathPtr& path)
63 reset(m_fullPath + m_pathSeparator + path->getFullPath());
64 return DPL::StaticPointerCast<IPath>(SharedFromThis());
67 bool Path::isAbsolute() const
69 return (!m_fullPath.empty() && (m_fullPath[0] == m_pathSeparator));
72 IPath::SeparatorType Path::getSeparator()
74 return m_pathSeparator;
77 bool Path::isValid(const std::string& str)
82 IPathPtr Path::clone() const
84 return Path::create(m_fullPath);
90 void Path::reset(const std::string& str)
93 ThrowMsg(Commons::InvalidArgumentException,
94 "Not a valid path: " + str + ".");
97 std::string tmp = Commons::String::unique(Commons::String::trim(
98 str), m_pathSeparator);
99 std::string::size_type pos = tmp.find_last_of(m_pathSeparator);
100 if (pos == std::string::npos) {
101 m_fullPath = m_name = tmp;
105 m_fullPath = m_path = m_pathSeparator;
107 m_fullPath = m_path = tmp.substr(0, pos);
108 m_fullPath += m_pathSeparator;
110 m_name = tmp.substr(pos + 1);
111 m_fullPath += m_name;