10 #include "nomos_utils.h"
11 #include <json-c/json.h>
13 static void writeHighlightInfoToJson(GArray* theMatches, json_object *resultsArray) {
15 for (currentLicence = 0; currentLicence < theMatches->len; ++currentLicence) {
19 for (highl = 0; highl < theLicence->
matchPositions->len; ++highl) {
21 json_object *result = json_object_new_object();
22 json_object_object_add(result,
"license", json_object_new_string(theLicence->
licenceName));
23 json_object_object_add(result,
"start", json_object_new_int(ourMatchv->
start));
24 json_object_object_add(result,
"end", json_object_new_int(ourMatchv->
end));
25 json_object_object_add(result,
"len", json_object_new_int(ourMatchv->
end - ourMatchv->
start));
26 json_object_array_add(resultsArray, result);
33 char realPathOfTarget[PATH_MAX];
34 json_object *result = json_object_new_object();
35 json_object *resultsArray = json_object_new_array();
36 json_object *licenses = json_object_new_array();
37 json_object *fileLocation = NULL;
38 json_object *aLicense = NULL;
42 writeHighlightInfoToJson(cur.
theMatches, resultsArray);
48 aLicense = json_object_new_string(cur.
licenseList[i]);
50 json_object_array_add(licenses, aLicense);
58 fileLocation = json_object_new_string(realPathOfTarget);
62 fileLocation = json_object_new_string(basename(cur.
targetFile));
64 json_object_object_add(result,
"file", fileLocation);
66 json_object_object_add(result,
"licenses", resultsArray);
69 json_object_object_add(result,
"licenses", licenses);
73 json_object_to_json_string_ext(result, JSON_C_TO_STRING_PRETTY));
77 printf(
",%s\n", prettyJson);
82 printf(
"%s\n", prettyJson);
87 json_object_put(result);
92 const char *escapedSeparator =
"\\/";
93 const char *pathSeparator =
"/";
94 const int escPathLen = 2;
95 const int pathSepLen = 1;
96 size_t resultLength = 0;
97 size_t remainingLength = -1;
106 tempjson = strdup(json);
109 for (count = 0; (tmp = strstr(tmp, escapedSeparator)); count++)
114 resultLength = strlen(tempjson) - ((escPathLen - pathSepLen) * count);
116 result = (
char*) calloc(resultLength + 1,
sizeof(
char));
118 strncpy(result, strtok(tempjson, escapedSeparator), resultLength);
119 remainingLength = resultLength - strlen(result);
121 while (count-- && remainingLength > 0)
123 strncat(result, pathSeparator, remainingLength);
124 strncat(result, strtok(NULL, escapedSeparator), remainingLength - 1);
125 remainingLength = resultLength - strlen(result);
133 mutexJson = (sem_t *) mmap(NULL,
sizeof(sem_t),
134 PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
135 printcomma = (gboolean *) mmap(NULL,
sizeof(gboolean),
136 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
137 sem_init(
mutexJson, 2, SEM_DEFAULT_VALUE);
void writeJson()
Write the scan output as a JSON.
char * unescapePathSeparator(const char *json)
Unescape the path separator from JSON.
int optionIsSet(int val)
Check if an CLI option is set.
sem_t * mutexJson
Mutex to handle JSON writes.
FUNCTION void parseLicenseList()
parse the comma separated list of license names found
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
gboolean * printcomma
True to print comma while printing JSON object.
GArray * matchPositions
Match positions.
char * licenceName
License names.
int start
Start position of match.
int end
End position of match.
char targetFile[myBUFSIZ]