aboutsummaryrefslogtreecommitdiff
path: root/mapman/src/mapdump.cpp
blob: af32a0fafb900b5af884aebb5876393c00b31f44 (plain) (blame)
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
#include "mapdump.hpp"

#include <algorithm>

#include <zlib.h>

bool load_dump(gzFile f, map_t &d)
{
    if (gzread(f, &d.id, 4) < 4) return false;
    int name_len;
    if (gzread(f, &name_len, 4) < 4) return false;
    d.locked = ((name_len & 0xf0000000) != 0);
    name_len &= 0x7fffffff;
    if (name_len)
    {
        char *name = new char[name_len];
        if (gzread(f, name, name_len) != name_len)
        {
            delete[] name;
            return false;
        }
        d.custom_name = std::string(name, name_len);
        delete[] name;
    } else d.custom_name = std::string();
    if (gzread(f, d.map_data.data(), 128 * 128) < 128 * 128)
        return false;
    return true;
}

bool load_dumps(const char *fn, std::vector<map_t> &dumps)
{
    gzFile f = gzopen(fn, "rb");
    dumps.clear();
    while (!gzeof(f))
    {
        map_t d;
        if (load_dump(f, d))
            dumps.emplace_back(std::move(d));
    }
    gzclose(f);
    return dumps.size() != 0;
}

std::vector<int> load_tally(const char *fn)
{
    std::vector<int> ret;
    gzFile f = gzopen(fn, "rb");
    while (!gzeof(f))
    {
        int t;
        gzread(f, &t, 4);
        ret.push_back(t);
    }
    std::sort(ret.begin(), ret.end());
    auto uend = std::unique(ret.begin(), ret.end());
    ret.erase(uend, ret.end());
    return ret;
}