summaryrefslogtreecommitdiff
path: root/package/etrax-tools/src/e100boot/sbl/cconv
blob: ce5ab3d705689aaa284538a621f623666b651c04 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl
#!
#! FILE NAME  : cconv
#!
#! PARAMETERS : Name of C program array variable.
#!
#! DESCRIPTION: Converts bytes of a binary file to C source code containing
#!              char array initialized with the binary file data.
#!
#! SUBROUTINES:
#!
#!---------------------------------------------------------------------------
#! HISTORY
#!
#! DATE         NAME            CHANGES
#! ----         ----            -------
#! Dec 15 1997  Sven Ekstrom    Initial version. Rewritten to Perl from C.
#! Dec 16 1997  Sven Ekstrom    Fixed bug that generated truncated result.
#!
#!---------------------------------------------------------------------------
#!
#! (C) Copyright 1997, Axis Communications AB, LUND, SWEDEN
#!
#!***************************************************************************
# @(#) cconv 1.2 12/16/97

#********************** CONSTANT SECTION ************************************

$MyName = 'cconv';

# 
# Number of bytes per line in the result.
#
$LineLength = 8;

#********************** MAIN PROGRAM SECTION ********************************

#
# Make sure the command line contains the name of a C array.
#
if (scalar @ARGV != 1 || $ARGV[0] =~ /^-/)
{
  die "$MyName: Usage:\n",
      "\n",
      "  Syntax\n",
      "  $MyName <name of C char array>\n",
      "\n",
      "  <name of C char array> : This is the name of the char array where\n",
      "                           the result is placed.\n",
      "\n",
      "  Description\n",
      "\n",
      "  Reads input from STDIN as binary data. Each byte of input data is\n",
      "  converted to C char data in hexadecimal form. The whole file read\n",
      "  from STDIN is converted and the result, C source code definition of\n",
      "  a char array, is printed on STDOUT.\n",
      "\n";
}

#
# Start with the name and version of this program and the name of the array.
#
print "\n",
      "/* $MyName 1.2 12/16/97, Copyright (C) 1997, Axis Communications AB */\n",
      "\n",
      "const char $ARGV[0]\[\] =\n",
      "{";

#
# Read all bytes from STDIN, convert them to char data and print them on
# STDOUT.
#
$CurrentOffset = 0;
while (!eof(STDIN))
{
  $Byte = ord(getc);

  if ($CurrentOffset % $LineLength == 0)
  {
    #
    # Start of a new line.
    #
    if ($CurrentOffset != 0)
    {
      #
      # This is not the first byte.
      #
      print ",";
    }
    #
    # The new line is indented by 2 spaces.
    #
    print "\n",
          "  ";
  }
  else
  {
    #
    # Continuing an old line.
    #
    print ", ";
  }

  #
  # Print the value of the byte as hex char data.
  #
  printf("'\\x%02x'", $Byte);

  $CurrentOffset++;
}

if ($CurrentOffset == 0)
{
  #
  # Initialize the array with a single byte of zero.
  #
  print "\n  '\\x00'";
}

#
# End with the closing bracket and semicolon.
#
print "\n",
      "};\n";

exit 0;


#********************** SUBROUTINE DEFINITION SECTION ***********************

#****************************************************************************
#*
#* SUBROUTINE  :
#*
#* PARAMETERS  :
#*
#* RETURNS     :
#*
#* SIDE EFFECTS:
#*
#* DESCRIPTION :
#*
#*---------------------------------------------------------------------------
#* HISTORY
#*
#* DATE          NAME            CHANGES
#* ----          ----            -------
#* May 05, 1995  Sven Ekstrom    Initial version
#*
#****************************************************************************

#sub NN
#{
#  local() = @_;
#
#}

#************************ END OF FILE cconv *********************************