File: gettextpoutils.cpp

package info (click to toggle)
poxml 4%3A17.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 464 kB
  • sloc: cpp: 1,623; xml: 212; makefile: 3; sh: 1
file content (156 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright 2014 Pino Toscano <pino@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

// #define POXML_DEBUG

#include "gettextpoutils.h"

#include <iostream>

static void gettext_xerror(int severity,
                           po_message_t /*message*/,
                           const char *filename, size_t /*lineno*/, size_t /*column*/,
                           int /*multiline_p*/, const char *message_text)
{
    std::cerr << severity << " " << filename << " " << message_text << std::endl;
}

static void gettext_xerror2(int severity,
                            po_message_t /*message1*/,
                            const char *filename1, size_t /*lineno1*/, size_t /*column1*/,
                            int /*multiline_p1*/, const char *message_text1,
                            po_message_t /*message2*/,
                            const char *filename2, size_t /*lineno2*/, size_t /*column2*/,
                            int /*multiline_p2*/, const char *message_text2)
{
    std::cerr << severity << " " << filename1 << " " << message_text1 << ", "
              << filename2 << " " << message_text2 << std::endl;
}

static void gettext_xerror_null(int,
                                po_message_t, const char *, size_t, size_t,
                                int, const char *)
{
}

static void gettext_xerror2_null(int,
                                 po_message_t, const char *, size_t, size_t,
                                 int, const char *,
                                 po_message_t, const char *, size_t, size_t,
                                 int, const char *)
{
}

po_xerror_handler po_msg_handler_cerr = { gettext_xerror, gettext_xerror2 };
po_xerror_handler po_msg_handler_null = { gettext_xerror_null, gettext_xerror2_null };

bool createPOWithHeader(const struct poheader *headers, const char *comments,
                        po_file_t *out_po, po_message_iterator_t *out_it)
{
#ifdef POXML_DEBUG
    std::cerr << "createPOWithHeader " << headers << ", " << comments << std::endl;
#endif
    po_file_t po = po_file_create();
    if (!po) {
        return false;
    }
    po_message_iterator_t it = po_message_iterator(po, NULL);
    if (!it) {
        po_file_free(po);
        return false;
    }

    po_message_t msg = po_message_create();
    if (!msg) {
        po_message_iterator_free(it);
        po_file_free(po);
        return false;
    }
    if (comments) {
        po_message_set_comments(msg, comments);
    }
    po_message_set_fuzzy(msg, 1);
    po_message_set_msgid(msg, "");
    po_message_set_msgstr(msg, "");
    po_message_insert(it, msg);

#ifdef POXML_DEBUG
    std::cerr << "start filling the header" << std::endl;
#endif
    const char *header = po_file_domain_header(po, NULL);
    char *newheader = (char *)header;
    for (const struct poheader *h = headers; h->name; ++h) {
#ifdef POXML_DEBUG
    std::cerr << "\theader, " << (void*)newheader << ", " << newheader << ", " << h->name << std::endl;
#endif
        char *oldheader = newheader;
        newheader = po_header_set_field(newheader, h->name, h->value);
        if (oldheader != header) {
            free(oldheader);
        }
        if (!newheader) {
            po_message_iterator_free(it);
            po_file_free(po);
            return false;
        }
    }
#ifdef POXML_DEBUG
    std::cerr << "done with the new header, setting it" << std::endl;
#endif
    po_message_set_msgstr(msg, newheader);
    free(newheader);

    *out_po = po;
    *out_it = it;

    return true;
}

bool readAndIteratePO(const char *file, const po_xerror_handler_t handler,
                      bool skipheader, poiteratefn fn, void *data)
{
    po_file_t po = po_file_read(file, handler);
    if (!po) {
        return false;
    }

    po_message_iterator_t it = po_message_iterator(po, NULL);
    po_message_t msg;
    if (skipheader) {
        msg = po_next_message(it);
        if (!msg || po_message_msgid(msg)[0] != '\0') {
            po_message_iterator_free(it);
            po_file_free(po);
            return false;
        }
    }

    while ((msg = po_next_message(it))) {
        if (!fn(msg, data)) {
            po_message_iterator_free(it);
            po_file_free(po);
            return false;
        }
    }

    po_message_iterator_free(it);
    po_file_free(po);

    return true;
}