Exemplo n.º 1
0
ret_code amb_init(ambiente_t* ambiente, const char* content, int length)
{
	ret_code ret = ERROR; 
	if (NULL!=content && NULL!=ambiente) {
		const TRexChar *begin, *end, *error;
		TRex* x;		
		CHECK(NULL!=(x=trex_compile(_TREXC("QtdMax=(\\d+)&PdvId=(\\d+)" \
				"&Recibo=(\\d)&Dormir=(\\d)&Cortesias=(\\d)&Credito=(\\d)&"),&error)));
		if (trex_search(x,_TREXC(content),&begin,&end)) {
			int n = trex_getsubexpcount(x);	
			if (n==7) {
				char *qtdMax, *pdvId, *credito;					
				rx_getnext(x, 1, &qtdMax);
				rx_getnext(x, 2, &pdvId);
				rx_getnext(x, 6, &credito);
				CHECK(NULL!=qtdMax);
				CHECK(NULL!=pdvId);
				CHECK(NULL!=credito);
				ambiente->credito = atoi(credito);
				ambiente->qtdMax = atoi(qtdMax);
				ambiente->pdvId = pdvId;
				free(credito);
				free(qtdMax);					
				ret = SUCCESS;
			}
			else {
				ret = ERROR;
			}
		} /* trex_search */
		trex_free(x);
	} /* NULL!=content */
	return ret;
}
/* Match a string against the given regular expression.
 * Returns true on a successful match.
 */
bool d_match(dstr line, const char *regex)
{
   re_cache_t *re;
   TRexMatch match;
   int i;

   re = compile_regex(regex);

   if (!trex_search(re->reg, line, NULL, NULL)) {
      return false;
   }

   trex_getsubexp(re->reg, 0, &match);
   d_assignn(d_before_match, line, match.begin - line);
   d_assign(d_after_match, match.begin + match.len);

   for (i = 0; i < MAX_MATCH; i++) {
      if (trex_getsubexp(re->reg, i, &match)) {
         strncpy(d_submatches[i], match.begin, match.len);
         d_submatches[i][match.len] = '\0';
      }
      else {
         d_submatches[i][0] = '\0';
      }
   }

   return true;
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
	const TRexChar *begin,*end;
	TRexChar sTemp[200];
	const TRexChar *error = NULL;
	TRex *x = trex_compile(_TREXC("(x{1,5})xx"),&error);
	if(x) {
		trex_sprintf(sTemp,_TREXC("xxxxxxx"));
		if(trex_search(x,sTemp,&begin,&end))
		{
			int i,n = trex_getsubexpcount(x);
			TRexMatch match;
			for(i = 0; i < n; i++)
			{
				TRexChar t[200];
				trex_getsubexp(x,i,&match);
				trex_sprintf(t,_TREXC("[%%d]%%.%ds\n"),match.len);
				trex_printf(t,i,match.begin);
			}
			trex_printf(_TREXC("match! %d sub matches\n"),trex_getsubexpcount(x));
		}
		else {
			trex_printf(_TREXC("no match!\n"));
		}
		trex_free(x);
	}
	else {
		trex_printf(_TREXC("compilation error [%s]!\n"),error?error:_TREXC("undefined"));
	}
	return 0;
}
Exemplo n.º 4
0
Keywords parse_c(const ParserSettings *ps, const char *text)
{
	static TrRegex tr_function("(?:([\\w:]+)[*&]*\\s+(?:[*&=]*\\s+)?[*&]*)?([^\\s\\(]+)\\s*(\\([^)]*\\))");
	static TrRegex tr_parameters("(\\$?\\w+|\\.\\.\\.)(\\s*=\\s*[\\\"\\w\\.']+)?\\s*[,)]");
	Keywords keywords;
	
	const TRexChar *begin, *end;
	int chevron = 0;

	// HACK: Duplicate the string so it can be modified and any commas within <...> needs removed to support templates e.g. std::pair<int, double>
	std::string dup(text);

	for (char &c : dup)
	{
		if (c == '<') chevron++;
		else if (c == '>') chevron--;
		else if (c == ',' && chevron > 0) c = ' ';
	}

	if (trex_search(tr_function.regex, dup.c_str(), &begin, &end))
	{
		std::vector<std::string> params;
		std::vector<std::string> function;
		TRexMatch params_match, func_match;
		const TRexChar *cur_params;
		const TRexChar *p_begin, *p_end;

		//trex_getsubexp(tr_function, 1, &return_match);	// not used for now
		trex_getsubexp(tr_function.regex, 2, &func_match);
		trex_getsubexp(tr_function.regex, 3, &params_match);

		keywords.function = std::string(func_match.begin, func_match.len);

		// For each param
		cur_params = params_match.begin;
		while (trex_searchrange(tr_parameters.regex, cur_params, end, &p_begin, &p_end))
		{
			TRexMatch param_match;
			trex_getsubexp(tr_parameters.regex, 1, &param_match);

			// handle "func(void)" by skipping it
			if (strncmp(param_match.begin, "void", 4) != 0)
				keywords.parameters.push_back(std::string(param_match.begin, param_match.len));

			cur_params = p_end;
		}
	}

	return keywords;
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
	TRex *r;
	char *buf, *err = 0, *beg, *end, *q;
	int l = 0;
	if (argc == 1) {
		fprintf(stderr, "Usage: cat in.file | %s <regexp>\n", argv[0]);
		return 0;
	}
	r = trex_compile(argv[1], &err);
	buf = calloc(BUF_SIZE, 1);
	while (fgets(buf, BUF_SIZE - 1, stdin)) {
		++l;
		for (q = buf; *q; ++q); if (q > buf) *(q-1) = 0;
		if (trex_search(r, buf, &beg, &end) == TRex_True)
			printf("%d:%s\n", l, buf);
	}
	free(buf);
	return 0;
}
Exemplo n.º 6
0
Keywords Parse_C(const ParserDefinition *pd, const char *text)
{
	Keywords keywords;
	std::vector<std::string> params;
	std::vector<std::string> function;
	const TRexChar *begin,*end;

	if(trex_search(tr_function, text, &begin, &end))
	{
		TRexMatch params_match, func_match;
		const TRexChar *cur_params;
		const TRexChar *p_begin, *p_end;

		//trex_getsubexp(tr_function, 1, &return_match);	// not used for now
		trex_getsubexp(tr_function, 2, &func_match);
		trex_getsubexp(tr_function, 3, &params_match);

		function.push_back(std::string(func_match.begin, func_match.len));

		// For each param
		cur_params = params_match.begin;
		while(trex_searchrange(tr_parameters, cur_params, end, &p_begin, &p_end))
		{
			TRexMatch param_match;
			trex_getsubexp(tr_parameters, 1, &param_match);

			// handle "func(void)" by skipping it
			if(strncmp(param_match.begin, "void", 4) != 0)
				params.push_back(std::string(param_match.begin, param_match.len));

			cur_params = p_end;
		}
		keywords["$PARAM"] = params;
		keywords["$FUNCTION"] = function;
	}

	return keywords;
}
Exemplo n.º 7
0
void test1()
{
	const char *error = NULL;
	const char *begin,*end;
	char *exp1 = "^([^ ]+) (/[^?# ]*)(\\?[^# ]*)?(#[^ ]*)? HTTP/([^ ]+)$";
	char *exp2 = "^([^ ]+): (.+)$";
	char *exp3 = "([^=]+)(=[^&])?(&([^=]+)(=[^&]))*";


	long long t = counter.Tick();
	TRex *x = trex_compile(exp3, &error);
	printf("compile %lld\n", counter.Tick() - t);

	t = counter.Tick();
	if(trex_search(x,"aaa=bbb&ccc=ddd&eee",&begin,&end))
	{
		printf("execute %lld\n", counter.Tick() - t);
		int i,n = trex_getsubexpcount(x);
		TRexMatch match;
		for(i = 1; i < n; i++)
		{
			t = counter.Tick();
			trex_getsubexp(x,i,&match);
			printf("trex_getsubexp %lld ", counter.Tick() - t);

			for(int j=0;j<match.len;j++)
				putchar(match.begin[j]);
			printf("\n");
		}
		printf("match! %d sub matches\n",trex_getsubexpcount(x));
	}
	else
	{
		printf("execute %lld\n", counter.Tick() - t);
		printf("no match!\n");
	}
}