]>
git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilPath.cpp
f08ff24aa858bdf51710b47c7645d7ab84687576
5 // Created by James McIlree on 4/8/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
15 std::string
Path::basename(const char* path
) {
16 size_t length
= strlen(path
);
21 * case: [any-single-character-paths]
24 return std::string(path
);
27 char* temp_cursor
= &temp
[PATH_MAX
- 1];
28 char* temp_end
= temp_cursor
;
29 *temp_end
= 0; // NULL terminate
31 const char* path_cursor
= &path
[length
-1];
33 while (path_cursor
>= path
) {
34 if (*path_cursor
== '/') {
35 // If we have copied one or more chars, we're done
36 if (temp_cursor
!= temp_end
)
37 return std::string(temp_cursor
);
39 *(--temp_cursor
) = *path_cursor
;
42 // Is the temp buffer full?
43 if (temp_cursor
== temp
)
44 return std::string(temp
);
49 if (path
[0] == '/' && temp_cursor
== temp_end
) {
50 *(--temp_cursor
) = '/';
53 return std::string(temp_cursor
);
56 std::string
Path::basename(std::string
& path
) {
57 return basename(path
.c_str());
60 bool Path::exists(const char *path
) {
62 return lstat(path
, &statinfo
) == 0;
65 bool Path::exists(std::string
& path
) {
66 return exists(path
.c_str());
69 bool Path::is_file(const char* path
, bool should_resolve_symlinks
) {
71 if (should_resolve_symlinks
) {
72 if (stat(path
, &statinfo
) == 0) {
73 if (S_ISREG(statinfo
.st_mode
)) {
78 if (lstat(path
, &statinfo
) == 0) {
79 if (S_ISREG(statinfo
.st_mode
)) {
88 bool Path::is_file(std::string
& path
, bool should_resolve_symlinks
) {
89 return is_file(path
.c_str(), should_resolve_symlinks
);