File: bin2c.c

package info (click to toggle)
thrust 0.89-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,756 kB
  • ctags: 986
  • sloc: ansic: 13,516; sh: 1,514; makefile: 308
file content (155 lines) | stat: -rw-r--r-- 2,436 bytes parent folder | download | duplicates (6)
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

/* Written by Peter Ekberg, peda@lysator.liu.se */

#ifdef HAVE_CONFIG_H
# include "../src/config.h"
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include <sys/types.h>
#include <stdio.h>

#include "../src/thrust_t.h"

#define LL (8)

void quit(char *str);
int writebuf(int count);

byte buf[LL+1];

void
quit(char *str)
{
  perror(str);
  exit(1);
}

int
writebuf(int count)
{
  int i;
  
#if PRINTF_RETURN >= 0
  if(
#endif
     printf("\t")
#if PRINTF_RETURN == 1
                  != 1
#endif
#if PRINTF_RETURN >= 0
                      )
    return(1)
#endif
      ;

  for(i=0; i<count-1; i++)
#if PRINTF_RETURN >= 0
    if(
#endif
       printf("0x%02x, ", buf[i])
#if PRINTF_RETURN == 1
                                  != 6
#endif
#if PRINTF_RETURN >= 0
                                      )
      return(1)
#endif
      ;

  if(i<count)
#if PRINTF_RETURN >= 0
    if(
#endif
       printf("0x%02x", buf[i])
#if PRINTF_RETURN == 1
                                != 4
#endif
#if PRINTF_RETURN >= 0
                                    )
      return(1)
#endif
      ;

  return(0);
}

int
main(int argc, char *argv[])
{
  int stat;
  int end=0;
  FILE *si;

  if(argc!=2) {
    fprintf(stderr, "%s: Usage '%s variable_name'\n",
	    argv[0],
	    argv[0]);
    exit(1);
  }

  si = fdopen(fileno(stdin), "rb");

#if PRINTF_RETURN >= 0
  if(
#endif
     printf("\nunsigned char %s[] = {\n", argv[1])
#if PRINTF_RETURN == 1
                                                   != (int)(22 + strlen(argv[1]))
#endif
#if PRINTF_RETURN >= 0
                                                                            )
    quit(argv[0])
#endif
      ;
  stat=fread(buf+LL, 1, 1, si);
  if(stat!=1) {
    if(ferror(si))
      quit(argv[0]);
    end=1;
  }
  while(!end) {
    *buf=*(buf+LL);
    stat=fread(buf+1, 1, LL, si);
    if(stat!=LL) {
      if(ferror(si))
	quit(argv[0]);
      end=1;
      if(writebuf(stat+1))
	quit(argv[0]);
    }
    else {
      if(writebuf(LL))
	quit(argv[0]);
#if PRINTF_RETURN >= 0
      if(
#endif
         printf(",\n")
#if PRINTF_RETURN == 1
                       != 2
#endif
#if PRINTF_RETURN >= 0
                           )
	quit(argv[0])
#endif
	  ;
    }
  }  
#if PRINTF_RETURN >= 0
  if(
#endif
     printf(" };\n")
#if PRINTF_RETURN == 1
                     != 4
#endif
#if PRINTF_RETURN >= 0
                         )
    quit(argv[0])
#endif
      ;

  return(0);
}