#!/usr/bin/env perl
##  asc2c -- convert an ASCII file into a statically initialised
##           C array of characters
##
##  Copyright (c) Ralf S. Engelschall, All Rights Reserved.

use strict;
use warnings;
use autodie;

my ( $in_fn, $out_fn, $name ) = @ARGV;

if ( @ARGV != 3 )
{
    die "Usage: $0 ascii-file c-file buffer-name\n";
}

open( my $in_fh, '<', $in_fn );

open( my $out_c_fh, '>', "$out_fn.c" );

$out_c_fh->print(
    "/* $in_fn.c -- automatically generated by asc2c */\n",
    "\n", "char *$name = \\\n",
);

while ( my $l = <$in_fh> )
{
    chomp $l;
    $l =~ s|\\|\\\\|g;
    $l =~ s|\"|\\\"|g;
    printf {$out_c_fh} "\"%s\\n\"\\\n", $l;
}

print {$out_c_fh} ";\n\n/*EOF*/\n";

close($in_fh);
close($out_c_fh);
