File: data2c.c

package info (click to toggle)
tuxpuck 0.8.2-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 880 kB
  • sloc: ansic: 3,317; makefile: 108
file content (49 lines) | stat: -rw-r--r-- 1,167 bytes parent folder | download | duplicates (2)
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
/* data2c - Copyright (C) 2001-2002 Jacob Kroon, see COPYING for details */

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>

void errorc(char *msg)
{
  fprintf(stderr, "%s\n", msg);
  exit(1);
}

void errorcc(char *msg1, char *msg2)
{
  fprintf(stderr, "%s%s\n", msg1, msg2);
  exit(1);
}

int main(int argc, char **argv)
{
  FILE *in = NULL, *out = NULL;
  char buffer[100];
  unsigned char ch;
  int i = 0;
  uint32_t size;
  struct stat theStat;

  if (argc != 3)
    errorc("Usage : data2c <file> <array>");
  if ((in = fopen(argv[1], "rb")) == NULL)
    errorcc("Couldn't open file for reading : ", argv[1]);
  stat(argv[1], &theStat);
  size = theStat.st_size;
  sprintf(buffer, "%s.c", argv[2]);
  if ((out = fopen(buffer, "wb")) == NULL)
    errorcc("Couldn't open file for writing : ", buffer);
  fprintf(out, "/* %s */\n", buffer);
  fprintf(out, "unsigned char %s[] = {\n", argv[2]);
  for (i = 0; i < 4; i++)
    fprintf(out, "%d,", ((uint8_t *) & size)[i]);
  while (fread(&ch, 1, 1, in) != 0)
    fprintf(out, "%d,", ch);
  fseek(out, -1, SEEK_CUR);
  fprintf(out, "};\n");
  fclose(in);
  fclose(out);
  return 0;
}