OILS / cpp / pylib.cc View on Github | oilshell.org

58 lines, 32 significant
1// pylib.cc
2
3#include "pylib.h"
4
5#include <sys/stat.h>
6
7namespace os_path {
8
9BigStr* rstrip_slashes(BigStr* s) {
10 // Strip all the rightmost slashes, but not if it's ALL slashes
11 int n = len(s);
12 if (n == 0) {
13 return s;
14 }
15
16 int new_len = n;
17 for (int i = n - 1; i >= 0; i--) {
18 char c = s->data_[i];
19 if (c == '/') {
20 new_len--;
21 } else {
22 break;
23 }
24 }
25
26 if (new_len == 0) { // it was all slashes, don't strip
27 return s;
28 }
29
30 // Truncate to new_len
31 BigStr* result = NewStr(new_len);
32 memcpy(result->data_, s->data_, new_len);
33 result->data_[new_len] = '\0';
34 return result;
35}
36
37} // namespace os_path
38
39namespace path_stat {
40
41bool exists(BigStr* path) {
42 struct stat st;
43 if (::stat(path->data_, &st) < 0) {
44 return false;
45 } else {
46 return true;
47 }
48}
49
50bool isdir(BigStr* path) {
51 struct stat st;
52 if (::stat(path->data_, &st) < 0) {
53 return false;
54 }
55 return S_ISDIR(st.st_mode);
56}
57
58} // namespace path_stat