Skip to content
Extraits de code Groupes Projets

Resolve "Drop libpcre dependency"

Fusionnées Kubat a demandé de fusionner drop-pcre vers master
2 fichiers
+ 20
70
Comparer les modifications
  • Côte à côte
  • En ligne
Fichiers
2
+ 20
69
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
#include <lektor/macro.h>
#include <lektor/defines.h>
#include <lektor/mkv.h>
#include <stdbool.h>
#include <stdio.h>
@@ -16,8 +18,6 @@
#include <sys/types.h>
#include <regex.h>
#include <pcre.h>
static const char *METADATA_TEMPLATE =
" <?xml version=\"1.0\" encoding=\"UTF-8\"?> "
" <Tags> "
@@ -141,88 +141,39 @@ error:
static bool
metadata_from_path(char *const mkvfile, struct kara_metadata *meta)
{
pcre *regex = NULL;
pcre_extra *pcre_extra = NULL;
bool sta = false;
const int sub_str_vec_len = 30;
int pcre_error_offset, pcre_exec_ret, sub_str_vec[sub_str_vec_len], i, num;
char num_str[LEKTOR_TAG_MAX];
const char *substr;
char *copy_to;
regex_t regex;
const size_t nmatch = 10;
regmatch_t pmatch[nmatch];
int reti, sta = false;
char msgbuf[LEKTOR_TAG_MAX];
static const char *rgx =
"^\\/(?:.+)\\/(vo|va|amv|cdg|autres|vocaloid)\\/"
"(jp|fr|en|ru|sp|it|ch|latin|multi|undefined)\\/(.+)\\/"
"(.+) - (OP|ED|IS|AMV|VOCA|PV|MV|LIVE)(\\d*) - (.+)\\.mkv$";
const char *pcre_error_str = NULL;
if ((regex = pcre_compile(rgx, 0, &pcre_error_str, &pcre_error_offset, NULL)) == NULL) {
fprintf(stderr, " ! metadata_from_path: failed to compile regex\n");
return false;
}
pcre_extra = pcre_study(regex, 0, &pcre_error_str);
if (pcre_error_str != NULL) {
fprintf(stderr, " ! metadata_from_path: failed to study regex: %s\n", pcre_error_str);
goto error;
}
RETURN_IF(regcomp(&regex, rgx, REG_EXTENDED), "Failed to compile regex", false);
memset(meta, 0, sizeof(struct kara_metadata));
memset(num_str, 0, LEKTOR_TAG_MAX * sizeof(char));
pcre_exec_ret = pcre_exec(regex, pcre_extra, mkvfile, strlen(mkvfile), 0, 0,
sub_str_vec, sub_str_vec_len);
memset(msgbuf, 0, LEKTOR_TAG_MAX * sizeof(char));
if (pcre_exec_ret < 0) {
fprintf(stderr, " ! metadata_from_path: PCRE error\n");
goto error;
}
reti = regexec(&regex, mkvfile, nmatch, pmatch, 0);
if (pcre_exec_ret == 0) {
// To much -> error in our case //
fprintf(stderr, " ! metadata_from_path: To much matches found for file: %s\n", mkvfile);
if (!reti) {
/* Match */
exit(100);
} else if (REG_NOMATCH == reti) {
fprintf(stderr, "No match for: %s\n", mkvfile);
goto error;
} else {
regerror(reti, &regex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Failed to execute regex: %s\n", msgbuf);
goto error;
}
for (i = 1; i < pcre_exec_ret; ++i) {
pcre_get_substring(mkvfile, sub_str_vec, pcre_exec_ret, i, &substr);
if (i == 1)
copy_to = meta->song_type;
else if (i == 2)
copy_to = meta->language;
else if (i == 3)
copy_to = meta->author_name;
else if (i == 4)
copy_to = meta->source_name;
else if (i == 5)
copy_to = meta->category;
else if (i == 6)
copy_to = num_str;
else if (i == 7)
copy_to = meta->song_name;
else {
pcre_free_substring(substr);
goto error;
}
snprintf(copy_to, LEKTOR_TAG_MAX, "%s", substr);
pcre_free_substring(substr);
}
meta->song_number = ((num = atoi(num_str)) <= 0) ? 1 : num;
sta = true;
error:
pcre_free(regex);
if (pcre_extra != NULL) {
#ifdef PCRE_CONFIG_JIT
pcre_free_study(pcre_extra);
#else
pcre_free(pcre_extra);
#endif
}
regfree(&regex);
return sta;
}
Chargement en cours