tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Filesystem / Path.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <memory>
17 #include <algorithm>
18 #include <iterator>
19 #include <stdlib.h>
20 #include <limits.h>
21 #include <dpl/log/log.h>
22 #include <dpl/assert.h>
23 #include <Commons/Exception.h>
24 #include <Commons/StringUtils.h>
25 #include "Path.h"
26
27 namespace WrtDeviceApis {
28 namespace Filesystem {
29
30 using namespace Api;
31
32 const Path::SeparatorType Path::m_pathSeparator = '/';
33
34 IPathPtr Path::create(const std::string& path)
35 {
36     DPL::SharedPtr<Path> result(new Path());
37     result->reset(path);
38     return DPL::StaticPointerCast<IPath>(result);
39 }
40
41 std::string Path::getFullPath() const
42 {
43     return m_fullPath;
44 }
45
46 std::string Path::getPath() const
47 {
48     return m_path;
49 }
50
51 std::string Path::getName() const
52 {
53     return m_name;
54 }
55
56 IPathPtr Path::append(const std::string& path)
57 {
58     reset(m_fullPath + m_pathSeparator + path);
59     return DPL::StaticPointerCast<IPath>(SharedFromThis());
60 }
61
62 IPathPtr Path::append(const IPathPtr& path)
63 {
64     reset(m_fullPath + m_pathSeparator + path->getFullPath());
65     return DPL::StaticPointerCast<IPath>(SharedFromThis());
66 }
67
68 bool Path::isAbsolute() const
69 {
70     return (!m_fullPath.empty() && (m_fullPath[0] == m_pathSeparator));
71 }
72
73 IPath::SeparatorType Path::getSeparator()
74 {
75     return m_pathSeparator;
76 }
77
78 bool Path::isValid(const std::string& str)
79 {
80     return !str.empty();
81 }
82
83 IPathPtr Path::clone() const
84 {
85     return Path::create(m_fullPath);
86 }
87
88 Path::Path()
89 {
90 }
91
92 void Path::reset(const std::string& str)
93 {
94     if (!isValid(str)) {
95         ThrowMsg(Commons::InvalidArgumentException,
96                  "Not a valid path: " + str + ".");
97     }
98
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;
104         m_path.clear();
105     } else {
106         if (0 == pos) {
107             m_fullPath = m_path = m_pathSeparator;
108         } else {
109             m_fullPath = m_path = tmp.substr(0, pos);
110             m_fullPath += m_pathSeparator;
111         }
112         m_name = tmp.substr(pos + 1);
113         m_fullPath += m_name;
114     }
115 }
116
117 } // Filesystem
118 } // WrtDeviceApis