~ubuntu-branches/ubuntu/raring/ydpdict/raring

« back to all changes in this revision

Viewing changes to src/ydpconvert.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcin Owsiany
  • Date: 2007-08-25 15:56:19 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070825155619-rf959blmuv2b2ywy
Tags: 0.99.2-2
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  ydpdict
3
 
 *  (C) Copyright 1998-2004 Wojtek Kaniewski <wojtekka@toxygen.net>
4
 
 *
5
 
 *  This program is free software; you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation; either version 2 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 
 */
19
 
 
20
 
#include <sys/types.h>
21
 
#include <string.h>
22
 
 
23
 
#include "ydpconvert.h"
24
 
#include "xmalloc.h"
25
 
 
26
 
/* tablice konwersji */
27
 
u_char table_cp_iso[] = TABLE_CP_ISO;
28
 
u_char table_iso_plain[] = TABLE_ISO_PLAIN;
29
 
u_char *table_unicode[] = TABLE_UNICODE;
30
 
u_char *table_phonetic_iso[] = TABLE_PHONETIC_ISO;
31
 
 
32
 
u_char *char_table_conv(u_char *inp, u_char *table)
33
 
{
34
 
        u_char *prev = inp;
35
 
 
36
 
        for (; *inp; inp++)
37
 
                if (*inp > 127)
38
 
                        *inp = table[*inp - 128];
39
 
  
40
 
        return prev;
41
 
}
42
 
       
43
 
u_char *string_table_conv(u_char *inp, u_char **table)
44
 
{
45
 
        static u_char buf[1024], letter[2] = " \0";
46
 
  
47
 
        memset(buf, 0, sizeof(buf));
48
 
        
49
 
        for (; *inp; inp++) {
50
 
                if (*inp > 127) {
51
 
                        strncat(buf, table[*inp - 128], sizeof(buf) - strlen(buf) - 1);
52
 
                } else {
53
 
                        letter[0] = *inp;
54
 
                        strncat(buf, letter, sizeof(buf) - strlen(buf) - 1);
55
 
                }
56
 
        }
57
 
  
58
 
        return buf;
59
 
}
60
 
 
61
 
u_char *convert_cp1250(u_char *buf, int alloc)
62
 
{
63
 
        return char_table_conv(alloc ? (u_char*) xstrdup(buf) : buf, table_cp_iso);
64
 
}
65
 
 
66
 
u_char *convert_plain(u_char *inp, int charset, int alloc)
67
 
{
68
 
        switch (charset) {
69
 
                case 0:
70
 
                        return char_table_conv(alloc ? (u_char*) xstrdup(inp) : inp, table_iso_plain);
71
 
                case 1:
72
 
                        return inp;
73
 
                case 2:
74
 
                case 3:
75
 
                        return string_table_conv(inp, table_unicode);
76
 
        }
77
 
        
78
 
        return NULL;
79
 
}
80
 
 
81
 
u_char *convert_phonetic(u_char *inp, int charset, int alloc)
82
 
{
83
 
        switch (charset) {
84
 
                case 0:
85
 
                        return char_table_conv(string_table_conv(inp, table_phonetic_iso), table_iso_plain);
86
 
                case 1:
87
 
                        return string_table_conv(inp, table_phonetic_iso);
88
 
                case 2:
89
 
                case 3:
90
 
                        return string_table_conv(inp, table_unicode);
91
 
        }
92
 
        
93
 
        return NULL;
94
 
}