Subversion Repositories SvarDOS

Rev

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

Rev 432 Rev 477
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("/lxxx - enforces the output array to be xxx bytes big");
16
  puts("/lxxx - enforces the output array to be xxx bytes big");
16
}
17
}
17
 
18
 
18
 
19
 
19
int main(int argc, char **argv) {
20
int main(int argc, char **argv) {
20
  char *fnamein = NULL, *fnameout = NULL, *varname = NULL;
21
  char *fnamein = NULL, *fnameout = NULL, *varname = NULL;
21
  char flag_c = 0;
22
  char stortype = 0; /* 'c' = const ; 's' = static */
22
  char *flag_l = "";
23
  char *flag_l = "";
23
  FILE *fdin, *fdout;
24
  FILE *fdin, *fdout;
24
  unsigned long len;
25
  unsigned long len;
25
  int c;
26
  int c;
26
 
27
 
27
  for (c = 1; c < argc; c++) {
28
  for (c = 1; c < argc; c++) {
28
    if ((argv[c][0] == '/') && (argv[c][1] == 'l')) {
29
    if ((argv[c][0] == '/') && (argv[c][1] == 'l')) {
29
      flag_l = argv[c] + 2;
30
      flag_l = argv[c] + 2;
30
      continue;
31
      continue;
31
    }
32
    }
32
    if ((argv[c][0] == '/') && (argv[c][1] == 'c')) {
33
    if ((argv[c][0] == '/') && (argv[c][1] == 'c')) {
33
      flag_c = 1;
34
      stortype = 'c';
-
 
35
      continue;
-
 
36
    }
-
 
37
    if ((argv[c][0] == '/') && (argv[c][1] == 's')) {
-
 
38
      stortype = 's';
34
      continue;
39
      continue;
35
    }
40
    }
36
    if (argv[c][0] == '/') {
41
    if (argv[c][0] == '/') {
37
      help();
42
      help();
38
      return(1);
43
      return(1);
39
    }
44
    }
40
    /* not a switch - so it's either infile, outfile or varname */
45
    /* not a switch - so it's either infile, outfile or varname */
41
    if (fnamein == NULL) {
46
    if (fnamein == NULL) {
42
      fnamein = argv[c];
47
      fnamein = argv[c];
43
    } else if (fnameout == NULL) {
48
    } else if (fnameout == NULL) {
44
      fnameout = argv[c];
49
      fnameout = argv[c];
45
    } else if (varname == NULL) {
50
    } else if (varname == NULL) {
46
      varname = argv[c];
51
      varname = argv[c];
47
    } else {
52
    } else {
48
      help();
53
      help();
49
      return(1);
54
      return(1);
50
    }
55
    }
51
  }
56
  }
52
 
57
 
53
  if (varname == NULL) {
58
  if (varname == NULL) {
54
    help();
59
    help();
55
    return(1);
60
    return(1);
56
  }
61
  }
57
 
62
 
58
  fdin = fopen(fnamein, "rb");
63
  fdin = fopen(fnamein, "rb");
59
  if (fdin == NULL) {
64
  if (fdin == NULL) {
60
    puts("ERROR: failed to open input file");
65
    puts("ERROR: failed to open input file");
61
    return(1);
66
    return(1);
62
  }
67
  }
63
 
68
 
64
  fdout = fopen(fnameout, "wb");
69
  fdout = fopen(fnameout, "wb");
65
  if (fdout == NULL) {
70
  if (fdout == NULL) {
66
    fclose(fdin);
71
    fclose(fdin);
67
    puts("ERROR: failed to open output file");
72
    puts("ERROR: failed to open output file");
68
    return(1);
73
    return(1);
69
  }
74
  }
70
 
75
 
71
  if (flag_c) fprintf(fdout, "const ");
76
  if (stortype == 'c') fprintf(fdout, "const ");
-
 
77
  if (stortype == 's') fprintf(fdout, "static ");
72
  fprintf(fdout, "char %s[%s] = {", varname, flag_l);
78
  fprintf(fdout, "char %s[%s] = {", varname, flag_l);
73
 
79
 
74
  for (len = 0;; len++) {
80
  for (len = 0;; len++) {
75
    c = getc(fdin);
81
    c = getc(fdin);
76
    if (c == EOF) break;
82
    if (c == EOF) break;
77
    if (len > 0) fprintf(fdout, ",");
83
    if (len > 0) fprintf(fdout, ",");
78
    if ((len & 15) == 0) fprintf(fdout, "\r\n");
84
    if ((len & 15) == 0) fprintf(fdout, "\r\n");
79
    fprintf(fdout, "%3u", c);
85
    fprintf(fdout, "%3u", c);
80
  }
86
  }
81
  fprintf(fdout, "};\r\n");
87
  fprintf(fdout, "};\r\n");
82
  fprintf(fdout, "#define %s_len %lu\r\n", varname, len);
88
  fprintf(fdout, "#define %s_len %lu\r\n", varname, len);
83
 
89
 
84
  fclose(fdin);
90
  fclose(fdin);
85
  fclose(fdout);
91
  fclose(fdout);
86
  return(0);
92
  return(0);
87
}
93
}
88
 
94