]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
move str_substitute() to htsstr_substitute()
authorJaroslav Kysela <perex@perex.cz>
Tue, 26 May 2015 19:50:56 +0000 (21:50 +0200)
committerJaroslav Kysela <perex@perex.cz>
Tue, 26 May 2015 19:50:56 +0000 (21:50 +0200)
src/dvr/dvr_rec.c
src/htsstr.c
src/htsstr.h
src/tvheadend.h
src/utils.c

index 78d13295490d0c4c2e6c2978962637a432e55367..94791eb2a9466697c20083b36c96ed08aec2ad93 100644 (file)
@@ -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 */
index 9be4e3a57a11123cdc83a8ab3719d5496396bba9..923bfedbcb32c183bb9258b50ff3fccca45f18c4 100644 (file)
@@ -22,6 +22,7 @@
 #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);
 
@@ -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;
+}
index ff98b15cf7956a70e73a956aaec49e7eb0e2c4be..f943522e78facb4b2ec6e519f82ac987b46ab2d1 100644 (file)
@@ -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__ */
index 6012e0b9ab5796da8cefed22223bb41f22c248c4..7ed2e0c85918f58df29754df3c3971175bb4c8b0 100644 (file)
@@ -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); }
index 40f195b795a284105ee22d559f62e68505a26abc..0e621c71f631d00de8ee962625dc11887824ded3 100644 (file)
@@ -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)
 {