cpp

Coverage Report

Created: 2022-07-20 01:16

/home/uke/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 mylib2.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 new Str(buf, new_len);
35
4
}
36
37
}  // namespace os_path
38
39
namespace path_stat {
40
41
0
bool exists(Str* path) {
42
0
  mylib::Str0 path0(path);
43
0
  struct stat st;
44
0
  if (::stat(path0.Get(), &st) < 0) {
45
0
    return false;
46
0
  } else {
47
0
    return true;
48
0
  }
49
0
}
50
51
}  // namespace path_stat