Welcome to mirror list, hosted at ThFree Co, Russian Federation.

msgfmt.c « msgfmt « blentranslation « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 720be11d3b73fdafea5a8cafb4f241c1b6c0c3bf (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * 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.
 *
 * The Original Code is Copyright (C) 2017 by Blender Foundation.
 * All rights reserved.
 */

/*
 * Based on C++ version by Sergey Sharybin <sergey.vfx@gmail.com>.
 * Based on Python script msgfmt.py from Python source code tree, which was written by
 * Martin v. Löwis <loewis@informatik.hu-berlin.de>
 *
 * Generate binary message catalog from textual translation description.
 *
 * This program converts a textual Uniform-style message catalog (.po file)
 * into a binary GNU catalog (.mo file).
 * This is essentially the same function as the GNU msgfmt program,
 * however, it is a simpler implementation.
 *
 * Usage: msgfmt input.po output.po
 */

#include <stdlib.h>
#include <string.h>

#include "BLI_dynstr.h"
#include "BLI_fileops.h"
#include "BLI_ghash.h"
#include "BLI_linklist.h"
#include "BLI_memarena.h"
#include "BLI_utildefines.h"

#include "MEM_guardedalloc.h"

/* Stupid stub necessary because some BLI files includes winstuff.h, which uses G a bit... */
#ifdef WIN32
typedef struct Global {
  void *dummy;
} Global;

Global G;
#endif

/* We cannot use NULL char until ultimate step, would give nightmare to our C string processing...
 * Using one of the UTF-8 invalid bytes (as per our BLI string_utf8.c) */
#define NULLSEP_STR "\xff"
#define NULLSEP_CHR '\xff'

typedef enum {
  SECTION_NONE = 0,
  SECTION_CTX = 1,
  SECTION_ID = 2,
  SECTION_STR = 3,
} eSectionType;

typedef struct Message {
  DynStr *ctxt;
  DynStr *id;
  DynStr *str;

  bool is_fuzzy;
} Message;

static char *trim(char *str)
{
  const size_t len = strlen(str);
  size_t i;

  if (len == 0) {
    return str;
  }

  for (i = 0; i < len && ELEM(str[0], ' ', '\t', '\r', '\n'); str++, i++) {
    /* pass */
  }

  char *end = &str[len - 1 - i];
  for (i = len; i > 0 && ELEM(end[0], ' ', '\t', '\r', '\n'); end--, i--) {
    /* pass */
  }

  end[1] = '\0';

  return str;
}

static char *unescape(char *str)
{
  char *curr, *next;
  for (curr = next = str; next[0] != '\0'; curr++, next++) {
    if (next[0] == '\\') {
      switch (next[1]) {
        case '\0':
          /* Get rid of trailing escape char... */
          curr--;
          break;
        case '\\':
          *curr = '\\';
          next++;
          break;
        case 'n':
          *curr = '\n';
          next++;
          break;
        case 't':
          *curr = '\t';
          next++;
          break;
        default:
          /* Get rid of useless escape char. */
          next++;
          *curr = *next;
      }
    }
    else if (curr != next) {
      *curr = *next;
    }
  }
  *curr = '\0';

  if (str[0] == '"' && *(curr - 1) == '"') {
    *(curr - 1) = '\0';
    return str + 1;
  }
  return str;
}

static int qsort_str_cmp(const void *a, const void *b)
{
  return strcmp(*(const char **)a, *(const char **)b);
}

static char **get_keys_sorted(GHash *messages, const uint32_t num_keys)
{
  GHashIterator iter;

  char **keys = MEM_mallocN(sizeof(*keys) * num_keys, __func__);
  char **k = keys;

  GHASH_ITER (iter, messages) {
    *k = BLI_ghashIterator_getKey(&iter);
    k++;
  }

  qsort(keys, num_keys, sizeof(*keys), qsort_str_cmp);

  return keys;
}

BLI_INLINE size_t uint32_to_bytes(const int value, char *bytes)
{
  size_t i;
  for (i = 0; i < sizeof(value); i++) {
    bytes[i] = (char)((value >> ((int)i * 8)) & 0xff);
  }
  return i;
}

BLI_INLINE size_t msg_to_bytes(char *msg, char *bytes, uint32_t size)
{
  /* Note that we also perform replacing of our NULLSEP placeholder by real NULL char... */
  size_t i;
  for (i = 0; i < size; i++, msg++, bytes++) {
    *bytes = (*msg == NULLSEP_CHR) ? '\0' : *msg;
  }
  return i;
}

typedef struct Offset {
  uint32_t key_offset, key_len, val_offset, val_len;
} Offset;

/* Return the generated binary output. */
static char *generate(GHash *messages, size_t *r_output_size)
{
  const uint32_t num_keys = BLI_ghash_len(messages);

  /* Get list of sorted keys. */
  char **keys = get_keys_sorted(messages, num_keys);
  char **vals = MEM_mallocN(sizeof(*vals) * num_keys, __func__);
  uint32_t tot_keys_len = 0;
  uint32_t tot_vals_len = 0;

  Offset *offsets = MEM_mallocN(sizeof(*offsets) * num_keys, __func__);

  for (int i = 0; i < num_keys; i++) {
    Offset *off = &offsets[i];

    vals[i] = BLI_ghash_lookup(messages, keys[i]);

    /* For each string, we need size and file offset.
     * Each string is NULL terminated; the NULL does not count into the size. */
    off->key_offset = tot_keys_len;
    off->key_len = (uint32_t)strlen(keys[i]);
    tot_keys_len += off->key_len + 1;

    off->val_offset = tot_vals_len;
    off->val_len = (uint32_t)strlen(vals[i]);
    tot_vals_len += off->val_len + 1;
  }

  /* The header is 7 32-bit unsigned integers.
   * Then comes the keys index table, then the values index table. */
  const uint32_t idx_keystart = 7 * 4;
  const uint32_t idx_valstart = idx_keystart + 8 * num_keys;
  /* We don't use hash tables, so the keys start right after the index tables. */
  const uint32_t keystart = idx_valstart + 8 * num_keys;
  /* and the values start after the keys */
  const uint32_t valstart = keystart + tot_keys_len;

  /* Final buffer representing the binary MO file. */
  *r_output_size = valstart + tot_vals_len;
  char *output = MEM_mallocN(*r_output_size, __func__);
  char *h = output;
  char *ik = output + idx_keystart;
  char *iv = output + idx_valstart;
  char *k = output + keystart;
  char *v = output + valstart;

  h += uint32_to_bytes(0x950412de, h);   /* Magic */
  h += uint32_to_bytes(0x0, h);          /* Version */
  h += uint32_to_bytes(num_keys, h);     /* Number of entries */
  h += uint32_to_bytes(idx_keystart, h); /* Start of key index */
  h += uint32_to_bytes(idx_valstart, h); /* Start of value index */
  h += uint32_to_bytes(0, h);            /* Size of hash table */
  h += uint32_to_bytes(0, h);            /* Offset of hash table */

  BLI_assert(h == ik);

  for (int i = 0; i < num_keys; i++) {
    Offset *off = &offsets[i];

    /* The index table first has the list of keys, then the list of values.
     * Each entry has first the size of the string, then the file offset. */
    ik += uint32_to_bytes(off->key_len, ik);
    ik += uint32_to_bytes(off->key_offset + keystart, ik);
    iv += uint32_to_bytes(off->val_len, iv);
    iv += uint32_to_bytes(off->val_offset + valstart, iv);

    k += msg_to_bytes(keys[i], k, off->key_len + 1);
    v += msg_to_bytes(vals[i], v, off->val_len + 1);
  }

  BLI_assert(ik == output + idx_valstart);
  BLI_assert(iv == output + keystart);
  BLI_assert(k == output + valstart);

  MEM_freeN(keys);
  MEM_freeN(vals);
  MEM_freeN(offsets);

  return output;
}

/* Add a non-fuzzy translation to the dictionary. */
static void add(GHash *messages, MemArena *memarena, const Message *msg)
{
  const size_t msgctxt_len = (size_t)BLI_dynstr_get_len(msg->ctxt);
  const size_t msgid_len = (size_t)BLI_dynstr_get_len(msg->id);
  const size_t msgstr_len = (size_t)BLI_dynstr_get_len(msg->str);
  const size_t msgkey_len = msgid_len + ((msgctxt_len == 0) ? 0 : msgctxt_len + 1);

  if (!msg->is_fuzzy && msgstr_len != 0) {
    char *msgkey = BLI_memarena_alloc(memarena, sizeof(*msgkey) * (msgkey_len + 1));
    char *msgstr = BLI_memarena_alloc(memarena, sizeof(*msgstr) * (msgstr_len + 1));

    if (msgctxt_len != 0) {
      BLI_dynstr_get_cstring_ex(msg->ctxt, msgkey);
      msgkey[msgctxt_len] = '\x04'; /* Context/msgid separator */
      BLI_dynstr_get_cstring_ex(msg->id, &msgkey[msgctxt_len + 1]);
    }
    else {
      BLI_dynstr_get_cstring_ex(msg->id, msgkey);
    }

    BLI_dynstr_get_cstring_ex(msg->str, msgstr);

    BLI_ghash_insert(messages, msgkey, msgstr);
  }
}

static void clear(Message *msg)
{
  BLI_dynstr_clear(msg->ctxt);
  BLI_dynstr_clear(msg->id);
  BLI_dynstr_clear(msg->str);
  msg->is_fuzzy = false;
}

static int make(const char *input_file_name, const char *output_file_name)
{
  GHash *messages = BLI_ghash_new(BLI_ghashutil_strhash_p_murmur, BLI_ghashutil_strcmp, __func__);
  MemArena *msgs_memarena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);

  const char *msgctxt_kw = "msgctxt";
  const char *msgid_kw = "msgid";
  const char *msgid_plural_kw = "msgid_plural";
  const char *msgstr_kw = "msgstr";
  const size_t msgctxt_len = strlen(msgctxt_kw);
  const size_t msgid_len = strlen(msgid_kw);
  const size_t msgid_plural_len = strlen(msgid_plural_kw);
  const size_t msgstr_len = strlen(msgstr_kw);

  /* Note: For now, we assume file encoding is always utf-8. */

  eSectionType section = SECTION_NONE;
  bool is_plural = false;

  Message msg = {
      .ctxt = BLI_dynstr_new_memarena(),
      .id = BLI_dynstr_new_memarena(),
      .str = BLI_dynstr_new_memarena(),
      .is_fuzzy = false,
  };

  LinkNode *input_file_lines = BLI_file_read_as_lines(input_file_name);
  LinkNode *ifl = input_file_lines;

  /* Parse the catalog. */
  for (int lno = 1; ifl; ifl = ifl->next, lno++) {
    char *l = ifl->link;
    const bool is_comment = (l[0] == '#');
    /* If we get a comment line after a msgstr, this is a new entry. */
    if (is_comment) {
      if (section == SECTION_STR) {
        add(messages, msgs_memarena, &msg);
        clear(&msg);
        section = SECTION_NONE;
      }
      /* Record a fuzzy mark. */
      if (l[1] == ',' && strstr(l, "fuzzy") != NULL) {
        msg.is_fuzzy = true;
      }
      /* Skip comments */
      continue;
    }
    if (strstr(l, msgctxt_kw) == l) {
      if (section == SECTION_STR) {
        /* New message, output previous section. */
        add(messages, msgs_memarena, &msg);
      }
      if (!ELEM(section, SECTION_NONE, SECTION_STR)) {
        printf("msgctxt not at start of new message on %s:%d\n", input_file_name, lno);
        return EXIT_FAILURE;
      }
      section = SECTION_CTX;
      l = l + msgctxt_len;
      clear(&msg);
    }
    else if (strstr(l, msgid_plural_kw) == l) {
      /* This is a message with plural forms. */
      if (section != SECTION_ID) {
        printf("msgid_plural not preceded by msgid on %s:%d\n", input_file_name, lno);
        return EXIT_FAILURE;
      }
      l = l + msgid_plural_len;
      BLI_dynstr_append(msg.id, NULLSEP_STR); /* separator of singular and plural */
      is_plural = true;
    }
    else if (strstr(l, msgid_kw) == l) {
      if (section == SECTION_STR) {
        add(messages, msgs_memarena, &msg);
      }
      if (section != SECTION_CTX) {
        clear(&msg);
      }
      section = SECTION_ID;
      l = l + msgid_len;
      is_plural = false;
    }
    else if (strstr(l, msgstr_kw) == l) {
      l = l + msgstr_len;
      // Now we are in a msgstr section
      section = SECTION_STR;
      if (l[0] == '[') {
        if (!is_plural) {
          printf("plural without msgid_plural on %s:%d\n", input_file_name, lno);
          return EXIT_FAILURE;
        }
        if ((l = strchr(l, ']')) == NULL) {
          printf("Syntax error on %s:%d\n", input_file_name, lno);
          return EXIT_FAILURE;
        }
        if (BLI_dynstr_get_len(msg.str) != 0) {
          BLI_dynstr_append(msg.str, NULLSEP_STR); /* Separator of the various plural forms. */
        }
      }
      else {
        if (is_plural) {
          printf("indexed msgstr required for plural on %s:%d\n", input_file_name, lno);
          return EXIT_FAILURE;
        }
      }
    }
    /* Skip empty lines. */
    l = trim(l);
    if (l[0] == '\0') {
      if (section == SECTION_STR) {
        add(messages, msgs_memarena, &msg);
        clear(&msg);
      }
      section = SECTION_NONE;
      continue;
    }
    l = unescape(l);
    if (section == SECTION_CTX) {
      BLI_dynstr_append(msg.ctxt, l);
    }
    else if (section == SECTION_ID) {
      BLI_dynstr_append(msg.id, l);
    }
    else if (section == SECTION_STR) {
      BLI_dynstr_append(msg.str, l);
    }
    else {
      printf("Syntax error on %s:%d\n", input_file_name, lno);
      return EXIT_FAILURE;
    }
  }
  /* Add last entry */
  if (section == SECTION_STR) {
    add(messages, msgs_memarena, &msg);
  }

  BLI_dynstr_free(msg.ctxt);
  BLI_dynstr_free(msg.id);
  BLI_dynstr_free(msg.str);
  BLI_file_free_lines(input_file_lines);

  /* Compute output */
  size_t output_size;
  char *output = generate(messages, &output_size);

  FILE *fp = BLI_fopen(output_file_name, "wb");
  fwrite(output, 1, output_size, fp);
  fclose(fp);

  MEM_freeN(output);
  BLI_ghash_free(messages, NULL, NULL);
  BLI_memarena_free(msgs_memarena);

  return EXIT_SUCCESS;
}

int main(int argc, char **argv)
{
  if (argc != 3) {
    printf("Usage: %s <input.po> <output.mo>\n", argv[0]);
    return EXIT_FAILURE;
  }
  const char *input_file = argv[1];
  const char *output_file = argv[2];

  return make(input_file, output_file);
}