CppParser
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1/*
2 The MIT License (MIT)
3
4 Copyright (c) 2018 Satya Das
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include "cpputil.h"
25
26#include "utils.h"
27
28#include <fstream>
29
30namespace fs = boost::filesystem;
31
33{
34 auto rbeg = rev(identifier.sz + identifier.len);
35 assert(*rbeg == '>');
36
37 auto rend = rev(identifier.sz);
38 int numTempl = 1;
39 for (++rbeg; rbeg != rend; ++rbeg)
40 {
41 if (*rbeg == '<')
42 {
43 --numTempl;
44 if (numTempl == 0)
45 {
46 CppToken clsName {identifier.sz, static_cast<size_t>(std::distance(rbeg, rend)) - 1};
47 return clsName;
48 }
49 }
50 else if (*rbeg == '>')
51 {
52 ++numTempl;
53 }
54 }
55
56 return CppToken {nullptr, 0U};
57}
58
60{
61 if (identifier.sz == nullptr)
62 return identifier;
63
64 if (identifier.sz[identifier.len - 1] == '>')
66
67 const char* scopeResolutor = "::";
68 const char* end = identifier.sz + identifier.len;
69 auto itr = std::find_end(identifier.sz, end, scopeResolutor, scopeResolutor + 2);
70 if (itr == end)
71 return identifier;
72 // skip white chars
73 for (itr = itr + 2; (itr != end) && !isprint(*itr); ++itr)
74 ;
75 const auto clsNameLen = static_cast<size_t>(end - itr);
76 return CppToken {itr, clsNameLen};
77}
78
79std::string readFile(const std::string& filename)
80{
81 std::string contents;
82 std::ifstream in(filename, std::ios::in | std::ios::binary);
83 if (in)
84 {
85 in.seekg(0, std::ios::end);
86 size_t size = static_cast<size_t>(in.tellg());
87 contents.resize(size + 3); // For adding last 2 nulls and a new line.
88 in.seekg(0, std::ios::beg);
89 in.read(contents.data(), size);
90 in.close();
91 auto len = stripChar(contents.data(), size, '\r');
92 assert(len <= size);
93 contents.resize(len + 3);
94 contents[len] = '\n';
95 contents[len + 1] = '\0';
96 contents[len + 2] = '\0';
97 }
98 return contents;
99}
100
101void collectFiles(std::vector<std::string>& files, const fs::path& path, const CppProgFileSelecter& fileSelector)
102{
103 if (fs::is_regular_file(path))
104 {
105 auto file = path.string();
106 if (fileSelector(file))
107 files.push_back(std::move(file));
108 }
109 else if (fs::is_directory(path))
110 {
111 for (fs::directory_iterator dirItr(path); dirItr != fs::directory_iterator(); ++dirItr)
112 {
113 collectFiles(files, *dirItr, fileSelector);
114 }
115 }
116
117 if (!files.empty())
118 std::sort(files.begin(), files.end());
119}
120
121std::vector<CppToken> explode(CppToken token, const char* delim)
122{
123 auto const delimLen = strlen(delim);
124 std::vector<CppToken> elems;
125
126 for (auto* p = token.sz; p < (token.sz + token.len);)
127 {
128 auto* q = strstr(p, delim);
129 if (q != nullptr)
130 {
131 elems.push_back(CppToken {p, static_cast<size_t>(q - p)});
132 p = q + delimLen;
133 }
134 else
135 {
136 elems.push_back(CppToken {p, static_cast<size_t>(token.sz + token.len - p)});
137 break;
138 }
139 }
140
141 return elems;
142}
143
144std::string pruneClassName(const CppToken& identifier)
145{
146 std::string ret;
147 for (size_t i = 0; i < identifier.len; ++i)
148 {
149 if (isprint(identifier.sz[i]))
150 ret += identifier.sz[i];
151 }
152
153 return ret;
154}
std::function< bool(const std::string &)> CppProgFileSelecter
Definition: cppprog.h:53
size_t stripChar(char *s, size_t len, char c)
Definition: string-utils.h:30
size_t len
Definition: cpptoken.h:37
const char * sz
Definition: cpptoken.h:36
static CppToken classNameFromTemplatedIdentifier(const CppToken &identifier)
Definition: utils.cpp:32
CppToken classNameFromIdentifier(const CppToken &identifier)
Definition: utils.cpp:59
std::vector< CppToken > explode(CppToken token, const char *delim)
Definition: utils.cpp:121
std::string pruneClassName(const CppToken &identifier)
Definition: utils.cpp:144
void collectFiles(std::vector< std::string > &files, const fs::path &path, const CppProgFileSelecter &fileSelector)
Definition: utils.cpp:101
std::string readFile(const std::string &filename)
Definition: utils.cpp:79
std::reverse_iterator< Iter > rev(Iter i)
Definition: utils.h:32