Subversion Repositories SvarDOS

Rev

Rev 477 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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