library/src/utility/BP-StringUtilities.cc
// parses a url to a script using pcre
parses a url to a script using pcre
parses a url to a script using pcre
char * BP_ParseURLToScriptPCRE(char *long_form_url, BP_BOOL require_valid_tld)
{
	if(!long_form_url)
		return NULL;
	char * regexp = NULL;
	if(require_valid_tld)
		regexp = GENERIC_MATCH_SCRIPT_FROM_URL_REQUIRE_TLD;
	else
		regexp = GENERIC_MATCH_SCRIPT_FROM_URL_TLD_NOT_REQUIRED;
	BP_REGEX_MATCH_INFO match_info = {0};
	size_t matched = BP_MatchRegularExpressionPCRE(long_form_url, bpstrlen(long_form_url), regexp, 0, 1, &match_info, true);
	char *ret_string = NULL;
	if(matched == ERR_SUCCESS)
	{
		size_t n = 0;
		for(; n < match_info.pcre_num_matched; n++)
		{
			ret_string = bpstrdup(match_info.preg_matches->matched_string);
			break;
		}
	}
	BP_DestroyRegexpMatchInfo(&match_info);
	if(ret_string)
	{
		char * tmp_path = BP_ParseURLToPathPCRE(long_form_url);
		if(tmp_path)
		{
			size_t path_len = bpstrlen(tmp_path);
			size_t ret_len = bpstrlen(ret_string);
			if(path_len > ret_len)
			{
				bpfree(ret_string);
				ret_string = tmp_path;
			}
			else
			{
				bpfree(tmp_path);
			}
		}
	}
	return ret_string;
}
char * BP_ParseURLToScriptPCRE ( char * long_form_url , BP_BOOL require_valid_tld ) { if ( ! long_form_url ) return NULL ; char * regexp = NULL ; if ( require_valid_tld ) regexp = GENERIC_MATCH_SCRIPT_FROM_URL_REQUIRE_TLD ; else regexp = GENERIC_MATCH_SCRIPT_FROM_URL_TLD_NOT_REQUIRED ; BP_REGEX_MATCH_INFO match_info = { 0 } ; size_t matched = BP_MatchRegularExpressionPCRE ( long_form_url , bpstrlen ( long_form_url ) , regexp , 0 , 1 , & match_info , true ) ; char * ret_string = NULL ; if ( matched == ERR_SUCCESS ) { size_t n = 0 ; for ( ; n < match_info . pcre_num_matched ; n ++ ) { ret_string = bpstrdup ( match_info . preg_matches -> matched_string ) ; break ; } } BP_DestroyRegexpMatchInfo ( & match_info ) ; if ( ret_string ) { char * tmp_path = BP_ParseURLToPathPCRE ( long_form_url ) ; if ( tmp_path ) { size_t path_len = bpstrlen ( tmp_path ) ; size_t ret_len = bpstrlen ( ret_string ) ; if ( path_len > ret_len ) { bpfree ( ret_string ) ; ret_string = tmp_path ; } else { bpfree ( tmp_path ) ; } } } return ret_string ; }
parses a url to a script using pcre
parses a url to a script using pcre
// ensure we have a url to parse // path matching regexp // char * regexp = "[a-zA-Z]{3,}\\:\\/\\/(?:[\\w-]+\\.)+(?:construction|photography|enterprises|contractors|technology|equipment|directory|ventures|plumbing|lighting|holdings|graphics|diamonds|clothing|singles|kitchen|gallery|voyage|travel|tattoo|museum|estate|camera|today|tips|sexy|post|name|mobi|menu|land|jobs|info|guru|coop|bike|asia|arpa|aero|xxx|uno|tel|pro|org|net|mil|int|gov|edu|com|cat|biz|zw|zm|za|yt|ye|ws|wf|vu|vn|vi|vg|ve|vc|va|uz|uy|us|uk|ug|ua|tz|tw|tv|tt|tr|tp|to|tn|tm|tl|tk|tj|th|tg|tf|td|tc|sz|sy|sx|sv|su|st|sr|so|sn|sm|sl|sk|sj|si|sh|sg|se|sd|sc|sb|sa|rw|ru|rs|ro|re|qa|py|pw|pt|ps|pr|pn|pm|pl|pk|ph|pg|pf|pe|pa|om|nz|nu|nr|np|no|nl|ni|ng|nf|ne|nc|na|mz|my|mx|mw|mv|mu|mt|ms|mr|mq|mp|mo|mn|mm|ml|mk|mh|mg|me|md|mc|ma|ly|lv|lu|lt|ls|lr|lk|li|lc|lb|la|kz|ky|kw|kr|kp|kn|km|ki|kh|kg|ke|jp|jo|jm|je|it|is|ir|iq|io|in|im|il|ie|id|hu|ht|hr|hn|hm|hk|gy|gw|gu|gt|gs|gr|gq|gp|gn|gm|gl|gi|gh|gg|gf|ge|gd|gb|ga|fr|fo|fm|fk|fj|fi|eu|et|es|er|eg|ee|ec|dz|do|dm|dk|dj|de|cz|cy|cx|cw|cv|cu|cr|co|cn|cm|cl|ck|ci|ch|cg|cf|cd|cc|ca|bz|by|bw|bv|bt|bs|br|bo|bn|bm|bj|bi|bh|bg|bf|be|bd|bb|ba|az|ax|aw|au|at|as|ar|aq|ao|an|am|al|ai|ag|af|ae|ad|ac)+"; // new regexp (awesome regexp) // char * regexp = "[a-zA-Z]{3,}\\:\\/\\/(?:[a-zA-Z0-9-@\\:]+\\.|[a-zA-Z0-9-@\\:]+\\.{1,})+(?:construction|photography|enterprises|contractors|technology|equipment|directory|ventures|plumbing|lighting|holdings|graphics|diamonds|clothing|singles|kitchen|gallery|voyage|travel|tattoo|museum|estate|camera|today|tips|sexy|post|name|mobi|menu|land|jobs|info|guru|coop|bike|asia|arpa|aero|xxx|uno|tel|pro|org|net|mil|int|gov|edu|com|cat|biz|zw|zm|za|yt|ye|ws|wf|vu|vn|vi|vg|ve|vc|va|uz|uy|us|uk|ug|ua|tz|tw|tv|tt|tr|tp|to|tn|tm|tl|tk|tj|th|tg|tf|td|tc|sz|sy|sx|sv|su|st|sr|so|sn|sm|sl|sk|sj|si|sh|sg|se|sd|sc|sb|sa|rw|ru|rs|ro|re|qa|py|pw|pt|ps|pr|pn|pm|pl|pk|ph|pg|pf|pe|pa|om|nz|nu|nr|np|no|nl|ni|ng|nf|ne|nc|na|mz|my|mx|mw|mv|mu|mt|ms|mr|mq|mp|mo|mn|mm|ml|mk|mh|mg|me|md|mc|ma|ly|lv|lu|lt|ls|lr|lk|li|lc|lb|la|kz|ky|kw|kr|kp|kn|km|ki|kh|kg|ke|jp|jo|jm|je|it|is|ir|iq|io|in|im|il|ie|id|hu|ht|hr|hn|hm|hk|gy|gw|gu|gt|gs|gr|gq|gp|gn|gm|gl|gi|gh|gg|gf|ge|gd|gb|ga|fr|fo|fm|fk|fj|fi|eu|et|es|er|eg|ee|ec|dz|do|dm|dk|dj|de|cz|cy|cx|cw|cv|cu|cr|co|cn|cm|cl|ck|ci|ch|cg|cf|cd|cc|ca|bz|by|bw|bv|bt|bs|br|bo|bn|bm|bj|bi|bh|bg|bf|be|bd|bb|ba|az|ax|aw|au|at|as|ar|aq|ao|an|am|al|ai|ag|af|ae|ad|ac)(?:[:0-9]?)+"; // printf("\n Using script regex?: %s", regexp); // create zeroed out inout structure // get matches // set the return string default to null // set matched // check for the matches // display matched string // destroy the match info // check path len // check ret length // if the ret length is less than the path, we have // a mismatch, so just return the path. // printf("\n Overwriting: %s - %s", ret_string, tmp_path); // set the tmp path // return null for failure