Subversion Repositories SvarDOS

Rev

Rev 477 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
349 mateuszvis 1
/*
2
 * translates a binary file to a C include.
3
 * used by the SvarCOM build process to embedd rcom inside COMMAND.COM
4
 *
5
 * Copyright (C) 2021 Mateusz Viste
6
 */
7
 
8
#include <stdio.h>
9
 
432 mateuszvis 10
 
11
static void help(void) {
12
  puts("usage: file2c [/c] [/lxxx] infile.dat outfile.c varname");
13
  puts("");
14
  puts("/c    - define the output array as CONST");
477 mateuszvis 15
  puts("/s    - define the output array as STATIC");
432 mateuszvis 16
  puts("/lxxx - enforces the output array to be xxx bytes big");
17
}
18
 
19
 
349 mateuszvis 20
int main(int argc, char **argv) {
432 mateuszvis 21
  char *fnamein = NULL, *fnameout = NULL, *varname = NULL;
477 mateuszvis 22
  char stortype = 0; /* 'c' = const ; 's' = static */
432 mateuszvis 23
  char *flag_l = "";
349 mateuszvis 24
  FILE *fdin, *fdout;
25
  unsigned long len;
26
  int c;
27
 
432 mateuszvis 28
  for (c = 1; c < argc; c++) {
29
    if ((argv[c][0] == '/') && (argv[c][1] == 'l')) {
30
      flag_l = argv[c] + 2;
31
      continue;
32
    }
33
    if ((argv[c][0] == '/') && (argv[c][1] == 'c')) {
477 mateuszvis 34
      stortype = 'c';
432 mateuszvis 35
      continue;
36
    }
477 mateuszvis 37
    if ((argv[c][0] == '/') && (argv[c][1] == 's')) {
38
      stortype = 's';
39
      continue;
40
    }
432 mateuszvis 41
    if (argv[c][0] == '/') {
42
      help();
43
      return(1);
44
    }
45
    /* not a switch - so it's either infile, outfile or varname */
46
    if (fnamein == NULL) {
47
      fnamein = argv[c];
48
    } else if (fnameout == NULL) {
49
      fnameout = argv[c];
50
    } else if (varname == NULL) {
51
      varname = argv[c];
52
    } else {
53
      help();
54
      return(1);
55
    }
56
  }
57
 
58
  if (varname == NULL) {
59
    help();
349 mateuszvis 60
    return(1);
61
  }
62
 
432 mateuszvis 63
  fdin = fopen(fnamein, "rb");
349 mateuszvis 64
  if (fdin == NULL) {
65
    puts("ERROR: failed to open input file");
66
    return(1);
67
  }
68
 
432 mateuszvis 69
  fdout = fopen(fnameout, "wb");
349 mateuszvis 70
  if (fdout == NULL) {
71
    fclose(fdin);
72
    puts("ERROR: failed to open output file");
73
    return(1);
74
  }
75
 
477 mateuszvis 76
  if (stortype == 'c') fprintf(fdout, "const ");
77
  if (stortype == 's') fprintf(fdout, "static ");
432 mateuszvis 78
  fprintf(fdout, "char %s[%s] = {", varname, flag_l);
79
 
349 mateuszvis 80
  for (len = 0;; len++) {
81
    c = getc(fdin);
82
    if (c == EOF) break;
83
    if (len > 0) fprintf(fdout, ",");
84
    if ((len & 15) == 0) fprintf(fdout, "\r\n");
85
    fprintf(fdout, "%3u", c);
86
  }
87
  fprintf(fdout, "};\r\n");
432 mateuszvis 88
  fprintf(fdout, "#define %s_len %lu\r\n", varname, len);
349 mateuszvis 89
 
90
  fclose(fdin);
91
  fclose(fdout);
92
  return(0);
93
}