From: Jaroslav Kysela Date: Tue, 26 May 2015 19:50:56 +0000 (+0200) Subject: move str_substitute() to htsstr_substitute() X-Git-Tag: v4.2.1~2454 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22b2ed9a8d2e23cf1ff692f0b6d3d499e92a6dd9;p=thirdparty%2Ftvheadend.git move str_substitute() to htsstr_substitute() --- diff --git a/src/dvr/dvr_rec.c b/src/dvr/dvr_rec.c index 78d132954..94791eb2a 100644 --- a/src/dvr/dvr_rec.c +++ b/src/dvr/dvr_rec.c @@ -298,7 +298,7 @@ static const char *dvr_sub_stop(const char *id, const void *aux) return dvr_do_prefix(id, buf); } -static str_substitute_t dvr_subs_entry[] = { +static htsstr_substitute_t dvr_subs_entry[] = { { .id = "t", .getval = dvr_sub_title }, { .id = " t", .getval = dvr_sub_title }, { .id = "-t", .getval = dvr_sub_title }, @@ -339,7 +339,7 @@ static const char *dvr_sub_strftime(const char *id, const void *aux) return buf; } -static str_substitute_t dvr_subs_time[] = { +static htsstr_substitute_t dvr_subs_time[] = { { .id = "a", .getval = dvr_sub_strftime }, /* The abbreviated name of the day of the week */ { .id = "A", .getval = dvr_sub_strftime }, /* The full name of the day of the week */ { .id = "b", .getval = dvr_sub_strftime }, /* The abbreviated month name */ @@ -409,17 +409,17 @@ static const char *dvr_sub_str(const char *id, const void *aux) return (const char *)aux; } -static str_substitute_t dvr_subs_extension[] = { +static htsstr_substitute_t dvr_subs_extension[] = { { .id = "x", .getval = dvr_sub_str }, { .id = NULL, .getval = NULL } }; -static str_substitute_t dvr_subs_tally[] = { +static htsstr_substitute_t dvr_subs_tally[] = { { .id = "n", .getval = dvr_sub_str }, { .id = NULL, .getval = NULL } }; -static str_substitute_t dvr_subs_postproc_entry[] = { +static htsstr_substitute_t dvr_subs_postproc_entry[] = { { .id = "t", .getval = dvr_sub_title }, { .id = "s", .getval = dvr_sub_subtitle }, { .id = "p", .getval = dvr_sub_episode }, @@ -441,7 +441,7 @@ static const char *dvr_sub_basename(const char *id, const void *aux) return basename(buf); } -static str_substitute_t dvr_subs_postproc_filename[] = { +static htsstr_substitute_t dvr_subs_postproc_filename[] = { { .id = "f", .getval = dvr_sub_str }, { .id = "b", .getval = dvr_sub_basename }, { .id = NULL, .getval = NULL } @@ -527,7 +527,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) fmtstr++; /* Substitute DVR entry formatters */ - str_substitute(fmtstr, path + l, sizeof(path) - l, '$', dvr_subs_entry, de); + htsstr_substitute(fmtstr, path + l, sizeof(path) - l, '$', dvr_subs_entry, de); /* Own directory? */ if (de->de_directory) { @@ -536,7 +536,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) strcpy(filename, dirsep + 1); else strcpy(filename, path + l); - str_substitute(de->de_directory, ptmp, sizeof(ptmp), '$', dvr_subs_entry, de); + htsstr_substitute(de->de_directory, ptmp, sizeof(ptmp), '$', dvr_subs_entry, de); s = ptmp; while (*s == '/') s++; @@ -549,11 +549,11 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) } /* Substitute time formatters */ - str_substitute(path + l, filename, sizeof(filename), '%', dvr_subs_time, &tm); + htsstr_substitute(path + l, filename, sizeof(filename), '%', dvr_subs_time, &tm); /* Substitute extension */ - str_substitute(filename, path + l, sizeof(path) - l, '$', dvr_subs_extension, - muxer_suffix(de->de_chain->prch_muxer, ss) ?: ""); + htsstr_substitute(filename, path + l, sizeof(path) - l, '$', dvr_subs_extension, + muxer_suffix(de->de_chain->prch_muxer, ss) ?: ""); /* Cleanup all directory names */ x = path + l; @@ -585,7 +585,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) } dirsep = path + l; } - str_unescape(path, filename, sizeof(filename)); + htsstr_unescape_to(path, filename, sizeof(filename)); if (makedirs(filename, cfg->dvr_muxcnf.m_directory_permissions, -1, -1) != 0) return -1; j = strlen(filename); @@ -602,7 +602,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) } else { number[0] = '\0'; } - str_substitute(filename + j, ptmp, sizeof(ptmp), '$', dvr_subs_tally, number); + htsstr_substitute(filename + j, ptmp, sizeof(ptmp), '$', dvr_subs_tally, number); s = cleanup_filename(cfg, ptmp); if (s == NULL) return -1; @@ -610,7 +610,7 @@ pvr_generate_filename(dvr_entry_t *de, const streaming_start_t *ss) /* Construct the final filename */ memcpy(path, filename, j); path[j] = '\0'; - str_unescape(s, path + j, sizeof(path) - j); + htsstr_unescape_to(s, path + j, sizeof(path) - j); free(s); if(stat(path, &st) == -1) { @@ -1026,10 +1026,10 @@ dvr_spawn_postproc(dvr_entry_t *de, const char *dvr_postproc) return; /* Substitute DVR entry formatters */ - str_substitute(dvr_postproc, buf1, sizeof(buf1), '%', dvr_subs_postproc_entry, de); + htsstr_substitute(dvr_postproc, buf1, sizeof(buf1), '%', dvr_subs_postproc_entry, de); buf2 = tvh_strdupa(buf1); /* Substitute filename formatters */ - str_substitute(buf2, buf1, sizeof(buf1), '%', dvr_subs_postproc_filename, filename); + htsstr_substitute(buf2, buf1, sizeof(buf1), '%', dvr_subs_postproc_filename, filename); args = htsstr_argsplit(buf1); /* no arguments at all */ diff --git a/src/htsstr.c b/src/htsstr.c index 9be4e3a57..923bfedbc 100644 --- a/src/htsstr.c +++ b/src/htsstr.c @@ -22,6 +22,7 @@ #include #include "htsstr.h" +#define MIN(a,b) ((a) < (b) ? (a) : (b)) static void htsstr_argsplit_add(char ***argv, int *argc, char *s); @@ -63,6 +64,43 @@ htsstr_unescape(char *str) { return str; } +char * +htsstr_unescape_to(const char *src, char *dst, size_t dstlen) +{ + char *res = dst; + + while (*src && dstlen > 0) { + if (*src == '\\') { + if (dstlen < 2) + break; + src++; + if (*src) { + if (*src == 'b') + *dst = '\b'; + else if (*src == 'f') + *dst = '\f'; + else if (*src == 'n') + *dst = '\n'; + else if (*src == 'r') + *dst = '\r'; + else if (*src == 't') + *dst = '\t'; + else + *dst = *src; + src++; dst++; dstlen--; + } + continue; + } else { + *dst = *src; src++; dst++; dstlen--; + } + } + if (dstlen == 0) + *(dst - 1) = '\0'; + else if (dstlen > 0) + *dst = '\0'; + return res; +} + static void htsstr_argsplit_add(char ***argv, int *argc, char *s) { @@ -146,3 +184,63 @@ htsstr_argsplit_free(char **argv) { free(argv); } + +char * +htsstr_substitute(const char *src, char *dst, size_t dstlen, + int first, htsstr_substitute_t *sub, const void *aux) +{ + htsstr_substitute_t *s; + const char *p, *x, *v; + char *res = dst; + size_t l; + + if (!dstlen) + return NULL; + while (*src && dstlen > 0) { + if (*src == '\\') { + if (dstlen < 2) + break; + *dst = '\\'; src++; dst++; dstlen--; + if (*src) + *dst = *src; src++; dst++; dstlen--; + continue; + } + if (first >= 0) { + if (*src != first) { + *dst = *src; src++; dst++; dstlen--; + continue; + } + src++; + } + for (s = sub; s->id; s++) { + for (p = s->id, x = src; *p; p++, x++) + if (*p != *x) + break; + if (*p == '\0') { + src = x; + if ((l = dstlen) > 0) { + v = s->getval(s->id, aux); + strncpy(dst, v, l); + l = MIN(strlen(v), l); + dst += l; + dstlen -= l; + } + break; + } + } + if (!s->id) { + if (first >= 0) { + *dst = first; + } else { + *dst = *src; + src++; + } + dst++; dstlen--; + } + } + if (dstlen == 0) + *(dst - 1) = '\0'; + else if (dstlen > 0) + *dst = '\0'; + return res; +} diff --git a/src/htsstr.h b/src/htsstr.h index ff98b15cf..f943522e7 100644 --- a/src/htsstr.h +++ b/src/htsstr.h @@ -25,8 +25,19 @@ char *hts_strndup(const char *str, size_t len); char *htsstr_unescape(char *str); +char *htsstr_unescape_to(const char *src, char *dst, size_t dstlen); + char **htsstr_argsplit(const char *str); void htsstr_argsplit_free(char **argv); +typedef struct { + const char *id; + const char *(*getval)(const char *id, const void *aux); +} htsstr_substitute_t; + +char * +htsstr_substitute(const char *src, char *dst, size_t dstlen, + int first, htsstr_substitute_t *sub, const void *aux); + #endif /* HTSSTR_H__ */ diff --git a/src/tvheadend.h b/src/tvheadend.h index 6012e0b9a..7ed2e0c85 100644 --- a/src/tvheadend.h +++ b/src/tvheadend.h @@ -758,16 +758,6 @@ char *url_encode(char *str); int mpegts_word_count(const uint8_t *tsb, int len, uint32_t mask); -typedef struct { - const char *id; - const char *(*getval)(const char *id, const void *aux); -} str_substitute_t; - -char *str_substitute(const char *src, char *dst, size_t dstlen, - int first, str_substitute_t *sub, const void *aux); - -char *str_unescape(const char *src, char *dst, size_t dstlen); - int deferred_unlink(const char *filename, const char *rootdir); static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); } diff --git a/src/utils.c b/src/utils.c index 40f195b79..0e621c71f 100644 --- a/src/utils.c +++ b/src/utils.c @@ -645,88 +645,6 @@ mpegts_word_count ( const uint8_t *tsb, int len, uint32_t mask ) return r; } -char *str_substitute(const char *src, char *dst, size_t dstlen, - int first, str_substitute_t *sub, const void *aux) -{ - str_substitute_t *s; - const char *p, *x, *v; - char *res = dst; - size_t l; - - if (!dstlen) - return NULL; - while (*src && dstlen > 0) { - if (*src == '\\') { - if (dstlen < 2) - break; - *dst = '\\'; src++; dst++; dstlen--; - if (*src) - *dst = *src; src++; dst++; dstlen--; - continue; - } - if (first >= 0) { - if (*src != first) { - *dst = *src; src++; dst++; dstlen--; - continue; - } - src++; - } - for (s = sub; s->id; s++) { - for (p = s->id, x = src; *p; p++, x++) - if (*p != *x) - break; - if (*p == '\0') { - src = x; - if ((l = dstlen) > 0) { - v = s->getval(s->id, aux); - strncpy(dst, v, l); - l = MIN(strlen(v), l); - dst += l; - dstlen -= l; - } - break; - } - } - if (!s->id) { - if (first >= 0) { - *dst = first; - } else { - *dst = *src; - src++; - } - dst++; dstlen--; - } - } - if (dstlen == 0) - *(dst - 1) = '\0'; - else if (dstlen > 0) - *dst = '\0'; - return res; -} - -char *str_unescape(const char *src, char *dst, size_t dstlen) -{ - char *res = dst; - - while (*src && dstlen > 0) { - if (*src == '\\') { - if (dstlen < 2) - break; - *dst = '\\'; src++; dst++; dstlen--; - if (*src) - *dst = *src; src++; dst++; dstlen--; - continue; - } else { - *dst = *src; src++; dst++; dstlen--; - } - } - if (dstlen == 0) - *(dst - 1) = '\0'; - else if (dstlen > 0) - *dst = '\0'; - return res; -} - static void deferred_unlink_cb(void *s, int dearmed) {