Exemple #1
0
/*
To make sure the hash value is not bigger than size, the result of the user 
provided hash function is used modulo size.

To get the correct position in the nodes table, hash is used as an index.

We have to search the linked list to see if data with the same key has beeen 
inserted before, in which case we just replace the data member of the 
hashnode_s struct. 
*/
int hashtbl_insert(HASHTBL *hashtbl, const char *key, void *data) {
	hashnode_s *node;
	hash_size hash = hashtbl->hashfunc(key) % hashtbl->size;

/*	fprintf(stderr, "hashtbl_insert() key = %s, hash = %d, data = %s\n", key, hash, (char*)data);*/

	node = hashtbl->nodes[hash];
	while(node) {
		if(!strcmp(node->key, key)) {
			node->data = data;
			return SUCCESS;
		}
		node = node->next;
	}

	if(!(node = malloc(sizeof(hashnode_s)))) 
	  return FAILURE;
	  
	if(!(node->key = myStrDup(key))) {
		free(node);
		return FAILURE;
	}
	
	node->data = data;
	node->next = hashtbl->nodes[hash];
	hashtbl->nodes[hash] = node;

	return SUCCESS;
}
static void createImageGroupClassificationHeaders(image_session_t *mySession, image_detected_count_t *count)
{
image_detected_count_t *current;
char header[myMAX_HEADER + 1];
const char *rating = "#########+";
uint16_t current_category;

	// modify headers
	if (!ci_http_response_headers(mySession->req))
		ci_http_response_create(mySession->req, 1, 1);
	snprintf(header, myMAX_HEADER, "X-IMAGE-GROUP-CATEGORIES:");
	// Loop through categories
	for(current_category = 0; current_category < num_image_categories; current_category++) {
		current = &count[current_category];
		// add each category to header
		if(current->count)
		{
			char *oldHeader = myStrDup(header);
			snprintf(header, myMAX_HEADER, "%s %s(%.*s)", oldHeader, current->category->name, (current->count > 10 ? 10 : current->count), rating);
			free(oldHeader);
		}
		mySession->featuresDetected += current->count;
	}
	// add IMAGE-CATEGORIES header
	header[myMAX_HEADER] = '\0';
	if(mySession->featuresDetected)
	{
		ci_http_response_add_header(mySession->req, header);
		ci_debug_printf(10, "Added header: %s\n", header);

		// Add other headers
		addReferrerHeaders(mySession->req, mySession->referrer_fhs_classification, mySession->referrer_fnb_classification);
	}
}
Exemple #3
0
/**
 * @brief Sets the Key and Value into the hashtable.
 *
 * @param hashtable A pointer to the hash table.
 * @param key The string that stores the key.
 * @param value The string that stores the value.
 * @return No return value.
 */
int ht_set( HashTable *hashtable, char *key, char *value ) {
	int bin = 0;
	Entry *newpair = NULL;
	Entry *next = NULL;
	Entry *last = NULL;
 
	bin = ht_hash( hashtable, key );

	next = hashtable->table[ bin ];
	while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {
		last = next;
		next = next->next;
	}
 
	/* There's already a pair.  Let's replace that string. */
	if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {
 	


		free( next->value );
		next->value = myStrDup( value );
		next->metadata += 1;
		return HASH_SET_UPDATE;
	/* Nope, could't find it.  Time to grow a pair. */
	} else {

		newpair = ht_newpair( key, value );

		/* Ensure we have not run out of memory */
		if (newpair == NULL)
			return HASH_SET_FAIL;

		/* We're at the start of the linked list in this bin. */
		if( next == hashtable->table[ bin ] ) {
			newpair->next = next;
			hashtable->table[ bin ] = newpair;
		/* We're at the end of the linked list in this bin. */
		} else if ( next == NULL ) {
			last->next = newpair;
	
		/* We're in the middle of the list. */
		} else  {
			newpair->next = next;
			last->next = newpair;
		}

		/* We have successfully inserted */
		return HASH_SET_INSERT;
	}
	return 0; // success
}
Exemple #4
0
/**
 * @brief Creates a pairing of Key and Value.
 *
 * @param key The string that saves the key.
 * @param value The string that saves the value.
 * @return Returns an Entry struct that contains a key and a value.
 */
Entry *ht_newpair( char *key, char *value ) {
	Entry *newpair;
 
 	/* Create a new Entry to store a key and value */
	if( ( newpair = malloc( sizeof( Entry ) ) ) == NULL ) {
		return NULL;
	}
 
 	/* A pointer to a new char* is returned containing a copy of the input string */
	if( ( newpair->key = myStrDup(key ) ) == NULL ) {
		return NULL;
	}
 
 	/* A pointer to a new char* is returned containing a copy of the input string */
	if( ( newpair->value = myStrDup(value ) ) == NULL ) {
		return NULL;
	}
 
 	/*When creating the new data initialize the metadata to */
 	newpair-> metadata = rand();
	newpair->next = NULL;
 
	return newpair;
}
static void createImageClassificationHeaders(image_session_t *mySession)
{
image_detected_t *current;
char header[myMAX_HEADER + 1];
const char *rating = "#########+";
int i;
uint16_t current_category;

	// modify headers
	if (!ci_http_response_headers(mySession->req))
		ci_http_response_create(mySession->req, 1, 1);
	snprintf(header, myMAX_HEADER, "X-IMAGE-CATEGORIES:");
	// Loop through categories
	for(current_category = 0; current_category < num_image_categories; current_category++) {
		current = &mySession->detected[current_category];
		if(current->detected)
		{
			// Loop the number of detected objects
			for(i = 0; i < current->detected->total; i++ )
			{
				// Reset rectangles to original size
				CvRect* r = (CvRect*)cvGetSeqElem( current->detected, i );
				r->x = (int)(r->x * mySession->scale);
				r->y = (int)(r->y * mySession->scale);
				r->height = (int)(r->height * mySession->scale);
				r->width = (int)(r->width * mySession->scale);
			}
			// add each category to header
			if(current->detected->total)
			{
				char *oldHeader = myStrDup(header);
				snprintf(header, myMAX_HEADER, "%s %s(%.*s)", oldHeader, current->category->name, (current->detected->total > 10 ? 10 : current->detected->total), rating);
				free(oldHeader);
			}
			mySession->featuresDetected += current->detected->total;
		}
	}
	// add IMAGE-CATEGORIES header
	header[myMAX_HEADER] = '\0';
	if(mySession->featuresDetected)
	{
		ci_http_response_add_header(mySession->req, header);
		ci_debug_printf(10, "Added header: %s\n", header);

		// Add other headers
		addReferrerHeaders(mySession->req, mySession->referrer_fhs_classification, mySession->referrer_fnb_classification);
	}
}
Exemple #6
0
bool bootlegLessThan (char * predValue, char * hashTableValue){

	char * temp = myStrDup (hashTableValue);
	temp = strtok (temp, "#");

	//char * theHashTableValue =  strtok(hashTableValue, '#');
	//char *theHashTableValue = "1234999999";
	char *p;

	long int pred_number = strtol( predValue, &p , 10 );
	long int ht_number = strtol(  temp, &p , 10 );
	free (temp);
	if ( ht_number < pred_number )
		return true;
	return false;
}
Exemple #7
0
/**
 * @brief Queries the hashtable and updates the array of strings called keysFound
 *
 * @param hashtable A pointer to the hash table.
 * @param operator The operator to determine what component to query.
 * @param predicate	The criteria to search for using the operator.
 * @param keysFound	The array of strings that contains all the keys that match the predicate and operation.
 * @return Returns the number of items found if successful, -1 if otherwise.
 */
int ht_query (HashTable *hashtable, Predicate * predicates, int numPredicates, char ** keysFound, int maxKeysFound){
	int numKeysFound = 0;
	Entry *temp = NULL;
	int x;
	int length = hashtable->size;
	for (x = 0; x < length; x++){ 	
		//fprintf(stderr, "X- ENTER: %d\n", x);
		temp = hashtable->table[ x ];
		while (temp != NULL) {
		    if (entry_query(temp, predicates, numPredicates)){
		    	if (numKeysFound < maxKeysFound){
		    		keysFound[numKeysFound] = myStrDup(temp->key);
		    	}
		    	numKeysFound++;
		    }
		    temp = temp->next;
		}
	}
	//if (numKeysFound == 0)
	//	return ;

	return numKeysFound;
}
Exemple #8
0
bool bootlegGreaterThan ( char * predValue, char * hashTableValue){

	printf ("fails Here \n");

	char * temp = myStrDup (hashTableValue);
	temp = strtok (temp, "#");

	//	int i = 0;
	//char * theHashTableValue =  strtok(hashTableValue, '#');
	//	char *theHashTableValue = "1234999999";
	char *p;

	int pred_number = strtol( predValue, &p, 10 );
	int ht_number = strtol(  temp, &p, 10 );

	free (temp);
	if (ht_number > pred_number){

		return true;
	}

	return false;

}