/
main.c
executable file
·211 lines (192 loc) · 7.27 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// main.c
// AnsiLove/C
//
// Copyright (C) 2011-2015 Stefan Vogt, Brian Cassidy, Frederic Cambus.
// All rights reserved.
//
// This source code is licensed under the BSD 3-Clause License.
// See the file LICENSE for details.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include "alconfig.h"
#include "strtolower.h"
#include "substr.h"
#include "ansilove.h"
// prototypes
void showHelp(void);
void listExamples(void);
void versionInfo(void);
void synopsis(void);
void showHelp(void)
{
printf("\nSUPPORTED FILE TYPES:\n"
" ANS BIN ADF IDF XB PCB TND ASC NFO DIZ\n"
" Files with custom suffix default to the ANSi renderer.\n\n"
"OPERANDS:\n"
" Optional values to adjust output. There are certain cases where you need to\n"
" set operands for proper rendering. However, this is occassionally. Results\n"
" turn out well with the built-in defaults. You may launch AnsiLove with the\n"
" option -e to get a list of basic examples, with and without operands. Note\n"
" that columns is restricted to BIN files, it won't affect other file types.\n\n"
"DOCUMENTATION:\n"
" Detailed help is available at the AnsiLove/C repository on GitHub.\n"
" <https://github.com/ansilove/AnsiLove-C>\n\n");
}
void listExamples(void)
{
printf("\nEXAMPLES:\n");
printf(" ansilove file.ans -i (output path/name identical to input, no operands)\n"
" ansilove file.ans -ir (same as -i, adds Retina @2x output file)\n"
" ansilove file.ans -o dir/file (custom path/name for output, no operands)\n"
" ansilove file.ans -or dir/file (same as -o, adds Retina @2x output file)\n"
" ansilove file.bin -s (just display SAUCE record, don't generate output)\n"
" ansilove file.bin -i terminus 8 1 202 (set font, bits, icecolors, columns)\n"
" ansilove file.ans -o dir/file terminus 8 1 (custom font, bits, icecolors)\n"
" ansilove file.ans -or dir/file 80x25 8 1 (DOS font, bits, iCE, Retina)\n\n"
"HINT:\n"
" Don't add .png suffix when specifying a custom path/name for output as it\n"
" will be added automatically.\n\n");
}
void versionInfo(void)
{
printf("All rights reserved.\n"
"\nFork me on GitHub: <https://github.com/ansilove/AnsiLove-C>\n"
"Bug reports: <https://github.com/ansilove/AnsiLove-C/issues>\n\n"
"This is free software, released under the 3-Clause BSD license.\n"
"<https://github.com/ansilove/AnsiLove-C/blob/master/LICENSE>\n\n"
"LibGD is copyright (C) Thomas Boutell, Pierre-Alain Joye and contributors.\n"
"<https://bitbucket.org/pierrejoye/gd-libgd>\n\n");
}
// following the IEEE Std 1003.1 for utility conventions
void synopsis(void)
{
printf("\nSYNOPSIS:\n"
" ansilove input -i [operands]\n"
" ansilove input -ir [operands]\n"
" ansilove input -o output [operands]\n"
" ansilove input -or output [operands]\n"
" ansilove input -s\n"
" ansilove -vhe\n\n"
"OPTIONS:\n"
" -i output identical to input with .png suffix added\n"
" -ir same as -i, creates additional Retina @2x output file\n"
" -o specify custom file name/path for output\n"
" -or same as -o, creates additional Retina @2x output file\n"
" -s display SAUCE record without generating output\n"
" -v version information, equivalent to --version\n"
" -h show help, equivalent to --help\n"
" -e print a list of examples\n\n"
"OPERANDS:\n"
" font bits icecolors columns\n\n");
}
int main(int argc, char *argv[])
{
printf("AnsiLove/C %s - ANSi / ASCII art to PNG converter\n"\
"Copyright (C) 2011-2015 Stefan Vogt, Brian Cassidy, Frederic Cambus.\n", VERSION);
// SAUCE record related bool types
bool justDisplaySAUCE = false;
bool fileHasSAUCE = false;
// retina output bool type
bool createRetinaRep = false;
// analyze options and do what has to be done
bool outputIdentical = false;
bool fileIsBinary = false;
bool fileIsANSi = false;
bool fileIsPCBoard = false;
bool fileIsTundra = false;
if (argc == 1 || argc >= 9) {
synopsis();
return EXIT_SUCCESS;
}
int getoptFlag;
char *bits;
char *columns;
char *font;
char *icecolors = NULL; // TODO : Re-enable icecolors
char *input, *output = NULL;
char *retinaout = NULL;
while ((getoptFlag = getopt(argc, argv, "b:c:ef:hi:o:rsv")) != -1) {
switch(getoptFlag) {
case 'b':
bits = optarg;
break;
case 'c':
columns = optarg;
break;
case 'e':
listExamples();
return EXIT_SUCCESS;
case 'f':
font = optarg;
break;
case 'h':
showHelp();
return EXIT_SUCCESS;
case 'i':
input = optarg;
break;
case 'o':
output = optarg;
break;
case 'r':
createRetinaRep = true;
break;
case 's':
justDisplaySAUCE = true;
break;
case 'v':
versionInfo();
return EXIT_SUCCESS;
}
}
argc -= optind;
argv += optind;
// create output file name if output is not specified
if (!output) {
int outputLen = strlen(input) + 5;
output = malloc(outputLen);
snprintf(output, outputLen, "%s%s", input, ".png");
}
// get file extension
char *fext = strrchr(input, '.');
fext = fext ? strtolower(fext) : "none";
// create the output file by invoking the appropiate function
if (strcmp(fext, ".pcb") == 0) {
// params: input, output, font, bits, icecolors
alPcBoardLoader(input, output, retinaout, font, bits, createRetinaRep);
fileIsPCBoard = true;
}
else if (strcmp(fext, ".bin") == 0) {
// params: input, output, columns, font, bits, icecolors
alBinaryLoader(input, output, retinaout, columns, font, bits, icecolors, createRetinaRep);
fileIsBinary = true;
}
else if (strcmp(fext, ".adf") == 0) {
// params: input, output, bits
alArtworxLoader(input, output, retinaout, bits, createRetinaRep);
}
else if (strcmp(fext, ".idf") == 0) {
// params: input, output, bits
alIcedrawLoader(input, output, retinaout, bits, fileHasSAUCE, createRetinaRep);
}
else if (strcmp(fext, ".tnd") == 0) {
alTundraLoader(input, output, retinaout, font, bits, fileHasSAUCE, createRetinaRep);
fileIsTundra = true;
}
else if (strcmp(fext, ".xb") == 0) {
// params: input, output, bits
alXbinLoader(input, output, retinaout, bits, createRetinaRep);
}
else {
// params: input, output, font, bits, icecolors, fext
//alAnsiLoader(input, output, retinaout, font, bits, icecolors, fext, createRetinaRep);
alAnsiLoader(input, output, retinaout, "80x25", "9", "1", fext, createRetinaRep);
fileIsANSi = true;
}
return EXIT_SUCCESS;
}