-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwithin.zig
More file actions
57 lines (47 loc) · 1.59 KB
/
within.zig
File metadata and controls
57 lines (47 loc) · 1.59 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
const std = @import("std");
const maxminddb = @import("maxminddb");
const db_path = "test-data/test-data/GeoLite2-City-Test.mmdb";
pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init;
const allocator = gpa.allocator();
defer _ = gpa.detectLeaks();
var db = try maxminddb.Reader.mmap(allocator, db_path);
defer db.unmap();
const network = if (db.metadata.ip_version == 4)
maxminddb.Network.all_ipv4
else
maxminddb.Network.all_ipv6;
var it = try db.within(allocator, maxminddb.geolite2.City, network, .{});
defer it.deinit();
// The iterator owns the values; each next() call invalidates the previous item.
var n: usize = 0;
while (try it.next()) |item| {
const continent = item.value.continent.code;
const country = item.value.country.iso_code;
var city: []const u8 = "";
if (item.value.city.names) |city_names| {
city = city_names.get("en") orelse "";
}
if (city.len != 0) {
std.debug.print("{f} {s}-{s}-{s}\n", .{
item.network,
continent,
country,
city,
});
} else if (country.len != 0) {
std.debug.print("{f} {s}-{s}\n", .{
item.network,
continent,
country,
});
} else if (continent.len != 0) {
std.debug.print("{f} {s}\n", .{
item.network,
continent,
});
}
n += 1;
}
std.debug.print("processed {d} items\n", .{n});
}