Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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 using namespace Api;
30
31 const Path::SeparatorType Path::m_pathSeparator = '/';
32
33 IPathPtr Path::create(const std::string& path)
34 {
35     DPL::SharedPtr<Path> result(new Path());
36     result->reset(path);
37     return DPL::StaticPointerCast<IPath>(result);
38 }
39
40 std::string Path::getFullPath() const
41 {
42     return m_fullPath;
43 }
44
45 std::string Path::getPath() const
46 {
47     return m_path;
48 }
49
50 std::string Path::getName() const
51 {
52     return m_name;
53 }
54
55 IPathPtr Path::append(const std::string& path)
56 {
57     reset(m_fullPath + m_pathSeparator + path);
58     return DPL::StaticPointerCast<IPath>(SharedFromThis());
59 }
60
61 IPathPtr Path::append(const IPathPtr& path)
62 {
63     reset(m_fullPath + m_pathSeparator + path->getFullPath());
64     return DPL::StaticPointerCast<IPath>(SharedFromThis());
65 }
66
67 bool Path::isAbsolute() const
68 {
69     return (!m_fullPath.empty() && (m_fullPath[0] == m_pathSeparator));
70 }
71
72 IPath::SeparatorType Path::getSeparator()
73 {
74     return m_pathSeparator;
75 }
76
77 bool Path::isValid(const std::string& str)
78 {
79     return !str.empty();
80 }
81
82 IPathPtr Path::clone() const
83 {
84     return Path::create(m_fullPath);
85 }
86
87 Path::Path()
88 {}
89
90 void Path::reset(const std::string& str)
91 {
92     if (!isValid(str)) {
93         ThrowMsg(Commons::InvalidArgumentException,
94                  "Not a valid path: " + str + ".");
95     }
96
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;
102         m_path.clear();
103     } else {
104         if (0 == pos) {
105             m_fullPath = m_path = m_pathSeparator;
106         } else {
107             m_fullPath = m_path = tmp.substr(0, pos);
108             m_fullPath += m_pathSeparator;
109         }
110         m_name = tmp.substr(pos + 1);
111         m_fullPath += m_name;
112     }
113 }
114 } // Filesystem
115 } // WrtDeviceApis