source: appstream-generator/src/asgen/backends/archlinux/listfile.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 2.5 KB
Line 
1/*
2 * Copyright (C) 2016 Matthias Klumpp <matthias@tenstral.net>
3 *
4 * Licensed under the GNU Lesser General Public License Version 3
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the license, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20module asgen.backends.archlinux.listfile;
21
22import std.stdio;
23import std.string;
24
25
26final class ListFile
27{
28
29private:
30    string[string] entries;
31
32public:
33
34    this ()
35    {
36    }
37
38    void loadData (const(ubyte)[] data)
39    {
40        string[] content;
41        auto dataStr = cast(string) data;
42        content = dataStr.splitLines ();
43
44        string blockName = null;
45        foreach (l; content) {
46            if ((l.startsWith ("%")) && (l.endsWith ("%"))) {
47                blockName = l[1..$-1];
48                continue;
49            }
50
51            if (l == "") {
52                blockName = null;
53                continue;
54            }
55
56            if (blockName !is null) {
57                if (blockName in entries)
58                    entries[blockName] ~= "\n" ~ l;
59                else
60                    entries[blockName] = l;
61                continue;
62            }
63        }
64    }
65
66    string getEntry (string name)
67    {
68        auto resP = name in entries;
69
70        if (resP is null)
71            // we found nothing
72            return null;
73        return *resP;
74    }
75}
76
77unittest
78{
79    writeln ("TEST: ", "Backend::Archlinux - ListFile");
80
81    string data = """%FILENAME%
82a2ps-4.14-6-x86_64.pkg.tar.xz
83
84%NAME%
85a2ps
86
87%VERSION%
884.14-6
89
90%DESC%
91An Any to PostScript filter
92
93%CSIZE%
94629320
95
96%MULTILINE%
97Blah1
98BLUBB2
99EtcEtcEtc3
100
101%SHA256SUM%
102a629a0e0eca0d96a97eb3564f01be495772439df6350600c93120f5ac7f3a1b5""";
103
104    auto lf = new ListFile ();
105    lf.loadData (cast(ubyte[]) data);
106
107    assert (lf.getEntry ("FILENAME") == "a2ps-4.14-6-x86_64.pkg.tar.xz");
108    assert (lf.getEntry ("VERSION") == "4.14-6");
109    assert (lf.getEntry ("MULTILINE") == "Blah1\nBLUBB2\nEtcEtcEtc3");
110    assert (lf.getEntry ("SHA256SUM") == "a629a0e0eca0d96a97eb3564f01be495772439df6350600c93120f5ac7f3a1b5");
111}
Note: See TracBrowser for help on using the repository browser.