Mercurial > msvpvf
annotate src/main.c @ 85:1acd477da42f
posix: improvements
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 17 Jun 2024 21:18:37 -0400 |
parents | c06dcab17923 |
children |
rev | line source |
---|---|
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include <stdio.h> |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
2 #include <stdint.h> |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
3 #include <stdlib.h> |
85 | 4 #include <stdarg.h> |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
5 #include <string.h> |
81
c06dcab17923
*: change license to BSD, update README for Unicode
Paper <paper@paper.us.eu.org>
parents:
79
diff
changeset
|
6 |
13 | 7 #include <getopt.h> |
42
d873a04165f9
use basename() on cli version
Paper <37962225+mrpapersonic@users.noreply.github.com>
parents:
16
diff
changeset
|
8 #include <libgen.h> |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
9 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
10 #include "common.h" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
11 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
12 /* non-portable functions */ |
85 | 13 static char* msvpvf_internal_strdup(const char* str) { |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
14 size_t len = strlen(str) + 1; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
15 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
16 char* copy = malloc(len); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
17 if (!copy) |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
18 return NULL; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
19 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
20 memcpy(copy, str, len); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
21 return copy; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
22 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
23 |
85 | 24 static char *msvpvf_internal_asnprintf(const char *format, ...) { |
25 va_list args; | |
26 int needed_size; | |
27 char *output = NULL; | |
28 | |
29 va_start(args, format); | |
30 | |
31 needed_size = vsnprintf(NULL, 0, format, args); | |
32 if (needed_size < 0) | |
33 return NULL; | |
34 | |
35 va_end(args); | |
36 | |
37 output = malloc((needed_size + 1) * sizeof(char)); | |
38 if (!output) | |
39 return NULL; | |
40 | |
41 va_start(args, format); | |
42 | |
43 needed_size = vsnprintf(output, needed_size + 1, format, args); | |
44 if (needed_size < 0) { | |
45 free(output); | |
46 return NULL; | |
47 } | |
48 | |
49 va_end(args); | |
50 | |
51 return output; | |
52 } | |
53 | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
54 /* source needs read permissions, target needs write permissions, both must be in binary mode */ |
85 | 55 static int copy_file(FILE* source, FILE* target) { |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
56 char ch[4096]; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
57 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
58 while (!feof(source)) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
59 size_t b = fread(ch, 1, sizeof(ch), source); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
60 if (b) |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
61 fwrite(ch, 1, b, target); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
62 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
63 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
64 return 0; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
65 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
66 |
85 | 67 static const char* type_names[] = { |
68 [TYPES_VF] = "Movie Studio", | |
69 [TYPES_VEG] = "Vegas Pro", | |
70 [TYPES_UNKNOWN] = "Unknown", | |
71 }; | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
72 |
85 | 73 static const char* type_extensions[] = { |
74 [TYPES_VF] = "vf", | |
75 [TYPES_VEG] = "veg", | |
76 }; | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
77 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
78 #ifndef ARRAYSIZE |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
79 #define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0])) |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
80 #endif |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
81 |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
82 static const char* help_text = |
85 | 83 "msvpvf help text\n" |
84 "author: Paper <paper@paper.us.eu.org>\n" | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
85 "usage: %s <input>... (arguments)\n" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
86 "\n" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
87 "arguments:\n" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
88 " -t, --type one of [vf,veg] (required)\n" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
89 " -v, --version version to convert to (required)\n" |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
90 " -h, --help show this screen (optional)\n"; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
91 |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
92 static struct option options_long[] = { |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
93 {"version", required_argument, NULL, 'v'}, |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
94 {"type", required_argument, NULL, 't'}, |
85 | 95 {"help", 0, NULL, 'h'}, |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
96 }; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
97 |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
98 int main(int argc, char *argv[]) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
99 uint8_t version = 0; |
85 | 100 enum types type = TYPES_VEG; /* ...? */ |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
101 |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
102 int c; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
103 int option_index = 0; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
104 while ((c = getopt_long(argc, argv, "v:t:h", options_long, &option_index)) != -1) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
105 /* option argument */ |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
106 switch (c) { |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
107 case 'v': |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
108 version = atoi(optarg); |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
109 break; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
110 case 't': |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
111 if (!strcmp(optarg, "vf")) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
112 type = TYPES_VF; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
113 } else if (!strcmp(optarg, "veg")) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
114 type = TYPES_VEG; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
115 } else { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
116 fprintf(stderr, "[ERROR]: Received an invalid type parameter!\n"); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
117 printf(help_text, argv[0]); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
118 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
119 |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
120 break; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
121 case 'h': |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
122 default: |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
123 printf(help_text, argv[0]); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
124 break; |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
125 } |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
126 } |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
127 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
128 if (argc <= optind) { |
85 | 129 fprintf(stderr, "[ERROR]: Missing input file(s)!\n"); |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
130 printf(help_text, argv[0]); |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
131 return 1; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
132 } |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
133 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
134 if (!version || !type) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
135 printf(help_text, (argc > 0) ? argv[0] : "msvpvf"); |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
136 return 1; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
137 } |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
138 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
139 while (optind < argc) { |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
140 const char* input = argv[optind++]; |
85 | 141 FILE *input_file, *output_file = NULL; |
142 uint8_t file_version = 0; | |
143 enum types file_type = TYPES_UNKNOWN; | |
144 | |
145 printf("Input file name: %s\n", input); | |
146 | |
147 /* print information about the input file */ | |
148 input_file = fopen(input, "rb"); | |
149 if (!input_file) { | |
150 fprintf(stderr, "[ERROR]: Error opening input file %s!\n\n", input); | |
151 continue; | |
152 } | |
153 | |
154 if (fgetc(input_file) == EOF) { | |
155 fprintf(stderr, "[ERROR]: Input file \"%s\" is empty.\n", input); | |
156 fclose(input_file); | |
157 continue; | |
158 } | |
159 | |
160 if (get_file_information(input_file, &file_version, &file_type)) { | |
161 fprintf(stderr, "[ERROR]: Failed to get file information for input file \"%s\"!\n", input); | |
162 fclose(input_file); | |
163 continue; | |
164 } | |
165 | |
166 printf("Input file version: %u\n", file_version); | |
167 printf("Input file type: %s\n\n", type_names[file_type]); | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
168 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
169 { |
85 | 170 /* open the output file... */ |
171 char* basec = msvpvf_internal_strdup(input); | |
172 char* bname = basename(basec); | |
173 int ext = strrchr(bname, '.') - bname; | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
174 |
85 | 175 char *output = msvpvf_internal_asnprintf("V%u_%.*s.%s", version, ext, bname, type_extensions[type]); |
176 if (!output) { | |
177 fprintf(stderr, "[ERROR]: msvpvf_internal_asnprintf failed!\n"); | |
178 free(basec); | |
179 free(output); | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
180 fclose(input_file); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
181 continue; |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
182 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
183 |
85 | 184 output_file = fopen(output, "w+b"); |
185 if (!output_file) { | |
186 fprintf(stderr, "[ERROR]: Failed to open output file %s! Do you have write permissions?\n", output); | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
187 free(basec); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
188 free(output); |
85 | 189 fclose(input_file); |
190 continue; | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
191 } |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
192 |
85 | 193 free(basec); |
194 free(output); | |
195 } | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
196 |
85 | 197 copy_file(input_file, output_file); |
198 | |
199 fflush(stdout); | |
200 fclose(input_file); | |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
201 |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
202 set_file_information(output_file, version, type); |
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
203 fclose(output_file); |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
204 } |
79
8f90d5addda9
*: refactor... basically everything!
Paper <paper@paper.us.eu.org>
parents:
58
diff
changeset
|
205 |
6
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
206 return 0; |
d1e5b8390cd3
Add Windows GUI version and a multitude of other changes
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
207 } |