-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathutils.cpp
More file actions
136 lines (118 loc) · 3.36 KB
/
Copy pathutils.cpp
File metadata and controls
136 lines (118 loc) · 3.36 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#include "utils.h"
#include "cppparser/string-utils.h"
#include <filesystem>
#include <cassert>
#include <fstream>
namespace fs = std::filesystem;
static CppToken ClassNameFromTemplatedIdentifier(const CppToken& identifier)
{
auto rbeg = std::reverse_iterator(identifier.sz + identifier.len);
assert(*rbeg == '>');
auto rend = std::reverse_iterator(identifier.sz);
int numTempl = 1;
for (++rbeg; rbeg != rend; ++rbeg)
{
if (*rbeg == '<')
{
--numTempl;
if (numTempl == 0)
{
CppToken clsName {identifier.sz, static_cast<size_t>(std::distance(rbeg, rend)) - 1};
return clsName;
}
}
else if (*rbeg == '>')
{
++numTempl;
}
}
return CppToken {nullptr, 0U};
}
CppToken ClassNameFromIdentifier(const CppToken& identifier)
{
if (identifier.sz == nullptr)
return identifier;
if (identifier.sz[identifier.len - 1] == '>')
return ClassNameFromIdentifier(ClassNameFromTemplatedIdentifier(identifier));
const char* scopeResolutor = "::";
const char* end = identifier.sz + identifier.len;
auto itr = std::find_end(identifier.sz, end, scopeResolutor, scopeResolutor + 2);
if (itr == end)
return identifier;
// skip white chars
for (itr = itr + 2; (itr != end) && !isprint(*itr); ++itr)
;
const auto clsNameLen = static_cast<size_t>(end - itr);
return CppToken {itr, clsNameLen};
}
std::string ReadFile(const std::string& filename)
{
std::string contents;
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
in.seekg(0, std::ios::end);
size_t size = static_cast<size_t>(in.tellg());
contents.resize(size + 3); // For adding last 2 nulls and a new line.
in.seekg(0, std::ios::beg);
in.read(contents.data(), size);
in.close();
auto len = StripChar(contents.data(), size, '\r');
assert(len <= size);
contents.resize(len + 3);
contents[len] = '\n';
contents[len + 1] = '\0';
contents[len + 2] = '\0';
}
return contents;
}
// void collectFiles(std::vector<std::string>& files, const fs::path& path, const CppProgFileSelecter& fileSelector)
// {
// if (fs::is_regular_file(path))
// {
// auto file = path.string();
// if (fileSelector(file))
// files.push_back(std::move(file));
// }
// else if (fs::is_directory(path))
// {
// for (fs::directory_iterator dirItr(path); dirItr != fs::directory_iterator(); ++dirItr)
// {
// collectFiles(files, *dirItr, fileSelector);
// }
// }
// if (!files.empty())
// std::sort(files.begin(), files.end());
// }
std::vector<CppToken> Explode(CppToken token, const char* delim)
{
auto const delimLen = strlen(delim);
std::vector<CppToken> elems;
for (auto* p = token.sz; p < (token.sz + token.len);)
{
auto* q = strstr(p, delim);
if (q != nullptr)
{
elems.push_back(CppToken {p, static_cast<size_t>(q - p)});
p = q + delimLen;
}
else
{
elems.push_back(CppToken {p, static_cast<size_t>(token.sz + token.len - p)});
break;
}
}
return elems;
}
std::string PruneClassName(const CppToken& identifier)
{
std::string ret;
for (size_t i = 0; i < identifier.len; ++i)
{
if (isprint(identifier.sz[i]))
ret += identifier.sz[i];
}
return ret;
}