1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/*
* txt2c: Converts text files to C strings
*
* Compile with:
* gcc txt2cs.c -o txt2cs
*
* Public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
const char *prefix = "";
const char *suffix = "\n";
FILE *in = stdin;
FILE *out = stdout;
int c;
while ((c = getopt(argc, argv, "np:s:h")) != -1) {
switch (c) {
case 'p':
prefix = optarg;
break;
case 's':
suffix = optarg;
break;
case 'h':
printf("Usage: %s [-n] [-p prefix] [-s suffix] [infile] [outfile]\n", argv[0]);
exit(0);
break;
}
}
if (optind < argc) {
if (strcmp(argv[optind], "-") != 0) {
if (!(in = fopen(argv[optind], "r"))) {
fprintf(stderr, "Can't open %s\n",
argv[optind]);
perror(argv[0]);
exit(1);
}
}
if (optind + 1 < argc) {
if (strcmp(argv[optind + 1], "-") != 0) {
if (!(out = fopen(argv[optind + 1], "w"))) {
fprintf(stderr, "Can't open %s\n",
argv[optind + 1]);
perror(argv[0]);
exit(1);
}
}
}
}
fputs(prefix, out);
int col = 1;
while ((c = fgetc(in)) != -1) {
if (col >= 78 - 6)
{
fputs("\n", out);
col = 0;
}
fprintf(out, " 0x%.2x,", c);
col += 6;
}
fputs(suffix, out);
return 0;
}
|