/home/andy/git/oilshell/oil/cpp/leaky_pylib.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // leaky_pylib.cc |
2 | | |
3 | | #include "leaky_pylib.h" |
4 | | |
5 | | #include <sys/stat.h> |
6 | | |
7 | | namespace os_path { |
8 | | |
9 | 5 | Str* rstrip_slashes(Str* s) { |
10 | | // Strip all the rightmost slashes, but not if it's ALL slashes |
11 | 5 | int n = len(s); |
12 | 5 | if (n == 0) { |
13 | 1 | return s; |
14 | 1 | } |
15 | | |
16 | 4 | int new_len = n; |
17 | 9 | for (int i = n - 1; i >= 0; i--) { |
18 | 8 | char c = s->data_[i]; |
19 | 8 | if (c == '/') { |
20 | 5 | new_len--; |
21 | 5 | } else { |
22 | 3 | break; |
23 | 3 | } |
24 | 8 | } |
25 | | |
26 | 4 | if (new_len == 0) { // it was all slashes, don't strip |
27 | 1 | return s; |
28 | 1 | } |
29 | | |
30 | | // TODO: use gc_mylib.h API instead |
31 | 3 | char* buf = static_cast<char*>(malloc(new_len + 1)); |
32 | 3 | memcpy(buf, s->data_, new_len); |
33 | 3 | buf[new_len] = '\0'; |
34 | 3 | return CopyBufferIntoNewStr(buf, new_len); |
35 | 4 | } |
36 | | |
37 | | } // namespace os_path |
38 | | |
39 | | namespace path_stat { |
40 | | |
41 | 0 | bool exists(Str* path) { |
42 | 0 | struct stat st; |
43 | 0 | if (::stat(path->data_, &st) < 0) { |
44 | 0 | return false; |
45 | 0 | } else { |
46 | 0 | return true; |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | } // namespace path_stat |