]> git.sur5r.net Git - cc65/blob - src/ld65/cfg/cvt-cfg.pl
Restructuring the object file format
[cc65] / src / ld65 / cfg / cvt-cfg.pl
1 #!/usr/bin/perl
2
3 # Check number of params
4 die "Usage: cvt-cfg.pl input output varname\n" unless ($#ARGV == 2);
5
6 # Get the parameters
7 $InputName  = shift (@ARGV);
8 $OutputName = shift (@ARGV);
9 $VarName    = shift (@ARGV);
10
11 # Open both files
12 open (IN, "<$InputName") or die "Cannot open $InputName\n";
13 open (OUT, ">$OutputName") or die "Cannot open $OutputName\n";
14
15 # Print the header to the output file
16 print OUT "static const char $VarName [] = \n";
17
18 # Read from input, print to output
19 while ($Line = <IN>) {
20
21     # Remove the newline
22     chomp $Line;
23
24     # Separate an existing comment. No need to be overly clever, just ignore
25     # hash marks in strings.
26     if ($Line =~ /(.*?)(\s*)(\#\s*)(.*?)\s*$/) {
27         $Line         = $1;
28         $CommentSpace = $2;
29         $Comment      = $4;
30     } else {
31         $CommentSpace = "";
32         $Comment      = "";
33     }
34
35     # Remove leading and trailing spaces
36     $Line =~ s/^\s*|\s*$//g;
37
38     # Replace control chars
39     $Line =~ s/\\/\\\\/g;
40     $Line =~ s/\"/\\\"/g;
41     $Line =~ s/\'/\\\'/g;
42
43     # Print to output
44     print OUT "\"$Line\\n\"";
45
46     # Add a comment if we have one
47     if ($Comment ne "") {
48         print OUT "$CommentSpace/* $Comment */";
49     }
50
51     # Terminate the line
52     print OUT "\n";
53 }
54
55 # Terminate the variable declaration
56 print OUT ";\n";
57
58 # Close the files
59 close IN;
60 close OUT;
61
62 # Done
63 exit 0;
64
65
66
67