2005-04-28 04:30:47 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2010-01-06 23:50:03 +00:00
|
|
|
* Copyright (C) 2005-2010, International Business Machines
|
2005-04-28 04:30:47 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: writesrc.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* 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>
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/putil.h"
|
2008-10-22 19:50:07 +00:00
|
|
|
#include "utrie2.h"
|
2005-04-28 04:30:47 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "writesrc.h"
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
static FILE *
|
|
|
|
usrc_createWithHeader(const char *path, const char *filename, const char *header) {
|
2005-04-28 04:30:47 +00:00
|
|
|
char buffer[1024];
|
|
|
|
const char *p;
|
|
|
|
char *q;
|
|
|
|
FILE *f;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if(path==NULL) {
|
|
|
|
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!=NULL) {
|
|
|
|
char year[8];
|
|
|
|
const struct tm *lt;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
time(&t);
|
|
|
|
lt=localtime(&t);
|
|
|
|
strftime(year, sizeof(year), "%Y", lt);
|
|
|
|
strftime(buffer, sizeof(buffer), "%Y-%m-%d", lt);
|
2010-01-06 23:50:03 +00:00
|
|
|
fprintf(f, header, year, filename, buffer);
|
2005-04-28 04:30:47 +00:00
|
|
|
} else {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"usrc_create(%s, %s): unable to create file\n",
|
2007-08-29 02:57:42 +00:00
|
|
|
path!=NULL ? path : "", filename);
|
2005-04-28 04:30:47 +00:00
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2010-01-06 23:50:03 +00:00
|
|
|
U_CAPI FILE * U_EXPORT2
|
|
|
|
usrc_create(const char *path, const char *filename) {
|
|
|
|
const char *header=
|
|
|
|
"/*\n"
|
|
|
|
" * Copyright (C) 1999-%s, International Business Machines\n"
|
|
|
|
" * Corporation and others. All Rights Reserved.\n"
|
|
|
|
" *\n"
|
|
|
|
" * file name: %s\n"
|
|
|
|
" *\n"
|
|
|
|
" * machine-generated on: %s\n"
|
|
|
|
" */\n\n";
|
|
|
|
return usrc_createWithHeader(path, filename, header);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI FILE * U_EXPORT2
|
|
|
|
usrc_createTextData(const char *path, const char *filename) {
|
|
|
|
const char *header=
|
|
|
|
"# Copyright (C) 1999-%s, International Business Machines\n"
|
|
|
|
"# Corporation and others. All Rights Reserved.\n"
|
|
|
|
"#\n"
|
|
|
|
"# file name: %s\n"
|
|
|
|
"#\n"
|
|
|
|
"# machine-generated on: %s\n"
|
|
|
|
"#\n\n";
|
|
|
|
return usrc_createWithHeader(path, filename, header);
|
|
|
|
}
|
|
|
|
|
2005-04-28 04:30:47 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2005-04-28 17:15:46 +00:00
|
|
|
usrc_writeArray(FILE *f,
|
|
|
|
const char *prefix,
|
|
|
|
const void *p, int32_t width, int32_t length,
|
|
|
|
const char *postfix) {
|
2005-04-28 04:30:47 +00:00
|
|
|
const uint8_t *p8;
|
|
|
|
const uint16_t *p16;
|
|
|
|
const uint32_t *p32;
|
|
|
|
uint32_t value;
|
|
|
|
int32_t i, col;
|
|
|
|
|
|
|
|
p8=NULL;
|
|
|
|
p16=NULL;
|
|
|
|
p32=NULL;
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "usrc_writeArray(width=%ld) unrecognized width\n", (long)width);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-28 17:15:46 +00:00
|
|
|
if(prefix!=NULL) {
|
|
|
|
fprintf(f, prefix, (long)length);
|
|
|
|
}
|
2005-04-28 04:30:47 +00:00
|
|
|
for(i=col=0; i<length; ++i, ++col) {
|
|
|
|
if(i>0) {
|
|
|
|
if(col<16) {
|
|
|
|
fputc(',', f);
|
|
|
|
} else {
|
|
|
|
fputs(",\n", f);
|
|
|
|
col=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(width) {
|
|
|
|
case 8:
|
|
|
|
value=p8[i];
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
value=p16[i];
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
value=p32[i];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value=0; /* unreachable */
|
|
|
|
break;
|
|
|
|
}
|
2005-04-28 17:15:46 +00:00
|
|
|
fprintf(f, value<=9 ? "%lu" : "0x%lx", (unsigned long)value);
|
|
|
|
}
|
|
|
|
if(postfix!=NULL) {
|
|
|
|
fputs(postfix, f);
|
2005-04-28 04:30:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
2008-10-22 19:50:07 +00:00
|
|
|
usrc_writeUTrie2Arrays(FILE *f,
|
|
|
|
const char *indexPrefix, const char *data32Prefix,
|
|
|
|
const UTrie2 *pTrie,
|
|
|
|
const char *postfix) {
|
2005-04-28 17:15:46 +00:00
|
|
|
if(pTrie->data32==NULL) {
|
2005-04-28 04:30:47 +00:00
|
|
|
/* 16-bit trie */
|
2005-04-28 17:15:46 +00:00
|
|
|
usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength+pTrie->dataLength, postfix);
|
2005-04-28 04:30:47 +00:00
|
|
|
} else {
|
|
|
|
/* 32-bit trie */
|
2005-04-28 17:15:46 +00:00
|
|
|
usrc_writeArray(f, indexPrefix, pTrie->index, 16, pTrie->indexLength, postfix);
|
2008-10-22 19:50:07 +00:00
|
|
|
usrc_writeArray(f, data32Prefix, pTrie->data32, 32, pTrie->dataLength, postfix);
|
2005-04-28 04:30:47 +00:00
|
|
|
}
|
2005-04-28 17:15:46 +00:00
|
|
|
}
|
2005-04-28 04:30:47 +00:00
|
|
|
|
2005-04-28 17:15:46 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2008-10-22 19:50:07 +00:00
|
|
|
usrc_writeUTrie2Struct(FILE *f,
|
|
|
|
const char *prefix,
|
|
|
|
const UTrie2 *pTrie,
|
|
|
|
const char *indexName, const char *data32Name,
|
|
|
|
const char *postfix) {
|
2005-04-28 17:15:46 +00:00
|
|
|
if(prefix!=NULL) {
|
|
|
|
fputs(prefix, f);
|
|
|
|
}
|
2008-10-22 19:50:07 +00:00
|
|
|
if(pTrie->data32==NULL) {
|
|
|
|
/* 16-bit trie */
|
|
|
|
fprintf(
|
|
|
|
f,
|
|
|
|
" %s,\n" /* index */
|
|
|
|
" %s+%ld,\n" /* data16 */
|
|
|
|
" NULL,\n", /* data32 */
|
|
|
|
indexName,
|
|
|
|
indexName,
|
|
|
|
(long)pTrie->indexLength);
|
|
|
|
} else {
|
|
|
|
/* 32-bit trie */
|
|
|
|
fprintf(
|
|
|
|
f,
|
|
|
|
" %s,\n" /* index */
|
|
|
|
" NULL,\n" /* data16 */
|
|
|
|
" %s,\n", /* data32 */
|
|
|
|
indexName,
|
|
|
|
data32Name);
|
2005-04-28 04:30:47 +00:00
|
|
|
}
|
|
|
|
fprintf(
|
|
|
|
f,
|
2008-10-22 19:50:07 +00:00
|
|
|
" %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 */
|
2008-10-22 22:28:06 +00:00
|
|
|
" 0x%lx,\n" /* highValueIndex */
|
2008-10-23 06:00:59 +00:00
|
|
|
" NULL, 0, FALSE, FALSE, 0, NULL\n",
|
2005-04-28 17:15:46 +00:00
|
|
|
(long)pTrie->indexLength, (long)pTrie->dataLength,
|
2008-10-22 19:50:07 +00:00
|
|
|
(short)pTrie->index2NullOffset, (short)pTrie->dataNullOffset,
|
|
|
|
(long)pTrie->initialValue, (long)pTrie->errorValue,
|
|
|
|
(long)pTrie->highStart, (long)pTrie->highValueIndex);
|
2005-04-28 17:15:46 +00:00
|
|
|
if(postfix!=NULL) {
|
|
|
|
fputs(postfix, f);
|
|
|
|
}
|
2005-04-28 04:30:47 +00:00
|
|
|
}
|
2010-12-31 18:21:36 +00:00
|
|
|
|
|
|
|
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!=NULL) {
|
|
|
|
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!=NULL) {
|
|
|
|
fputs(postfix, f);
|
|
|
|
}
|
|
|
|
}
|