1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright (c) 2011-2014, Chris Johns <chrisj@rtems.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <algorithm>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <rld.h>
namespace rld
{
namespace path
{
std::string
basename (const std::string& name)
{
size_t b = name.find_last_of (RLD_PATH_SEPARATOR);
if (b != std::string::npos)
return name.substr (b + 1);
return name;
}
std::string
dirname (const std::string& name)
{
size_t b = name.find_last_of (RLD_PATH_SEPARATOR);
if (b != std::string::npos)
return name.substr (0, b - 1);
return name;
}
std::string
extension (const std::string& name)
{
size_t b = name.find_last_of ('.');
if (b != std::string::npos)
return name.substr (b);
return name;
}
void
path_split (const std::string& path, paths& paths)
{
strings ps;
rld::split (path, ps, RLD_PATHSTR_SEPARATOR);
if (ps.size ())
{
for (strings::iterator psi = ps.begin ();
psi != ps.end ();
++psi)
{
if (check_directory (*psi))
paths.push_back (*psi);
}
}
}
void
path_join (const std::string& path_, const std::string& file_, std::string& joined)
{
if ((path_[path_.size () - 1] != RLD_PATH_SEPARATOR) &&
(file_[0] != RLD_PATH_SEPARATOR))
joined = path_ + RLD_PATH_SEPARATOR + file_;
else if ((path_[path_.size () - 1] == RLD_PATH_SEPARATOR) &&
(file_[0] == RLD_PATH_SEPARATOR))
joined = path_ + &file_[1];
else
joined = path_ + file_;
}
bool
check_file (const std::string& path)
{
struct stat sb;
if (::stat (path.c_str (), &sb) == 0)
if (S_ISREG (sb.st_mode))
return true;
return false;
}
bool
check_directory (const std::string& path)
{
struct stat sb;
if (::stat (path.c_str (), &sb) == 0)
if (S_ISDIR (sb.st_mode))
return true;
return false;
}
void
find_file (std::string& path, const std::string& name, paths& search_paths)
{
for (paths::iterator pi = search_paths.begin ();
pi != search_paths.end ();
++pi)
{
path_join (*pi, name, path);
if (check_file (path))
return;
}
path.clear ();
}
}
}
|