File size: 1,796 Bytes
dc2106c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

 * SPDX-License-Identifier: Apache-2.0

 */

// Copyright (c) ONNX Project Contributors.

#include "onnx/common/path.h"

namespace ONNX_NAMESPACE {
#ifdef _WIN32
#else

std::string path_join(const std::string& origin, const std::string& append) {
  if (origin.find_last_of(k_preferred_path_separator) != origin.length() - 1) {
    return origin + k_preferred_path_separator + append;
  }
  return origin + append;
}

std::string clean_relative_path(const std::string& path) {
  if (path.empty()) {
    return ".";
  }

  std::string out;

  size_t n = path.size();

  size_t r = 0;
  size_t dotdot = 0;

  while (r < n) {
    if (path[r] == k_preferred_path_separator) {
      r++;
      continue;
    }

    if (path[r] == '.' && (r + 1 == n || path[r + 1] == k_preferred_path_separator)) {
      r++;
      continue;
    }

    if (path[r] == '.' && path[r + 1] == '.' && (r + 2 == n || path[r + 2] == k_preferred_path_separator)) {
      r += 2;

      if (out.size() > dotdot) {
        while (out.size() > dotdot && out.back() != k_preferred_path_separator) {
          out.pop_back();
        }
        if (!out.empty())
          out.pop_back();
      } else {
        if (!out.empty()) {
          out.push_back(k_preferred_path_separator);
        }

        out.push_back('.');
        out.push_back('.');
        dotdot = out.size();
      }

      continue;
    }

    if (!out.empty() && out.back() != k_preferred_path_separator) {
      out.push_back(k_preferred_path_separator);
    }

    for (; r < n && path[r] != k_preferred_path_separator; r++) {
      out.push_back(path[r]);
    }
  }

  if (out.empty()) {
    out.push_back('.');
  }

  return out;
}
#endif

} // namespace ONNX_NAMESPACE