-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
192 lines (170 loc) · 4.48 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
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
#include "convert.h"
#include "inspect.h"
#include "parser.h"
typedef struct {
bool inspect_mode;
bool show_large_preview_mode;
bool show_small_preview_mode;
bool export_layer_data;
const char *export_dir;
const char *in;
char *out;
} options_t;
static void
usage (const char *progname)
{
printf ("%s [-h] [--help] <input sl1 file> [<output ctb file>] \n\
%s <-i> <ctb file> \n\
%s <-l|-s|-e> <dir> <ctb file> \n\
\n\
Convert a sl1 file into a ctb v4 file. \n\
Output file will have the same name as input file with the \n\
.sl1 extension replaced with .ctb, unless `output ctb file` is \n\
specified. \n\
\n\
When `-i` option is provided, inspect the given file instead.\n\
This works with both ctb files and sl1 files.\n\
\n\
When `-l` or `-s` option is provided, export preview image instead.\n\
`-l` exports the large preview, and `-s` exports the small preview.\n\
The file will be saved in `dir` and will be named `preview.png`.\n\
Only works for ctb files.\n\
\n\
When -e option is provided, export layers and their headers in `dir`.\n\
Only works for ctb files.\n\
\n\
Options: \n\
\n\
-i : inspect file. (ctb or sl1) \n\
-l : export large preview. (ctb only) \n\
-s : export small preview. (ctb only) \n\
-e : export layer data to <dir> (ctb only) \n\
-h|--help : show this help \n\
\n", progname, progname, progname);
}
static void
parse_options (options_t *options, const size_t argc, char ** const argv)
{
for (size_t i = 1; i < argc; i++)
{
if (strncmp (argv[i], "-i", 3) == 0)
{
options->inspect_mode = true;
continue;
}
if (strncmp (argv[i], "-l", 3) == 0)
{
if (argc < i+2)
{
usage (argv[0]);
exit (1);
}
options->show_large_preview_mode = true;
options->export_dir = argv[++i];
continue;
}
if (strncmp (argv[i], "-s", 3) == 0)
{
if (argc < i+2)
{
usage (argv[0]);
exit (1);
}
options->show_small_preview_mode = true;
options->export_dir = argv[++i];
continue;
}
if (strncmp (argv[i], "-h", 3) == 0 || strncmp (argv[i], "--help", 7) == 0)
{
usage (argv[0]);
exit (0);
}
if (strncmp (argv[i], "-e", 3) == 0)
{
if (argc < i+2)
{
usage (argv[0]);
exit (1);
}
options->export_layer_data = true;
options->export_dir = argv[++i];
continue;
}
if (!options->in)
{
options->in = argv[i];
continue;
}
if (!options->out)
{
options->out = strdup (argv[i]);
continue;
}
}
if (!options->in)
{
usage (argv[0]);
exit (1);
}
if (!options->out)
{
size_t len = strlen (options->in);
char *ptr = (char *) options->in;
ptr += len - 4;
if (strncmp (ptr, ".sl1", 5) == 0)
{
options->out = strdup (options->in);
options->out[len-3] = 'c';
options->out[len-2] = 't';
options->out[len-1] = 'b';
}
else
{
options->out = xalloc (len + 5);
snprintf (options->out, len + 5, "%s.ctb", options->in);
}
}
}
int
main (int argc, char **argv)
{
int err = 0;
options_t *options = xalloc (sizeof (options_t));
parse_options (options, argc, argv);
if (options->inspect_mode)
{
err = inspect (options->in);
goto cleanup;
}
if (options->show_large_preview_mode)
{
err = show_preview_image (options->in, PREVIEW_LARGE, options->export_dir);
goto cleanup;
}
if (options->show_small_preview_mode)
{
err = show_preview_image (options->in, PREVIEW_SMALL, options->export_dir);
goto cleanup;
}
if (options->export_layer_data)
{
err = export_layers (options->in, options->export_dir);
goto cleanup;
}
if (strncmp (options->in, options->out, 1000) == 0)
{
fprintf (stderr, "main.c: Please specify an output file different from input file.\n\n");
usage (argv[0]);
exit (1);
}
err = convert (options->in, options->out);
cleanup:
if (options->out) free (options->out);
free (options);
return err;
}