~ubuntu-branches/ubuntu/trusty/lifelines/trusty

« back to all changes in this revision

Viewing changes to src/gedlib/xlat.c

  • Committer: Bazaar Package Importer
  • Author(s): Felipe Augusto van de Wiel (faw)
  • Date: 2007-05-23 23:49:53 UTC
  • mfrom: (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20070523234953-ogno9rnbmth61i7p
Tags: 3.0.50-2etch1
* Changing docs/ll-reportmanual.xml and docs/ll-userguide.xml to fix
  documentation build problems (Closes: #418347).

* lifelines-reports
  - Adding a dependency to lifelines >= 3.0.50 to prevent file conflict.
    (Closes: #405500).

* Updating French translation. Thanks to Bernard Adrian. (Closes: #356671).

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include "gedcomi.h"
20
20
#include "xlat.h" 
21
21
#include "arch.h"
 
22
#include "lloptions.h"
 
23
#include "log.h"
 
24
#include "vtable.h"
 
25
 
22
26
 
23
27
/*********************************************
24
28
 * local types
35
39
        INT uparam; /* opaque number used by client */
36
40
};
37
41
/* dynamically loadable translation table, entry in dyntt list */
38
 
typedef struct tag_dyntt {
 
42
struct tag_dyntt {
 
43
        struct tag_vtable *vtable; /* generic object */
 
44
        INT refcnt; /* ref-countable object */
 
45
        STRING name;
39
46
        STRING path;
40
47
        TRANTABLE tt; /* when loaded */
41
48
        BOOLEAN loadfailure;
42
 
} *DYNTT;
 
49
};
 
50
typedef struct tag_dyntt *DYNTT;
43
51
 
44
52
/* step of a translation, either iconv_src or trantble is NULL */
45
53
typedef struct xlat_step_s {
60
68
static XLSTEP create_dyntt_step(DYNTT dyntt);
61
69
static XLAT create_null_xlat(BOOLEAN adhoc);
62
70
static XLAT create_xlat(CNSTRING src, CNSTRING dest, BOOLEAN adhoc);
63
 
static DYNTT create_dyntt(TRANTABLE tt, STRING path);
 
71
static DYNTT create_dyntt(TRANTABLE tt, CNSTRING name, CNSTRING path);
 
72
static void destroy_dyntt(DYNTT dyntt);
 
73
static void dyntt_destructor(VTABLE *obj);
64
74
static void free_dyntts(void);
65
75
static void free_xlat(XLAT xlat);
66
76
static DYNTT get_conversion_dyntt(CNSTRING src, CNSTRING dest);
68
78
static void load_dyntt_if_needed(DYNTT dyntt);
69
79
static void load_dynttlist_from_dir(STRING dir);
70
80
static int select_tts(const struct dirent *entry);
71
 
static void zero_dyntt(DYNTT dyntt);
72
81
 
73
82
/*********************************************
74
83
 * local variables
78
87
static TABLE f_dyntts=0; /* cache of dynamic translation tables */
79
88
static char f_ttext[] = ".tt";
80
89
 
 
90
static struct tag_vtable vtable_for_dyntt = {
 
91
        VTABLE_MAGIC
 
92
        , "dyntt"
 
93
        , &dyntt_destructor
 
94
        , &refcountable_isref
 
95
        , &refcountable_addref
 
96
        , &refcountable_release
 
97
        , 0 /* copy_fnc */
 
98
        , &generic_get_type_name
 
99
};
 
100
 
81
101
/*********************************************
82
102
 * local & exported function definitions
83
103
 * body of module
132
152
                strfree(&xstep->iconv_dest);
133
153
                xstep->dyntt = 0; /* f_dyntts owns dyntt memory */
134
154
        ENDLIST
135
 
        make_list_empty(xlat->steps);
136
 
        remove_list(xlat->steps, 0);
 
155
        destroy_list(xlat->steps);
 
156
        strfree(&xlat->src);
 
157
        strfree(&xlat->dest);
 
158
        stdfree(xlat);
137
159
}
138
160
/*==========================================================
139
161
 * create_iconv_step -- Create an iconv step of a translation chain
167
189
 * Created: 2002/12/10 (Perry Rapp)
168
190
 *========================================================*/
169
191
static DYNTT
170
 
create_dyntt (TRANTABLE tt, STRING path)
 
192
create_dyntt (TRANTABLE tt, CNSTRING name, CNSTRING path)
171
193
{
172
194
        DYNTT dyntt = (DYNTT)stdalloc(sizeof(*dyntt));
173
195
        memset(dyntt, 0, sizeof(*dyntt));
 
196
        dyntt->vtable = &vtable_for_dyntt;
 
197
        dyntt->refcnt = 1;
174
198
        dyntt->tt = tt;
 
199
        dyntt->name = strsave(name);
175
200
        dyntt->path = strsave(path);
176
201
        return dyntt;
177
202
}
240
265
                XLSTEP xstep = create_iconv_step(zs_str(zsrc), zs_str(zdest));
241
266
                enqueue_list(xlat->steps, xstep);
242
267
        } else {
243
 
                DYNTT dyntt = get_conversion_dyntt(zs_str(zsrc), zs_str(zdest));
244
 
                if (dyntt)
 
268
                STRING src = zs_str(zsrc), dest = zs_str(zdest);
 
269
                BOOLEAN foundit = FALSE;
 
270
                DYNTT dyntt = get_conversion_dyntt(src, dest);
 
271
                if (dyntt) {
245
272
                        add_dyntt_step(xlat, dyntt);
246
 
                else
 
273
                        foundit = TRUE;
 
274
                }
 
275
                else if (!eqstr(src, "UTF-8") && !eqstr(dest, "UTF-8")) {
 
276
                        /* try going through UTF-8 as intermediate step */
 
277
                        DYNTT dyntt1 = get_conversion_dyntt(src, "UTF-8");
 
278
                        DYNTT dyntt2 = get_conversion_dyntt("UTF-8", dest);
 
279
                        if (dyntt1 && dyntt2) {
 
280
                                add_dyntt_step(xlat, dyntt1);
 
281
                                add_dyntt_step(xlat, dyntt2);
 
282
                                foundit = TRUE;
 
283
                        }
 
284
                }
 
285
                if (!foundit)
247
286
                        xlat->valid = FALSE; /* missing main conversion */
248
287
        }
249
288
 
298
337
        ZSTR zttname = zs_news(src);
299
338
        zs_appc(zttname, '_');
300
339
        zs_apps(zttname, dest);
301
 
        dyntt = (DYNTT)valueof_ptr(f_dyntts, zs_str(zttname));
 
340
        dyntt = (DYNTT)valueof_obj(f_dyntts, zs_str(zttname));
 
341
        if (getlloptint("TTPATH.debug", 0)) {
 
342
                log_outf("ttpath.dbg",
 
343
                        _("ttpath get_conversion_dyntt:from <%s> to <%s>: %s"),
 
344
                        src, dest,
 
345
                        dyntt ? _("succeeded") : _("failed")
 
346
                        );
 
347
        }
302
348
        zs_free(&zttname);
303
349
        return dyntt;
304
350
}
313
359
        ZSTR zttname = zs_news(codeset);
314
360
        zs_apps(zttname, "__");
315
361
        zs_apps(zttname, subcoding);
316
 
        dyntt = (DYNTT)valueof_ptr(f_dyntts, zs_str(zttname));
 
362
        dyntt = (DYNTT)valueof_obj(f_dyntts, zs_str(zttname));
 
363
        if (getlloptint("TTPATH.debug", 0)) {
 
364
                log_outf("ttpath.dbg",
 
365
                        _("ttpath get_subcoding_dyntt from <%s> to subcode <%s>: %s"),
 
366
                        codeset,subcoding,
 
367
                        dyntt ? _("succeeded") : _("failed")
 
368
                        );
 
369
        }
317
370
        zs_free(&zttname);
318
371
        return dyntt;
319
372
}
327
380
        ZSTR zerr=zs_new();
328
381
        if (dyntt->tt || dyntt->loadfailure)
329
382
                return;
330
 
        if (!init_map_from_file(dyntt->path, dyntt->path, &dyntt->tt, zerr)) {
 
383
        if (!init_map_from_file(dyntt->path, dyntt->name, &dyntt->tt, zerr)) {
331
384
                dyntt->loadfailure = TRUE;
332
385
        }
333
386
        zs_free(&zerr);
358
411
                } else if (xstep->dyntt) {
359
412
                        /* a custom translation table step */
360
413
                        if (xstep->dyntt->tt)
361
 
                                custom_translate(zstr, xstep->dyntt->tt);
 
414
                                custom_translatez(zstr, xstep->dyntt->tt);
362
415
                }
363
416
        ENDLIST
364
417
        return cvtd;
365
418
}
366
419
/*==========================================================
367
 
 * xl_load_all_tts -- Load internal list of available translation
 
420
 * xl_load_all_dyntts -- Load internal list of available translation
368
421
 *  tables (based on *.tt files in TTPATH)
369
422
 * Created: 2002/11/27 (Perry Rapp)
370
423
 *========================================================*/
373
426
{
374
427
        STRING dirs,p;
375
428
        free_dyntts();
 
429
        if (getlloptint("TTPATH.debug", 0)) {
 
430
                if (!ttpath ||  !ttpath[0])
 
431
                        log_outf("ttpath.dbg", _("No TTPATH config variable"));
 
432
                else
 
433
                        log_outf("ttpath.dbg", "ttpath: %s", ttpath);
 
434
        }
376
435
        if (!ttpath ||  !ttpath[0])
377
436
                return;
378
 
        f_dyntts = create_table(FREEBOTH);
 
437
        f_dyntts = create_table_obj();
379
438
        dirs = (STRING)stdalloc(strlen(ttpath)+2);
380
439
        /* find directories in dirs & delimit with zeros */
381
440
        chop_path(ttpath, dirs);
397
456
        struct dirent **programs;
398
457
        INT n = scandir(dir, &programs, select_tts, alphasort);
399
458
        INT i;
 
459
        if (getlloptint("TTPATH.debug", 0)) {
 
460
                log_outf("ttpath.dbg", _("ttpath checking dir <%s>"), dir);
 
461
        }
400
462
        for (i=0; i<n; ++i) {
401
463
                CNSTRING ttfile = programs[i]->d_name;
402
464
                /* filename without extension */
408
470
                        UTF-8_ISO-8859-1 (type 1; code conversion)
409
471
                        UTF-8__HTML (type 2; subcoding)
410
472
                */
 
473
                if (getlloptint("TTPATH.debug", 0)) {
 
474
                        log_outf("ttpath.dbg", _("ttpath file <%s> typed as %d"), ttfile, ntype);
 
475
                }
411
476
                if (ntype==1 || ntype==2) {
412
 
                        if (!valueof_ptr(f_dyntts, zs_str(zfile))) {
 
477
                        if (!valueof_obj(f_dyntts, zs_str(zfile))) {
413
478
                                TRANTABLE tt=0; /* will be loaded when needed */
414
479
                                STRING path = concat_path_alloc(dir, ttfile);
415
 
                                DYNTT dyntt = create_dyntt(tt, path);
 
480
                                DYNTT dyntt = create_dyntt(tt, ttfile, path);
416
481
                                strfree(&path);
417
 
                                insert_table_ptr(f_dyntts, strsave(zs_str(zfile)), dyntt);
 
482
                                insert_table_obj(f_dyntts, zs_str(zfile), dyntt);
 
483
                                --dyntt->refcnt; /* leave table as sole owner */
418
484
                        }
419
485
                }
420
486
                zs_free(&zfile);
421
487
                zs_free(&zsrc);
422
488
                zs_free(&zdest);
423
489
        }
424
 
        if (n>0)
 
490
        if (n>0) {
 
491
                for (i=0; i<n; ++i) {
 
492
                        stdfree(programs[i]);
 
493
                }
425
494
                stdfree(programs);
 
495
        }
426
496
}
427
497
/*===========================================================
428
498
 * check_tt_name -- Parse tt filename to see what codesets it does
489
559
void
490
560
xl_free_adhoc_xlats (void)
491
561
{
492
 
    XLAT xlattemp;
493
 
        LIST newlist;
 
562
    XLAT xlattemp=0;
 
563
        LIST newlist=0;
494
564
        if (!f_xlats)
495
565
                return;
496
566
        /* we don't have a way to delete items from a list,
504
574
                        back_list(newlist, xlattemp);
505
575
                }
506
576
        ENDLIST
507
 
        make_list_empty(f_xlats);
508
 
        remove_list(f_xlats, 0);
 
577
        destroy_list(f_xlats);
509
578
        f_xlats = newlist;
510
579
}
511
580
/*==========================================================
515
584
void
516
585
xl_free_xlats (void)
517
586
{
518
 
        XLAT xlattemp;
 
587
        XLAT xlattemp=0;
519
588
        if (!f_xlats)
520
589
                return;
521
590
        FORLIST(f_xlats, el)
522
591
                xlattemp = (XLAT)el;
523
592
                free_xlat(xlattemp);
524
593
        ENDLIST
525
 
        make_list_empty(f_xlats);
526
 
        remove_list(f_xlats, 0);
 
594
        destroy_list(f_xlats);
527
595
        f_xlats = 0;
528
596
}
529
597
/*==========================================================
534
602
free_dyntts (void)
535
603
{
536
604
        if (f_dyntts) {
537
 
                struct tag_table_iter tabit;
538
 
                xl_free_xlats(); /* xlats point into f_dyntts */
539
 
                if (begin_table(f_dyntts, &tabit)) {
540
 
                        VPTR ptr;
541
 
                        STRING key;
542
 
                        while (next_table_ptr(&tabit, &key, &ptr)) {
543
 
                                DYNTT dyntt = (DYNTT)ptr;
544
 
                                zero_dyntt(dyntt);
545
 
                        }
546
 
                }
547
605
                destroy_table(f_dyntts);
548
606
                f_dyntts = 0;
549
607
        }
550
608
}
551
609
/*==========================================================
552
 
 * zero_dyntt -- Empty (free) contents of a dyntt
553
 
 *  NB: This does not free the dyntt memory itself
554
 
 *  b/c that is handled in the f_dyntts cache table
555
 
 * Created: 2002/12/13 (Perry Rapp)
 
610
 * xlat_shutdown -- Clear and free allocated objects
556
611
 *========================================================*/
 
612
void
 
613
xlat_shutdown (void)
 
614
{
 
615
        free_dyntts();
 
616
}
 
617
/*=================================================
 
618
 * destroy_table -- destroy all element & memory for table
 
619
 *===============================================*/
557
620
static void
558
 
zero_dyntt (DYNTT dyntt)
 
621
destroy_dyntt (DYNTT dyntt)
559
622
{
 
623
        if (!dyntt) return;
 
624
        ASSERT(dyntt->vtable == &vtable_for_dyntt);
 
625
        strfree(&dyntt->name);
560
626
        strfree(&dyntt->path);
561
627
        remove_trantable(dyntt->tt);
562
628
        dyntt->tt = 0;
 
629
        stdfree(dyntt);
563
630
}
564
631
/*==========================================================
565
632
 * xl_parse_codeset -- Parse out subcode suffixes of a codeset
685
752
{
686
753
        return xlat->dest;
687
754
}
688
 
 
 
755
/*=================================================
 
756
 * dyntt_destructor -- destructor for dyntt
 
757
 *  (destructor entry in vtable)
 
758
 *===============================================*/
 
759
static void
 
760
dyntt_destructor (VTABLE *obj)
 
761
{
 
762
        DYNTT tab = (DYNTT)obj;
 
763
        ASSERT(tab->vtable == &vtable_for_dyntt);
 
764
        destroy_dyntt(tab);
 
765
}