library/src/blockclass/subclasses/BP_ConfigParser.cc
//! parses a config from a file
parses a config from a file
parses a config from a file
bool BP_ConfigParser::ParseConfigFromFile(char *file)
{
	Block * file_data = new Block();
	if(file_data->FileGetContents(file) != ERR_SUCCESS)
	{
		printf("\n [+] Cannot read config contents.");
		return false;
	}
	if(file_data->got_contents_size == 0)
		return false;
	BP_StringUtils * str_utils = new BP_StringUtils(file_data->got_contents);
	str_utils->Explode("[\n]");
	P_BP_CONFIG_ITEM config_items = NULL;
	size_t config_item_counter    = 0;
	size_t i = 0;
	for(;
		i < str_utils->exploded_count;
		i++
		)
	{
		size_t x = 0;
		for(;
			x < this->token_count;
			x++
			)
		{
			size_t token_len = bpstrlen(this->tokens[x]);
			size_t line_len  = bpstrlen(str_utils->exploded[i]);
			if(line_len < token_len)
				continue;
			if(
					memcmp(str_utils->exploded[i], this->tokens[x], token_len) == 0
				)
			{
				size_t val_len = line_len - token_len;
				if(val_len == 0)
					continue;
				if( strnlen(&str_utils->exploded[i][token_len-1], 10) == 1)
				{
					continue;
				}
				config_item_counter++;
				config_items = (P_BP_CONFIG_ITEM) bprealloc(config_items, sizeof(BP_CONFIG_ITEM) * config_item_counter);
				config_items[config_item_counter-1].config_item_data = bpstrdup((char *) &str_utils->exploded[i][token_len]);
				config_items[config_item_counter-1].token_used       = bpstrndup((char *) this->tokens[x], token_len);
			}
		}
	}
	this->config_items       = config_items;
	this->config_items_count = config_item_counter;
	if(str_utils)
		delete str_utils;
	if(file_data)
		delete file_data;
	return true;
}
bool BP_ConfigParser :: ParseConfigFromFile ( char * file ) { Block * file_data = new Block ( ) ; if ( file_data -> FileGetContents ( file ) != ERR_SUCCESS ) { printf ( " \n " ) ; return false ; } if ( file_data -> got_contents_size == 0 ) return false ; BP_StringUtils * str_utils = new BP_StringUtils ( file_data -> got_contents ) ; str_utils -> Explode ( " \n " ) ; P_BP_CONFIG_ITEM config_items = NULL ; size_t config_item_counter = 0 ; size_t i = 0 ; for ( ; i < str_utils -> exploded_count ; i ++ ) { size_t x = 0 ; for ( ; x < this -> token_count ; x ++ ) { size_t token_len = bpstrlen ( this -> tokens [ x ] ) ; size_t line_len = bpstrlen ( str_utils -> exploded [ i ] ) ; if ( line_len < token_len ) continue ; if ( memcmp ( str_utils -> exploded [ i ] , this -> tokens [ x ] , token_len ) == 0 ) { size_t val_len = line_len - token_len ; if ( val_len == 0 ) continue ; if ( strnlen ( & str_utils -> exploded [ i ] [ token_len - 1 ] , 10 ) == 1 ) { continue ; } config_item_counter ++ ; config_items = ( P_BP_CONFIG_ITEM ) bprealloc ( config_items , sizeof ( BP_CONFIG_ITEM ) * config_item_counter ) ; config_items [ config_item_counter - 1 ] . config_item_data = bpstrdup ( ( char * ) & str_utils -> exploded [ i ] [ token_len ] ) ; config_items [ config_item_counter - 1 ] . token_used = bpstrndup ( ( char * ) this -> tokens [ x ] , token_len ) ; } } } this -> config_items = config_items ; this -> config_items_count = config_item_counter ; if ( str_utils ) delete str_utils ; if ( file_data ) delete file_data ; return true ; }
parses a config from a file
parses a config from a file
// create block and get contents // check for file read data // create string util object from data and explode along newline // array of config items and the counter indicating the stack size // now walk exploded strings and match tokens // now walk match tokens // check to make sure the string is long enough for the token // now check to match token in string; any line which is not a token is omitted. // calculate the difference in the line // cant have a value length of 0 // check string length to make sure we have an item (len == 1 means its only a newline) // add config item // set config item information before return // delete the string utils // delete the file data // return indicating success