~ubuntu-branches/ubuntu/gutsy/icu/gutsy-updates

« back to all changes in this revision

Viewing changes to source/tools/genrb/write.c

  • Committer: Package Import Robot
  • Author(s): Jay Berkenbilt
  • Date: 2005-11-19 11:29:31 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20051119112931-vcizkrp10tli4enw
Tags: 3.4-3
Explicitly build with g++ 3.4.  The current ICU fails its test suite
with 4.0 but not with 3.4.  Future versions should work properly with
4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
*******************************************************************************
3
 
*
4
 
*   Copyright (C) 1998-2000, International Business Machines
5
 
*   Corporation and others.  All Rights Reserved.
6
 
*
7
 
*******************************************************************************
8
 
*
9
 
* File write.c
10
 
*
11
 
* Modification History:
12
 
*
13
 
*   Date        Name        Description
14
 
*   06/01/99    stephen     Creation.
15
 
*******************************************************************************
16
 
*/
17
 
 
18
 
#include <stdio.h>
19
 
#include "write.h"
20
 
#include "cmemory.h"
21
 
#include "cstring.h"
22
 
#include "filestrm.h"
23
 
#include "unicode/ustring.h"
24
 
#include "error.h"
25
 
#include "list.h"
26
 
 
27
 
 
28
 
/* Protos */
29
 
static void write_ustring(FileStream *rb, const UChar *data);
30
 
static void write_string(FileStream *rb, const char *data);
31
 
static void write_strlist(FileStream *rb, const char *name,
32
 
              const struct SStringList *list);
33
 
static void write_strlist2d(FileStream *rb, const char *name,
34
 
                const struct SStringList2d *list);
35
 
static void write_taglist(FileStream *rb, const char *name,
36
 
              const struct STaggedList *list);
37
 
 
38
 
/* Special values */
39
 
static const int32_t sBOM = 0x021C;
40
 
 
41
 
static const int32_t sEOF = -1;
42
 
 
43
 
static const int32_t sSTRINGLIST = 1;
44
 
static const int32_t sSTRINGLIST2D = 2;
45
 
static const int32_t sTAGGEDLIST = 3;
46
 
 
47
 
static const UChar gCollationElementsTag [] = {
48
 
  /* "CollationElements" */
49
 
  0x0043, 0x006f, 0x006c, 0x006c, 0x0061, 0x0074, 0x0069, 0x006f, 0x006e,
50
 
  0x0045, 0x006c, 0x0065, 0x006d, 0x0065, 0x006e, 0x0074, 0x0073, 0x0000
51
 
};
52
 
 
53
 
/* Write a null-terminated UChar array */
54
 
static void
55
 
write_ustring(FileStream *rb,
56
 
              const UChar *data)
57
 
{
58
 
  int32_t len;
59
 
 
60
 
  len = u_strlen(data);
61
 
 
62
 
  /* Write the string's length */
63
 
  T_FileStream_write(rb, &len, sizeof(len));
64
 
 
65
 
  /* Write the string's data */
66
 
  T_FileStream_write(rb, data, sizeof(UChar) * len);
67
 
}
68
 
 
69
 
static void
70
 
write_string(FileStream *rb,
71
 
             const char *data) {
72
 
    int32_t len;
73
 
 
74
 
    len = uprv_strlen(data);
75
 
    T_FileStream_write(rb, &len, sizeof(len));
76
 
 
77
 
    T_FileStream_write(rb, data, sizeof(char) * len);
78
 
}
79
 
 
80
 
/* Write a string list */
81
 
static void
82
 
write_strlist(FileStream *rb,
83
 
              const char *name,
84
 
              const struct SStringList *list)
85
 
{
86
 
  int32_t i;
87
 
 
88
 
  /* Write out the value indicating this is a string list */
89
 
  T_FileStream_write(rb, &sSTRINGLIST, sizeof(sSTRINGLIST));
90
 
 
91
 
  /* Write the name of this string list */
92
 
  write_string(rb, name);
93
 
 
94
 
  /* Write the item count */
95
 
  T_FileStream_write(rb, &list->fCount, sizeof(list->fCount));
96
 
 
97
 
  /* Write each string in the list */
98
 
  for(i = 0; i < list->fCount; ++i) {
99
 
    write_ustring(rb, list->fData[i]);
100
 
  }
101
 
}
102
 
 
103
 
/* Write a 2-d string list */
104
 
static void
105
 
write_strlist2d(FileStream *rb,
106
 
                const char *name,
107
 
                const struct SStringList2d *list)
108
 
{
109
 
  int32_t i, j;
110
 
  int32_t itemcount;
111
 
 
112
 
  /* Write out the value indicating this is a 2-d string list */
113
 
  T_FileStream_write(rb, &sSTRINGLIST2D, sizeof(sSTRINGLIST2D));
114
 
 
115
 
  /* Write the name of this 2-d string list */
116
 
  write_string(rb, name);
117
 
 
118
 
  /* Write the row count */
119
 
  T_FileStream_write(rb, &list->fRowCount, sizeof(list->fRowCount));
120
 
 
121
 
  /* Write out each row */
122
 
  for(i = 0; i < list->fRowCount; ++i) {
123
 
    itemcount = (i == list->fRowCount - 1 ? list->fCount: list->fRows[i+1])
124
 
      - list->fRows[i];
125
 
 
126
 
    /* Write out the count of strings in this row */
127
 
    T_FileStream_write(rb, &itemcount, sizeof(itemcount));
128
 
 
129
 
    /* Write out each string in the row */
130
 
    for(j = 0; j < itemcount; ++j) {
131
 
      write_ustring(rb, list->fData[list->fRows[i] + j]);
132
 
    }
133
 
  }
134
 
}
135
 
 
136
 
/* Write a tagged list */
137
 
static void
138
 
write_taglist(FileStream *rb,
139
 
              const char *name,
140
 
              const struct STaggedList *list)
141
 
{
142
 
/*  int32_t i;   */
143
 
  struct SStringPair *current;
144
 
 
145
 
  /* Write out the value indicating this is a tagged list */
146
 
  T_FileStream_write(rb, &sTAGGEDLIST, sizeof(sTAGGEDLIST));
147
 
 
148
 
  /* Write the name of this tagged list */
149
 
  write_string(rb, name);
150
 
 
151
 
  /* Write the item count */
152
 
  T_FileStream_write(rb, &list->fCount, sizeof(list->fCount));
153
 
 
154
 
  /* Write out each key/value pair */
155
 
  current = list->fFirst;
156
 
  while(current != NULL) {
157
 
      write_string(rb, current->fKey);
158
 
      write_ustring(rb, current->fValue);
159
 
      current = current->fNext;
160
 
  }
161
 
 
162
 
 
163
 
/*  for(i = 0; i < list->fCount; ++i) {
164
 
//    write_ustring(rb, list->fData[i].fKey);
165
 
//    write_ustring(rb, list->fData[i].fValue);
166
 
//  } */
167
 
}
168
 
 
169
 
/* Write a parsed SRBItemList to a file */
170
 
void
171
 
rb_write(FileStream *f,
172
 
         struct SRBItemList *data,
173
 
         UErrorCode *status)
174
 
{
175
 
/*  int32_t i; */
176
 
  struct SRBItem *item;
177
 
 
178
 
  if(U_FAILURE(*status)) return;
179
 
 
180
 
  /* Write the byte order mark to the file */
181
 
  T_FileStream_write(f, &sBOM, sizeof(sBOM));
182
 
 
183
 
  /* Write the locale name to the file */
184
 
  write_ustring(f, data->fLocale);
185
 
 
186
 
  item = data->fFirst;
187
 
 
188
 
  /* Successively write each list item */
189
 
/*  for(i = 0; i < data->fCount; ++i) {  */
190
 
  while(item != NULL) {
191
 
 
192
 
/*    item = data->fData[i];  */
193
 
 
194
 
    switch(item->fData->fType) {
195
 
    case eStringList:
196
 
      /*if(u_strcmp(item->fTag, gCollationElementsTag) == 0)
197
 
      puts("got CollationElements");*/
198
 
      write_strlist(f, item->fTag, &item->fData->u.fStringList);
199
 
      break;
200
 
 
201
 
    case eStringList2d:
202
 
      write_strlist2d(f, item->fTag, &item->fData->u.fStringList2d);
203
 
      break;
204
 
 
205
 
    case eTaggedList:
206
 
      write_taglist(f, item->fTag, &item->fData->u.fTaggedList);
207
 
      break;
208
 
 
209
 
    case eEmpty:
210
 
      *status = U_INTERNAL_PROGRAM_ERROR;
211
 
      setErrorText("Unexpected empty item found");
212
 
      goto finish;
213
 
      /*break;*/
214
 
    }
215
 
    item = item->fNext;
216
 
  }
217
 
 
218
 
  /* Indicate the end of the data */
219
 
  T_FileStream_write(f, &sEOF, sizeof(sEOF));
220
 
 
221
 
  /* Check if any errors occurred during writing */
222
 
  if(T_FileStream_error(f) != 0) {
223
 
    *status = U_FILE_ACCESS_ERROR;
224
 
  }
225
 
 
226
 
 finish:
227
 
  ;
228
 
 
229
 
  /* clean up */
230
 
}