-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ini_parser.zig
More file actions
253 lines (219 loc) · 7.79 KB
/
test_ini_parser.zig
File metadata and controls
253 lines (219 loc) · 7.79 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// INI Parser Test
// Tests the INI file parsing functionality used by C&C Generals
const std = @import("std");
// INI Parser implementation
const INIValue = union(enum) {
string: []const u8,
integer: i64,
float: f64,
boolean: bool,
};
const INIEntry = struct {
key: []const u8,
value: INIValue,
};
const INISection = struct {
name: []const u8,
entries: std.ArrayListUnmanaged(INIEntry),
fn init(name: []const u8) INISection {
return .{
.name = name,
.entries = .empty,
};
}
fn deinit(self: *INISection, allocator: std.mem.Allocator) void {
self.entries.deinit(allocator);
}
fn get(self: *const INISection, key: []const u8) ?INIValue {
for (self.entries.items) |entry| {
if (std.mem.eql(u8, entry.key, key)) {
return entry.value;
}
}
return null;
}
};
const INIParser = struct {
allocator: std.mem.Allocator,
sections: std.ArrayListUnmanaged(INISection),
current_section: ?*INISection,
fn init(allocator: std.mem.Allocator) INIParser {
return .{
.allocator = allocator,
.sections = .empty,
.current_section = null,
};
}
fn deinit(self: *INIParser) void {
for (self.sections.items) |*section| {
section.deinit(self.allocator);
}
self.sections.deinit(self.allocator);
}
fn parse(self: *INIParser, content: []const u8) !void {
var lines = std.mem.splitSequence(u8, content, "\n");
while (lines.next()) |line| {
const trimmed = std.mem.trim(u8, line, " \t\r");
// Skip empty lines and comments
if (trimmed.len == 0) continue;
if (trimmed[0] == ';' or trimmed[0] == '#') continue;
// Check for section header
if (trimmed[0] == '[') {
if (std.mem.indexOfScalar(u8, trimmed, ']')) |end| {
const section_name = trimmed[1..end];
try self.sections.append(self.allocator, INISection.init(section_name));
self.current_section = &self.sections.items[self.sections.items.len - 1];
}
continue;
}
// Parse key=value
if (std.mem.indexOfScalar(u8, trimmed, '=')) |eq_pos| {
const key = std.mem.trim(u8, trimmed[0..eq_pos], " \t");
const value_str = std.mem.trim(u8, trimmed[eq_pos + 1 ..], " \t");
const value = parseValue(value_str);
if (self.current_section) |section| {
try section.entries.append(self.allocator, .{ .key = key, .value = value });
}
}
}
}
fn parseValue(str: []const u8) INIValue {
// Try boolean
if (std.mem.eql(u8, str, "Yes") or std.mem.eql(u8, str, "yes") or std.mem.eql(u8, str, "true") or std.mem.eql(u8, str, "TRUE")) {
return .{ .boolean = true };
}
if (std.mem.eql(u8, str, "No") or std.mem.eql(u8, str, "no") or std.mem.eql(u8, str, "false") or std.mem.eql(u8, str, "FALSE")) {
return .{ .boolean = false };
}
// Try integer
if (std.fmt.parseInt(i64, str, 10)) |int_val| {
return .{ .integer = int_val };
} else |_| {}
// Try float
if (std.fmt.parseFloat(f64, str)) |float_val| {
return .{ .float = float_val };
} else |_| {}
// Default to string
return .{ .string = str };
}
fn getSection(self: *const INIParser, name: []const u8) ?*const INISection {
for (self.sections.items) |*section| {
if (std.mem.eql(u8, section.name, name)) {
return section;
}
}
return null;
}
};
// Test data matching C&C Generals INI format
const test_gamedata_ini =
\\; C&C Generals GameData.ini sample
\\
\\[GameData]
\\ShellMapName = Maps/ShellMap1/ShellMap1.map
\\MapName = Maps/Default/Default.map
\\MoveHintName = SCCursors.ani
\\UseFPSLimit = Yes
\\FramesPerSecondLimit = 30
\\ChipsetType = 0
\\MaxShellScreens = 4
\\UseCloudMap = Yes
\\UseLightMap = Yes
\\BilinearTerrainTex = Yes
\\TrilinearTerrainTex = No
\\MultiPassTerrain = No
\\AdjustClipDistances = No
\\StretchTerrain = Yes
\\UseHalfHeightMap = Yes
\\DrawEntireTerrain = No
\\TerrainLOD = AUTOMATIC
\\TerrainLODTargetTimeMS = 10
\\MaxCameraHeight = 275.0
\\MinCameraHeight = 120.0
\\MaxCameraHeightEdge = 300.0
\\MinCameraHeightEdge = 130.0
\\CameraAdjustSpeed = 0.3
\\ScrollAmountCutoff = 10.0
\\CameraPitch = 37.5
\\CameraYaw = 0.0
\\CameraHeight = 225.0
\\CameraScrollSpeedWhenMax = 0.8
\\CameraScrollSpeedWhenMin = 0.3
\\
\\[Multiplayer]
\\InitialCreditsMin = 5000
\\InitialCreditsMax = 100000
\\StartCountdownTimer = 5
\\MaxBeaconsPerPlayer = 3
\\UseShroud = Yes
\\ShowRandomPlayerTemplate = Yes
\\ShowRandomStartPos = Yes
\\ShowRandomColor = Yes
\\
\\[PlayerTemplate FactionAmerica]
\\Side = America
\\BaseSide = America
\\DisplayName = GUI:USA
\\StartingBuilding = AmericaCommandCenter
\\ProductionCostChange = 0%
\\ProductionTimeChange = 0%
\\IntrinsicSciences = SCIENCE_USA
\\IsPlayableSide = Yes
\\
;
pub fn main() !void {
const print = std.debug.print;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
print("================================================================================\n", .{});
print(" C&C Generals INI Parser Test\n", .{});
print("================================================================================\n\n", .{});
var parser = INIParser.init(allocator);
defer parser.deinit();
print("Parsing GameData.ini sample...\n\n", .{});
try parser.parse(test_gamedata_ini);
print("Parsed {d} sections:\n", .{parser.sections.items.len});
for (parser.sections.items) |section| {
print("\n[{s}] - {d} entries\n", .{ section.name, section.entries.items.len });
for (section.entries.items) |entry| {
switch (entry.value) {
.string => |s| print(" {s} = \"{s}\"\n", .{ entry.key, s }),
.integer => |i| print(" {s} = {d}\n", .{ entry.key, i }),
.float => |f| print(" {s} = {d:.2}\n", .{ entry.key, f }),
.boolean => |b| print(" {s} = {}\n", .{ entry.key, b }),
}
}
}
// Test specific value retrieval
print("\n--- Value Retrieval Tests ---\n", .{});
if (parser.getSection("GameData")) |section| {
if (section.get("MaxCameraHeight")) |val| {
switch (val) {
.float => |f| print("MaxCameraHeight = {d:.1}\n", .{f}),
else => print("MaxCameraHeight has unexpected type\n", .{}),
}
}
if (section.get("UseFPSLimit")) |val| {
switch (val) {
.boolean => |b| print("UseFPSLimit = {}\n", .{b}),
else => print("UseFPSLimit has unexpected type\n", .{}),
}
}
if (section.get("FramesPerSecondLimit")) |val| {
switch (val) {
.integer => |i| print("FramesPerSecondLimit = {d}\n", .{i}),
else => print("FramesPerSecondLimit has unexpected type\n", .{}),
}
}
}
if (parser.getSection("Multiplayer")) |section| {
if (section.get("InitialCreditsMin")) |val| {
switch (val) {
.integer => |i| print("InitialCreditsMin = {d}\n", .{i}),
else => print("InitialCreditsMin has unexpected type\n", .{}),
}
}
}
print("\nAll INI parser tests passed!\n", .{});
}