This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
166 lines (140 loc) · 3.14 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// "fputs format"..
// (error:|warning:) <description>
//int length(char* a)
int length(const char* a)
{
return strlen(a);
}
void usage()
{
char* str = "usage.. ./file-to-c <--help/file>\n"
"parameters..\n"
"* --help (show this text)\n";
fputs(str, stdout);
}
int main(int argc, char** argv)
{
if(argc == 1) //< NOTE: argv[0] == "./file-to-c"
{
usage();
return 0;
}
char* ARGUMENT = argv[1]; //< don't modify
// TODO: replace w. arguments-mini..?
if(strcmp(ARGUMENT, "--help") == 0)
{
usage();
return 0;
}
else
{
int pathToInputfileLength = length(ARGUMENT);
// TODO: if any extension.. replace extension w. .h instead..?
int pathToOutputfileLength = pathToInputfileLength + 2;
// pathToOutputfile == <ARGUMENT>.h
//char* pathToOutputfile = new char[pathToOutputfileLength + 1];
char* pathToOutputfile = malloc(pathToOutputfileLength);
int a = 0;
memcpy(pathToOutputfile, ARGUMENT, pathToInputfileLength);
a += pathToInputfileLength;
pathToOutputfile[a] = '.';
++a;
pathToOutputfile[a] = 'h';
++a;
pathToOutputfile[a] = '\0';
FILE* b = fopen(ARGUMENT, "rb");
if(b == NULL)
{
printf("error: failed to open file %s\n", ARGUMENT);
return 1;
}
fseek(b, 0, SEEK_END);
int inputfileLength = ftell(b);
fseek(b, 0, SEEK_SET);
struct
{
struct
{
FILE* a;
} file;
} outputfile;
outputfile.file.a = fopen(pathToOutputfile, "w");
//char* inputfile = new char[inputfileLength];
char* inputfile = malloc(inputfileLength);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
fread((void*)inputfile, 1, inputfileLength, b);
#pragma GCC diagnostic pop
fclose(b);
//#ifndef <to upper ARGUMENT>_H
fwrite("#ifndef ", 1, 8, outputfile.file.a);
for(int i = 0; i < pathToInputfileLength; ++i)
{
char c;
if(ARGUMENT[i] == '.')
{
c = '_';
}
else
{
c = toupper(ARGUMENT[i]);
}
fwrite(&c, 1, 1, outputfile.file.a);
}
fwrite("_H\n", 1, 3, outputfile.file.a);
//#define <to upper ARGUMENT>_H
fwrite("#define ", 1, 8, outputfile.file.a);
for(int i = 0; i < pathToInputfileLength; ++i)
{
char c;
if(ARGUMENT[i] == '.')
{
c = '_';
}
else
{
c = toupper(ARGUMENT[i]);
}
fwrite(&c, 1, 1, outputfile.file.a);
}
fwrite("_H\n\n\n", 1, 5, outputfile.file.a);
fwrite("char ", 1, 5, outputfile.file.a);
for(int i = 0; i < pathToInputfileLength; ++i)
{
char c;
if(ARGUMENT[i] == '.')
{
c = '_';
}
else
{
c = ARGUMENT[i];
}
fwrite(&c, 1, 1, outputfile.file.a);
}
fwrite("[] = {", 1, 6, outputfile.file.a);
for(int i = 0; i < inputfileLength; ++i)
{
if(i > 0)
{
fwrite(",",1,1,outputfile.file.a);
}
char c[3+1]; //< max 255 <- 3 characters
sprintf(c, "%hhu", inputfile[i]);
fwrite(c, 1, length(c), outputfile.file.a);
}
fwrite("};\n\n", 1, 4, outputfile.file.a);
fwrite("#endif\n", 1, 7, outputfile.file.a);
fclose(outputfile.file.a);
//delete pathToOutputfile;
free(pathToOutputfile);
//delete inputfile;
free(inputfile);
return 0;
}
return 1;
}