fixed newslot in table.cpp, that would return false, even though the slot was correct...
[vox.git] / examples / bin2c.vx
blob0839951a7d4075a09d703786bda4dc77635f50fb
3 class BinaryToRepr
5     fieldname = ""
6     result = []
7     space = "    "
8     norawchars = false
10     function isPrintable(char)
11     {
12         return false
13     }
15     function mkPrintable(char)
16     {
17         local tmpresult
18         if(this.norawchars)
19         {
20             return "%04s".format("0x%04x".format(char.tointeger()))
21         }
22         else
23         {
24             tmpresult = char
25             if(this.isPrintable(char))
26             {
27                 return "'%s'".format(char)
28             }
29             else
30             {
31                 tmpresult = "0x%.2x".format(char.tointeger())
32             }
33             return "%04s".format(tmpresult)
34         }
35     }
37     function processFile(filename)
38     {
39         this.processHandle(io.open(filename, "r"), true)
40     }
42     function processHandle(handle, do_close=false)
43     {
44         local c
45         local rawdata = []
46         while((c = handle.readchar()) != null)
47         {
48             rawdata.push(c)
49         }
50         if(do_close) handle.close()
51         this.processData(rawdata)
52     }
54     function processData(rawdata)
55     {
56         local templine = []
57         local idx = 0
58         local strlen = 0
59         local strlen = rawdata.len()
60         this.result.push(
61             "const char %s[%d] = {\n%s".format(this.fieldname,
62                                                strlen+1,
63                                                this.space))
64         foreach(char in rawdata)
65         {
66             char = this.mkPrintable(char)
67             if(idx > 0)
68             {
69                 if ((idx % 10) == 0)
70                 {
71                     this.result.push("\n%s".format(this.space))
72                 }
73             }
74             this.result.push("%s".format(char))
75             if ((idx + 1) < strlen)
76             {
77                 this.result.push(", ")
78             }
79             idx += 1
80         }
81         this.result.push(", ");
82         this.result.push(this.mkPrintable('\0'));
83         this.result.push("\n};")
84     }
86     function rawData()
87     {
88         local data = ""
89         foreach(itm in this.result)
90         {
91             data += itm
92         }
93         return data
94     }
96     function setSpace(instr)
97     {
98         this.space = instr
99     }
101     function setFieldName(instr)
102     {
103         this.fieldname = instr
104     }
106     function constructor()
107     {
108         this.fieldname = "rawdata"
109     }
113 function main()
115     bin2c = BinaryToRepr()
116     if(system.argv.len() > 1)
117     {
118         fieldname = system.argv[1]
119         filename = system.argv[2]
120         bin2c.setFieldName(fieldname)
121         if(filename == "-" || filename == "/dev/stdin")
122         {
123             io.stderr.write("** reading from stdin **\n")
124             bin2c.processHandle(io.stdin)
125         }
126         else
127         {
128             bin2c.processFile(filename)
129         }
130         if(vargv.len() > 3)
131         {
132             outfile := ARGV[3]
133             handle := io.open(outfile, "w")
134             handle.write(bin2c.rawData())
135             handle.close()
136         }
137         else
138         {
139             print(bin2c.rawData())
140         }
141         return 0
142     }
143     else
144     {
145         println("Usage: bin2c.vx <fieldname> <filename> [<outfile>]")
146         return 1
147     }
150 main()