forked from simonsj/fdupes-jody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact_printjson.c
164 lines (145 loc) · 4.6 KB
/
act_printjson.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
/* Print comprehensive information to stdout in JSON format
* This file is part of jdupes; see jdupes.c for license information */
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include "jdupes.h"
#include "version.h"
#include "jody_win_unicode.h"
#include "act_printjson.h"
#define IS_CONT(a) ((a & 0xc0) == 0x80)
#define GET_CONT(a) (a & 0x3f)
#define TO_HEX(a) (char)(((a) & 0x0f) <= 0x09 ? ((a) & 0x0f) + 0x30 : ((a) & 0x0f) + 0x57)
#ifndef __GNUC__
#define __builtin_expect(v,e) (v)
#endif
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
#if defined(__GNU__) && !defined(PATH_MAX)
#define PATH_MAX 1024
#endif
/** Decodes a single UTF-8 codepoint, consuming bytes. */
static inline uint32_t decode_utf8(const char * restrict * const string) {
uint32_t ret = 0;
/** Eat problems up silently. */
assert(!IS_CONT(**string));
while (unlikely(IS_CONT(**string)))
(*string)++;
/** ASCII. */
if (likely(!(**string & 0x80)))
return (uint32_t)*(*string)++;
/** Multibyte 2, 3, 4. */
if ((**string & 0xe0) == 0xc0) {
ret = *(*string)++ & 0x1f;
ret = (ret << 6) | GET_CONT(*(*string)++);
return ret;
}
if ((**string & 0xf0) == 0xe0) {
ret = *(*string)++ & 0x0f;
ret = (ret << 6) | GET_CONT(*(*string)++);
ret = (ret << 6) | GET_CONT(*(*string)++);
return ret;
}
if ((**string & 0xf8) == 0xf0) {
ret = *(*string)++ & 0x07;
ret = (ret << 6) | GET_CONT(*(*string)++);
ret = (ret << 6) | GET_CONT(*(*string)++);
ret = (ret << 6) | GET_CONT(*(*string)++);
return ret;
}
/** We shouldn't be here... Because 5 and 6 bytes are impossible... */
assert(0);
return 0xffffffff;
}
/** Escapes a single UTF-16 code unit for JSON. */
static inline void escape_uni16(uint16_t u16, char ** const json) {
*(*json)++ = '\\';
*(*json)++ = 'u';
*(*json)++ = TO_HEX(u16 >> 12);
*(*json)++ = TO_HEX(u16 >> 8);
*(*json)++ = TO_HEX(u16 >> 4);
*(*json)++ = TO_HEX(u16);
}
/** Escapes a UTF-8 string to ASCII JSON format. */
static void json_escape(const char * restrict string, char * restrict const target)
{
uint32_t curr = 0;
char *escaped = target;
while (*string != '\0' && (escaped - target) < (PATH_MAX * 2 - 1)) {
switch (*string) {
case '\"':
case '\\':
*escaped++ = '\\';
*escaped++ = *string++;
break;
default:
curr = decode_utf8(&string);
if (curr == 0xffffffff) break;
if (likely(curr < 0xffff)) {
if (likely(curr < 0x20 || curr > 0x7f))
escape_uni16((uint16_t)curr, &escaped);
else
*escaped++ = (char)curr;
} else {
curr -= 0x10000;
escape_uni16((uint16_t)(0xD800 + ((curr >> 10) & 0x03ff)), &escaped);
escape_uni16((uint16_t)(0xDC00 + (curr & 0x03ff)), &escaped);
}
break;
}
}
*escaped = '\0';
return;
}
extern void printjson(file_t * restrict files, const int argc, char **argv)
{
file_t * restrict tmpfile;
int arg = 0, comma = 0, len = 0;
char *temp = string_malloc(PATH_MAX * 2);
char *temp2 = string_malloc(PATH_MAX * 2);
char *temp_insert = temp;
LOUD(fprintf(stderr, "printjson: %p\n", files));
/* Output information about the jdupes command environment */
printf("{\n \"jdupesVersion\": \"%s\",\n \"jdupesVersionDate\": \"%s\",\n", VER, VERDATE);
printf(" \"commandLine\": \"");
while (arg < argc) {
len = sprintf(temp_insert, " %s", argv[arg]);
assert(len >= 0);
temp_insert += len;
arg++;
}
json_escape(temp + 1, temp2); /* Skip the starting space */
printf("%s\",\n", temp2);
printf(" \"extensionFlags\": \"");
if (extensions[0] == NULL) printf("none\",\n");
else for (int c = 0; extensions[c] != NULL; c++)
printf("%s%s", extensions[c], extensions[c+1] == NULL ? "\",\n" : " ");
printf(" \"matchSets\": [\n");
while (files != NULL) {
if (ISFLAG(files->flags, FF_HAS_DUPES)) {
if (comma) printf(",\n");
printf(" {\n \"fileSize\": %" PRIdMAX ",\n \"fileList\": [\n { \"filePath\": \"", (intmax_t)files->size);
sprintf(temp, "%s", files->d_name);
json_escape(temp, temp2);
fwprint(stdout, temp2, 0);
printf("\"");
tmpfile = files->duplicates;
while (tmpfile != NULL) {
printf(" },\n { \"filePath\": \"");
sprintf(temp, "%s", tmpfile->d_name);
json_escape(temp, temp2);
fwprint(stdout, temp2, 0);
printf("\"");
tmpfile = tmpfile->duplicates;
}
printf(" }\n ]\n }");
comma = 1;
}
files = files->next;
}
printf("\n ]\n}\n");
string_free(temp); string_free(temp2);
return;
}