-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
448 lines (422 loc) · 13.3 KB
/
cli.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* libospl - Open Source Photo Library
an opensource and multiplateform photo library management that can be used
to store and sort all your photos.
Copyright (C) 2019-2020 Angelo Frangione
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "includes/ospl.h"
#include "includes/ospl_cli.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
char *ARGS[ARGS_COUNT] = {"create", "import", "photo", "export", "album", "folder"};
char *ALBUM_ARGS[ALB_ARG_CNT] = {"list", "create", "rename", "delete", "phoadd", "phodel", "pholist", "phomove"};
enum {LIST = 1, CREATE, RENAME, DELETE, PHOADD, PHODEL, PHOLIST, PHOMOVE};
char *PHOTO_ARGS[PHO_ARG_CNT] = {"list", "info", "delete"};
char *g_library_path = NULL;
char *g_database_path = NULL;
int print_mode = 0; // 0 = normal printing, 1 = verbose printing, 2 = json printing
int isnumeric(char *s)
{
while (*s)
{
if (!isdigit(*s))
return 0;
++s;
}
return 1;
}
void show_usage()
{
printf("\nOSPL - Open source photo library v%s.%s.%s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
printf("Utility to manage an OSPL\n");
printf("================================================================================\n");
printf("Usage: ospl <verb> <librarypath> <options>, where <verb> is as follows:\n");
printf("--------------------------------------------------------------------------------\n");
printf("ospl create creating library\n");
printf("ospl import importing photos\n");
printf("ospl photo managing photos\n");
printf("ospl export exporting photos\n");
printf("ospl album managing albums\n");
printf("ospl folder managing folders\n");
printf("--------------------------------------------------------------------------------\n");
printf("ospl <verb> with no options will provide help on that verb\n");
}
void usage_create()
{
printf("\nCreates a shiny new ospl library at <librarypath>\n");
printf("--------------------------------------------------------------------------------\n");
printf("Usage: ospl create <librarypath> [options]\n");
printf("Options that are not mandatory (only one of these):\n");
printf("-v verbose printing\n");
}
void usage_import()
{
printf("\nImport specified photos into <librarypath>\n");
printf("--------------------------------------------------------------------------------\n");
printf("Usage: ospl import <librarypath> <options>\n");
printf("OPTIONS:\n");
printf("At least one of the following :\n");
printf(" -f <path> <optionnal album>: specifies a file to import into optionnal album\n");
printf(" -F <path> <optionnal album>: specifies a folder to import into optionnal album\n");
printf("Options that are not mandatory :\n");
printf(" -v : human readable maximum verbosity\n");
printf("// -a <album_id> : import into album_id album (not implemented yet)\n");
}
void usage_export(char *arg)
{
}
void usage_album(char *arg)
{
printf("\nManages albums at <librarypath>\n");
printf("--------------------------------------------------------------------------------\n");
printf("Usage: ospl album <verb> <librarypath> <options>, where <verb> is as follows:\n");
printf("--------------------------------------------------------------------------------\n");
printf("ospl album list <librarypath> list albums\n");
printf("ospl album create <librarypath> <name> creating albums\n");
printf("ospl album rename <librarypath> <album> <name> renaming albums\n");
printf("ospl album delete <librarypath> <album> deleting albums\n");
printf("ospl album phoadd <librarypath> <album> <photo> add a photo into album\n");
printf("ospl album phodel <librarypath> <album> <photo> delete a photo from album\n");
printf("ospl album pholist <librarypath> <album> list photos inside an album\n");
printf("ospl album phomove <librarypath> <photo> <old> <new> move photo to another album\n");
printf("--------------------------------------------------------------------------------\n");
}
void usage_folder(char *arg)
{
}
void usage_photo(char **arg)
{
printf("\nManages photos at <librarypath>\n");
printf("--------------------------------------------------------------------------------\n");
printf("Usage: ospl photo <verb> <librarypath> <options>, where <verb> is as follows:\n");
printf("--------------------------------------------------------------------------------\n");
printf("ospl photo list <librarypath> list every photo\n");
printf("ospl photo info <librarypath> <id> show more info about a photo\n");
printf("ospl photo delete <librarypath> <id> remove photo from library\n");
printf("--------------------------------------------------------------------------------\n");
}
/**
* \brief Returns the index of given arg in available arguments
*
* \param arg Argument to get index of
* \return The index of arg in available arguments
*/
static int indexof_arg(char *arg, char **args, int count)
{
for (int i = 0; i < count; i++)
if (!strcmp(args[i], arg))
return (i + 1);
return 0;
}
/**
* \brief Initiate the cli global variables
*
* \param av library path
* \return 0 with success and 1 when an error occurs
*/
static int initiate_path(char *path)
{
int path_len = strlen(path);
g_library_path = calloc(sizeof(char), (path_len + 2));
if (!g_library_path)
return 1;
g_database_path = calloc(sizeof(char), (path_len + DATABASE_NAME_LEN + 1));
if (!g_database_path)
return 1;
strcat(g_library_path, path);
if (g_library_path[path_len - 1] != '/')
strcat(g_library_path, "/");
strcat(g_database_path, g_library_path);
strcat(g_database_path, DATABASE_FILENAME);
return 0;
}
void usage_unrecognized(char *arg)
{
printf("ospl: did not recognize verb '%s' type \"ospl\" for a list\n", arg);
}
static int return_1()
{
return 1;
}
static int import(int ac, char **av)
{
printf("import/ (args: %d) 1: %s 2: %s 3: %s\n", ac, av[0], av[1], av[2]);
if (ac < 5)
{
usage_import();
return 0;
}
else if (!strcmp(av[1], "-f") && ac < 6)
{
initiate_path(av[0]);
if (library_exists(av[0]))
{
if (ospl_import_photo(av[0], av[2]) >= 0)
printf("photo imported with success\n");
else
printf("failed importing photo\n");
}
else
printf("Library not found: %s\n", av[0]);
}
else if (!strcmp(av[1], "-F") && ac < 6)
{
initiate_path(av[0]);
if (library_exists(av[0]))
ospl_import_folder(av[0], av[2]);
else
printf("Library not found: %s\n", av[0]);
}
else if (!strcmp(av[1], "-f") && ac < 7)
{
initiate_path(av[0]);
if (library_exists(av[0]))
{
if (ospl_import_photo_in_album(av[0], av[2], 1) >= 0)
printf("photo imported with success\n");
else
printf("failed importing photo\n");
}
else
printf("Library not found: %s\n", av[0]);
}
else if (!strcmp(av[1], "-F") && ac < 7)
{
initiate_path(av[0]);
if (library_exists(av[0]))
ospl_import_folder_in_album(av[0], av[2], 1);
else
printf("Library not found: %s\n", av[0]);
}
return 0;
}
static int export(int ac, char **av)
{
printf("export/ (args: %d) 1: %s 2: %s\n", ac, av[0], av[1]);
return 0;
}
static int album (int ac, char **av)
{
int r, ret;
printf("album/ (args: %d) 0: %s 1: %s\n", ac, av[0], av[1]);
if (ac < 4)
{
usage_album(NULL);
return 0;
}
else if ((r = indexof_arg(av[0], ALBUM_ARGS, ALB_ARG_CNT)))
{
if(!library_exists(av[1]))
{
printf("Library not found: %s\n", av[1]);
return 0;
}
switch(r)
{
case LIST :
printf("Album list:\n=================\n");
printf("%3s | %10s\n", "id", "name");
printf("----------------\n");
t_album alb_list[ALB_LIMITS] = { 0 };
ospl_album_list(av[1], alb_list);
for(int i = 0; alb_list[i].id; i++)
printf("%3d | %10s\n", alb_list[i].id, alb_list[i].name);
break;
case CREATE :
if (ac < 5)
{
usage_album(NULL);
return 0;
}
ret = ospl_album_create(av[1], av[2]);
if (ret >= SUCCESS)
printf("album %s created\n", av[2]);
else
printf("error while creating album: %d\n", ret);
break;
case RENAME :
if (ac < 6 || !isnumeric(av[2]))
{
usage_album(NULL);
return 0;
}
ret = ospl_album_rename(av[1], atoi(av[2]), av[3]);
if (ret >= SUCCESS)
printf("album %d renamed to %s\n", atoi(av[2]), av[3]);
else
printf("error while creating album: %d\n", ret);
break;
case DELETE :
if (ac < 5 || !isnumeric(av[2]))
{
usage_album(NULL);
return 0;
}
ret = ospl_album_delete(av[1], atoi(av[2]));
if (ret < 0)
printf("error deleting album: %s\n", ospl_enum_error(ret));
else
printf("deleted album %s\n", av[2]);
break;
case PHOADD :
if (ac < 6 || !isnumeric(av[2]) || !isnumeric(av[3]))
{
usage_album(NULL);
return 0;
}
ospl_album_add_photo(av[1], atoi(av[2]), atoi(av[3]));
printf("photo %d added to album %d\n", atoi(av[2]), atoi(av[3]));
break;
case PHODEL :
if (ac < 6 || !isnumeric(av[2]) || !isnumeric(av[3]))
{
usage_album(NULL);
return 0;
}
ospl_album_delete_photo(av[1], atoi(av[2]), atoi(av[3]));
printf("photo %d deleted from album %d\n", atoi(av[2]), atoi(av[3]));
break;
case PHOLIST :
if (ac < 5 || !isnumeric(av[2]))
{
usage_album(NULL);
return 0;
}
t_photos pho_list[6500] = { 0 };
ospl_album_list_photos(av[1], atoi(av[2]), pho_list);
printf("Photo inside album %d:\n", atoi(av[2]));
printf("----------------------\n");
// printf("%d\n", pho_list[0]);
int i = 0;
while(pho_list[i].id)
{
printf("%s : %s\n", pho_list[i].original_name, pho_list[i].new_name);
++i;
}
break;
case PHOMOVE :
if (ac < 7 || !isnumeric(av[2]) || !isnumeric(av[3]) || !isnumeric(av[4]))
{
usage_album(NULL);
return 0;
}
ospl_album_move_photo(av[1], atoi(av[2]), atoi(av[3]), atoi(av[4]));
printf("photo %d moved from album %d to album %d\n", atoi(av[2]), atoi(av[3]), atoi(av[4]));
break;
}
}
else
usage_album(NULL);
return 0;
}
static int create(int ac, char **av)
{
initiate_path(av[0]);
if (av[1] && !strcmp(av[1], "-v"))
{
printf("g_library_path: {%s}\ng_database_path: {%s}\n",
g_library_path, g_database_path);
}
if (ospl_create_library(g_library_path) < 0)
perror(NULL);
else
printf("created library at path: %s\n", av[0]);
return 0;
}
static int photo(int ac, char **av)
{
int r;
printf("photo/ (args: %d) 1: %s 2: %s\n", ac, av[1], av[2]);
if (ac < 4)
{
usage_photo(NULL);
return 0;
}
else if ((r = indexof_arg(av[0], PHOTO_ARGS, PHO_ARG_CNT)))
{
if(!library_exists(av[1]))
{
printf("Library not found: %s\n", av[1]);
return 0;
}
switch(r)
{
case 1 :
printf("list\n");
t_photos pho_list[6500] = { 0 };
ospl_photo_list(av[1], pho_list);
printf("%5s|%32s|%52s|%24s|%11s|%10s\n",
"id", "hash", "original_name", "import_datetime",
"exif_height", "exif_width");
for(int i = 0; pho_list[i].import_year; i++)
printf("%5d|%32s|%52s|%24s|%11d|%10d\n",
pho_list[i].id, pho_list[i].hash,
pho_list[i].original_name, pho_list[i].import_datetime,
pho_list[i].exif_height, pho_list[i].exif_width);
break;
break;
case 2:
printf("info\n");
if (ac < 5 || !isnumeric(av[2]))
{
usage_photo(NULL);
return 0;
}
t_photos pho = { 0 };
ospl_photo_get(av[1], atoi(av[2]), &pho);
printf("%5s|%32s|%52s|%24s|%11s|%10s\n",
"id", "hash", "original_name", "import_datetime",
"exif_height", "exif_width");
printf("%5d|%32s|%52s|%24s|%11d|%10d\n",
pho.id, pho.hash, pho.original_name, pho.import_datetime,
pho.exif_height, pho.exif_width);
break;
case 3:
if (ac < 5 || !isnumeric(av[2]))
{
usage_photo(NULL);
return 0;
}
ospl_photo_delete(av[1], atoi(av[2]));
printf("deleted photo %s from library\n", av[2]);
break;
}
}
else
usage_photo(NULL);
return 0;
}
int main(int ac, char *av[])
{
if (ac == 1)
show_usage();
else if (ac == 2)
{
printf("index of %s: %d\n", av[1], indexof_arg(av[1], ARGS, ARGS_COUNT));
void (*usage_launcher[1 + ARGS_COUNT])() = {usage_unrecognized, usage_create, usage_import, usage_photo, usage_export, usage_album, usage_folder};
usage_launcher[indexof_arg(av[1], ARGS, ARGS_COUNT)](av[1]);
}
else
{
printf("index of %s: %d\n", av[1], indexof_arg(av[1], ARGS, ARGS_COUNT));
void (*usage_launcher[1 + ARGS_COUNT])() = {usage_unrecognized, usage_create, usage_import, usage_photo, usage_export, usage_album};
int (*function_launcher[1 + ARGS_COUNT])() = {return_1, create, import, photo, export, album};
if (function_launcher[indexof_arg(av[1], ARGS, ARGS_COUNT)](ac, &av[2]) == 1)
usage_launcher[indexof_arg(av[1], ARGS, ARGS_COUNT)](av[2]);
}
free(g_library_path);
free(g_database_path);
}