コード例 #1
0
ファイル: testRegexp.c プロジェクト: ArielleBassanelli/gempak
static void
testRegexpFile(const char *filename) {
    xmlRegexpPtr comp = NULL;
    FILE *input;
    char expression[5000];
    int len;

    input = fopen(filename, "r");
    if (input == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot open %s for reading\n", filename);
	return;
    }
    while (fgets(expression, 4500, input) != NULL) {
	len = strlen(expression);
	len--;
	while ((len >= 0) && 
	       ((expression[len] == '\n') || (expression[len] == '\t') ||
		(expression[len] == '\r') || (expression[len] == ' '))) len--;
	expression[len + 1] = 0;      
	if (len >= 0) {
	    if (expression[0] == '#')
		continue;
	    if ((expression[0] == '=') && (expression[1] == '>')) {
		char *pattern = &expression[2];

		if (comp != NULL) {
		    xmlRegFreeRegexp(comp);
		    comp = NULL;
		}
		printf("Regexp: %s\n", pattern) ;
		comp = xmlRegexpCompile((const xmlChar *) pattern);
		if (comp == NULL) {
		    printf("   failed to compile\n");
		    break;
		}
	    } else if (comp == NULL) {
		printf("Regexp: %s\n", expression) ;
		comp = xmlRegexpCompile((const xmlChar *) expression);
		if (comp == NULL) {
		    printf("   failed to compile\n");
		    break;
		}
	    } else if (comp != NULL) {
		testRegexp(comp, expression);
	    }
	}
    }
    fclose(input);
    if (comp != NULL)
	xmlRegFreeRegexp(comp);
}
コード例 #2
0
ファイル: regexp.cpp プロジェクト: AdrienCourtois/Domoleaf
bool
checkRegExp (const char *regexp)
{
  xmlRegexpPtr x = xmlRegexpCompile ((const xmlChar *) regexp);
  if (!x)
    return 0;
  xmlRegFreeRegexp (x);
  return 1;
}
コード例 #3
0
ファイル: regexp.cpp プロジェクト: AdrienCourtois/Domoleaf
bool
validateString (const char *regexp, const char *str)
{
  xmlRegexpPtr x = xmlRegexpCompile ((const xmlChar *) regexp);
  if (!x)
    return 0;
  int r = xmlRegexpExec (x, (const xmlChar *) str);
  xmlRegFreeRegexp (x);
  return r == 1;
}
コード例 #4
0
ファイル: xsdre.c プロジェクト: tail-f-systems/xsdre
static ERL_NIF_TERM run(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    struct regexp *rp = NULL;
    struct regexp r;
    ErlNifBinary strbin;
    ERL_NIF_TERM ret;

    if (!enif_get_resource(env, argv[1], regexp_type, (void **)&rp)) {
        ErlNifBinary patternbin;
        if (enif_inspect_iolist_as_binary(env, argv[1], &patternbin)) {
            char patternstr[patternbin.size + 1];
            xmlRegexpPtr xre;
            patternstr[patternbin.size] = 0;
            memcpy(patternstr, patternbin.data, patternbin.size);
            if ((xre = xmlRegexpCompile((xmlChar *)patternstr)) != NULL) {
                r.xre = xre;
                r.string = NULL;
                rp = &r;
            } else {
                return last_error(env, "Bad Pattern");
            }
        } else {
            return enif_make_badarg(env);
        }
    }

    if (enif_inspect_iolist_as_binary(env, argv[0], &strbin)) {
        char string[strbin.size + 1];
        string[strbin.size] = 0;
        memcpy(string, strbin.data, strbin.size);
        switch (xmlRegexpExec(rp->xre, (xmlChar *)string)) {
        case 1:
            /* FIXME NYI */
            ret = enif_make_tuple2(env, am_match(env),
                                   enif_make_list(env, 0));
            break;
        case 0:
            ret = am_nomatch(env);
            break;
        default:
            ret = last_error(env, NULL);
        }
    } else {
        ret = enif_make_badarg(env);
    }

    if (rp == &r) {
        xmlRegFreeRegexp(r.xre);
    }

    return ret;
}
コード例 #5
0
static void
testRegexpFile(const char *filename) {
    FILE *input;
    char expr[5000];
    int len;
    int ret;
    int i;
    xmlAutomataPtr am;
    xmlAutomataStatePtr states[1000];
    xmlRegexpPtr regexp = NULL;
    xmlRegExecCtxtPtr exec = NULL;

    for (i = 0;i<1000;i++)
	states[i] = NULL;

    input = fopen(filename, "r");
    if (input == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot open %s for reading\n", filename);
	return;
    }

    am = xmlNewAutomata();
    if (am == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot create automata\n");
	fclose(input);
	return;
    }
    states[0] = xmlAutomataGetInitState(am);
    if (states[0] == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Cannot get start state\n");
	xmlFreeAutomata(am);
	fclose(input);
	return;
    }
    ret = 0;

    while (fgets(expr, 4500, input) != NULL) {
	if (expr[0] == '#')
	    continue;
	len = strlen(expr);
	len--;
	while ((len >= 0) && 
	       ((expr[len] == '\n') || (expr[len] == '\t') ||
		(expr[len] == '\r') || (expr[len] == ' '))) len--;
	expr[len + 1] = 0;      
	if (len >= 0) {
	    if ((am != NULL) && (expr[0] == 't') && (expr[1] == ' ')) {
		char *ptr = &expr[2];
		int from, to;

		from = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		if (states[from] == NULL)
		    states[from] = xmlAutomataNewState(am);
		ptr++;
		to = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		if (states[to] == NULL)
		    states[to] = xmlAutomataNewState(am);
		ptr++;
		xmlAutomataNewTransition(am, states[from], states[to],
			                 BAD_CAST ptr, NULL);
	    } else if ((am != NULL) && (expr[0] == 'e') && (expr[1] == ' ')) {
		char *ptr = &expr[2];
		int from, to;

		from = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		if (states[from] == NULL)
		    states[from] = xmlAutomataNewState(am);
		ptr++;
		to = scanNumber(&ptr);
		if (states[to] == NULL)
		    states[to] = xmlAutomataNewState(am);
		xmlAutomataNewEpsilon(am, states[from], states[to]);
	    } else if ((am != NULL) && (expr[0] == 'f') && (expr[1] == ' ')) {
		char *ptr = &expr[2];
		int state;

		state = scanNumber(&ptr);
		if (states[state] == NULL) {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad state %d : %s\n", state, expr);
		    break;
		}
		xmlAutomataSetFinalState(am, states[state]);
	    } else if ((am != NULL) && (expr[0] == 'c') && (expr[1] == ' ')) {
		char *ptr = &expr[2];
		int from, to;
		int min, max;

		from = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		if (states[from] == NULL)
		    states[from] = xmlAutomataNewState(am);
		ptr++;
		to = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		if (states[to] == NULL)
		    states[to] = xmlAutomataNewState(am);
		ptr++;
		min = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		ptr++;
		max = scanNumber(&ptr);
		if (*ptr != ' ') {
		    xmlGenericError(xmlGenericErrorContext,
			    "Bad line %s\n", expr);
		    break;
		}
		ptr++;
		xmlAutomataNewCountTrans(am, states[from], states[to],
			                 BAD_CAST ptr, min, max, NULL);
	    } else if ((am != NULL) && (expr[0] == '-') && (expr[1] == '-')) {
		
		regexp = xmlAutomataCompile(am);
		xmlFreeAutomata(am);
		am = NULL;
		if (regexp == NULL) {
		    xmlGenericError(xmlGenericErrorContext,
			    "Failed to compile the automata");
		    break;
		}
	    } else if ((expr[0] == '=') && (expr[1] == '>')) {
		if (regexp == NULL) {
		    printf("=> failed not compiled\n");
		} else {
		    if (exec == NULL)
			exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
		    if (ret == 0) {
			ret = xmlRegExecPushString(exec, NULL, NULL);
		    }
		    if (ret == 1)
			printf("=> Passed\n");
		    else if ((ret == 0) || (ret == -1))
			printf("=> Failed\n");
		    else if (ret < 0)
			printf("=> Error\n");
		    xmlRegFreeExecCtxt(exec);
		    exec = NULL;
		}
		ret = 0;
	    } else if (regexp != NULL) {
		if (exec == NULL)
		    exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
		ret = xmlRegExecPushString(exec, BAD_CAST expr, NULL);
	    } else {
		xmlGenericError(xmlGenericErrorContext,
			"Unexpected line %s\n", expr);
	    }
	}
    }
    fclose(input);
    if (regexp != NULL)
	xmlRegFreeRegexp(regexp);
    if (exec != NULL)
	xmlRegFreeExecCtxt(exec);
    if (am != NULL)
	xmlFreeAutomata(am);
}
コード例 #6
0
int main(int argc, char **argv) {

    xmlInitMemory();

    if (argc == 1) {
	int ret;
	xmlAutomataPtr am;
	xmlAutomataStatePtr start, cur;
	xmlRegexpPtr regexp;
	xmlRegExecCtxtPtr exec;

	am = xmlNewAutomata();
	start = xmlAutomataGetInitState(am);

	
	cur = xmlAutomataNewTransition(am, start, NULL, BAD_CAST"a", NULL);
	xmlAutomataNewTransition(am, cur, cur, BAD_CAST"b", NULL);
	xmlAutomataNewTransition(am, cur, cur, BAD_CAST"a", NULL);
	cur = xmlAutomataNewCountTrans(am, cur, NULL, BAD_CAST"a", 2, 3, NULL);
	xmlAutomataSetFinalState(am, cur);

	
	regexp = xmlAutomataCompile(am);
	xmlFreeAutomata(am);

	
	xmlRegexpPrint(stdout, regexp);
	exec = xmlRegNewExecCtxt(regexp, NULL, NULL);
	ret = xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"b", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	ret =xmlRegExecPushString(exec, BAD_CAST"a", NULL);
	if (ret == 1)
	    printf("final\n");
	else if (ret < 0)
	    printf("error\n");
	if (ret == 0) {
	    ret = xmlRegExecPushString(exec, NULL, NULL);
	    if (ret == 1)
		printf("final\n");
	    else if (ret < 0)
		printf("error\n");
	}
	xmlRegFreeExecCtxt(exec);

	
	xmlRegFreeRegexp(regexp);
    } else {
	int i;

	for (i = 1;i < argc;i++)
	    testRegexpFile(argv[i]);
    }

    xmlCleanupParser();
    xmlMemoryDump();
    return(0);
}
コード例 #7
0
ファイル: testRegexp.c プロジェクト: ArielleBassanelli/gempak
int main(int argc, char **argv) {
    xmlRegexpPtr comp = NULL;
#ifdef LIBXML_EXPR_ENABLED
    xmlExpNodePtr expr = NULL;
    int use_exp = 0;
    xmlExpCtxtPtr ctxt = NULL;
#endif
    const char *pattern = NULL;
    char *filename = NULL;
    int i;

    xmlInitMemory();

    if (argc <= 1) {
	usage(argv[0]);
	return(1);
    }
    for (i = 1; i < argc ; i++) {
	if (!strcmp(argv[i], "-"))
	    break;

	if (argv[i][0] != '-')
	    continue;
	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug"))) {
	    debug++;
	} else if ((!strcmp(argv[i], "-repeat")) ||
	         (!strcmp(argv[i], "--repeat"))) {
	    repeat++;
#ifdef LIBXML_EXPR_ENABLED
	} else if ((!strcmp(argv[i], "-expr")) ||
	         (!strcmp(argv[i], "--expr"))) {
	    use_exp++;
#endif
	} else if ((!strcmp(argv[i], "-i")) || (!strcmp(argv[i], "-f")) ||
		   (!strcmp(argv[i], "--input")))
	    filename = argv[++i];
        else {
	    fprintf(stderr, "Unknown option %s\n", argv[i]);
	    usage(argv[0]);
	}
    }

#ifdef LIBXML_EXPR_ENABLED
    if (use_exp)
	ctxt = xmlExpNewCtxt(0, NULL);
#endif

    if (filename != NULL) {
#ifdef LIBXML_EXPR_ENABLED
        if (use_exp)
	    runFileTest(ctxt, filename);
	else
#endif
	    testRegexpFile(filename);
    } else {
#ifdef LIBXML_EXPR_ENABLED
        if (use_exp) {
	    for (i = 1; i < argc ; i++) {
		if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
		    if (pattern == NULL) {
			pattern = argv[i];
			printf("Testing expr %s:\n", pattern);
			expr = xmlExpParse(ctxt, pattern);
			if (expr == NULL) {
			    printf("   failed to compile\n");
			    break;
			}
			if (debug) {
			    exprDebug(ctxt, expr);
			}
		    } else {
			testReduce(ctxt, expr, argv[i]);
		    }
		}
	    }
	    if (expr != NULL)
		xmlExpFree(ctxt, expr);
	} else
#endif
        {
	    for (i = 1; i < argc ; i++) {
		if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
		    if (pattern == NULL) {
			pattern = argv[i];
			printf("Testing %s:\n", pattern);
			comp = xmlRegexpCompile((const xmlChar *) pattern);
			if (comp == NULL) {
			    printf("   failed to compile\n");
			    break;
			}
			if (debug)
			    xmlRegexpPrint(stdout, comp);
		    } else {
			testRegexp(comp, argv[i]);
		    }
		}
	    }
	    if (comp != NULL)
		xmlRegFreeRegexp(comp);
        }
    }
#ifdef LIBXML_EXPR_ENABLED
    if (ctxt != NULL) {
	printf("Ops: %d nodes, %d cons\n",
	       xmlExpCtxtNbNodes(ctxt), xmlExpCtxtNbCons(ctxt));
	xmlExpFreeCtxt(ctxt);
    }
#endif
    xmlCleanupParser();
    xmlMemoryDump();
    return(0);
}
コード例 #8
0
ファイル: xsdre.c プロジェクト: tail-f-systems/xsdre
static void destroy_regexp(ErlNifEnv *env, void *obj)
{
    struct regexp *r = obj;
    xmlRegFreeRegexp(r->xre);
    enif_free(r->string);
}