Files
h2inc-old/h2inc.py
Lerking 993195fe22 Add files via upload
Updated process_files
2018-02-12 07:27:36 +01:00

79 lines
2.5 KiB
Python

# You are free to use and/or change this code for
# your own needs.
# Original code (c)2018 Jan Lerking
# Program to convert C-header (*.h) files to nasm include files (*.inc),
# for direct usage in assembly programming using nasm/yasm.
import os
import sys
from h2inc_parser import parseline, parseparsed
import h2inc_gui
tupline = []
preproc = ()
filelist = []
folderlist = []
cnt = 0
def sourcedir_filecnt(sourcedir):
### Return the number of files, ending with '.h', in sourcedir - including subdirectories ###
cnt = 0
global filelist
for folderName, subfolders, files in os.walk(sourcedir):
for file in files:
if file.lower().endswith('.h'):
cnt += 1
filelist += [folderName+'/'+file]
print(folderName+'/'+file)
#print(filelist)
return cnt
def sourcedir_foldercnt(sourcedir):
### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ###
global cnt
global folderlist
for folderName, subfolders, files in os.walk(sourcedir):
if subfolders:
for subfolder in subfolders:
sourcedir_foldercnt(subfolder)
tempf = [file for file in files if file.lower().endswith('.h')]
if tempf:
cnt = cnt+1
#print(folderName)
folderlist += [folderName]
#print(folderlist)
#print(len(folderlist))
return cnt
def process_files(sourcedir, destdir):
#global folderlist
global filelist
global preproc
outfile = ''
cnt = 0
for f in filelist:
inputfile = f
filehandle = open(f, 'r')
#cnt += 1
print(os.path.basename(f))
for lines in filehandle:
print(lines)
preproc = preproc+tuple([parseline(lines)])
filehandle.close()
for elements in preproc:
outfile = outfile+parseparsed(elements)
outputfile = os.path.splitext(inputfile)[0]+'.inc'
outputfile = str(outputfile).replace(str(sourcedir.get()), str(destdir.get()))
print(outputfile)
if not os.path.exists(os.path.dirname(outputfile)):
try:
os.makedirs(os.path.dirname(outputfile))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
newfile = open(outputfile, "w")
newfile.write(outfile)
newfile.close()
cnt += 1