-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_spec.cc
82 lines (69 loc) · 2.46 KB
/
func_spec.cc
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
#include <utility>
#include <sstream>
#include <optional>
#include <cstring>
#include <iterator>
#include "converter.h"
namespace converter::func_spec {
ArgType Arg::parse_arg_type(char const* argtype) {
if (strcmp(argtype, "int") == 0) return ArgType::int_t;
if (strcmp(argtype, "uint") == 0) return ArgType::uint_t;
if (strcmp(argtype, "long") == 0) return ArgType::long_t;
if (strcmp(argtype, "ulong") == 0) return ArgType::ulong_t;
if (strcmp(argtype, "longlong") == 0) return ArgType::longlong_t;
if (strcmp(argtype, "ulonglong") == 0) return ArgType::ulonglong_t;
if (strcmp(argtype, "ptr") == 0) return ArgType::ptr_t;
throw std::invalid_argument{std::string{"invalid argument specified for function: "} + argtype + "."};
}
size_t Arg::bytes_32() const {
switch (type) {
case ArgType::longlong_t:
case ArgType::ulonglong_t:
return 8;
default:
return 4;
}
}
size_t Arg::bytes_64() const {
switch (type) {
case ArgType::int_t:
case ArgType::uint_t:
return 4;
default:
return 8;
}
}
bool Arg::size_differs() const {
return bytes_32() != bytes_64();
}
std::optional<ArgType> Ret::parse_ret_type(char const* argtype) {
if (strcmp(argtype, "void") == 0) return {};
return std::make_optional<>(Arg::parse_arg_type(argtype));
}
Function Function::from_line_decl(std::string const& decl) {
std::istringstream iss{decl};
std::string name;
iss >> name;
std::string ret_type;
iss >> ret_type;
Ret ret{ret_type.c_str()};
std::vector<Arg> args;
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(args));
if (args.size() > 6) {
throw UnsupportedFileContent{"More than 6 args specified for a function <" + name + ">."};
}
return Function(std::move(name), std::move(ret), std::move(args));
}
Functions::Functions(std::ifstream& func_stream) {
std::string buff;
while (!func_stream.eof()) {
std::getline(func_stream, buff);
if (buff.empty())
break;
functions.insert(Function::from_line_decl(buff));
buff.clear();
}
}
}