-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdb-db.c
3084 lines (2586 loc) · 82.9 KB
/
pdb-db.c
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PReVo - A portable version of ReVo for Android
* Copyright (C) 2012, 2014, 2016, 2017 Neil Roberts
*
* 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; version 2 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include "pdb-db.h"
#include "pdb-lang.h"
#include "pdb-error.h"
#include "pdb-doc.h"
#include "pdb-mkdir.h"
#include "pdb-error.h"
#include "pdb-strcmp.h"
#include "pdb-roman.h"
#include "pdb-list.h"
#include "pdb-file.h"
#include "pdb-span.h"
#include "pdb-trim.h"
/* An article file comprises of up to 16 articles. The articles are
* bundled together to reduce the number of files because the package
* installer in older versions of Android seems to have a bug with
* large numbers of files. We don't want to put them all in one file
* however because it is not possible to seek within an asset file. */
/* Each article in the file has the following format:
* • A 4-byte little endian number representing the size of the
* article data in bytes
* • A spanned string representing the title of the article.
* • A list of sections for the article.
*
* Each section consists of the following:
*
* • A three-byte string representing the language code for this
* section. If the code is shorter than three bytes then the
* remaining bytes will be set to zero. The code will usually be
* ‘eo’ for esperanto but it can but other codes for the translated
* sections.
* • A spanned string for the section title.
* • A spanned string with the section contents.
*
* A spanned string comprises of:
* • A two byte little endian number for the length of the string data
* • The string data in UTF-8
* • A list of string spans. These comprise of:
* • A two-byte length to mark the end of the span
* • A two-byte string offset to mark the start of the span
* • Two 16-bit numbers of extra data whose meaning depends on the
* span type number above.
* • One byte as a number representing the intended formatting of the span.
* The list of spans is terminated by two zero bytes (which would otherwise
* appear as the start of a zero-length span).
*
* The length and string offset are counted in 16-bit units as if the
* string was encoded in UTF-16.
*/
/* If a single output file is selected, the format is as follows:
* • Three bytes for the magic string ‘PRD’
* • A one byte file format version number. The current version is 67
* • Four bytes in little-endian format representing the number of articles.
* • Four bytes in little-endian for each article representing the
* offset in the file to the data for that article.
* • Four bytes in little-endian representing the number of languages.
* • A table of offsets to the indices for each language consisting of:
* • Four bytes reserved for a null terminated string representing the
* language code
* • Four bytes in little-endian for the offset to the index for that
* language.
* The entry for each language is a fixed length which means the table can
* be binary chopped.
* • The data for each language. This consists of:
* • A null-terminated string in UTF-8 representing the full language name.
* • The data for the index as described in pdb-trie.c
* • The data for each article in the format described above except for the
* following:
* • The span offsets and lengths are stored as byte counts rather than
* as counts of UTF-16 code points.
*/
/* The first version of the database is 66 because the magic string
* used to be the four letter code ‘PRDB’. The B has been hijacked as
* a version number.
*
* Subsequent changes to the format are:
*
* Version 67:
* • The language codes for each section were added.
*/
static const char pdb_db_magic[4] = "PRDC";
typedef struct
{
int length;
char *text;
/* A list of PdbSpans */
PdbList spans;
} PdbDbSpannableString;
typedef struct
{
int section_num;
char lang_code[4];
PdbDbSpannableString title;
PdbDbSpannableString text;
} PdbDbSection;
typedef struct
{
int article_num;
PdbDbSpannableString title;
/* A list of PdbDbSections */
GList *sections;
} PdbDbArticle;
/* A reference represents a position in an article. The reference can
* either directly contain a pointer to the section and article or it
* can refer to it indirectly via a mark name. These are used in a
* PdbDbLink to store the location the link refers to and also as the
* value of the index entries */
typedef enum
{
PDB_DB_REFERENCE_TYPE_MARK,
PDB_DB_REFERENCE_TYPE_DIRECT
} PdbDbReferenceType;
typedef struct
{
PdbDbReferenceType type;
union
{
struct
{
PdbDbArticle *article;
PdbDbSection *section;
} direct;
char *mark;
} d;
} PdbDbReference;
/* A link stores a delayed reference to a section from a reference
* span. These are all collected in the 'references' list so that they
* can be resolved later once all of the articles are loaded */
typedef struct
{
PdbDbSpannableString *string;
PdbSpan *span;
PdbDbReference *reference;
} PdbDbLink;
/* PdbDbMark is only used as the value of the 'marks' hash table. This
* points to a particular secion in an article and is used to convert
* the mark name to its actual section */
typedef struct
{
PdbDbArticle *article;
PdbDbSection *section;
/* If the mark is in a particular sence then this will be the sence
* number. Otherwise it will be -1. */
int sence;
} PdbDbMark;
struct _PdbDb
{
PdbLang *lang;
GPtrArray *articles;
/* Temporary storage location for the word root. This is only valid
* while parsing an article */
char *word_root;
/* Hash table of alternative roots. This will only contain anything
* while parsing an article. */
GHashTable *root_variants;
/* Temporary hash table of the translations indexed by language
* code. This only contains anything while parsing an article */
GHashTable *translations;
GHashTable *marks;
/* This is a list of links. Each link contains a reference to a
* section (either directly a pointer or a mark name) and and a
* pointer to the span. The data in the span will be replaced by an
* article and mark number as a post-processing step once all of the
* articles have been read so that the references can be resolved.
* The links are sorted by the offset */
GList *links;
};
typedef struct
{
const char *name;
const char *replacement;
} PdbDbReplacement;
static const PdbDbReplacement
pdb_db_ref_types[] =
{
{ "vid", "→" },
{ "hom", "→" },
{ "dif", "=" },
{ "sin", "⇒" },
{ "ant", "⇝" },
{ "super", "↗" },
{ "sub", "↘" },
{ "prt", "↘" },
{ "malprt", "↗" },
/* { "lst", "??" }, */
{ "ekz", "●" }
};
static const PdbDbReplacement
pdb_db_styles[] =
{
{ "KOMUNE", "(komune) " },
{ "FIG", "(figure) " },
{ "ARK", "(arkaismo) " },
{ "EVI", "(evitinde) " },
{ "FRAZ", "(frazaĵo) " },
{ "VULG", "(vulgare) " },
{ "RAR", "(malofte) " },
{ "POE", "(poezie) " },
{ "NEO", "(neologismo) " }
};
static void
pdb_db_reference_free (PdbDbReference *entry)
{
switch (entry->type)
{
case PDB_DB_REFERENCE_TYPE_MARK:
g_free (entry->d.mark);
break;
case PDB_DB_REFERENCE_TYPE_DIRECT:
break;
}
g_slice_free (PdbDbReference, entry);
}
static PdbDbReference *
pdb_db_reference_copy (const PdbDbReference *entry_in)
{
PdbDbReference *entry_out = g_slice_dup (PdbDbReference, entry_in);
switch (entry_in->type)
{
case PDB_DB_REFERENCE_TYPE_MARK:
entry_out->d.mark = g_strdup (entry_out->d.mark);
break;
case PDB_DB_REFERENCE_TYPE_DIRECT:
break;
}
return entry_out;
}
static const char *
pdb_db_reference_get_name (const PdbDbReference *ref)
{
switch (ref->type)
{
case PDB_DB_REFERENCE_TYPE_MARK:
return ref->d.mark;
case PDB_DB_REFERENCE_TYPE_DIRECT:
return ref->d.direct.section->title.text;
}
g_assert_not_reached ();
}
static void
pdb_db_link_free (PdbDbLink *link)
{
pdb_db_reference_free (link->reference);
g_slice_free (PdbDbLink, link);
}
static void
pdb_db_add_index_entry (PdbDb *db,
const char *lang,
const char *name,
const char *display_name,
const PdbDbReference *entry_in)
{
PdbTrie *trie;
if (*name == '\0')
{
fprintf (stderr,
_("empty name found for index entry \"%s\" "
"in language \"%s\"\n"),
pdb_db_reference_get_name (entry_in),
lang);
return;
}
if (display_name && *display_name == '\0')
{
fprintf (stderr,
_("empty display name found for index entry "
"\"%s\" in language \"%s\"\n"),
pdb_db_reference_get_name (entry_in),
lang);
return;
}
trie = pdb_lang_get_trie (db->lang, lang);
if (trie)
{
PdbDbReference *entry =
pdb_db_reference_copy (entry_in);
const char *p;
/* Check if any of the characters in the name are upper case */
for (p = name; *p; p = g_utf8_next_char (p))
{
gunichar ch = g_utf8_get_char (p);
if (g_unichar_isupper (ch))
break;
}
/* If we found an uppercase character then we'll additionally
* add a lower case representation of the name so that the
* search can be case insensitive */
if (*p || display_name)
{
GString *buf = g_string_new (NULL);
for (p = name; *p; p = g_utf8_next_char (p))
{
gunichar ch = g_unichar_tolower (g_utf8_get_char (p));
g_string_append_unichar (buf, ch);
}
pdb_trie_add_word (trie,
buf->str,
display_name ? display_name : name,
entry);
g_string_free (buf, TRUE);
}
else
{
pdb_trie_add_word (trie,
name,
NULL,
entry);
}
}
}
static void
pdb_db_append_tld (PdbDb *db,
GString *buf,
char **atts)
{
const char *root = db->word_root;
const char *lit = NULL;
const char *var;
char **att;
for (att = atts; att[0]; att += 2)
{
if (!strcmp (att[0], "lit"))
{
lit = att[1];
}
else if (!strcmp (att[0], "var"))
{
var = g_hash_table_lookup (db->root_variants, att[1]);
if (var == NULL)
{
fprintf (stderr,
_("missing variant \"%s\" for root \"%s\"\n"),
att[1],
db->word_root);
}
else
{
root = var;
}
}
}
if (lit)
{
g_string_append (buf, lit);
if (*root)
root = g_utf8_next_char (root);
}
g_string_append (buf, root);
}
static void
pdb_db_mark_free (PdbDbMark *mark)
{
g_slice_free (PdbDbMark, mark);
}
static void
pdb_db_destroy_spannable_string (PdbDbSpannableString *string)
{
g_free (string->text);
pdb_span_free_list (&string->spans);
}
static void
pdb_db_free_section_cb (void *ptr,
void *user_data)
{
PdbDbSection *section = ptr;
pdb_db_destroy_spannable_string (§ion->title);
pdb_db_destroy_spannable_string (§ion->text);
g_slice_free (PdbDbSection, section);
}
static void
pdb_db_free_section_list (GList *sections)
{
g_list_foreach (sections, pdb_db_free_section_cb, NULL);
g_list_free (sections);
}
static int
pdb_db_get_element_num (PdbDocElementNode *element)
{
PdbDocNode *n;
int num = 0;
/* Count the matching elements before this one */
for (n = element->node.prev; n; n = n->prev)
if (n->type == PDB_DOC_NODE_TYPE_ELEMENT &&
!strcmp (((PdbDocElementNode *) n)->name, element->name))
num++;
/* If this is the first matching element then check if it's also the
* only one */
if (num == 0)
{
for (n = element->node.next; n; n = n->next)
if (n->type == PDB_DOC_NODE_TYPE_ELEMENT &&
!strcmp (((PdbDocElementNode *) n)->name, element->name))
/* It's not the only one so return a real number */
return 0;
/* It is the only one so return -1 */
return -1;
}
else
return num;
}
static PdbDbMark *
pdb_db_add_mark (PdbDb *db,
PdbDbArticle *article,
PdbDbSection *section,
const char *mark_name)
{
PdbDbMark *mark = g_slice_new (PdbDbMark);
mark->article = article;
mark->section = section;
mark->sence = -1;
g_hash_table_insert (db->marks,
g_strdup (mark_name),
mark);
return mark;
}
static int
pdb_db_get_sence_number (const PdbDocElementNode *elem)
{
/* Look for a containing “snc” element */
while (elem && elem->node.type == PDB_DOC_NODE_TYPE_ELEMENT)
{
if (!strcmp (elem->name, "snc"))
{
int sence_num = 1;
/* Count the previous sences */
for (PdbDocNode *node = elem->node.prev; node; node = node->prev)
{
if (node->type == PDB_DOC_NODE_TYPE_ELEMENT &&
!strcmp (((PdbDocElementNode *) node)->name, "snc"))
sence_num++;
}
return sence_num;
}
elem = (PdbDocElementNode *) elem->node.parent;
}
return -1;
}
static void
pdb_db_add_mark_from_element_to_reference (PdbDb *db,
const PdbDbReference *reference,
PdbDocElementNode *elem)
{
/* Only add marks to direct references */
if (reference->type != PDB_DB_REFERENCE_TYPE_DIRECT)
return;
const char *mrk = pdb_doc_get_attribute (elem, "mrk");
if (mrk == NULL)
return;
PdbDbMark *mark = pdb_db_add_mark (db,
reference->d.direct.article,
reference->d.direct.section,
mrk);
mark->sence = pdb_db_get_sence_number (elem);
}
typedef struct
{
GString *buf;
PdbList spans;
} PdbDbTranslationData;
static void
pdb_db_free_translation_data_cb (void *user_data)
{
PdbDbTranslationData *data = user_data;
if (data->buf)
g_string_free (data->buf, TRUE);
pdb_span_free_list (&data->spans);
g_slice_free (PdbDbTranslationData, data);
}
static int
pdb_db_compare_language_code (const void *a,
const void *b,
void *user_data)
{
PdbDb *db = user_data;
const char *code_a = a;
const char *code_b = b;
const char *name_a, *name_b;
name_a = pdb_lang_get_name (db->lang, code_a);
if (name_a == NULL)
name_a = code_a;
name_b = pdb_lang_get_name (db->lang, code_b);
if (name_b == NULL)
name_b = code_b;
return pdb_strcmp (name_a, name_b);
}
static void
trim_trailing_commas (GString *buf)
{
gsize len = buf->len;
while (len > 0 &&
(g_ascii_isspace (buf->str[len - 1]) ||
buf->str[len - 1] == ','))
len--;
g_string_truncate (buf, len);
}
static gboolean
pdb_db_get_trd_link (PdbDb *db,
PdbDocElementNode *trd_elem,
const PdbDbReference *reference,
GString *buf,
PdbList *spans,
GError **error)
{
PdbDocElementNode *parent, *kap;
PdbDocNode *n;
int sence_num = -1;
int subsence_num = -1;
PdbSpan *span;
PdbDbLink *link;
int span_start, span_end;
/* Check that the parent is either <snc> or <subsnc> */
parent = (PdbDocElementNode *) trd_elem->node.parent;
if (!strcmp (parent->name, "dif"))
parent = (PdbDocElementNode *) parent->node.parent;
if (!strcmp (parent->name, "subsnc"))
{
subsence_num = pdb_db_get_element_num (parent);
parent = (PdbDocElementNode *) parent->node.parent;
}
if (!strcmp (parent->name, "snc"))
{
sence_num = pdb_db_get_element_num (parent);
parent = (PdbDocElementNode *) parent->node.parent;
}
if (!strcmp (parent->name, "subdrv") ||
!strcmp (parent->name, "subart"))
parent = (PdbDocElementNode *) parent->node.parent;
if (strcmp (parent->name, "drv") &&
strcmp (parent->name, "art"))
{
g_set_error (error,
PDB_ERROR,
PDB_ERROR_BAD_FORMAT,
_("%s tag found with unknown parent %s"),
trd_elem->name,
parent->name);
return FALSE;
}
kap = pdb_doc_get_child_element (&parent->node, "kap");
if (kap == NULL)
{
g_set_error (error,
PDB_ERROR,
PDB_ERROR_BAD_FORMAT,
_("drv node found without a kap"));
return FALSE;
}
span_start = buf->len;
for (n = kap->node.first_child; n; n = n->next)
{
switch (n->type)
{
case PDB_DOC_NODE_TYPE_TEXT:
{
PdbDocTextNode *text = (PdbDocTextNode *) n;
const char *p, *end;
for (p = text->data, end = text->data + text->len;
p < end;
p++)
if (g_ascii_isspace (*p))
{
if (buf->len > 0 &&
!g_ascii_isspace (buf->str[buf->len - 1]))
g_string_append_c (buf, ' ');
}
else
g_string_append_c (buf, *p);
}
break;
case PDB_DOC_NODE_TYPE_ELEMENT:
{
PdbDocElementNode *elem = (PdbDocElementNode *) n;
if (!strcmp (elem->name, "tld") ||
!strcmp (elem->name, "rad"))
g_string_append_c (buf, '~');
}
break;
}
}
/* If the root has variants then the text of the kap element will
* have trailing commas */
trim_trailing_commas (buf);
if (sence_num != -1)
{
g_string_append_printf (buf, " %i", sence_num + 1);
if (subsence_num != -1)
g_string_append_printf (buf, ".%c", subsence_num + 'a');
}
span_end = buf->len;
span = g_slice_new0 (PdbSpan);
span->span_length = span_end - span_start;
span->span_start = span_start;
span->type = PDB_SPAN_REFERENCE;
/* Add this span to the end of the list */
pdb_list_insert (spans->prev, &span->link);
link = g_slice_new (PdbDbLink);
link->span = span;
link->reference = pdb_db_reference_copy (reference);
link->string = NULL;
db->links = g_list_prepend (db->links, link);
return TRUE;
}
static gboolean
pdb_db_is_empty_translation (PdbDocElementNode *element)
{
PdbDocNode *node;
/* Some of the documents (eg, 'missupozi' for French) have empty
* translations. We want to totally skip these so that they don't
* mess up the index */
for (node = element->node.first_child; node; node = node->next)
switch (node->type)
{
case PDB_DOC_NODE_TYPE_TEXT:
{
PdbDocTextNode *text = (PdbDocTextNode *) node;
const char *end = text->data + text->len;
const char *p;
for (p = text->data; p < end; p++)
if (!g_ascii_isspace (*p))
return FALSE;
}
break;
case PDB_DOC_NODE_TYPE_ELEMENT:
if (!pdb_db_is_empty_translation ((PdbDocElementNode *) node))
return FALSE;
}
return TRUE;
}
static gboolean
pdb_db_add_trd_index (PdbDb *db,
PdbDocElementNode *element,
const char *lang_code,
const PdbDbReference *reference,
GError **error)
{
PdbDocElementNode *ind;
GString *display_name;
display_name = g_string_new (NULL);
pdb_doc_append_element_text_with_ignore (element,
display_name,
"ofc",
NULL);
pdb_trim_buf (display_name);
if ((ind = pdb_doc_get_child_element (&element->node, "ind")))
{
GString *real_name = g_string_new (NULL);
pdb_doc_append_element_text (ind, real_name);
pdb_trim_buf (real_name);
pdb_db_add_index_entry (db,
lang_code,
real_name->str,
display_name->str,
reference);
g_string_free (real_name, TRUE);
}
else if (pdb_doc_element_has_child_element (element))
{
GString *real_name = g_string_new (NULL);
/* We don't want <klr> tags in the index name */
pdb_doc_append_element_text_with_ignore (element,
real_name,
"ofc",
"klr",
NULL);
pdb_trim_buf (real_name);
pdb_db_add_index_entry (db,
lang_code,
real_name->str,
display_name->str,
reference);
g_string_free (real_name, TRUE);
}
else
{
pdb_db_add_index_entry (db,
lang_code,
display_name->str,
NULL,
reference);
}
g_string_free (display_name, TRUE);
return TRUE;
}
static gboolean
pdb_db_add_translation_index (PdbDb *db,
PdbDocElementNode *element,
const PdbDbReference *reference,
GError **error)
{
const char *lang_code;
lang_code = pdb_doc_get_attribute (element, "lng");
if (lang_code == NULL)
{
g_set_error (error,
PDB_ERROR,
PDB_ERROR_BAD_FORMAT,
_("%s element with no lng attribute"),
element->name);
return FALSE;
}
if (!strcmp (element->name, "trdgrp"))
{
PdbDocNode *node;
for (node = element->node.first_child; node; node = node->next)
if (node->type == PDB_DOC_NODE_TYPE_ELEMENT &&
!pdb_db_add_trd_index (db,
(PdbDocElementNode *) node,
lang_code,
reference,
error))
return FALSE;
return TRUE;
}
else
return pdb_db_add_trd_index (db, element, lang_code, reference, error);
}
static gboolean
pdb_db_handle_translation (PdbDb *db,
PdbDocElementNode *element,
const PdbDbReference *reference,
GError **error)
{
const char *lang_code;
PdbDbTranslationData *data;
GString *content;
/* Silently ignore empty translations */
if (pdb_db_is_empty_translation (element))
return TRUE;
lang_code = pdb_doc_get_attribute (element, "lng");
if (lang_code == NULL)
{
g_set_error (error,
PDB_ERROR,
PDB_ERROR_BAD_FORMAT,
_("%s element with no lng attribute"),
element->name);
return FALSE;
}
else if (strlen (lang_code) > 3)
{
g_set_error (error,
PDB_ERROR,
PDB_ERROR_BAD_FORMAT,
_("The language code “%s” is too long"),
lang_code);
return FALSE;
}
if ((data = g_hash_table_lookup (db->translations,
lang_code)) == NULL)
{
data = g_slice_new (PdbDbTranslationData);
data->buf = g_string_new (NULL);
pdb_list_init (&data->spans);
g_hash_table_insert (db->translations,
g_strdup (lang_code),
data);
}
else if (data->buf->len > 0)
g_string_append (data->buf, "; ");
if (!pdb_db_get_trd_link (db,
element,
reference,
data->buf,
&data->spans,
error))
return FALSE;
g_string_append (data->buf, ": ");
content = g_string_new (NULL);
pdb_doc_append_element_text (element, content);
pdb_trim_buf (content);
g_string_append_len (data->buf, content->str, content->len);
g_string_free (content, TRUE);
return pdb_db_add_translation_index (db,
element,
reference,
error);
}
static gboolean
pdb_db_find_translations_recursive_skip (PdbDb *db,
PdbDocElementNode *root_node,
const char *skip,
const PdbDbReference *reference,
GError **error)
{
GPtrArray *stack;
gboolean ret = TRUE;
pdb_db_add_mark_from_element_to_reference (db, reference, root_node);
stack = g_ptr_array_new ();
if (root_node->node.first_child)
g_ptr_array_add (stack, root_node->node.first_child);
while (stack->len > 0)
{
PdbDocNode *node = g_ptr_array_index (stack, stack->len - 1);
g_ptr_array_set_size (stack, stack->len - 1);
if (node->next)
g_ptr_array_add (stack, node->next);
if (node->type == PDB_DOC_NODE_TYPE_ELEMENT)
{
PdbDocElementNode *element = (PdbDocElementNode *) node;
if (!strcmp (element->name, "trdgrp") ||
!strcmp (element->name, "trd"))
{
if (!pdb_db_handle_translation (db,
element,
reference,
error))
{
ret = FALSE;
break;
}
}
else if (node->first_child &&
strcmp (element->name, "ekz") &&
strcmp (element->name, "bld") &&
strcmp (element->name, "adm") &&
strcmp (element->name, "fnt") &&
(skip == NULL || strcmp (element->name, skip)))
g_ptr_array_add (stack, node->first_child);
pdb_db_add_mark_from_element_to_reference (db, reference, element);
}
}
g_ptr_array_free (stack, TRUE);