123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /*
- approximate_regex - just an execution wrapper of libtre for
- fixok.ut.emphi.ru
- Copyright (C) 2016 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <tre/regex.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- int main ( int argc, char *argv[] )
- {
- if ( argc != 3 ) {
- fprintf ( stderr, "syntax: %s key_list_file key\n", argv[0] );
- return EINVAL;
- }
- const char *const key_list_path = argv[1];
- const char *const key = argv[2];
- FILE *key_list_file = fopen ( key_list_path, "r" );
- if ( key_list_file == NULL ) {
- fprintf ( stderr, "Cannot open file \"%s\" for reading: %s\n", key_list_path, strerror ( errno ) );
- return errno;
- }
- regex_t reg;
- int rc = tre_regcomp ( ®, key, REG_LITERAL );
- if ( rc != 0 ) {
- fprintf ( stderr, "regcomp() error: %i\n", rc );
- return EINVAL;
- }
- regamatch_t match;
- match.nmatch = 0;
- match.pmatch = NULL;
- regaparams_t params;
- params.cost_ins = 1;
- params.cost_del = 1;
- params.cost_subst = 1;
- params.max_cost = 4;
- params.max_ins = 4;
- params.max_del = 4;
- params.max_subst = 4;
- params.max_err = 4;
- char *line = NULL;
- size_t size = 0;
- while ( getline ( &line, &size, key_list_file ) > 0 ) {
- switch ( tre_regaexec ( ®, line, &match, params, 0 ) ) {
- case REG_NOMATCH:
- break;
- case 0: // found
- printf ( "%s", line );
- return 0;
- case REG_ESPACE:
- fprintf ( stderr, "Cannot allocate memory\n" );
- return ENOMEM;
- }
- }
- return 1;
- }
|