Converted file2str to python (nw)

This commit is contained in:
Miodrag Milanovic 2014-12-15 15:19:55 +01:00
parent 00eeff4ade
commit 09a636c2ff
5 changed files with 62 additions and 127 deletions

View File

@ -959,14 +959,14 @@ ifdef CPPCHECK
@$(CPPCHECK) $(CPPCHECKFLAGS) $<
endif
$(OBJ)/%.lh: $(SRC)/%.lay $(FILE2STR_TARGET)
$(OBJ)/%.lh: $(SRC)/%.lay $(SRC)/build/file2str.py
@echo Converting $<...
@$(FILE2STR) $< $@ layout_$(basename $(notdir $<))
@$(PYTHON) $(SRC)/build/file2str.py $< $@ layout_$(basename $(notdir $<))
$(OBJ)/%.fh: $(SRC)/%.png $(PNG2BDC_TARGET) $(FILE2STR_TARGET)
$(OBJ)/%.fh: $(SRC)/%.png $(PNG2BDC_TARGET) $(SRC)/build/file2str.py
@echo Converting $<...
@$(PNG2BDC) $< $(OBJ)/temp.bdc
@$(FILE2STR) $(OBJ)/temp.bdc $@ font_$(basename $(notdir $<)) UINT8
@$(PYTHON) $(SRC)/build/file2str.py $(OBJ)/temp.bdc $@ font_$(basename $(notdir $<)) UINT8
$(DRIVLISTOBJ): $(DRIVLISTSRC)
@echo Compiling $<...

View File

@ -18,14 +18,12 @@ OBJDIRS += \
# set of build targets
#-------------------------------------------------
FILE2STR_TARGET = $(BUILDOUT)/file2str$(BUILD_EXE)
MAKEDEP_TARGET = $(BUILDOUT)/makedep$(BUILD_EXE)
MAKEMAK_TARGET = $(BUILDOUT)/makemak$(BUILD_EXE)
MAKELIST_TARGET = $(BUILDOUT)/makelist$(BUILD_EXE)
PNG2BDC_TARGET = $(BUILDOUT)/png2bdc$(BUILD_EXE)
VERINFO_TARGET = $(BUILDOUT)/verinfo$(BUILD_EXE)
FILE2STR = $(FILE2STR_TARGET)
MAKEDEP = $(MAKEDEP_TARGET)
MAKEMAK = $(MAKEMAK_TARGET)
MAKELIST = $(MAKELIST_TARGET)
@ -34,7 +32,6 @@ VERINFO = $(VERINFO_TARGET)
ifneq ($(TERM),cygwin)
ifeq ($(TARGETOS),win32)
FILE2STR = $(subst /,\,$(FILE2STR_TARGET))
MAKEDEP = $(subst /,\,$(MAKEDEP_TARGET))
MAKEMAK = $(subst /,\,$(MAKEMAK_TARGET))
MAKELIST = $(subst /,\,$(MAKELIST_TARGET))
@ -45,7 +42,6 @@ endif
ifneq ($(CROSS_BUILD),1)
BUILD += \
$(FILE2STR_TARGET) \
$(MAKEDEP_TARGET) \
$(MAKEMAK_TARGET) \
$(MAKELIST_TARGET) \
@ -54,19 +50,6 @@ BUILD += \
#-------------------------------------------------
# file2str
#-------------------------------------------------
FILE2STROBJS = \
$(BUILDOBJ)/file2str.o \
$(FILE2STR_TARGET): $(FILE2STROBJS) $(LIBOCORE)
@echo Linking $@...
$(LD) $(LDFLAGS) $^ $(LIBS) -o $@
#-------------------------------------------------
# makedep
#-------------------------------------------------
@ -155,9 +138,6 @@ else
#-------------------------------------------------
# It's a CROSS_BUILD. Ensure the targets exist.
#-------------------------------------------------
$(FILE2STR_TARGET):
@echo $@ should be built natively. Nothing to do.
$(MAKEDEP_TARGET):
@echo $@ should be built natively. Nothing to do.

View File

@ -1,101 +0,0 @@
/***************************************************************************
file2str.c
Simple file to string converter.
Copyright Nicola Salmoria and the MAME Team.
Visit http://mamedev.org for licensing and usage restrictions.
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/*-------------------------------------------------
main - primary entry point
-------------------------------------------------*/
int main(int argc, char *argv[])
{
const char *srcfile, *dstfile, *varname, *type;
FILE *src, *dst;
unsigned char *buffer;
int bytes, offs;
int terminate = 1;
/* needs at least three arguments */
if (argc < 4)
{
fprintf(stderr,
"Usage:\n"
" laytostr <source.lay> <output.h> <varname> [<type>]\n"
"\n"
"The default <type> is char, with an assumed NULL terminator\n"
);
return 0;
}
/* extract arguments */
srcfile = argv[1];
dstfile = argv[2];
varname = argv[3];
type = (argc >= 5) ? argv[4] : "char";
if (argc >= 5)
terminate = 0;
/* open source file */
src = fopen(srcfile, "rb");
if (src == NULL)
{
fprintf(stderr, "Unable to open source file '%s'\n", srcfile);
return 1;
}
/* determine file size */
fseek(src, 0, SEEK_END);
bytes = ftell(src);
fseek(src, 0, SEEK_SET);
/* allocate memory */
buffer = (unsigned char *)malloc(bytes + 1);
if (buffer == NULL)
{
fclose(src);
fprintf(stderr, "Out of memory allocating %d byte buffer\n", bytes);
return 1;
}
/* read the source file */
fread(buffer, 1, bytes, src);
buffer[bytes] = 0;
fclose(src);
/* open dest file */
dst = fopen(dstfile, "w");
if (dst == NULL)
{
free(buffer);
fprintf(stderr, "Unable to open output file '%s'\n", dstfile);
return 1;
}
/* write the initial header */
fprintf(dst, "extern const %s %s[];\n", type, varname);
fprintf(dst, "const %s %s[] =\n{\n\t", type, varname);
/* write out the data */
for (offs = 0; offs < bytes + terminate; offs++)
{
fprintf(dst, "0x%02x%s", buffer[offs], (offs != bytes + terminate - 1) ? "," : "");
if (offs % 16 == 15)
fprintf(dst, "\n\t");
}
fprintf(dst, "\n};\n");
/* close the files */
free(buffer);
fclose(dst);
return 0;
}

56
src/build/file2str.py Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/python
import string
import sys
import os
if (len(sys.argv) < 4) :
print('Usage:')
print(' file2str <source.lay> <output.h> <varname> [<type>]')
print('')
print('The default <type> is char, with an assumed NULL terminator')
sys.exit(0)
terminate = 1
srcfile = sys.argv[1]
dstfile = sys.argv[2]
varname = sys.argv[3]
if (len(sys.argv) >= 5) :
type = sys.argv[4]
terminate = 0
else:
type = 'char'
try:
myfile = open(srcfile, 'rb')
except IOError:
print("Unable to open source file '%s'" % srcfile)
sys.exit(-1)
bytes = os.path.getsize(srcfile)
try:
dst = open(dstfile,'w')
dst.write('extern const %s %s[];\n' % ( type, varname ));
dst.write('const %s %s[] =\n{\n\t' % ( type, varname));
offs = 0
with open(srcfile, "rb") as src:
while True:
chunk = src.read(16)
if chunk:
for b in chunk:
dst.write('0x%02x' % ord(b))
offs = offs + 1
if offs != bytes:
dst.write(',')
else:
break
if offs != bytes:
dst.write('\n\t')
if terminate == 1:
dst.write(',0x00')
dst.write('\n};\n')
dst.close()
except IOError:
print("Unable to open output file '%s'" % dstfile)
sys.exit(-1)

View File

@ -1868,9 +1868,9 @@ $(MESSOBJ)/yamaha.a: \
$(MESS_DRIVERS)/fb01.o \
$(MESS_DRIVERS)/ymmu100.o: $(MESS_DRIVERS)/ymmu100.inc
$(MESS_DRIVERS)/ymmu100.inc: $(MESSSRC)/drivers/ymmu100.ppm $(FILE2STR_TARGET)
$(MESS_DRIVERS)/ymmu100.inc: $(MESSSRC)/drivers/ymmu100.ppm $(SRC)/build/file2str.py
@echo Converting $<...
@$(FILE2STR) $(MESSSRC)/drivers/ymmu100.ppm $@ ymmu100_bkg UINT8
@$(PYTHON) $(SRC)/build/file2str.py $(MESSSRC)/drivers/ymmu100.ppm $@ ymmu100_bkg UINT8
$(MESSOBJ)/zenith.a: \
$(MESS_DRIVERS)/z100.o \