/** \brief Get the rf_pk for rf_shortname Checks the cache to get the rf_pk for this shortname. If it doesn't exist, add it to both license_ref and the license_ref cache (the hash table). @param pcroot @param rf_shortname @return rf_pk of the matched license or 0 */ FUNCTION long get_rfpk(cacheroot_t *pcroot, char *rf_shortname) { long rf_pk; size_t len; if ((len = strlen(rf_shortname)) == 0) { printf("ERROR! Nomos.c get_rfpk() passed empty name"); return (0); } /* is this in the cache? */ rf_pk = lrcache_lookup(pcroot, rf_shortname); if (rf_pk) return rf_pk; /* shortname was not found, so add it */ /* add to the license_ref table */ rf_pk = add2license_ref(rf_shortname); /* add to the cache */ lrcache_add(pcroot, rf_pk, rf_shortname); return (rf_pk); } /* get_rfpk */
/**************************************************** getLicsInStr Given a string with | separated license names return an integer array of rf_pk's @param PGconn *pgConn Database connection object @param char *nameStr string of lic names eg "bsd | gpl" @param cacheroot_t *pcroot License cache @return an array of rf_pk's that match the names in nameStr if nameStr contains a license name that is not in the license_ref file, then 0 is returned since there is no way to match all the listed licenses. ****************************************************/ FUNCTION int *getLicsInStr(PGconn *pgConn, char *nameStr, cacheroot_t *pcroot) { char *fcnName = "getLicsInStr"; char *delims = "|\n\r "; char *sp; int *pkArray; int *pkArrayHead = 0; int lic_count = 1; int lr_pk; int matchNumb = 0; if (!nameStr) return 0; /* count how many seperators are in nameStr number of licenses is the count +1 */ sp = nameStr; while (*sp) if (*sp++ == *delims) lic_count++; /* we need lic_count+1 int array. This sets the array to the max possible size +1 for null termination */ pkArray = calloc(lic_count+1, sizeof(int)); if (!pkArray) { printf("FATAL: %s.%s.%d Unable to allocate %d int array.\n", __FILE__, fcnName, __LINE__, lic_count+1); return 0; } pkArrayHead = pkArray; /* save head of array */ /* read each line then read each license in the line Comments start with leading # */ while ((sp = strtok(nameStr, delims)) != 0) { /* look up license rf_pk */ lr_pk = lrcache_lookup(pcroot, sp); if (lr_pk) { /* save rf_pk in match_every array */ pkArray[matchNumb++] = lr_pk; } else { /* license not found in license_ref table, so this can never match */ matchNumb = 0; break; } nameStr = 0; // for strtok } if (matchNumb == 0) { free(pkArrayHead); pkArrayHead = 0; } return pkArrayHead; }
/**************************************************** getMatchOnly Read the match only file (bucket type 2) @param PGconn *pgConn Database connection object @param int bucketpool_pk @param char *filename File name of match_only file @return an array of rf_pk's that match the licenses in filename. or 0 if error. ****************************************************/ FUNCTION int *getMatchOnly(PGconn *pgConn, int bucketpool_pk, char *filename, cacheroot_t *pcroot) { char *fcnName = "getMatchOnly"; char *delims = ",\t\n\r"; char *sp; char filepath[256]; char inbuf[256]; int *match_only = 0; int line_count = 0; int lr_pk; int matchNumb = 0; FILE *fin; /* put together complete file path to match_only file */ snprintf(filepath, sizeof(filepath), "%s/bucketpools/%d/%s", PROJECTSTATEDIR, bucketpool_pk, filename); /* open filepath */ fin = fopen(filepath, "r"); if (!fin) { printf("FATAL: %s.%s.%d Failure to open bucket file %s (pool=%d).\nError: %s\n", __FILE__, fcnName, __LINE__, filepath, bucketpool_pk, strerror(errno)); return 0; } /* count lines in file */ while (fgets(inbuf, sizeof(inbuf), fin)) line_count++; /* calloc match_only array as lines+1. This set the array to the max possible size +1 for null termination */ match_only = calloc(line_count+1, sizeof(int)); if (!match_only) { printf("FATAL: %s.%s.%d Unable to allocate %d int array.\n", __FILE__, fcnName, __LINE__, line_count+1); return 0; } /* read each line fgets A match_only file has one license per line, no leading whitespace. Comments start with leading # */ rewind(fin); while (fgets(inbuf, sizeof(inbuf), fin)) { /* input string should only contain 1 token (license name) */ sp = strtok(inbuf, delims); /* comment? */ if ((sp == 0) || (*sp == '#')) continue; /* look up license rf_pk */ lr_pk = lrcache_lookup(pcroot, sp); if (lr_pk) { /* save rf_pk in match_only array */ match_only[matchNumb++] = lr_pk; //printf("MATCH_ONLY license: %s, FOUND\n", sp); } else { //printf("MATCH_ONLY license: %s, NOT FOUND in DB - ignored\n", sp); } } return match_only; }