diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-23 01:47:18 +0000 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-23 01:47:18 +0000 |
commit | 4e206fae5be6093b36c7779ba2a4a3679f0e754b (patch) | |
tree | 3a1377ae7781debeb04c17464f9eab13f7e47131 /heading.cpp | |
download | godot3-tscn-compactor-4e206fae5be6093b36c7779ba2a4a3679f0e754b.tar.gz godot3-tscn-compactor-4e206fae5be6093b36c7779ba2a4a3679f0e754b.tar.bz2 godot3-tscn-compactor-4e206fae5be6093b36c7779ba2a4a3679f0e754b.zip |
something
Diffstat (limited to 'heading.cpp')
-rw-r--r-- | heading.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/heading.cpp b/heading.cpp new file mode 100644 index 0000000..ff4a1a6 --- /dev/null +++ b/heading.cpp | |||
@@ -0,0 +1,121 @@ | |||
1 | #include <stdlib.h> | ||
2 | |||
3 | #include <sstream> | ||
4 | #include <stdexcept> | ||
5 | #include <string> | ||
6 | #include <string_view> | ||
7 | |||
8 | #include "heading.h" | ||
9 | |||
10 | Heading::Heading(std::string_view line) { | ||
11 | std::string original_line(line); | ||
12 | |||
13 | if (line[0] != '[') { | ||
14 | std::ostringstream errormsg; | ||
15 | errormsg << "Heading must start with [." << std::endl | ||
16 | << "Bad heading: " << original_line; | ||
17 | throw std::invalid_argument(errormsg.str()); | ||
18 | } | ||
19 | |||
20 | line.remove_prefix(1); | ||
21 | int divider = line.find_first_of(" ]"); | ||
22 | if (divider == std::string_view::npos) { | ||
23 | std::ostringstream errormsg; | ||
24 | errormsg << "Malformatted heading: " << line << std::endl | ||
25 | << "Original line: " << original_line; | ||
26 | throw std::invalid_argument(errormsg.str()); | ||
27 | } | ||
28 | |||
29 | type_ = line.substr(0, divider); | ||
30 | line.remove_prefix(divider + 1); | ||
31 | |||
32 | while (!line.empty()) { | ||
33 | divider = line.find_first_of("="); | ||
34 | if (divider == std::string_view::npos) { | ||
35 | std::ostringstream errormsg; | ||
36 | errormsg << "Malformatted heading: " << line << std::endl | ||
37 | << "Original line: " << original_line; | ||
38 | throw std::invalid_argument(errormsg.str()); | ||
39 | } | ||
40 | |||
41 | std::string key(line.substr(0, divider)); | ||
42 | line.remove_prefix(divider + 1); | ||
43 | |||
44 | if (line[0] == '"') { | ||
45 | line.remove_prefix(1); | ||
46 | divider = line.find_first_of("\""); | ||
47 | |||
48 | if (divider == std::string_view::npos) { | ||
49 | std::ostringstream errormsg; | ||
50 | errormsg << "Malformatted heading: " << line << std::endl | ||
51 | << "Original line: " << original_line; | ||
52 | throw std::invalid_argument(errormsg.str()); | ||
53 | } | ||
54 | |||
55 | std::string strval(line.substr(0, divider)); | ||
56 | line.remove_prefix(divider + 2); | ||
57 | |||
58 | keyvals_[key] = strval; | ||
59 | } else if (line[0] == 'S' || line[0] == 'E') { | ||
60 | ResourceRef rrval; | ||
61 | rrval.internal = (line[0] == 'S'); | ||
62 | |||
63 | line.remove_prefix(13); // SubResource(# | ||
64 | divider = line.find_first_of(" "); | ||
65 | |||
66 | if (divider == std::string_view::npos) { | ||
67 | std::ostringstream errormsg; | ||
68 | errormsg << "Malformatted heading: " << line << std::endl | ||
69 | << "Original line: " << original_line; | ||
70 | throw std::invalid_argument(errormsg.str()); | ||
71 | } | ||
72 | |||
73 | rrval.id = std::atoi(line.substr(0, divider).data()); | ||
74 | line.remove_prefix(divider + 3); | ||
75 | |||
76 | keyvals_[key] = rrval; | ||
77 | } else { | ||
78 | divider = line.find_first_of(" ]"); | ||
79 | |||
80 | if (divider == std::string_view::npos) { | ||
81 | std::ostringstream errormsg; | ||
82 | errormsg << "Malformatted heading: " << line << std::endl | ||
83 | << "Original line: " << original_line; | ||
84 | throw std::invalid_argument(errormsg.str()); | ||
85 | } | ||
86 | |||
87 | int numval = std::atoi(line.substr(0, divider).data()); | ||
88 | line.remove_prefix(divider + 1); | ||
89 | |||
90 | keyvals_[key] = numval; | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | std::string Heading::ToString() const { | ||
96 | std::ostringstream output; | ||
97 | output << "["; | ||
98 | output << type_; | ||
99 | |||
100 | for (const auto& [key, value] : keyvals_) { | ||
101 | output << " " << key << "="; | ||
102 | |||
103 | if (std::holds_alternative<int>(value)) { | ||
104 | output << std::get<int>(value); | ||
105 | } else if (std::holds_alternative<ResourceRef>(value)) { | ||
106 | const auto& rrval = std::get<ResourceRef>(value); | ||
107 | if (rrval.internal) { | ||
108 | output << "Sub"; | ||
109 | } else { | ||
110 | output << "Ext"; | ||
111 | } | ||
112 | output << "Resource( " << rrval.id << " )"; | ||
113 | } else { | ||
114 | output << "\"" << std::get<std::string>(value) << "\""; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | output << "]" << std::endl; | ||
119 | |||
120 | return output.str(); | ||
121 | } | ||