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
|
/*
*******************************************************************************
*
* Copyright (C) 2005-2010, International Business Machines
* 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"
#include "utrie2.h"
#include "cstring.h"
#include "writesrc.h"
static FILE *
usrc_createWithHeader(const char *path, const char *filename, const char *header) {
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);
fprintf(f, header, year, filename, buffer);
} else {
fprintf(
stderr,
"usrc_create(%s, %s): unable to create file\n",
path!=NULL ? path : "", filename);
}
return f;
}
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);
}
U_CAPI void U_EXPORT2
usrc_writeArray(FILE *f,
const char *prefix,
const void *p, int32_t width, int32_t length,
const char *postfix) {
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;
}
if(prefix!=NULL) {
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);
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;
}
fprintf(f, value<=9 ? "%lu" : "0x%lx", (unsigned long)value);
}
if(postfix!=NULL) {
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==NULL) {
/* 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!=NULL) {
fputs(prefix, f);
}
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);
}
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 */
" NULL, 0, FALSE, FALSE, 0, NULL\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!=NULL) {
fputs(postfix, f);
}
}
|