66 lines
1016 B
C
66 lines
1016 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
FILE *in;
|
|
int c;
|
|
long count = 0;
|
|
long col = 0;
|
|
char *var_name;
|
|
char *p;
|
|
|
|
if(argc != 2) {
|
|
fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
in = fopen(argv[1], "rb");
|
|
if(!in) {
|
|
perror("Error opening input file");
|
|
return 1;
|
|
}
|
|
|
|
var_name = (char *)malloc(strlen(argv[1]) + 1);
|
|
if(!var_name) {
|
|
perror("Memory allocation failed");
|
|
fclose(in);
|
|
return 1;
|
|
}
|
|
|
|
strcpy(var_name, argv[1]);
|
|
|
|
for(p = var_name; *p; ++p)
|
|
if(!isalnum((unsigned char)*p)) *p = '_';
|
|
|
|
printf("unsigned char %s[] = {\n", var_name);
|
|
|
|
c = fgetc(in);
|
|
while(c != EOF) {
|
|
printf(" 0x%02x", c);
|
|
count++;
|
|
|
|
int next = fgetc(in);
|
|
if(next != EOF) {
|
|
printf(",");
|
|
ungetc(next, in);
|
|
if(++col >= 12) {
|
|
printf("\n");
|
|
col = 0;
|
|
}
|
|
}
|
|
|
|
c = fgetc(in);
|
|
}
|
|
|
|
printf("\n};\n");
|
|
printf("unsigned int %s_len = %lu;\n", var_name, count);
|
|
free(var_name);
|
|
fclose(in);
|
|
|
|
return 0;
|
|
}
|