-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_list.c
112 lines (84 loc) · 2.69 KB
/
db_list.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
/**
* @file do_list.c
* @brief Implementation de la commande do_list
*
* @author Dominique Roduit, Thierry Treyer
* @date 9 Mar 2016
*/
#include <string.h>
#include <json-c/json.h>
#include "pictDB.h"
/* Fonctions "privées" pour do_list */
const char* do_list_stdout (const struct pictdb_file* db_file);
const char* do_list_json (const struct pictdb_file* db_file);
/********************************************************************//**
* Affiche complet de la base de donnée
*/
const char* do_list (const struct pictdb_file* db_file, enum do_list_mode mode)
{
const char *response = NULL;
switch (mode) {
case STDOUT:
response = do_list_stdout(db_file);
break;
case JSON:
response = do_list_json(db_file);
break;
default:
response = "unimplemented do_list mode";
}
return response;
}
const char* do_list_stdout (const struct pictdb_file* db_file)
{
const struct pictdb_header header = db_file->header;
print_header(&header);
if (header.num_files == 0) {
puts("<< empty database >>");
} else {
uint32_t i = 0, num_files = 0;
while (i < header.max_files && num_files < header.num_files) {
struct pict_metadata metadata = db_file->metadata[i];
if (metadata.is_valid == NON_EMPTY) {
print_metadata(&metadata);
num_files++;
}
i++;
}
}
return NULL;
}
const char* do_list_json (const struct pictdb_file* db_file)
{
const struct pictdb_header header = db_file->header;
const char* response = NULL;
json_object* json_response = json_object_new_object();
json_object* pictures = json_object_new_array();
uint32_t i = 0, num_files = 0;
while (i < header.max_files && num_files < header.num_files) {
struct pict_metadata metadata = db_file->metadata[i];
if (metadata.is_valid == NON_EMPTY) {
json_object* pict_id = json_object_new_string(metadata.pict_id);
json_object_array_add(pictures, pict_id);
num_files++;
}
i++;
}
json_object_object_add(json_response, "Pictures", pictures);
const char *json_buffer = json_object_to_json_string(json_response);
if (json_buffer == NULL)
goto error;
size_t response_length = strlen(json_buffer);
response = calloc(response_length + 1, sizeof(char));
if (response == NULL)
goto error;
strcpy((char*)response, json_buffer);
json_object_put(json_response); // Free
return response;
error:
if (json_response != NULL)
json_object_put(json_response);
if (response != NULL)
free((void*)response);
return NULL;
}