1 // Copyright (C) 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"
30 usrc_createWithHeader(const char *path, const char *filename,
31 const char *generator, const char *header) {
41 /* concatenate path and filename, with U_FILE_SEP_CHAR in between if necessary */
42 uprv_strcpy(buffer, path);
43 q=buffer+uprv_strlen(buffer);
44 if(q>buffer && (c=*(q-1))!=U_FILE_SEP_CHAR && c!=U_FILE_ALT_SEP_CHAR) {
47 uprv_strcpy(q, filename);
59 strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
60 fprintf(f, header, filename, buffer);
62 fprintf(f, header, 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 // TODO: Add parameter for the first year this file was generated, not before 2016.
76 static const char *header=
77 "// Copyright (C) 2016 and later: Unicode, Inc. and others.\n"
78 "// License & terms of use: http://www.unicode.org/copyright.html\n"
80 "// Copyright (C) 1999-2016, International Business Machines\n"
81 "// Corporation and others. All Rights Reserved.\n"
85 "// machine-generated by: %s\n"
87 return usrc_createWithHeader(path, filename, generator, header);
90 U_CAPI FILE * U_EXPORT2
91 usrc_createTextData(const char *path, const char *filename, const char *generator) {
92 // TODO: Add parameter for the first year this file was generated, not before 2016.
93 static const char *header=
94 "# Copyright (C) 2016 and later: Unicode, Inc. and others.\n"
95 "# License & terms of use: http://www.unicode.org/copyright.html\n"
96 "# Copyright (C) 1999-2016, International Business Machines\n"
97 "# Corporation and others. All Rights Reserved.\n"
101 "# machine-generated by: %s\n"
103 return usrc_createWithHeader(path, filename, generator, header);
106 U_CAPI void U_EXPORT2
107 usrc_writeArray(FILE *f,
109 const void *p, int32_t width, int32_t length,
110 const char *postfix) {
122 p8=(const uint8_t *)p;
125 p16=(const uint16_t *)p;
128 p32=(const uint32_t *)p;
131 fprintf(stderr, "usrc_writeArray(width=%ld) unrecognized width\n", (long)width);
135 fprintf(f, prefix, (long)length);
137 for(i=col=0; i<length; ++i, ++col) {
157 value=0; /* unreachable */
160 fprintf(f, value<=9 ? "%lu" : "0x%lx", (unsigned long)value);
167 U_CAPI void U_EXPORT2
168 usrc_writeUTrie2Arrays(FILE *f,
169 const char *indexPrefix, const char *data32Prefix,
171 const char *postfix) {
172 if(pTrie->data32==NULL) {
174 usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength+pTrie->dataLength, postfix);
177 usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, postfix);
178 usrc_writeArray(f, data32Prefix, pTrie->data32, 32, pTrie->dataLength, postfix);
182 U_CAPI void U_EXPORT2
183 usrc_writeUTrie2Struct(FILE *f,
186 const char *indexName, const char *data32Name,
187 const char *postfix) {
191 if(pTrie->data32==NULL) {
196 " %s+%ld,\n" /* data16 */
197 " NULL,\n", /* data32 */
200 (long)pTrie->indexLength);
206 " NULL,\n" /* data16 */
207 " %s,\n", /* data32 */
213 " %ld,\n" /* indexLength */
214 " %ld,\n" /* dataLength */
215 " 0x%hx,\n" /* index2NullOffset */
216 " 0x%hx,\n" /* dataNullOffset */
217 " 0x%lx,\n" /* initialValue */
218 " 0x%lx,\n" /* errorValue */
219 " 0x%lx,\n" /* highStart */
220 " 0x%lx,\n" /* highValueIndex */
221 " NULL, 0, FALSE, FALSE, 0, NULL\n",
222 (long)pTrie->indexLength, (long)pTrie->dataLength,
223 (short)pTrie->index2NullOffset, (short)pTrie->dataNullOffset,
224 (long)pTrie->initialValue, (long)pTrie->errorValue,
225 (long)pTrie->highStart, (long)pTrie->highValueIndex);
231 U_CAPI void U_EXPORT2
232 usrc_writeArrayOfMostlyInvChars(FILE *f,
234 const char *p, int32_t length,
235 const char *postfix) {
240 fprintf(f, prefix, (long)length);
243 for(i=col=0; i<length; ++i, ++col) {
246 /* Break long lines. Try to break at interesting places, to minimize revision diffs. */
248 /* Very long line. */
250 /* Long line, break after terminating NUL. */
251 (col>=24 && prev2>=0x20 && prev==0) ||
252 /* Medium-long line, break before non-NUL, non-character byte. */
253 (col>=16 && (prev==0 || prev>=0x20) && 0<c && c<0x20)
261 fprintf(f, c<0x20 ? "%u" : "'%c'", c);