aboutsummaryrefslogtreecommitdiff
path: root/src/util/makeblob.pl
blob: 55badcefe0c00ca13b6848451805b3cdbbcbd20c (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/perl -w

use diagnostics;
use warnings;
use strict;

my $items_per_line = 16;
my $items_per_file = 128 * 1024;

$#ARGV == 2 or die "ARGV=@ARGV";

my $basename = $ARGV[0];
my $arrangement = $ARGV[1];
my $thorn = $ARGV[2];
$thorn ne '' or die;



# Write several files, each with a maximum length

my $done = 0;
for (my $count = 0; ! $done; ++ $count) {
    my $fcount      = sprintf "%04d", $count;
    my $fcount_next = sprintf "%04d", $count + 1;
    
    open FILE, "> $basename-$fcount.c" or die;
    print FILE <<EOF;
/* This is an auto-generated file -- do not edit */

\#include <stddef.h>
\#include <stdlib.h>

struct datainfo
{
  unsigned char const * data;
  size_t length;
  struct datainfo const * next;
};

EOF
    
    print FILE "static unsigned char const data_${fcount} [] = {";
    my $bytes;
    for ($bytes = 0; $bytes < $items_per_file; ++ $bytes) {
        my $ch = getc;
        if (! defined $ch) {
            $done = 1;
            last;
        }
        if ($bytes != 0) {
            printf FILE ",";
        }
        if ($bytes % $items_per_line == 0) {
            printf FILE "\n";
            printf FILE "  ";
        }
        printf FILE "%3d", ord $ch;
    }
    printf FILE "\n";
    printf FILE "};\n";
    
    print FILE "\n";
    if (! $done) {
        print FILE "struct datainfo const cactus_data_${fcount_next}_${thorn};\n";
    }
    print FILE "struct datainfo const cactus_data_${fcount}_${thorn} =\n";
    print FILE "{\n";
    print FILE "  data_${fcount},\n";
    print FILE "  ${bytes}UL,\n";
    if (! $done) {
        print FILE "  & cactus_data_${fcount_next}_${thorn}\n";
    }
    else {
        print FILE "  NULL\n";
    }
    print FILE "};\n";
    
    close FILE;
}



# Write meta-file

{
    open FILE, "> $basename.c" or die;
    printf FILE <<EOF;
/* This is an auto-generated file -- do not edit */

\#include <stddef.h>
\#include <stdlib.h>

struct datainfo
{
  unsigned char const * data;
  size_t length;
  struct datainfo const * next;
};

struct sourceinfo
{
  struct datainfo const * first;
  char const * arrangement;
  char const * thorn;
};

struct datainfo const cactus_data_0000_$thorn;
struct sourceinfo const cactus_source_$thorn =
{
  & cactus_data_0000_$thorn,
  \"$arrangement\",
  \"$thorn\"
};
EOF
    close FILE;
}