File: FieldsDescription.cpp

package info (click to toggle)
libmediainfo 26.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,940 kB
  • sloc: cpp: 273,456; ansic: 6,574; asm: 2,100; xml: 1,476; java: 1,245; sh: 1,203; cs: 1,189; python: 653; makefile: 444; pascal: 197; javascript: 169
file content (203 lines) | stat: -rw-r--r-- 6,246 bytes parent folder | download | duplicates (2)
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
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license that can
 *  be found in the License.html file in the root of the source tree.
 */

// TODO: very ugly automation, it deserves a refactoring
// Usage: ..\..\.. ..\..\..\..\MediaArea-Website

#include <iostream>
#include <string>
#include "ZenLib/ZtringListList.h"
#include "ZenLib/File.h"
using namespace ZenLib;
using namespace std;

const char* StreamKinds[] =
{
    "General",
    "Video",
    "Audio",
    "Text",
    "Other",
    "Image",
    "Menu",
};

ZtringListList Load(const string& Name, const char* ToAdd = nullptr, bool IgnoreErrors = false) {
    File InF(Ztring().From_Local(Name));
    auto InF_Size = InF.Size_Get();
    if (InF_Size > ((size_t)-1) / 2)
        return {};
    auto Size = (size_t)InF_Size;
    if (Size == 0) {
        if (!IgnoreErrors)
            std::cerr << "Can not open " << Name << '\n';
        return {};
    }
    uint8_t* Buffer = new uint8_t[Size];
    if (InF.Read(Buffer, Size) != Size) {
        if (!IgnoreErrors)
            std::cerr << "Can not read " << Name << '\n';
        return {};
    }
    Ztring In;
    In.From_UTF8((const char*)Buffer, Size);
    delete[] Buffer;

    if (ToAdd) {
        In += Ztring().From_UTF8(ToAdd);
    }

    ZtringListList List;
    List.Write(In);
    if (List.empty()) {
        if (!IgnoreErrors)
            std::cerr << "Can not extract " << Name << '\n';
    }

    return std::move(List);
}


int main(int argc, char* argv[]) {
    string ToInsert;
    size_t i = 1;
    for (const auto& StreamKind : StreamKinds) {
        ToInsert += R"( <thead>
  <tr onclick="Line_Show(')";
        ToInsert += StreamKind;
        ToInsert += R"x(')" id=")x";
        ToInsert += StreamKind;
        ToInsert += R"(H" style="cursor: pointer">
   <td colspan="5" class="xheader">)";
        ToInsert += StreamKind;
        ToInsert += R"(</td>
  </tr>
  <tr onclick="Line_Hide(')";
        ToInsert += StreamKind;
        ToInsert += R"x(')" id=")x";
        ToInsert += StreamKind;
        ToInsert += R"(S" style="cursor: pointer">
   <td colspan="5" class="xheader">)";
        ToInsert += StreamKind;
        ToInsert += R"(</td>
  </tr>
 </thead>
 <tbody id=")";
        ToInsert += StreamKind;
        ToInsert += R"(">
)";

        string Name = string(argv[1]) + "/Source/Resource/Text/Stream/" + StreamKind + ".csv";
        string Name_Mapping = string(argv[1]) + "/Source/Resource/Text/Mapping/" + StreamKind + ".csv";

        const char* ToAdd = nullptr;
        if (strcmp(StreamKind, "General") == 0) {
            ToAdd = "IsTruncated;;; Y YIY;;; Indicate if the file is detected as truncated\n";
        }

        auto List = Load(Name, ToAdd);
        auto IgnoreMissingMapping = strcmp(StreamKind, "General") != 0;
        auto Mapping = Load(Name_Mapping, nullptr, IgnoreMissingMapping);
        if (List.empty() || (!IgnoreMissingMapping && Mapping.empty())) {
            return 1;
        }


        for (const auto& Line : List)
        {
            if (Line.size() <= 6 || Line[3].size() <= 2 || Line[3][2] != __T('Y'))
                continue;
            ToInsert += R"(  <tr>
   <td id="mi)";
            ToInsert += to_string(i);
            ToInsert += R"(">)";
            ToInsert += Line[0].To_UTF8();
            ToInsert += R"(</td>
   <td id="de)";
            ToInsert += to_string(i);
            ToInsert += R"(">)";
            if (!Line[6].empty())
                ToInsert += Line[6].To_UTF8();
            else
                ToInsert += "&nbsp;";
            ToInsert += R"(</td>
   <td id="mp)";
            ToInsert += to_string(i);
            ToInsert += R"(">)";
            auto j = Mapping.Find(Line[0]);

            if (j != (size_t)-1 && Mapping[j].size() > 1 && !Mapping[j][1].empty())
                ToInsert += Mapping[j][1].To_UTF8();
            else
                ToInsert += "&nbsp;";
            ToInsert += R"(</td>
   <td id="eb)";
            ToInsert += to_string(i);
            ToInsert += R"(">)";
            if (j != (size_t)-1 && Mapping[j].size() > 2 && !Mapping[j][2].empty())
                ToInsert += Mapping[j][2].To_UTF8();
            else
                ToInsert += "&nbsp;";
            ToInsert += R"(</td>
   <td id="pb)";
            ToInsert += to_string(i);
            ToInsert += R"(">)";
            if (j != (size_t)-1 && Mapping[j].size() > 3 && !Mapping[j][3].empty())
                ToInsert += Mapping[j][3].To_UTF8();
            else
                ToInsert += "&nbsp;";
            ToInsert += R"(</td>
  </tr>
)";
            i++;
        }
        ToInsert += R"( </tbody>
)";
    }

    string Name = string(argv[2]) + "/src/MediaInfoBundle/Resources/views/Support/fields.html.twig";
    File OutputF(Ztring().From_Local(Name));
    auto OutputF_Size = OutputF.Size_Get();
    if (OutputF_Size > ((size_t)-1) / 2)
        return {};
    auto Size = (size_t)OutputF_Size;
    if (Size == 0) {
        std::cerr << "Can not open " << Name << '\n';
        return 1;
    }
    uint8_t* Buffer = new uint8_t[Size + 1];
    if (OutputF.Read(Buffer, Size) != Size) {
        std::cerr << "Can not read " << Name << '\n';
        return 1;
    }
    Buffer[Size] = '\0';
    auto In = strstr((const char*)Buffer, " <thead>");
    if (!In) {
        std::cerr << "Can find start in " << Name << '\n';
        return 1;
    }
    In = strstr(In + 1, " <thead>");
    if (!In) {
        std::cerr << "Can find start in " << Name << '\n';
        return 1;
    }
    auto Out = strstr(In + 1, "</table>");
    if (!Out) {
        std::cerr << "Can find end in " << Name << '\n';
        return 1;
    }

    OutputF.Close();
    OutputF.Create(Ztring().From_Local(Name), true);
    OutputF.Write(Buffer, In - (const char*)Buffer);
    OutputF.Write((const int8u*)ToInsert.c_str(), ToInsert.size());
    OutputF.Write((const int8u*)Out, (const char*)Buffer + Size - Out);
    OutputF.Close();

    delete[] Buffer;

    return 0;
}