22 #include "string_operations.h" 25 static char* ignoredLicenseNames[] = {
"Void",
"No_license_found"};
26 static char* ignoredLicenseTexts[] = {
"License by Nomos.",
"License by Ninka."};
30 int ignoredLicenseNamesCount =
sizeof(ignoredLicenseNames)/
sizeof(
char*);
31 for (
int i = 0; i < ignoredLicenseNamesCount; i++) {
32 if (strcmp(license->shortname, ignoredLicenseNames[i]) == 0)
36 int ignoredLicenseTextsCount =
sizeof(ignoredLicenseTexts)/
sizeof(
char*);
37 for (
int i = 0; i < ignoredLicenseTextsCount; i++) {
38 GArray* ignoredTokens = tokenize(ignoredLicenseTexts[i], DELIMITERS);
39 if (tokensEquals(license->tokens, ignoredTokens)) {
40 tokens_free(ignoredTokens);
43 tokens_free(ignoredTokens);
50 GArray* licenses = g_array_new(TRUE, FALSE,
sizeof (
License));
52 for (
int j = 0; j < PQntuples(licensesResult); j++) {
53 long refId = atol(PQgetvalue(licensesResult, j, 0));
54 char* licShortName = PQgetvalue(licensesResult, j, 1);
57 license.refId = refId;
58 license.shortname = g_strdup(licShortName);
60 char* licenseText = getLicenseTextForLicenseRefId(dbManager, refId);
61 GArray* licenseTokens = tokenize(licenseText, DELIMITERS);
64 license.tokens = licenseTokens;
66 if (!isIgnoredLicense(&license))
67 g_array_append_val(licenses, license);
69 tokens_free(license.tokens);
70 g_free(license.shortname);
74 return buildLicenseIndexes(licenses, minAdjacentMatches, maxLeadingDiff);
77 void licenses_free(
Licenses* licenses) {
79 GArray* licenseArray = licenses->licenses;
80 for (guint i = 0; i < licenseArray->len; i++) {
81 License* license = license_index(licenseArray, i);
82 tokens_free(license->tokens);
83 if (license->shortname) {
84 g_free(license->shortname);
88 g_array_free(licenseArray, TRUE);
90 g_array_free(licenses->shortLicenses, TRUE);
92 GArray* indexes = licenses->indexes;
93 for (guint i = 0; i < indexes->len; i++) {
94 GHashTable* index = g_array_index(indexes, GHashTable*, i);
95 g_hash_table_unref(index);
97 g_array_free(indexes, TRUE);
103 guint uint32_hash (gconstpointer v) {
104 uint32_t u = *(uint32_t*)v;
108 gboolean uint32_equal (gconstpointer v1, gconstpointer v2) {
109 uint32_t u1 = *(uint32_t*)v1;
110 uint32_t u2 = *(uint32_t*)v2;
115 static void g_array_free_true(
void* ptr) {
116 g_array_free(ptr, TRUE);
119 uint32_t getKey(
const GArray* tokens,
unsigned minAdjacentMatches,
unsigned searchedStart) {
121 for (guint i = 0; (i < minAdjacentMatches) && (i+searchedStart < tokens->len); i++)
123 Token* nToken = tokens_index(tokens, i+searchedStart);
124 result = (result << 1) + nToken->hashedContent;
130 Licenses* buildLicenseIndexes(GArray* licenses,
unsigned minAdjacentMatches,
unsigned maxLeadingDiff) {
135 #define is_short(license) ( (license)->tokens->len <= minAdjacentMatches ) 136 GArray* shortLicenses = g_array_new(FALSE, FALSE,
sizeof(
License));
137 for (guint i = 0; i < licenses->len; i++) {
138 License* license = license_index(licenses, i);
139 if (is_short(license)) {
140 g_array_append_val(shortLicenses, *license);
144 GArray* indexes = g_array_new(FALSE, FALSE,
sizeof(GHashTable*));
146 for (
unsigned sPos = 0; sPos <= maxLeadingDiff; sPos++) {
147 GHashTable* index = g_hash_table_new_full(uint32_hash, uint32_equal, free, g_array_free_true);
148 g_array_append_val(indexes, index);
150 for (guint i = 0; i < licenses->len; i++) {
151 License* license = license_index(licenses, i);
152 if (!is_short(license)) {
153 uint32_t* key = malloc(
sizeof(uint32_t));
154 *key = getKey(license->tokens, minAdjacentMatches, sPos);
156 GArray* indexedLicenses = g_hash_table_lookup(index, key);
157 if (!indexedLicenses)
159 indexedLicenses = g_array_new(FALSE, FALSE,
sizeof(
License));
160 g_hash_table_replace(index, key, indexedLicenses);
164 g_array_append_val(indexedLicenses, *license);
170 result->licenses = licenses;
171 result->shortLicenses = shortLicenses;
172 result->indexes = indexes;
173 result->minAdjacentMatches = minAdjacentMatches;
178 const GArray* getShortLicenseArray(
const Licenses* licenses) {
179 return licenses->shortLicenses;
182 const GArray* getLicenseArrayFor(
const Licenses* licenses,
unsigned searchPos,
const GArray* searchedTokens,
unsigned searchedStart) {
183 const GArray* indexes = licenses->indexes;
185 guint minAdjacentMatches = licenses->minAdjacentMatches;
187 if (indexes->len <= searchPos) {
188 return licenses->licenses;
191 GHashTable* index = g_array_index(indexes, GHashTable*, searchPos);
192 uint32_t key = getKey(searchedTokens, minAdjacentMatches, searchedStart);
193 GArray* result = g_hash_table_lookup(index, &key);
fo_dbManager * dbManager
fo_dbManager object