/* ******************************************************************************* * * Copyright (C) 1999, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* * file name: gennames.c * encoding: US-ASCII * tab size: 8 (not used) * indentation:4 * * created on: 1999nov01 * created by: Markus W. Scherer * * This program reads a binary file and creates a C source code file * with a byte array that contains the data of the binary file. * * 12/09/1999 weiv Added multiple file handling */ #include #include #include "unicode/utypes.h" #include "cmemory.h" #include "cstring.h" #include "filestrm.h" #include "toolutil.h" static uint16_t column=0xffff; /* prototypes --------------------------------------------------------------- */ static void writeCCode(const char *filename); static void getOutFilename(const char *inFilename, char *outFilename, char *entryName); static void write8(FileStream *out, uint8_t byte); /* -------------------------------------------------------------------------- */ extern int main(int argc, char *argv[]) { if(argc<=1) { fprintf(stderr, "usage: %s filename1 filename2 ...\n" "\tread the binary input file and \n" "\tcreate a .c file with a byte array that contains the input file's data\n", argv[0]); } else { while (--argc) { fprintf(stdout, "Generating C code for %s\n", getLongPathname(argv[argc])); column=0xffff; writeCCode(getLongPathname(argv[argc])); } } return 0; } static void writeCCode(const char *filename) { char buffer[4096], entry[40]; FileStream *in, *out; size_t i, length; in=T_FileStream_open(filename, "rb"); if(in==NULL) { fprintf(stderr, "genccode: unable to open input file %s\n", filename); exit(U_FILE_ACCESS_ERROR); } getOutFilename(filename, buffer, entry); out=T_FileStream_open(buffer, "w"); if(out==NULL) { fprintf(stderr, "genccode: unable to open output file %s\n", buffer); exit(U_FILE_ACCESS_ERROR); } T_FileStream_writeLine(out, "#include \"unicode/utypes.h\"\nU_CAPI const struct U_EXPORT2 {\n double bogus;\n uint8_t bytes "); T_FileStream_writeLine(out, "["); sprintf(buffer, "%d", T_FileStream_size(in) ); T_FileStream_writeLine(out, buffer); T_FileStream_writeLine(out, "]; \n} "); for(i=0;i=100) { s[i++]='0'+byte/100; byte%=100; } if(i>0 || byte>=10) { s[i++]='0'+byte/10; byte%=10; } s[i++]='0'+byte; s[i]=0; /* write the value, possibly with comma and newline */ if(column==0xffff) { /* first byte */ column=1; } else if(column<16) { T_FileStream_writeLine(out, ","); ++column; } else { T_FileStream_writeLine(out, ",\n"); column=1; } T_FileStream_writeLine(out, s); }