ICU-157 generate a .c file from a binary data file for inclusion in a data DLL

X-SVN-Rev: 199
This commit is contained in:
Markus Scherer 1999-11-20 01:12:28 +00:00
parent bc7e7a3c96
commit c4630dc3e2
2 changed files with 277 additions and 0 deletions

View file

@ -0,0 +1,177 @@
/*
*******************************************************************************
* *
* COPYRIGHT: *
* (C) Copyright International Business Machines Corporation, 1999 *
* Licensed Material - Program-Property of IBM - All Rights Reserved. *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include "utypes.h"
#include "cmemory.h"
#include "cstring.h"
#include "filestrm.h"
#include "../toolutil/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 filename\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 {
writeCCode(getLongPathname(argv[1]));
}
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 \"utypes.h\"\nU_CAPI const uint8_t U_EXPORT2 ");
T_FileStream_writeLine(out, entry);
T_FileStream_writeLine(out, "[]={\n");
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]);
}
}
T_FileStream_writeLine(out, "\n};\n");
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
getOutFilename(const char *inFilename, char *outFilename, char *entryName) {
const char *basename=findBasename(inFilename), *suffix=icu_strrchr(basename, '.');
/* copy path */
while(inFilename<basename) {
*outFilename++=*inFilename++;
}
if(suffix==NULL) {
/* the filename does not have a suffix */
icu_strcpy(entryName, inFilename);
icu_strcpy(outFilename, inFilename);
icu_strcat(outFilename, ".c");
} 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;
}
if(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);
}

View file

@ -0,0 +1,100 @@
# Microsoft Developer Studio Project File - Name="genccode" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=genccode - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "genccode.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "genccode.mak" CFG="genccode - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "genccode - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "genccode - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "genccode - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /Za /W3 /GX /O2 /I "..\..\common" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 toolutil.lib icuuc.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /libpath:"..\toolutil\Release" /libpath:"..\..\..\lib\Release"
!ELSEIF "$(CFG)" == "genccode - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /Za /W3 /Gm /GX /ZI /Od /I "..\..\common" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 toolutil.lib icuuc.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\toolutil\Debug" /libpath:"..\..\..\lib\Debug"
!ENDIF
# Begin Target
# Name "genccode - Win32 Release"
# Name "genccode - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\genccode.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project