File: mkdata.cpp

package info (click to toggle)
nqc 2.3.r1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,452 kB
  • ctags: 2,325
  • sloc: cpp: 14,379; lex: 304; yacc: 259; makefile: 126; ansic: 25
file content (101 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <string.h>
#include "SRecord.h"

//static char *sTestArgs[]={"mkdata", "rcx.nqh", "rcx_data.h", "rcxData" };
static char *sTestArgs[]={"mkdata", "-s", "fastdl.srec", "RCX_nub.h", "rcxNub" };


int main(int argc, char **argv)
{
	FILE *src;
	FILE *dst;
	int n;
	bool srecMode = false;
	SRecord srec;
	
	if (argc == 0)
	{
		// special case for debugging under the Metrowerks console
		argv = sTestArgs;
		argc = sizeof(sTestArgs) / sizeof(char *);
	}

	if (argc == 5 && (strcmp(argv[1],"-s")==0))
	{
		for(int i=1; i<4; i++)
			argv[i]=argv[i+1];
		argc--;
		srecMode = true;
	}
	
	if (argc != 4)
	{
		fprintf(stderr, "Usage: mkdata [-s] sourceFile destFile arrayName\n");
		return -1;
	}
	
	src = fopen(argv[1], "r");
	if (!src)
	{
		fprintf(stderr, "Error: could not open %s\n", argv[1]);
		return -1;
	}
	if (srecMode && !srec.Read(src, 65536))
	{
		fprintf(stderr, "Error: %s is not a valid S-Record file\n", argv[1]);
		return -1;
	}
	
	dst = fopen(argv[2], "w");
	if (!dst)
	{
		fprintf(stderr, "Error: could not create %s\n", argv[2]);
		fclose(src);
		return -1;
	}
	
	fprintf(dst,"static const %s char %s[]={\n", srecMode ? "unsigned" : "", argv[3]);
	
	n = 0;
	
	if (srecMode)
	{
		const UByte *ptr = srec.GetData();
		int length = srec.GetLength();
		
		while(length--)
		{
			fprintf(dst,"0x%02x,", *ptr++);
			if (++n == 16)
			{
				n = 0;
				fputs("\n", dst);
			}
		}
	}
	else
	{
		int c;
		while((c=fgetc(src)) != EOF)
		{
			if (c=='\n')
				fputs("\'\\n\',", dst);
			else
				fprintf(dst,"%d,", c);

			if (++n == 16)
			{
				n = 0;
				fputs("\n", dst);
			}
		}
	}
		
	fprintf(dst,"};\n");

	fclose(src);
	fclose(dst);
	
	return 0;
}