Work on EXPLAIN schema. First implementation of sub-schema facility
[yaz-moved-to-github.git] / odr / odr_unicode.c
1 /*
2  * Copyright (c) 1997, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_unicode.c,v $
7  * Revision 1.1  1997-09-17 12:25:49  adam
8  * First Unicode attempt.
9  *
10  */
11
12 #include <odr.h>
13
14 #if YAZ_UNICODE
15 int odr_unicode(ODR o, wchar_t **p, int opt)
16 {
17     int cons = 0, res;
18     Odr_oct *t;
19
20     if (o->error)
21         return 0;
22     if (o->t_class < 0)
23     {
24         o->t_class = ODR_UNIVERSAL;
25         o->t_tag = ODR_OCTETSTRING;
26     }
27     if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt)) < 0)
28         return 0;
29     if (!res)
30         return opt;
31     if (o->direction == ODR_PRINT)
32     {
33         size_t i, wlen = wcslen(*p);
34         fprintf(o->print, "%sL'", odr_indent(o));
35         for (i = 0; i < wlen; i++)
36         {
37             if ((*p)[i] > 126 || (*p)[i] == '\\')
38                 fprintf (o->print, "\\%04lX", (*p)[i]);
39             else
40             {
41                 int ch = (*p)[i];
42                 fprintf (o->print, "%c", ch);
43             }
44         }
45         fprintf(o->print, "'\n");
46         return 1;
47     }
48     t = odr_malloc(o, sizeof(Odr_oct));
49     if (o->direction == ODR_ENCODE)
50     {
51         size_t i, wlen = 1+wcslen(*p);
52         t->size = t->len = wlen*2;
53         t->buf = odr_malloc (o, t->size);
54         for (i = 0; i < wlen; i++)
55         {
56             t->buf[i*2] = (*p)[i] & 255;
57             t->buf[i*2+1] = ((*p)[i] >> 8) & 255;
58         }
59     }
60     else
61     {
62         t->size= 0;
63         t->len = 0;
64         t->buf = 0;
65     }
66     if (!ber_octetstring(o, t, cons))
67         return 0;
68     if (o->direction == ODR_DECODE)
69     {
70         size_t i, wlen = t->len/2;
71         *p = odr_malloc (o, wlen*sizeof(**p));
72         for (i = 0; i<wlen; i++)
73             (*p)[i] = t->buf[i*2] + (t->buf[i*2+1]<<8);
74     }
75     return 1;
76 }
77
78 #endif