struct treenode *addtree2(struct treenode *node, char *word, int linenumber){

  int condition;

  if(node == NULL){
    //new word arrived so setup new struct and allocate memory
    node = talloc();
    node->word = stringduplicate(word);  //so we use strdup to get memory for string
    //had we not bothered and pointed to the string parameter (word), the memory
    //will be cleaned up after the function call allow the memory to be re-used
    //as it is from the stack
    //alloc gets heap which is there till we remove it
    node->count = 1;
    node->left = node->right = NULL;  //set pointers left & right to NULL
    node->startinglinelist = lalloc();
    node->startinglinelist->linenumbervalue = linenumber;
    node->startinglinelist->next = NULL;  
  } else if((condition = strcmp(word, node->word)) == 0){
    node->count++;
    linenode(node, linenumber);
  }else if(condition < 0)
    node->left = addtree2(node->left, word, linenumber);
  else
    node->right = addtree2(node->right, word, linenumber);

  return node;
}
Exemplo n.º 2
0
		void Parser::parseText(const std::string& text, Color c, FontPtr font)
		{
			//clear old markup
			m_formatting.clear();
			m_view.clear();
			m_root.reset(new TextNode());
			m_root->type = NormalTag;

			size_t len = text.length();
			if(!len) return;

			m_color = c;
			m_font = font;
			m_list = false;

			m_root->len = len;
			m_root->content = 0;

			size_t pos = 0;
			size_t linelen = 0;
			while(pos < len)
			{
				size_t newline = text.find_first_of("\n", pos);
				if(newline == std::string::npos)
				{
					newline = len;
					linelen = len - pos;
				}
				else
					linelen = newline - pos;

				PTextNode linenode(new TextNode());
				linenode->start = pos;
				linenode->len = linelen;
				linenode->content = pos;
				linenode->parent = m_root.get();
				linenode->type = NormalTag;		
				m_root->children.push_back(linenode);

				m_paragraph = true;
				size_t last = pos + linelen;
				pos = parseString(text, pos, linelen, linenode);
				if(pos == last)
					m_list = false;

			}
		}