All pastes #523854 Raw Edit

Anonymous

public text v1 · immutable
#523854 ·published 2007-05-30 22:35 UTC
rendered paste body
BOOL niche_read_index ()
{
	FILE  *file               = NULL;
	Niche *niche;
	Niche *parent;
	BOOL   retval             = TRUE;
	char  *filename           = STRDUP (gift_conf ("niches"));
	char  *name;
	char  *parent_name;
	char   line[128];
	char  *tag;
	int    i, j;
	int    num_dead_keywords;
	int    num_keywords;
	int    num_niches;
	
	if (!(file = file_open (filename, "ra")))
		goto niche_parse_fail;
	
	if (!read_line (line, sizeof (line), file)))
		goto niche_parse_fail;
		
	num_niches = ATOI (line);
	
	for (i = 0; i < num_niches; i++)
	{
		if (!read_line (line, sizeof (line), file))
			goto niche_parse_fail;
			
		name = STRDUP (line);
		
		if (!read_line (line, sizeof (line), file))
		{
			free (name);
			goto niche_parse_fail;
		}
			
		tag   = line;
		niche = niche_new (name, tag);
		free (name);
		
		/* not sure what would cause this ... but just in case */
		if (!niche)
			goto niche_parse_fail;
		
		if (!read_line (line, sizeof (line), file))
			goto niche_parse_fail;
			
		parent_name = line;
		
		/* set a parent for this niche, if the index was written correctly, 
		 * the parent will already have been read because it was written to 
		 * the index first */
		if (STRCMP (parent, NULL_STR) != 0)
		{
			parent = niche_find (parent_name);
			
			/* no niche by given name, should never happen */
			if (!parent)
				goto niche_parse_fail;
			
			niche_add_child (parent, niche);
		}
		
		if (!read_line (line, sizeof (line), file))
			goto niche_parse_fail;
			
		num_keywords = ATOI (line);
		
		/* read in all keywords */
		for (j = 0; j < num_keywords; j++)
		{
			if (!read_line (line, sizeof (line), file))
				goto niche_parse_fail;
				
			niche_add_keyword (niche, line);
		}
		
		num_dead_keywords = ATOI (line);
		
		/* read in all dead keywords */
		for (j = 0; j < num_dead_keywords; j++)
		{
			if (!read_line (line, sizeof (line), file))
				goto niche_parse_fail;
				
			niche_remove_keyword (niche, line);
		}
	}
	
	/* success! skip fail section */
	goto niche_parse_success;
	
/* this is why exceptions were invented kids */
niche_parse_fail:
	GT->dbg (GT, "failed to parse niche index for reading: %s", filename);
	retval = FALSE;
	
niche_parse_success:
	if (file)
		fclose (file);
	
	free (filename);
	
	return retval;
}