~mil/mepo

ref: 06fc4143c8068bcf968f0c91537b0792d1077197 mepo/src/util/utilprefs.zig -rw-r--r-- 4.0 KiB
06fc4143Miles Alan Fix bug with wordexp_filepath always returning error 2 months ago
                                                                                
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
const std = @import("std");

const PrefValueEnum = enum { b, t, f, u };
const PrefValueUnion = union(PrefValueEnum) { b: bool, t: ?[:0]const u8, f: f64, u: u8 };
const PrefMapping = struct {
    name: []const u8,
    value: PrefValueUnion,
    desc: []const u8,
};

const n_prefs = 16;
var prefs_mapping: [n_prefs]PrefMapping = mapping: {
    var mapping: [n_prefs]PrefMapping = undefined;
    mapping[0] = .{
        .name = "debug_message_enabled",
        .value = .{ .b = true },
        .desc = "TODO",
    };
    mapping[1] = .{
        .name = "distance_unit_tf_km_mi",
        .value = .{ .b = false },
        .desc = "TODO",
    };
    mapping[2] = .{
        .name = "overlay_debugbar",
        .value = .{ .b = true },
        .desc = "TODO",
    };
    mapping[3] = .{
        .name = "help",
        .value = .{ .b = false },
        .desc = "TODO",
    };
    mapping[4] = .{
        .name = "overlay_pindetails",
        .value = .{ .b = true },
        .desc = "TODO",
    };
    mapping[5] = .{
        .name = "crosshair_size",
        .value = .{ .u = 15 },
        .desc = "TODO",
    };
    mapping[6] = .{
        .name = "drag_scale",
        .value = .{ .u = 2 },
        .desc = "TODO",
    };
    mapping[7] = .{
        .name = "fontsize_ui",
        .value = .{ .u = 20 },
        .desc = "TODO",
    };
    mapping[8] = .{
        .name = "zoom",
        .value = .{ .u = 14 },
        .desc = "TODO",
    };
    mapping[9] = .{
        .name = "debug_tiles",
        .value = .{ .b = false },
        .desc = "TODO",
    };
    mapping[10] = .{
        .name = "debug_stderr",
        .value = .{ .b = false },
        .desc = "TODO",
    };
    mapping[11] = .{
        .name = "tile_cache_max_n_transfers",
        .value = .{ .u = 20 },
        .desc = "TODO",
    };
    mapping[12] = .{
        .name = "tile_cache_expiry_seconds",
        .value = .{ .f = -1 },
        .desc = "TODO",
    };
    mapping[13] = .{
        .name = "tile_cache_network",
        .value = .{ .b = true },
        .desc = "TODO",
    };
    mapping[14] = .{
        .name = "tile_cache_dir",
        .value = .{ .t = null },
        .desc = "TODO",
    };
    mapping[15] = .{
        .name = "tile_cache_url",
        .value = .{ .t = null },
        .desc = "TODO",
    };
    break :mapping mapping;
};

pub fn get_u8(prefname: []const u8) f64 {
    for (prefs_mapping) |pref| {
        if (std.mem.eql(u8, pref.name, prefname)) return pref.value.u;
    }
    return 0;
}

pub fn get(prefname: []const u8) PrefValueUnion {
    for (prefs_mapping) |pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            return pref.value;
        }
    }
    return .{ .b = false };
}

pub fn set_n(prefname: []const u8, value: f64) void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .b => {
                    pref.value = .{ .b = value == 1 };
                },
                .u => {
                    pref.value = .{ .u = @floatToInt(u8, value) };
                },
                .f => {
                    pref.value = .{ .f = value };
                },
                .t => {},
            }
        }
    }
}

pub fn set_t(allocator: std.mem.Allocator, prefname: []const u8, value: []const u8) !void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .t => {
                    if (pref.value.t) |heap_allocated_text| allocator.free(heap_allocated_text);
                    pref.value.t = try allocator.dupeZ(u8, value);
                },
                else => {}
            }
        }
    }
}

pub fn toggle_bool(prefname: []const u8) void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .b => {
                    pref.value = .{ .b = !pref.value.b };
                },
                else => {},
            }
        }
    }
}