P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
path.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_PATH_H_
18#define LIB_PATH_H_
19
20/*
21 It's 2015 and C++ still does not have a portable way to manipulate pathnames.
22 This code is not portable, but at least the interfaces should be.
23*/
24
25#include "cstring.h"
26
27namespace Util {
28// Represents a filename path, e.g., /usr/local/bin/file.exe
29class PathName final {
30 private:
31 static const char pathSeparators[2];
32 cstring str;
33
34 const char *findLastSeparator() const;
35
36 public:
37 static inline cstring separator() {
38#ifdef _WIN32
39 return "\\";
40#else
41 return "/";
42#endif
43 }
44
45 PathName(cstring str) : str(str) {} // NOLINT(runtime/explicit)
46 PathName(const char *str) : str(str) {} // NOLINT(runtime/explicit)
47 PathName(const std::string &str) : str(str) {} // NOLINT(runtime/explicit)
48 // get the file name extension. It starts at the last dot.
49 // e.g, exe
50 cstring getExtension() const;
51 // extract just the filename, including the extension
52 // e.g., file.exe
53 PathName getFilename() const;
54 // extract the filename without folder, excluding the extension
55 // e.g., file
56 cstring getBasename() const;
57 // extract the folder
58 // e.g., /usr/local/bin
59 PathName getFolder() const;
60 cstring toString() const { return str; }
61 bool isNullOrEmpty() const { return str.isNullOrEmpty(); }
62 bool operator==(const PathName &other) const { return str == other.str; }
63 bool operator!=(const PathName &other) const { return str != other.str; }
64 PathName join(cstring component) const;
65
66 static PathName empty;
67};
68} // namespace Util
69
70#endif /* LIB_PATH_H_ */
Definition path.h:29
Definition cstring.h:72