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 },
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 */
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 },
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 }
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) {
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++;
}
/* 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;
}
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);
} 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;
/* 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) {
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 */
#include <string.h>
#include "htsstr.h"
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
static void htsstr_argsplit_add(char ***argv, int *argc, char *s);
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)
{
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;
+}