FreeBASIC  0.91.0
makedriver.bas
Go to the documentation of this file.
1 '' Small program to embed the fbportio.sys binary into a C module, as an array
2 '' of bytes.
3 
4 #define NULL 0
5 
6 sub fatalCantAccessFile(byref filename as string)
7  print "Error: Could not access file: '" + filename + "'"
8  end 1
9 end sub
10 
11 function fileLoad(byref filename as string, byval psize as integer ptr) as ubyte ptr
12  dim as integer f = freefile()
13  if (open(filename, for binary, access read, as #f)) then
15  end if
16 
17  dim as ubyte ptr p = NULL
18  dim as integer size = lof(f)
19 
20  if (size > 0) then
21  p = allocate(size)
22 
23  dim as integer result = get(#f, , *p, size, size)
24  if (result or (size <= 0)) then
26  end if
27  end if
28 
29  close #f
30 
31  *psize = size
32  return p
33 end function
34 
35 sub writeOut(byref filename as string, byref text as string)
36  dim as integer f = freefile()
37  if (open(filename, for output, as #f)) then
39  end if
40  print #f, text;
41  close #f
42 end sub
43 
44  if (__FB_ARGC__ <> 3) then
45  print "Usage: makedriver fbportio.sys fbportio-driver.h"
46  end 1
47  end if
48 
49  dim as string inputfile = *__FB_ARGV__[1]
50  dim as string outputfile = *__FB_ARGV__[2]
51 
52  dim as integer size = 0
53  dim as ubyte ptr p = fileLoad(inputfile, @size)
54 
55  dim as string emit
56  emit += "#define FBPORTIO_DRIVER_SIZE " + str(size) + !"\n"
57  emit += !"const unsigned char fbportio_driver[FBPORTIO_DRIVER_SIZE] = {\n"
58 
59  for i as integer = 0 to (size - 1)
60  if ((i mod 16) = 0) then
61  '' Indent at line begin
62  emit += " "
63  end if
64 
65  emit += "0x" + hex(p[i], 2)
66 
67  if (i < (size - 1)) then
68  emit += ","
69  '' Emit a newline every 16 bytes
70  if (((i + 1) mod 16) = 0) then
71  emit += !"\n"
72  else
73  emit += " "
74  end if
75  else
76  emit += !"\n"
77  end if
78  next
79 
80  emit += !"};\n"
81 
83