1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2005-2012, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: writesrc.c
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2005apr23
* created by: Markus W. Scherer
*
* Helper functions for writing source code for data.
*/
#include <stdio.h>
#include <time.h>
// The C99 standard suggested that C++ implementations not define PRId64 etc. constants
// unless this macro is defined.
// See the Notes at https://en.cppreference.com/w/cpp/types/integer .
// Similar to defining __STDC_LIMIT_MACROS in unicode/ptypes.h .
#ifndef __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
#endif
#include <cinttypes>
#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/ucptrie.h"
#include "unicode/errorcode.h"
#include "unicode/uniset.h"
#include "unicode/usetiter.h"
#include "unicode/utf16.h"
#include "utrie2.h"
#include "cstring.h"
#include "writesrc.h"
#include "util.h"
U_NAMESPACE_BEGIN
ValueNameGetter::~ValueNameGetter() {}
U_NAMESPACE_END
U_NAMESPACE_USE
static FILE *
usrc_createWithoutHeader(const char *path, const char *filename) {
char buffer[1024];
const char *p;
char *q;
FILE *f;
char c;
if(path==nullptr) {
p=filename;
} else {
/* concatenate path and filename, with U_FILE_SEP_CHAR in between if necessary */
uprv_strcpy(buffer, path);
q=buffer+uprv_strlen(buffer);
if(q>buffer && (c=*(q-1))!=U_FILE_SEP_CHAR && c!=U_FILE_ALT_SEP_CHAR) {
*q++=U_FILE_SEP_CHAR;
}
uprv_strcpy(q, filename);
p=buffer;
}
f=fopen(p, "w");
if (f==nullptr) {
fprintf(
stderr,
"usrc_create(%s, %s): unable to create file\n",
path!=nullptr ? path : "", filename);
}
return f;
}
U_CAPI FILE * U_EXPORT2
usrc_create(const char *path, const char *filename, int32_t copyrightYear, const char *generator) {
FILE *f = usrc_createWithoutHeader(path, filename);
if (f == nullptr) {
return f;
}
usrc_writeCopyrightHeader(f, "//", copyrightYear);
usrc_writeFileNameGeneratedBy(f, "//", filename, generator);
return f;
}
U_CAPI FILE * U_EXPORT2
usrc_createTextData(const char *path, const char *filename, int32_t copyrightYear, const char *generator) {
FILE *f = usrc_createWithoutHeader(path, filename);
if (f == nullptr) {
return f;
}
usrc_writeCopyrightHeader(f, "#", copyrightYear);
usrc_writeFileNameGeneratedBy(f, "#", filename, generator);
return f;
}
U_CAPI void U_EXPORT2
usrc_writeCopyrightHeader(FILE *f, const char *prefix, int32_t copyrightYear) {
fprintf(f,
"%s Copyright (C) %d and later: Unicode, Inc. and others.\n"
"%s License & terms of use: http://www.unicode.org/copyright.html\n",
prefix, copyrightYear, prefix);
if (copyrightYear <= 2016) {
fprintf(f,
"%s Copyright (C) 1999-2016, International Business Machines\n"
"%s Corporation and others. All Rights Reserved.\n",
prefix, prefix);
}
}
U_CAPI void U_EXPORT2
usrc_writeFileNameGeneratedBy(
FILE *f,
const char *prefix,
const char *filename,
const char *generator) {
char buffer[1024];
const struct tm *lt;
time_t t;
const char *pattern =
"%s\n"
"%s file name: %s\n"
"%s\n"
"%s machine-generated by: %s\n"
"\n";
time(&t);
lt=localtime(&t);
if(generator==nullptr) {
strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
fprintf(f, pattern, prefix, prefix, filename, prefix, prefix, buffer);
} else {
fprintf(f, pattern, prefix, prefix, filename, prefix, prefix, generator);
}
}
U_CAPI void U_EXPORT2
usrc_writeArray(FILE *f,
const char *prefix,
const void *p, int32_t width, int32_t length,
const char *indent,
const char *postfix) {
const uint8_t *p8;
const uint16_t *p16;
const uint32_t *p32;
const int64_t *p64; // Signed due to TOML!
int64_t value; // Signed due to TOML!
int32_t i, col;
p8=nullptr;
p16=nullptr;
p32=nullptr;
p64=nullptr;
switch(width) {
case 8:
p8=(const uint8_t *)p;
break;
case 16:
p16=(const uint16_t *)p;
break;
case 32:
p32=(const uint32_t *)p;
break;
case 64:
p64=(const int64_t *)p;
break;
default:
fprintf(stderr, "usrc_writeArray(width=%ld) unrecognized width\n", (long)width);
return;
}
if(prefix!=nullptr) {
fprintf(f, prefix, (long)length);
}
for(i=col=0; i<length; ++i, ++col) {
if(i>0) {
if(col<16) {
fputc(',', f);
} else {
fputs(",\n", f);
fputs(indent, f);
col=0;
}
}
switch(width) {
case 8:
value=p8[i];
break;
case 16:
value=p16[i];
break;
case 32:
value=p32[i];
break;
case 64:
value=p64[i];
break;
default:
value=0; /* unreachable */
break;
}
fprintf(f, value<=9 ? "%" PRId64 : "0x%" PRIx64, value);
}
if(postfix!=nullptr) {
fputs(postfix, f);
}
}
U_CAPI void U_EXPORT2
usrc_writeUTrie2Arrays(FILE *f,
const char *indexPrefix, const char *data32Prefix,
const UTrie2 *pTrie,
const char *postfix) {
if(pTrie->data32==nullptr) {
/* 16-bit trie */
usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength+pTrie->dataLength, "", postfix);
} else {
/* 32-bit trie */
usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, "", postfix);
usrc_writeArray(f, data32Prefix, pTrie->data32, 32, pTrie->dataLength, "", postfix);
}
}
U_CAPI void U_EXPORT2
usrc_writeUTrie2Struct(FILE *f,
const char *prefix,
const UTrie2 *pTrie,
const char *indexName, const char *data32Name,
const char *postfix) {
if(prefix!=nullptr) {
fputs(prefix, f);
}
if(pTrie->data32==nullptr) {
/* 16-bit trie */
fprintf(
f,
" %s,\n" /* index */
" %s+%ld,\n" /* data16 */
" nullptr,\n", /* data32 */
indexName,
indexName,
(long)pTrie->indexLength);
} else {
/* 32-bit trie */
fprintf(
f,
" %s,\n" /* index */
" nullptr,\n" /* data16 */
" %s,\n", /* data32 */
indexName,
data32Name);
}
fprintf(
f,
" %ld,\n" /* indexLength */
" %ld,\n" /* dataLength */
" 0x%hx,\n" /* index2NullOffset */
" 0x%hx,\n" /* dataNullOffset */
" 0x%lx,\n" /* initialValue */
" 0x%lx,\n" /* errorValue */
" 0x%lx,\n" /* highStart */
" 0x%lx,\n" /* highValueIndex */
" nullptr, 0, false, false, 0, nullptr\n",
(long)pTrie->indexLength, (long)pTrie->dataLength,
(short)pTrie->index2NullOffset, (short)pTrie->dataNullOffset,
(long)pTrie->initialValue, (long)pTrie->errorValue,
(long)pTrie->highStart, (long)pTrie->highValueIndex);
if(postfix!=nullptr) {
fputs(postfix, f);
}
}
U_CAPI void U_EXPORT2
usrc_writeUCPTrieArrays(FILE *f,
const char *indexPrefix, const char *dataPrefix,
const UCPTrie *pTrie,
const char *postfix,
UTargetSyntax syntax) {
const char* indent = (syntax == UPRV_TARGET_SYNTAX_TOML) ? " " : "";
usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, indent, postfix);
int32_t width=
pTrie->valueWidth==UCPTRIE_VALUE_BITS_16 ? 16 :
pTrie->valueWidth==UCPTRIE_VALUE_BITS_32 ? 32 :
pTrie->valueWidth==UCPTRIE_VALUE_BITS_8 ? 8 : 0;
usrc_writeArray(f, dataPrefix, pTrie->data.ptr0, width, pTrie->dataLength, indent, postfix);
}
U_CAPI void U_EXPORT2
usrc_writeUCPTrieStruct(FILE *f,
const char *prefix,
const UCPTrie *pTrie,
const char *indexName, const char *dataName,
const char *postfix,
UTargetSyntax syntax) {
if(prefix!=nullptr) {
fputs(prefix, f);
}
if (syntax == UPRV_TARGET_SYNTAX_CCODE) {
fprintf(
f,
" %s,\n" // index
" { %s },\n", // data (union)
indexName,
dataName);
}
const char* pattern =
(syntax == UPRV_TARGET_SYNTAX_CCODE) ?
" %ld, %ld,\n" // indexLength, dataLength
" 0x%lx, 0x%x,\n" // highStart, shifted12HighStart
" %d, %d,\n" // type, valueWidth
" 0, 0,\n" // reserved32, reserved16
" 0x%x, 0x%lx,\n" // index3NullOffset, dataNullOffset
" 0x%lx,\n" // nullValue
:
"indexLength = %ld\n"
"dataLength = %ld\n"
"highStart = 0x%lx\n"
"shifted12HighStart = 0x%x\n"
"type = %d\n"
"valueWidth = %d\n"
"index3NullOffset = 0x%x\n"
"dataNullOffset = 0x%lx\n"
"nullValue = 0x%lx\n"
;
fprintf(
f,
pattern,
(long)pTrie->indexLength, (long)pTrie->dataLength,
(long)pTrie->highStart, pTrie->shifted12HighStart,
pTrie->type, pTrie->valueWidth,
pTrie->index3NullOffset, (long)pTrie->dataNullOffset,
(long)pTrie->nullValue);
if(postfix!=nullptr) {
fputs(postfix, f);
}
}
U_CAPI void U_EXPORT2
usrc_writeUCPTrie(FILE *f, const char *name, const UCPTrie *pTrie, UTargetSyntax syntax) {
int32_t width=
pTrie->valueWidth==UCPTRIE_VALUE_BITS_16 ? 16 :
pTrie->valueWidth==UCPTRIE_VALUE_BITS_32 ? 32 :
pTrie->valueWidth==UCPTRIE_VALUE_BITS_8 ? 8 : 0;
char line[100], line2[100], line3[100], line4[100];
switch (syntax) {
case UPRV_TARGET_SYNTAX_CCODE:
snprintf(line, sizeof(line), "static const uint16_t %s_trieIndex[%%ld]={\n", name);
snprintf(line2, sizeof(line2), "static const uint%d_t %s_trieData[%%ld]={\n", (int)width, name);
snprintf(line3, sizeof(line3), "\n};\n\n");
break;
case UPRV_TARGET_SYNTAX_TOML:
snprintf(line, sizeof(line), "index = [\n ");
snprintf(line2, sizeof(line2), "data_%d = [\n ", (int)width);
snprintf(line3, sizeof(line3), "\n]\n");
break;
default:
UPRV_UNREACHABLE_EXIT;
}
usrc_writeUCPTrieArrays(f, line, line2, pTrie, line3, syntax);
switch (syntax) {
case UPRV_TARGET_SYNTAX_CCODE:
snprintf(line, sizeof(line), "static const UCPTrie %s_trie={\n", name);
snprintf(line2, sizeof(line2), "%s_trieIndex", name);
snprintf(line3, sizeof(line3), "%s_trieData", name);
snprintf(line4, sizeof(line4), "};\n\n");
break;
case UPRV_TARGET_SYNTAX_TOML:
line[0] = 0;
line2[0] = 0;
line3[0] = 0;
line4[0] = 0;
break;
default:
UPRV_UNREACHABLE_EXIT;
}
usrc_writeUCPTrieStruct(f, line, pTrie, line2, line3, line4, syntax);
}
U_CAPI void U_EXPORT2
usrc_writeUnicodeSet(
FILE *f,
const USet *pSet,
UTargetSyntax syntax) {
// ccode is not yet supported
U_ASSERT(syntax == UPRV_TARGET_SYNTAX_TOML);
// Write out a list of ranges
const UnicodeSet* set = UnicodeSet::fromUSet(pSet);
UnicodeSetIterator it(*set);
fprintf(f, "# Inclusive ranges of the code points in the set.\n");
fprintf(f, "ranges = [\n");
bool seenFirstString = false;
while (it.nextRange()) {
if (it.isString()) {
if (!seenFirstString) {
seenFirstString = true;
fprintf(f, "]\nstrings = [\n");
}
const UnicodeString& str = it.getString();
fprintf(f, " ");
usrc_writeStringAsASCII(f, str.getBuffer(), str.length(), syntax);
fprintf(f, ",\n");
} else {
U_ASSERT(!seenFirstString);
UChar32 start = it.getCodepoint();
UChar32 end = it.getCodepointEnd();
fprintf(f, " [0x%x, 0x%x],\n", start, end);
}
}
fprintf(f, "]\n");
}
U_CAPI void U_EXPORT2
usrc_writeUCPMap(
FILE *f,
const UCPMap *pMap,
icu::ValueNameGetter *valueNameGetter,
UTargetSyntax syntax) {
// ccode is not yet supported
U_ASSERT(syntax == UPRV_TARGET_SYNTAX_TOML);
(void) syntax; // silence unused variable errors
// Print out list of ranges
UChar32 start = 0, end;
uint32_t value;
fprintf(f, "# Code points `a` through `b` have value `v`, corresponding to `name`.\n");
fprintf(f, "ranges = [\n");
while ((end = ucpmap_getRange(pMap, start, UCPMAP_RANGE_NORMAL, 0, nullptr, nullptr, &value)) >= 0) {
if (valueNameGetter != nullptr) {
const char *name = valueNameGetter->getName(value);
fprintf(f, " {a=0x%x, b=0x%x, v=%u, name=\"%s\"},\n", start, end, value, name);
} else {
fprintf(f, " {a=0x%x, b=0x%x, v=%u},\n", start, end, value);
}
start = end + 1;
}
fprintf(f, "]\n");
}
U_CAPI void U_EXPORT2
usrc_writeArrayOfMostlyInvChars(FILE *f,
const char *prefix,
const char *p, int32_t length,
const char *postfix) {
int32_t i, col;
int prev2, prev, c;
if(prefix!=nullptr) {
fprintf(f, prefix, (long)length);
}
prev2=prev=-1;
for(i=col=0; i<length; ++i, ++col) {
c=(uint8_t)p[i];
if(i>0) {
/* Break long lines. Try to break at interesting places, to minimize revision diffs. */
if(
/* Very long line. */
col>=32 ||
/* Long line, break after terminating NUL. */
(col>=24 && prev2>=0x20 && prev==0) ||
/* Medium-long line, break before non-NUL, non-character byte. */
(col>=16 && (prev==0 || prev>=0x20) && 0<c && c<0x20)
) {
fputs(",\n", f);
col=0;
} else {
fputc(',', f);
}
}
fprintf(f, c<0x20 ? "%u" : "'%c'", c);
prev2=prev;
prev=c;
}
if(postfix!=nullptr) {
fputs(postfix, f);
}
}
U_CAPI void U_EXPORT2
usrc_writeStringAsASCII(FILE *f,
const char16_t* ptr, int32_t length,
UTargetSyntax) {
// For now, assume all UTargetSyntax values are valid here.
fprintf(f, "\"");
int32_t i = 0;
UChar32 cp;
while (i < length) {
U16_NEXT(ptr, i, length, cp);
if (cp == u'"') {
fprintf(f, "\\\"");
} else if (ICU_Utility::isUnprintable(cp)) {
UnicodeString u16result;
ICU_Utility::escapeUnprintable(u16result, cp);
std::string u8result;
u16result.toUTF8String(u8result);
fprintf(f, "%s", u8result.data());
} else {
U_ASSERT(cp < 0x80);
char s[2] = {static_cast<char>(cp), 0};
fprintf(f, "%s", s);
}
}
fprintf(f, "\"");
}
|