コード例 #1
0
static opentracing::expected<bool> ExtractSpanContext(
    const PropagationOptions& propagation_options,
    const opentracing::TextMapReader& carrier,
    SpanContextData& span_context_data, KeyCompare key_compare) {
  auto value_maybe =
      LookupKey(carrier, propagation_options.propagation_key, key_compare);
  if (!value_maybe) {
    if (value_maybe.error() == opentracing::key_not_found_error) {
      return false;
    } else {
      return opentracing::make_unexpected(value_maybe.error());
    }
  }
  auto value = *value_maybe;
  std::string base64_decoding;
  try {
    base64_decoding = Base64::decode(value.data(), value.size());
  } catch (const std::bad_alloc&) {
    return opentracing::make_unexpected(
        std::make_error_code(std::errc::not_enough_memory));
  }
  if (base64_decoding.empty()) {
    return opentracing::make_unexpected(
        opentracing::span_context_corrupted_error);
  }
  std::istringstream istream{base64_decoding};
  return ExtractSpanContext(propagation_options, istream, span_context_data);
}
コード例 #2
0
/*
 * UpdateIndexFromFile - Update the given hashtable index from the index data file, by inserting a word node (if a word node is nonexistent)
 *	and add a document node to the word node's list of documents if word node already exists.
 *
 * Returns 1 if succesful.
 */
int UpdateIndexFromFile(char *word, int document_Id, int freq, HashTable *index) {
	// Initialize word_node and word

	WordNode *word_node = calloc(1, sizeof(WordNode));
	word_node->word = calloc((strlen(word) + 1), sizeof(char));
	strcpy(word_node->word, word);
	// Initialize Document Node
	DocumentNode *document = calloc(1, sizeof(DocumentNode));
	document->doc_id = document_Id;
	document->freq = freq;
	document->next = NULL;
	word_node->page = document;

	if (!AddObjectToHashTable(index, word_node, word_node->word)) {
		// word_node 
		HashTableNode *lookup_node = LookupKey(index, word_node->word);
		WordNode *increment_word_node = lookup_node->object;
		// Check that we get a node
		if (!(increment_word_node)) {
			fprintf(stderr, "There was something wrong\n");
			exit(-1);
		}
		// Get the first document node
		DocumentNode *increment_document_node = increment_word_node->page;

		// Looop through next and compare doc_id until we match
		while (increment_document_node->next != NULL) {
			if (increment_document_node->doc_id == document_Id) {
				fprintf(stderr, "There is a duplicate document id it's for word %s\n" , word_node->word);
				free(document);
				free(word_node->word);
				free(word_node);
				exit(-1);
			}
			increment_document_node = (DocumentNode *)increment_document_node->next;
		}

		// Test last document node and increment
		if (increment_document_node->doc_id == document_Id) {
			fprintf(stderr, "There is a duplicate document id for word %s\n", word_node->word);
			free(document);
			free(word_node->word);
			free(word_node);
			exit(-1);
		}
		// If nothing is matched, then finally add the document to next
		increment_document_node->next = document;
		free(word_node->word);
		free(word_node);
	
	} else {

		return 1;
	}
	// If nothing happens then we failed
	return 0;
}
コード例 #3
0
ファイル: IniEx.cpp プロジェクト: cbucher/LocalizeRC
//Removes key and it's value from given section
BOOL CIniEx::RemoveKey(CString Section,CString Key)
{
	int nIndex=LookupSection(&Section);
	if (nIndex==-1) return FALSE;

	int nKeyIndex=LookupKey(nIndex,&Key);
	if (nKeyIndex==-1) return FALSE;

	m_Keys[nIndex]->RemoveAt(nKeyIndex);
	m_Values[nIndex]->RemoveAt(nKeyIndex);
	m_Changed=TRUE;
	return TRUE;
}
コード例 #4
0
ファイル: IniEx.cpp プロジェクト: cbucher/LocalizeRC
//writes Key=value given section
void CIniEx::SetValue(CString Section,CString Key,CString Value)
{
	//file opened?
	ASSERT(!m_FileName.IsEmpty());

	//if given key already existing, overwrite it
	int nIndex=LookupSection(&Section);
	int nKeyIndex;
	if (nIndex==-1)
	{
		//if key not exist grow arrays (if necessary)
		m_Changed=TRUE;
		m_SectionNo++;
		GrowIfNecessary();
		m_Keys[m_SectionNo-1]=new CStringArray;
		m_Values[m_SectionNo-1]=new CStringArray;
		nIndex=m_SectionNo-1;
		m_Sections.SetAtGrow(m_SectionNo-1,Section);
	}

	
	//looking up keys for section
	nKeyIndex=LookupKey(nIndex,&Key);
	
	//if key exist -> overwrite it
	//if not add to end of array
	if (nKeyIndex!=-1) 
	{
		if (CompareStrings(&m_Values[nIndex]->GetAt(nKeyIndex),&Value)!=0)
			m_Changed=TRUE;
		m_Values[nIndex]->SetAt(nKeyIndex,Value);
	}
	else	//if not exist
	{
		m_Changed=TRUE;
		m_Keys[nIndex]->Add(Key);
		m_Values[nIndex]->Add(Value);
	}
}
コード例 #5
0
ファイル: parse.c プロジェクト: joonhyongcho/TinySearchEngine
List *ParseWordsToList(char *parse_string, HashTable *word_index) {
 	// make sure beginning of string is a word
 	while (!isalpha(parse_string[0])) {
		int delete_index = 0; 
		memmove(&parse_string[delete_index], &parse_string[delete_index + 1], strlen(parse_string) - delete_index);
 	}


 	// Get the first word and keep a pointer to the original string 
 	char *copy_parse_string = strdup(parse_string);
 	char *parse_string_pointer = copy_parse_string;
 	char *word = strsep(&copy_parse_string, " ");

 	// Check that the first word is not OR or AND

 	// Make list node that holds the entire group of nodes connected by AND
 	ListNode *AND_group = calloc(1, sizeof(List));
 	AND_group->object = NULL;
 	AND_group->prev = NULL;
 	AND_group->next = NULL;

 	// Allocate list of all groups and set variables to AND_group	
 	List *all_groups = calloc(1, sizeof(List));
 	all_groups->current = AND_group;
 	all_groups->head = AND_group;
 	all_groups->tail = AND_group;

 	// Until end of query
 	while (word != NULL) {
 		// Disregard word if it is AND and move on to the next one
 		if (strcmp(word, "AND") == 0) {
 			word = strsep(&copy_parse_string, " ");
 			continue;
 		}
 		// if (strcmp(word, "\0") == 0) {
 				
 		// }

 		// If the word is OR, then we make a new list, with a new list node and word node for the next word
 		if (strcmp(word, "OR") == 0) {

 			// Check that there are words in the previous list
 			if (all_groups->current->object == NULL) {
 				word = strsep(&copy_parse_string, " ");
 				continue;
 			}

 			// allocate new list
 			ListNode *new_AND_group = calloc(1, sizeof(ListNode));
 			new_AND_group->object = NULL;
 			new_AND_group->prev = all_groups->tail;
 			new_AND_group->next = NULL;

 			// connect previous list to new list and update List tail and current
 			all_groups->current->next = new_AND_group;
 			if (all_groups->tail != all_groups->current) {
 				fprintf(stderr, "Something was wrong with updating tail_pointer\n");
 				exit(-1);
 			}
 			all_groups->tail = new_AND_group;
 			all_groups->current = new_AND_group;

 			AND_group = new_AND_group;

 			// get next word
 			word = strsep(&copy_parse_string, " "); 	

 			continue;
 		}

 		/* Since word is neither OR or AND, we can look it up and made a new node */ 
 		// normalize word		
 		NormalizeWord(word);

 		/* Find the word in the hashtable */
 		HashTableNode *current_hash_node;
 		// if the word does not exist in the hashtable discard entire group until new "OR" 
 		if ((current_hash_node = LookupKey(word_index, word)) == NULL) {

 			// Make new list of list nodes
 			ListNode *new_AND_group = calloc(1, sizeof(List));
 			new_AND_group->object = NULL;
 			new_AND_group->prev = NULL;
 			new_AND_group->next = NULL;

 			// Remove current list 
 			ListNode *list_to_remove = all_groups->current;
 			if (list_to_remove->prev != NULL) {
 				(list_to_remove->prev)->next = new_AND_group;
				new_AND_group->prev = list_to_remove->prev;
 			} else if (list_to_remove->prev == NULL) {
 				all_groups->head = new_AND_group;

 			}
 			// set tail and current for all_groups list
 			all_groups->tail = new_AND_group;
 			all_groups->current = new_AND_group;
 			AND_group = new_AND_group;

 			// Free entire group
 			ListNode *current_list_node = (ListNode *)list_to_remove->object;
 			ListNode *next_list_node;

 			while (current_list_node != NULL) {
 				ListNode *current_list_node_pointer = current_list_node;
 				next_list_node = current_list_node->next;
 				free(current_list_node_pointer);
 				current_list_node = next_list_node;
 			}
 			free(list_to_remove);

 			// Look for next OR
 			while (word != NULL && strcmp(word, "OR") != 0) {
 				word = strsep(&copy_parse_string, " ");
 			}
 			if (word == NULL) {
 				//remove the new lit

 			}
 		} 
 		// Otherwise, if there is a word, there, get the WordNode, create a new one to put in the list,
 		else {

 			// Get the word node from the hashtable
			WordNode *word_node_to_add = (WordNode *)current_hash_node->object;

			// Make a list node to hold new word node
			ListNode *new_AND_node = calloc(1, sizeof(ListNode));
			new_AND_node->next = NULL;
			new_AND_node->object = word_node_to_add;

 			// If the group's first word node has not been set, then make it the first
			if (AND_group->object == NULL) {
				AND_group->object = new_AND_node;
				new_AND_node->prev = NULL;
			} 
			// else we add the list node to the end of the group
			else {
				// get the first list node from the group				
	 			ListNode *current_list_node = AND_group->object;
	 			ListNode *next_list_node;
	 			// Get to the last list node in the group
	 			while (current_list_node->next != NULL) {
	 				next_list_node = current_list_node->next;
	 				current_list_node = next_list_node;
	 			} 	
	 			// Connect the last list node to the new list node	
	 			current_list_node->next = new_AND_node;
	 			new_AND_node->prev = current_list_node;
			}		
 		}

 		word = strsep(&copy_parse_string, " ");
 	}

 	free(parse_string_pointer);
 	
 	// Check the case in which there is nothing that works.
 	ListNode *irregular_list_node = all_groups->head;
 	WordNode *irregular_word_node = (WordNode *)irregular_list_node->object;

 	if (irregular_word_node == NULL) {
 		free(copy_parse_string);
 		free(irregular_list_node);
 		free(all_groups);
 		return NULL;
 	}
 	// Check that the end of all groups is not NULL
 	ListNode *current_group_to_check = all_groups->tail;
 	if (current_group_to_check->object == NULL) {
 		all_groups->tail = current_group_to_check->prev;
 		all_groups->tail->next = NULL;
 		free(current_group_to_check);
 		all_groups->current = all_groups->tail;
 	}

 	return all_groups;
}
コード例 #6
0
/* 
 * UpdateIndex - Takes a word, and document ID, creates hashnode, wordnode, and document node, and tries
 *	adding it to a hashtable, which is populated by wordnodes
 * @word: The word we are going to add into the index data structre
 * @document_Id: the document id of the document where the word was found
 * @index: index data structure that holds words and frequences (hashtable)
 *
 * Returns 1 if succesfully updated, or 0 if not.
 *
 */
int UpdateIndex(char *word, int document_Id, HashTable *index) {
	/* Initialize the WordNode and DocumentNode */

	// Initialize word_node and word
	WordNode *word_node = malloc(1 * sizeof(WordNode));
	if (!word_node)
		return 0;
	word_node->word = strdup(word);

	if (!(word_node->word)) {
		free(word_node);
		return 0;
	}

	// Initialize Document Node
	DocumentNode *document = calloc(1, sizeof(DocumentNode));
	if (!document) {
		free(word_node);
		free(word_node->word);
		return 0;
	}

	document->doc_id = document_Id;
	document->freq = 1;
	document->next = NULL;
	word_node->page = document;

	/* If not added, then we need to look up the word node associated with the word */
	if (!AddObjectToHashTable(index, word_node, word_node->word)) {
		// Since a word_node for the word exists, then retrieve the HashtableNode and get the WordNode
		HashTableNode *lookup_node = (HashTableNode *)LookupKey(index, word_node->word);
		WordNode *increment_word_node = lookup_node->object;
		// Check that we get a node
		if (!(increment_word_node)) {
			fprintf(stderr, "There was something wrong\n");
			exit(-1);
		}

		// Get the first document node
		DocumentNode *increment_document_node = increment_word_node->page;
		// Loop throught all document nodes and compare the document id's
		int increment_flag = 0;
		while (increment_document_node->next != NULL) {
			if (increment_document_node->doc_id == document_Id) {
				// Increment document node frequency and add one to the increment flag
				(increment_document_node->freq)++;
				increment_flag++;
				// Free document, word, and wordnode
				free(document);
				free(word_node->word);
				free(word_node);
				return 1;
			}
			increment_document_node = (DocumentNode *)increment_document_node->next;
		}
		// Test last document node id and incremement if the id's match
		if (increment_document_node->doc_id == document_Id) {
			// Increment document node frequency and add one to the increment flag
			(increment_document_node->freq)++;
			increment_flag++;
			// Free document, word, and wordnode
			free(document);
			free(word_node->word);
			free(word_node);
			return 1;
		}

		if (increment_flag == 0) {
			increment_document_node->next = document;
			free(word_node->word);
			free(word_node);
			return 1; 
		}
	}
	// If there is no WordNode of word in HashTable, we have just added it and we need to populate fields correctly
	else {
		return 1;
	}

	// If nothing happens then we failed
	return 0;
}
コード例 #7
0
ファイル: dim.c プロジェクト: gitpan/Text-AsciiTeX
Tdim
dim(char *txt, struct Tgraph * graph)
{
	/* a linewidth mechanism were cool, i.e. automatic braking of the line */
	/* baceline should jump current y down, x should be the maximum x of all lines */
	/* a flag for linebreak should be placed, containing the y jump size */
	/* so that the draw routines know when to add to y and reset x zo 0 */
	int             i;
	int             len = strlen(txt);	/* length of text passed
						 * to parse */
	Tdim            our;	/* the dimensions of our current object */
	char           *gpos;	/* points to the tree node's text */
	char *end;
	PRSDEF          K;	/* keynumber, result from the
				 * keywordlookup */
	our.x = 0;
	our.y = 1;
	our.baseline = 0;
	graph->children = 0;	/* at the beginning the tree doesn't have
				 * children. We must first find them */
	graph->txt = (char *) malloc(len + 1);	/* allocating the same
						 * length is OK. Special
						 * characters in output
						 * are 2 chars
						 * long--shorter than in
						 * the input */
	gpos = graph->txt;	/* we setup now this pointer */
	*gpos = 0;
	if (*(end=findLineEnd(txt))!='\0')
	{
		/* the current level contains one or more line ends */
		/* the current level will become aan array of lines */
		int nlines=0;
		char * start=txt;
		char          **lines = (char **) malloc(sizeof(char *));
		Tdim            out;
		if (SYNTAX_ERR_FLAG==S_ERR)
			return out;
		*gpos = 1;		/* See parsedef.h for the keyword
				 * definitions */
		gpos++;
		*gpos = (char) ARRAY;
		gpos++;
		*gpos = 0;
		newChild(graph);
		graph->down[graph->children - 1]->options = malloc((2)*sizeof(char));
		graph->down[graph->children - 1]->options[0] = 'c'; /* default col alignment */
		graph->down[graph->children - 1]->options[1] = '\0'; /* default col alignment */
		/* count how many lines we have */
		while (1)
		{
			lines =(char **) realloc(lines,(nlines + 1) * (sizeof(char *)));
			lines[nlines] = (char *) malloc(end - start + 1);
			strncpy(lines[nlines], start, end - start);
			lines[nlines][end - start] = '\0';	/* terminate the string */
			nlines++;
			if (*end=='\0')
				break;
			start=end+1;
			end=findLineEnd(start);
		}
		/* fill the array with the lines */

#define Array (graph->down[graph->children-1])
		Array->array = malloc(sizeof(Tarray));
		Array->array->rows = nlines;
		Array->array->cols = 1;
		Array->array->rowy = (int *) calloc(nlines, sizeof(int));
		Array->array->colx = (int *) calloc(1, sizeof(int));
		for (i = 0; i < nlines; i++)
		{
			out = dim(lines[i], newChild(Array));
			if (out.x > Array->array->colx[0])
				Array->array->colx[0] = out.x;
			if (out.y > Array->array->rowy[i])
				Array->array->rowy[i] = out.y;
			free(lines[i]);
		}
		free(lines);
		Array->dim.x = 0;
		Array->dim.x += Array->array->colx[0];
		Array->dim.y = 0;
		for (i = 0; i < nlines; i++)
			Array->dim.y += Array->array->rowy[i];

		Array->dim.y += Array->array->rows - 1;
		Array->dim.x += Array->array->cols - 1;
	
		Array->dim.baseline = Array->dim.y / 2;
	
		our.x += Array->dim.x;
		if (our.baseline < Array->dim.baseline)
		{	
			our.y += Array->dim.baseline - our.baseline;
			our.baseline = Array->dim.baseline;
		}
		if (our.y < Array->dim.y)
			our.y = Array->dim.y;
#undef Array
		graph->dim = our;
		return our;
	}
	for (i = 0; i < len; i++)
	{
		if(SYNTAX_ERR_FLAG==S_ERR)
			return our;
		if ((txt[i] != '\\') && (txt[i] != '_') && (txt[i] != '^'))
		{
			our.x++;
			*gpos = txt[i];
			gpos++;
			*gpos = 0;
		} else
		{
			K = LookupKey(txt + i, Keys);
			switch (K)
			{
			case SUPER:
				i += dimSuperscript(txt + i, &gpos, &our,
						    graph);
				break;
			case SUB:
				i += dimSubscript(txt + i, &gpos, &our,
						  graph);
				break;
			case FRAC:
				i += dimFrac(txt + i, &gpos, &our, graph);
				break;
			case SQRT:
				i += dimSqrt(txt + i, &gpos, &our, graph);
				break;
			case OVERLINE:
				i += dimOverl(txt + i, &gpos, &our, graph);
				break;
			case UNDERLINE:
				i += dimUnderl(txt + i, &gpos, &our,
					       graph);
				break;
			case LIMIT:
				i += dimLimit(txt + i, &gpos, &our, graph);
				break;
			case BRACES:
				i += dimBrace(txt + i, &gpos, &our, graph);
				break;
			case ARRAY:
				i += dimArray(txt + i, &gpos, &our, graph);
				break;
			case TO:
				i += dimTo(txt + i, &gpos, &our, graph);
				break;
			case LEADSTO:
				i += dimLeadsto(txt + i, &gpos, &our,
						graph);
				break;
			case SUM:
				i += dimSum(txt + i, &gpos, &our, graph);
				break;
			case PROD:
				i += dimProd(txt + i, &gpos, &our, graph);
				break;
			case INT:
				i += dimInt(txt + i, &gpos, &our, graph);
				break;
			case OINT:
				i += dimOint(txt + i, &gpos, &our, graph);
				break;
			case INFTY:
				strcat(gpos, "oo");
				gpos += 2;
				our.x += 2;
				i += 5;
				break;
			case RCEIL:
				i += dimRceil(txt + i, &gpos, &our, graph);
				break;
			case LCEIL:
				i += dimLceil(txt + i, &gpos, &our, graph);
				break;
			case RFLOOR:
				i += dimRfloor(txt + i, &gpos, &our,
					       graph);
				break;
			case LFLOOR:
				i += dimLfloor(txt + i, &gpos, &our,
					       graph);
				break;
			case ESCAPE:
				i++;
				our.x++;
				*gpos = txt[i];
				gpos++;
				*gpos = 0;
				break;
			case ERR:
			default:
				fprintf(stderr,
					"I screwed up in dim, this should never happen!\n");
				exit(1);
				break;
			}
		}
	}
	graph->dim = our;
	return our;
}