11 #include "string_operations.h"
14 static char* ignoredLicenseNames[] = {
"Void",
"No_license_found"};
15 static char* ignoredLicenseTexts[] = {
"License by Nomos.",
"License by Ninka."};
19 int ignoredLicenseNamesCount =
sizeof(ignoredLicenseNames)/
sizeof(
char*);
20 for (
int i = 0; i < ignoredLicenseNamesCount; i++) {
21 if (strcmp(
license->shortname, ignoredLicenseNames[i]) == 0)
25 int ignoredLicenseTextsCount =
sizeof(ignoredLicenseTexts)/
sizeof(
char*);
26 for (
int i = 0; i < ignoredLicenseTextsCount; i++) {
27 GArray* ignoredTokens = tokenize(ignoredLicenseTexts[i], DELIMITERS);
28 if (tokensEquals(
license->tokens, ignoredTokens)) {
29 tokens_free(ignoredTokens);
32 tokens_free(ignoredTokens);
39 GArray* licenses = g_array_new(TRUE, FALSE,
sizeof (
License));
41 for (
int j = 0; j < PQntuples(licensesResult); j++) {
42 long refId = atol(PQgetvalue(licensesResult, j, 0));
43 char* licShortName = PQgetvalue(licensesResult, j, 1);
47 license.shortname = g_strdup(licShortName);
49 char* licenseText = getLicenseTextForLicenseRefId(
dbManager, refId);
50 GArray* licenseTokens = tokenize(licenseText, DELIMITERS);
55 if (!isIgnoredLicense(&
license))
56 g_array_append_val(licenses,
license);
63 return buildLicenseIndexes(licenses, minAdjacentMatches, maxLeadingDiff);
66 void licenses_free(
Licenses* licenses) {
68 GArray* licenseArray = licenses->licenses;
69 for (guint i = 0; i < licenseArray->len; i++) {
77 g_array_free(licenseArray, TRUE);
79 g_array_free(licenses->shortLicenses, TRUE);
81 GArray* indexes = licenses->indexes;
82 for (guint i = 0; i < indexes->len; i++) {
83 GHashTable* index = g_array_index(indexes, GHashTable*, i);
84 g_hash_table_unref(index);
86 g_array_free(indexes, TRUE);
92 guint uint32_hash (gconstpointer v) {
93 uint32_t u = *(uint32_t*)v;
97 gboolean uint32_equal (gconstpointer v1, gconstpointer v2) {
98 uint32_t u1 = *(uint32_t*)v1;
99 uint32_t u2 = *(uint32_t*)v2;
104 static void g_array_free_true(
void* ptr) {
105 g_array_free(ptr, TRUE);
108 uint32_t getKey(
const GArray* tokens,
unsigned minAdjacentMatches,
unsigned searchedStart) {
110 for (guint i = 0; (i < minAdjacentMatches) && (i+searchedStart < tokens->len); i++)
112 Token* nToken = tokens_index(tokens, i+searchedStart);
113 result = (result << 1) + nToken->hashedContent;
119 Licenses* buildLicenseIndexes(GArray* licenses,
unsigned minAdjacentMatches,
unsigned maxLeadingDiff) {
124 #define is_short(license) ( (license)->tokens->len <= minAdjacentMatches )
125 GArray* shortLicenses = g_array_new(FALSE, FALSE,
sizeof(
License));
126 for (guint i = 0; i < licenses->len; i++) {
129 g_array_append_val(shortLicenses, *
license);
133 GArray* indexes = g_array_new(FALSE, FALSE,
sizeof(GHashTable*));
135 for (
unsigned sPos = 0; sPos <= maxLeadingDiff; sPos++) {
136 GHashTable* index = g_hash_table_new_full(uint32_hash, uint32_equal, free, g_array_free_true);
137 g_array_append_val(indexes, index);
139 for (guint i = 0; i < licenses->len; i++) {
142 uint32_t* key = malloc(
sizeof(uint32_t));
143 *key = getKey(
license->tokens, minAdjacentMatches, sPos);
145 GArray* indexedLicenses = g_hash_table_lookup(index, key);
146 if (!indexedLicenses)
148 indexedLicenses = g_array_new(FALSE, FALSE,
sizeof(
License));
149 g_hash_table_replace(index, key, indexedLicenses);
153 g_array_append_val(indexedLicenses, *
license);
159 result->licenses = licenses;
160 result->shortLicenses = shortLicenses;
161 result->indexes = indexes;
162 result->minAdjacentMatches = minAdjacentMatches;
167 const GArray* getShortLicenseArray(
const Licenses* licenses) {
168 return licenses->shortLicenses;
171 const GArray* getLicenseArrayFor(
const Licenses* licenses,
unsigned searchPos,
const GArray* searchedTokens,
unsigned searchedStart) {
172 const GArray* indexes = licenses->indexes;
174 guint minAdjacentMatches = licenses->minAdjacentMatches;
176 if (indexes->len <= searchPos) {
177 return licenses->licenses;
180 GHashTable* index = g_array_index(indexes, GHashTable*, searchPos);
181 uint32_t key = getKey(searchedTokens, minAdjacentMatches, searchedStart);
182 GArray* result = g_hash_table_lookup(index, &key);
fo_dbManager * dbManager
fo_dbManager object