-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_arr_file.c
98 lines (87 loc) · 2.31 KB
/
print_arr_file.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
static inline void failed_malloc() { //helper
fprintf(stderr, "Failed to allocate memory\n");
exit(-1);
}
size_t filelen = 0;
char *readfile(char *path) { //returns entire file content, including EOF
FILE *fp = fopen(path, "rb");
if(fp == NULL) {
fprintf(stderr, "cannot open file\n");
return NULL;
}
char *contents;
fseek(fp, 0, SEEK_END);
filelen = ftell(fp);
fseek(fp, 0, SEEK_SET); //get file length and return to start again
contents = malloc(filelen*sizeof(char) + 1); //add EOF
if(contents == NULL) return NULL; //return to allow cleanup
fread(contents, filelen, 1, fp);
contents[filelen] = EOF;
return contents;
}
static void help(char *argv) {
printf("Usage: %s [OPTIONS]\n", argv);
printf("prints file values in different fashion\n");
printf("Options: \n");
printf(" -f, --file [filename] file to get values of\n");
printf(" -c, --c-array print contents in C array\n");
exit(0);
}
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"file", required_argument, NULL, 'f'},
{"c-array", no_argument, NULL, 'c'},
{NULL, 0, NULL, 0}
};
void printContents(char *content) {
for(int i = 0; i < filelen; i++) putchar(content[i]);
}
void printCArray(char *content, char *filename) {
printf("char %s_arr[%lu] = { ", filename, filelen);
for(int i = 0; i < filelen-1; i++) {
printf("0x%02x, ", content[i]);
}
printf("0x%02x };\n", content[filelen-1]);
}
int main(int argc, char **argv) {
int opt;
char *infile = NULL;
size_t infile_path_len = 0;
int c_array = 0;
if(argc < 2) help(argv[0]);
while((opt = getopt_long(argc, argv, "hf:c", long_options, NULL)) != -1) {
switch(opt) {
case 'f':
infile_path_len = strlen(optarg);
infile = malloc(sizeof(char)*infile_path_len + 1); //including NULL character
if(infile == NULL) failed_malloc();
memcpy(infile, optarg, infile_path_len);
infile[infile_path_len] = 0x0;
break;
case 'c':
c_array = 1;
break;
case 'h':
default:
help(argv[0]);
break;
}
}
if(infile == NULL) {
fprintf(stderr, "Please provide an input file\n");
exit(-1);
}
char *contents = readfile(infile);
if(c_array == 1) {
printCArray(contents, infile);
} else {
printContents(contents);
}
free(contents);
free(infile);
return 0;
}