1999-11-20 01:12:28 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 1999, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
1999-11-20 01:12:28 +00:00
|
|
|
*******************************************************************************
|
|
|
|
* 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.
|
1999-12-12 02:33:22 +00:00
|
|
|
*
|
|
|
|
* 12/09/1999 weiv Added multiple file handling
|
1999-11-20 01:12:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
1999-11-20 01:12:28 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "filestrm.h"
|
1999-11-23 04:49:35 +00:00
|
|
|
#include "toolutil.h"
|
2000-04-18 20:51:36 +00:00
|
|
|
#include "uoptions.h"
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
static uint16_t column=0xffff;
|
|
|
|
|
|
|
|
/* prototypes --------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2000-04-18 20:51:36 +00:00
|
|
|
writeCCode(const char *filename, const char *destdir);
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
static void
|
2000-04-18 20:51:36 +00:00
|
|
|
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName);
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
write8(FileStream *out, uint8_t byte);
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
2000-04-18 20:51:36 +00:00
|
|
|
static UOption options[]={
|
|
|
|
UOPTION_HELP_H,
|
|
|
|
UOPTION_HELP_QUESTION_MARK,
|
|
|
|
UOPTION_DESTDIR
|
|
|
|
};
|
|
|
|
|
1999-11-20 01:12:28 +00:00
|
|
|
extern int
|
2000-04-25 21:33:15 +00:00
|
|
|
main(int argc, const char *argv[]) {
|
2000-04-18 20:51:36 +00:00
|
|
|
/* read command line options */
|
|
|
|
argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
|
|
|
|
|
|
|
|
/* error handling, printing usage message */
|
|
|
|
if(argc<0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"error in command line argument \"%s\"\n",
|
|
|
|
argv[-argc]);
|
|
|
|
}
|
|
|
|
if(argc<0 || options[0].doesOccur || options[1].doesOccur) {
|
1999-11-20 01:12:28 +00:00
|
|
|
fprintf(stderr,
|
2000-04-18 20:51:36 +00:00
|
|
|
"usage: %s [-options] filename1 filename2 ...\n"
|
|
|
|
"\tread each binary input file and \n"
|
|
|
|
"\tcreate a .c file with a byte array that contains the input file's data\n"
|
|
|
|
"\toptions:\n"
|
|
|
|
"\t\t-h or -? or --help this usage text\n"
|
|
|
|
"\t\t-d or --destdir destination directory, followed by the path\n",
|
1999-11-20 01:12:28 +00:00
|
|
|
argv[0]);
|
|
|
|
} else {
|
1999-12-12 02:33:22 +00:00
|
|
|
while (--argc) {
|
|
|
|
fprintf(stdout, "Generating C code for %s\n", getLongPathname(argv[argc]));
|
|
|
|
column=0xffff;
|
2000-04-18 20:51:36 +00:00
|
|
|
writeCCode(getLongPathname(argv[argc]), options[2].value);
|
1999-12-12 02:33:22 +00:00
|
|
|
}
|
1999-11-20 01:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-04-18 20:51:36 +00:00
|
|
|
writeCCode(const char *filename, const char *destdir) {
|
1999-11-20 01:12:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-04-18 20:51:36 +00:00
|
|
|
getOutFilename(filename, destdir, buffer, entry);
|
1999-11-20 01:12:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
T_FileStream_writeLine(out, "#include \"unicode/utypes.h\"\nU_CAPI const struct U_EXPORT2 {\n double bogus;\n uint8_t bytes ");
|
1999-11-30 23:58:15 +00:00
|
|
|
|
1999-12-15 05:14:03 +00:00
|
|
|
T_FileStream_writeLine(out, "[");
|
|
|
|
sprintf(buffer, "%d", T_FileStream_size(in) );
|
|
|
|
T_FileStream_writeLine(out, buffer);
|
|
|
|
T_FileStream_writeLine(out, "]; \n} ");
|
1999-11-30 23:58:15 +00:00
|
|
|
for(i=0;i<strlen(entry);i++)
|
|
|
|
{
|
|
|
|
if(entry[i]=='-')
|
|
|
|
{
|
|
|
|
entry[i]='_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-15 05:14:03 +00:00
|
|
|
T_FileStream_writeLine(out, entry);
|
|
|
|
T_FileStream_writeLine(out,"={ 0, {\n");
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
length=T_FileStream_read(in, buffer, sizeof(buffer));
|
|
|
|
if(length==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(i=0; i<length; ++i) {
|
|
|
|
write8(out, (uint8_t)buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-15 05:14:03 +00:00
|
|
|
T_FileStream_writeLine(out, "\n}\n};\n");
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
if(T_FileStream_error(in)) {
|
|
|
|
fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
|
|
|
|
exit(U_FILE_ACCESS_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(T_FileStream_error(out)) {
|
|
|
|
fprintf(stderr, "genccode: file write error while generating from file %s\n", filename);
|
|
|
|
exit(U_FILE_ACCESS_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
T_FileStream_close(out);
|
|
|
|
T_FileStream_close(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-04-18 20:51:36 +00:00
|
|
|
getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName) {
|
1999-12-28 23:57:50 +00:00
|
|
|
const char *basename=findBasename(inFilename), *suffix=uprv_strrchr(basename, '.');
|
1999-11-20 01:12:28 +00:00
|
|
|
|
|
|
|
/* copy path */
|
2000-04-18 20:51:36 +00:00
|
|
|
if(destdir!=NULL && *destdir!=0) {
|
|
|
|
do {
|
|
|
|
*outFilename++=*destdir++;
|
|
|
|
} while(*destdir!=0);
|
|
|
|
if(*(outFilename-1)!=U_FILE_SEP_CHAR) {
|
|
|
|
*outFilename++=U_FILE_SEP_CHAR;
|
|
|
|
}
|
|
|
|
inFilename=basename;
|
|
|
|
} else {
|
|
|
|
while(inFilename<basename) {
|
|
|
|
*outFilename++=*inFilename++;
|
|
|
|
}
|
1999-11-20 01:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(suffix==NULL) {
|
|
|
|
/* the filename does not have a suffix */
|
1999-12-28 23:57:50 +00:00
|
|
|
uprv_strcpy(entryName, inFilename);
|
|
|
|
uprv_strcpy(outFilename, inFilename);
|
|
|
|
uprv_strcat(outFilename, ".c");
|
1999-11-20 01:12:28 +00:00
|
|
|
} else {
|
|
|
|
/* copy basename */
|
|
|
|
while(inFilename<suffix) {
|
|
|
|
*outFilename++=*entryName++=*inFilename++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* replace '.' by '_' */
|
|
|
|
*outFilename++=*entryName++='_';
|
|
|
|
++inFilename;
|
|
|
|
|
|
|
|
/* copy suffix */
|
|
|
|
while(*inFilename!=0) {
|
|
|
|
*outFilename++=*entryName++=*inFilename++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*entryName=0;
|
|
|
|
|
|
|
|
/* add ".c" */
|
|
|
|
*outFilename++='.';
|
|
|
|
*outFilename++='c';
|
|
|
|
*outFilename=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
write8(FileStream *out, uint8_t byte) {
|
|
|
|
char s[4];
|
|
|
|
int i=0;
|
|
|
|
|
|
|
|
/* convert the byte value to a string */
|
|
|
|
if(byte>=100) {
|
|
|
|
s[i++]='0'+byte/100;
|
|
|
|
byte%=100;
|
|
|
|
}
|
1999-11-22 19:35:39 +00:00
|
|
|
if(i>0 || byte>=10) {
|
1999-11-20 01:12:28 +00:00
|
|
|
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);
|
|
|
|
}
|