Subversion Repositories SvarDOS

Rev

Rev 397 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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