2 *******************************************************************************
4 * Copyright (C) 2005-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: writesrc.c
10 * tab size: 8 (not used)
13 * created on: 2005apr23
14 * created by: Markus W. Scherer
16 * Helper functions for writing source code for data.
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
28 usrc_createWithHeader(const char *path, const char *filename,
29 const char *generator, const char *header) {
39 /* concatenate path and filename, with U_FILE_SEP_CHAR in between if necessary */
40 uprv_strcpy(buffer, path);
41 q=buffer+uprv_strlen(buffer);
42 if(q>buffer && (c=*(q-1))!=U_FILE_SEP_CHAR && c!=U_FILE_ALT_SEP_CHAR) {
45 uprv_strcpy(q, filename);
57 strftime(year, sizeof(year), "%Y", lt);
59 strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
60 fprintf(f, header, year, filename, buffer);
62 fprintf(f, header, year, filename, generator);
67 "usrc_create(%s, %s): unable to create file\n",
68 path!=NULL ? path : "", filename);
73 U_CAPI FILE * U_EXPORT2
74 usrc_create(const char *path, const char *filename, const char *generator) {
75 static const char *header=
77 " * Copyright (C) 1999-%s, International Business Machines\n"
78 " * Corporation and others. All Rights Reserved.\n"
82 " * machine-generated by: %s\n"
84 return usrc_createWithHeader(path, filename, generator, header);
87 U_CAPI FILE * U_EXPORT2
88 usrc_createTextData(const char *path, const char *filename, const char *generator) {
89 static const char *header=
90 "# Copyright (C) 1999-%s, International Business Machines\n"
91 "# Corporation and others. All Rights Reserved.\n"
95 "# machine-generated by: %s\n"
97 return usrc_createWithHeader(path, filename, generator, header);
100 U_CAPI void U_EXPORT2
101 usrc_writeArray(FILE *f,
103 const void *p, int32_t width, int32_t length,
104 const char *postfix) {
116 p8=(const uint8_t *)p;
119 p16=(const uint16_t *)p;
122 p32=(const uint32_t *)p;
125 fprintf(stderr, "usrc_writeArray(width=%ld) unrecognized width\n", (long)width);
129 fprintf(f, prefix, (long)length);
131 for(i=col=0; i<length; ++i, ++col) {
151 value=0; /* unreachable */
154 fprintf(f, value<=9 ? "%lu" : "0x%lx", (unsigned long)value);
161 U_CAPI void U_EXPORT2
162 usrc_writeUTrie2Arrays(FILE *f,
163 const char *indexPrefix, const char *data32Prefix,
165 const char *postfix) {
166 if(pTrie->data32==NULL) {
168 usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength+pTrie->dataLength, postfix);
171 usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, postfix);
172 usrc_writeArray(f, data32Prefix, pTrie->data32, 32, pTrie->dataLength, postfix);
176 U_CAPI void U_EXPORT2
177 usrc_writeUTrie2Struct(FILE *f,
180 const char *indexName, const char *data32Name,
181 const char *postfix) {
185 if(pTrie->data32==NULL) {
190 " %s+%ld,\n" /* data16 */
191 " NULL,\n", /* data32 */
194 (long)pTrie->indexLength);
200 " NULL,\n" /* data16 */
201 " %s,\n", /* data32 */
207 " %ld,\n" /* indexLength */
208 " %ld,\n" /* dataLength */
209 " 0x%hx,\n" /* index2NullOffset */
210 " 0x%hx,\n" /* dataNullOffset */
211 " 0x%lx,\n" /* initialValue */
212 " 0x%lx,\n" /* errorValue */
213 " 0x%lx,\n" /* highStart */
214 " 0x%lx,\n" /* highValueIndex */
215 " NULL, 0, FALSE, FALSE, 0, NULL\n",
216 (long)pTrie->indexLength, (long)pTrie->dataLength,
217 (short)pTrie->index2NullOffset, (short)pTrie->dataNullOffset,
218 (long)pTrie->initialValue, (long)pTrie->errorValue,
219 (long)pTrie->highStart, (long)pTrie->highValueIndex);
225 U_CAPI void U_EXPORT2
226 usrc_writeArrayOfMostlyInvChars(FILE *f,
228 const char *p, int32_t length,
229 const char *postfix) {
234 fprintf(f, prefix, (long)length);
237 for(i=col=0; i<length; ++i, ++col) {
240 /* Break long lines. Try to break at interesting places, to minimize revision diffs. */
242 /* Very long line. */
244 /* Long line, break after terminating NUL. */
245 (col>=24 && prev2>=0x20 && prev==0) ||
246 /* Medium-long line, break before non-NUL, non-character byte. */
247 (col>=16 && (prev==0 || prev>=0x20) && 0<c && c<0x20)
255 fprintf(f, c<0x20 ? "%u" : "'%c'", c);