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 {
32 const Path::SeparatorType Path::m_pathSeparator = '/';
34 IPathPtr Path::create(const std::string& path)
36 DPL::SharedPtr<Path> result(new Path());
38 return DPL::StaticPointerCast<IPath>(result);
41 std::string Path::getFullPath() const
46 std::string Path::getPath() const
51 std::string Path::getName() const
56 IPathPtr Path::append(const std::string& path)
58 reset(m_fullPath + m_pathSeparator + path);
59 return DPL::StaticPointerCast<IPath>(SharedFromThis());
62 IPathPtr Path::append(const IPathPtr& path)
64 reset(m_fullPath + m_pathSeparator + path->getFullPath());
65 return DPL::StaticPointerCast<IPath>(SharedFromThis());
68 bool Path::isAbsolute() const
70 return (!m_fullPath.empty() && (m_fullPath[0] == m_pathSeparator));
73 IPath::SeparatorType Path::getSeparator()
75 return m_pathSeparator;
78 bool Path::isValid(const std::string& str)
83 IPathPtr Path::clone() const
85 return Path::create(m_fullPath);
92 void Path::reset(const std::string& str)
95 ThrowMsg(Commons::InvalidArgumentException,
96 "Not a valid path: " + str + ".");
99 std::string tmp = Commons::String::unique(Commons::String::trim(
100 str), m_pathSeparator);
101 std::string::size_type pos = tmp.find_last_of(m_pathSeparator);
102 if (pos == std::string::npos) {
103 m_fullPath = m_name = tmp;
107 m_fullPath = m_path = m_pathSeparator;
109 m_fullPath = m_path = tmp.substr(0, pos);
110 m_fullPath += m_pathSeparator;
112 m_name = tmp.substr(pos + 1);
113 m_fullPath += m_name;