]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/writesrc.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2005-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
10 * file name: writesrc.c
12 * tab size: 8 (not used)
15 * created on: 2005apr23
16 * created by: Markus W. Scherer
18 * Helper functions for writing source code for data.
23 #include "unicode/utypes.h"
24 #include "unicode/putil.h"
25 #include "unicode/ucptrie.h"
31 usrc_createWithHeader(const char *path
, const char *filename
,
32 const char *header
, const char *generator
) {
42 /* concatenate path and filename, with U_FILE_SEP_CHAR in between if necessary */
43 uprv_strcpy(buffer
, path
);
44 q
=buffer
+uprv_strlen(buffer
);
45 if(q
>buffer
&& (c
=*(q
-1))!=U_FILE_SEP_CHAR
&& c
!=U_FILE_ALT_SEP_CHAR
) {
48 uprv_strcpy(q
, filename
);
60 strftime(buffer
, sizeof(buffer
), "%Y-%m-%d", lt
);
61 fprintf(f
, header
, filename
, buffer
);
63 fprintf(f
, header
, filename
, generator
);
68 "usrc_create(%s, %s): unable to create file\n",
69 path
!=NULL
? path
: "", filename
);
74 U_CAPI
FILE * U_EXPORT2
75 usrc_create(const char *path
, const char *filename
, int32_t copyrightYear
, const char *generator
) {
78 if(copyrightYear
<=2016) {
80 "// © 2016 and later: Unicode, Inc. and others.\n"
81 "// License & terms of use: http://www.unicode.org/copyright.html\n"
83 "// Copyright (C) 1999-2016, International Business Machines\n"
84 "// Corporation and others. All Rights Reserved.\n"
88 "// machine-generated by: %s\n"
92 "// © %d and later: Unicode, Inc. and others.\n"
93 "// License & terms of use: http://www.unicode.org/copyright.html\n"
97 "// machine-generated by: %%s\n"
102 return usrc_createWithHeader(path
, filename
, header
, generator
);
105 U_CAPI
FILE * U_EXPORT2
106 usrc_createTextData(const char *path
, const char *filename
, const char *generator
) {
107 // TODO: Add parameter for the first year this file was generated, not before 2016.
108 static const char *header
=
109 "# Copyright (C) 2016 and later: Unicode, Inc. and others.\n"
110 "# License & terms of use: http://www.unicode.org/copyright.html\n"
111 "# Copyright (C) 1999-2016, International Business Machines\n"
112 "# Corporation and others. All Rights Reserved.\n"
116 "# machine-generated by: %s\n"
118 return usrc_createWithHeader(path
, filename
, header
, generator
);
121 U_CAPI
void U_EXPORT2
122 usrc_writeArray(FILE *f
,
124 const void *p
, int32_t width
, int32_t length
,
125 const char *postfix
) {
137 p8
=(const uint8_t *)p
;
140 p16
=(const uint16_t *)p
;
143 p32
=(const uint32_t *)p
;
146 fprintf(stderr
, "usrc_writeArray(width=%ld) unrecognized width\n", (long)width
);
150 fprintf(f
, prefix
, (long)length
);
152 for(i
=col
=0; i
<length
; ++i
, ++col
) {
172 value
=0; /* unreachable */
175 fprintf(f
, value
<=9 ? "%lu" : "0x%lx", (unsigned long)value
);
182 U_CAPI
void U_EXPORT2
183 usrc_writeUTrie2Arrays(FILE *f
,
184 const char *indexPrefix
, const char *data32Prefix
,
186 const char *postfix
) {
187 if(pTrie
->data32
==NULL
) {
189 usrc_writeArray(f
, indexPrefix
, pTrie
->index
, 16, pTrie
->indexLength
+pTrie
->dataLength
, postfix
);
192 usrc_writeArray(f
, indexPrefix
, pTrie
->index
, 16, pTrie
->indexLength
, postfix
);
193 usrc_writeArray(f
, data32Prefix
, pTrie
->data32
, 32, pTrie
->dataLength
, postfix
);
197 U_CAPI
void U_EXPORT2
198 usrc_writeUTrie2Struct(FILE *f
,
201 const char *indexName
, const char *data32Name
,
202 const char *postfix
) {
206 if(pTrie
->data32
==NULL
) {
211 " %s+%ld,\n" /* data16 */
212 " NULL,\n", /* data32 */
215 (long)pTrie
->indexLength
);
221 " NULL,\n" /* data16 */
222 " %s,\n", /* data32 */
228 " %ld,\n" /* indexLength */
229 " %ld,\n" /* dataLength */
230 " 0x%hx,\n" /* index2NullOffset */
231 " 0x%hx,\n" /* dataNullOffset */
232 " 0x%lx,\n" /* initialValue */
233 " 0x%lx,\n" /* errorValue */
234 " 0x%lx,\n" /* highStart */
235 " 0x%lx,\n" /* highValueIndex */
236 " NULL, 0, FALSE, FALSE, 0, NULL\n",
237 (long)pTrie
->indexLength
, (long)pTrie
->dataLength
,
238 (short)pTrie
->index2NullOffset
, (short)pTrie
->dataNullOffset
,
239 (long)pTrie
->initialValue
, (long)pTrie
->errorValue
,
240 (long)pTrie
->highStart
, (long)pTrie
->highValueIndex
);
246 U_CAPI
void U_EXPORT2
247 usrc_writeUCPTrieArrays(FILE *f
,
248 const char *indexPrefix
, const char *dataPrefix
,
249 const UCPTrie
*pTrie
,
250 const char *postfix
) {
251 usrc_writeArray(f
, indexPrefix
, pTrie
->index
, 16, pTrie
->indexLength
, postfix
);
253 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_16
? 16 :
254 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_32
? 32 :
255 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_8
? 8 : 0;
256 usrc_writeArray(f
, dataPrefix
, pTrie
->data
.ptr0
, width
, pTrie
->dataLength
, postfix
);
259 U_CAPI
void U_EXPORT2
260 usrc_writeUCPTrieStruct(FILE *f
,
262 const UCPTrie
*pTrie
,
263 const char *indexName
, const char *dataName
,
264 const char *postfix
) {
271 " { %s },\n", // data (union)
276 " %ld, %ld,\n" // indexLength, dataLength
277 " 0x%lx, 0x%x,\n" // highStart, shifted12HighStart
278 " %d, %d,\n" // type, valueWidth
279 " 0, 0,\n" // reserved32, reserved16
280 " 0x%x, 0x%lx,\n" // index3NullOffset, dataNullOffset
281 " 0x%lx,\n", // nullValue
282 (long)pTrie
->indexLength
, (long)pTrie
->dataLength
,
283 (long)pTrie
->highStart
, pTrie
->shifted12HighStart
,
284 pTrie
->type
, pTrie
->valueWidth
,
285 pTrie
->index3NullOffset
, (long)pTrie
->dataNullOffset
,
286 (long)pTrie
->nullValue
);
292 U_CAPI
void U_EXPORT2
293 usrc_writeUCPTrie(FILE *f
, const char *name
, const UCPTrie
*pTrie
) {
295 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_16
? 16 :
296 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_32
? 32 :
297 pTrie
->valueWidth
==UCPTRIE_VALUE_BITS_8
? 8 : 0;
298 char line
[100], line2
[100], line3
[100];
299 sprintf(line
, "static const uint16_t %s_trieIndex[%%ld]={\n", name
);
300 sprintf(line2
, "static const uint%d_t %s_trieData[%%ld]={\n", (int)width
, name
);
301 usrc_writeUCPTrieArrays(f
, line
, line2
, pTrie
, "\n};\n\n");
302 sprintf(line
, "static const UCPTrie %s_trie={\n", name
);
303 sprintf(line2
, "%s_trieIndex", name
);
304 sprintf(line3
, "%s_trieData", name
);
305 usrc_writeUCPTrieStruct(f
, line
, pTrie
, line2
, line3
, "};\n\n");
308 U_CAPI
void U_EXPORT2
309 usrc_writeArrayOfMostlyInvChars(FILE *f
,
311 const char *p
, int32_t length
,
312 const char *postfix
) {
317 fprintf(f
, prefix
, (long)length
);
320 for(i
=col
=0; i
<length
; ++i
, ++col
) {
323 /* Break long lines. Try to break at interesting places, to minimize revision diffs. */
325 /* Very long line. */
327 /* Long line, break after terminating NUL. */
328 (col
>=24 && prev2
>=0x20 && prev
==0) ||
329 /* Medium-long line, break before non-NUL, non-character byte. */
330 (col
>=16 && (prev
==0 || prev
>=0x20) && 0<c
&& c
<0x20)
338 fprintf(f
, c
<0x20 ? "%u" : "'%c'", c
);