[vlc-commits] vlmshell: use vlc_memstream instead of sprintf()

Rémi Denis-Courmont git at videolan.org
Fri Feb 24 21:01:51 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Feb 24 21:56:51 2017 +0200| [18ba62cea056dfb63b838021b6d33ab4a402f20b] | committer: Rémi Denis-Courmont

vlmshell: use vlc_memstream instead of sprintf()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=18ba62cea056dfb63b838021b6d33ab4a402f20b
---

 src/input/vlmshell.c | 208 +++++++++++++--------------------------------------
 1 file changed, 53 insertions(+), 155 deletions(-)

diff --git a/src/input/vlmshell.c b/src/input/vlmshell.c
index d30df1f..6c12b3e 100644
--- a/src/input/vlmshell.c
+++ b/src/input/vlmshell.c
@@ -52,6 +52,7 @@
 #include <vlc_charset.h>
 #include <vlc_fs.h>
 #include <vlc_sout.h>
+#include <vlc_memstream.h>
 #include "../stream_output/stream_output.h"
 #include "../libvlc.h"
 
@@ -1593,199 +1594,96 @@ static int Load( vlm_t *vlm, char *file )
 
 static char *Save( vlm_t *vlm )
 {
-    char *save = NULL;
-    char psz_header[] = "\n"
-                        "# VLC media player VLM command batch\n"
-                        "# http://www.videolan.org/vlc/\n\n" ;
-    char *p;
-    int i,j;
-    int i_length = strlen( psz_header );
+    const char *psz_header = "\n"
+                             "# VLC media player VLM command batch\n"
+                             "# http://www.videolan.org/vlc/\n\n" ;
 
-    for( i = 0; i < vlm->i_media; i++ )
-    {
-        vlm_media_sys_t *media = vlm->media[i];
-        vlm_media_t *p_cfg = &media->cfg;
-
-        if( p_cfg->b_vod )
-            i_length += strlen( "new * vod " ) + strlen(p_cfg->psz_name);
-        else
-            i_length += strlen( "new * broadcast " ) + strlen(p_cfg->psz_name);
-
-        if( p_cfg->b_enabled )
-            i_length += strlen( "enabled" );
-        else
-            i_length += strlen( "disabled" );
-
-        if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
-            i_length += strlen( " loop\n" );
-        else
-            i_length += strlen( "\n" );
-
-        for( j = 0; j < p_cfg->i_input; j++ )
-            i_length += strlen( "setup * input \"\"\n" ) + strlen( p_cfg->psz_name ) + strlen( p_cfg->ppsz_input[j] );
-
-        if( p_cfg->psz_output != NULL )
-            i_length += strlen( "setup * output \n" ) + strlen(p_cfg->psz_name) + strlen(p_cfg->psz_output);
+    struct vlc_memstream stream;
 
-        for( j = 0; j < p_cfg->i_option; j++ )
-            i_length += strlen("setup * option \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->ppsz_option[j]);
-
-        if( p_cfg->b_vod && p_cfg->vod.psz_mux )
-            i_length += strlen("setup * mux \n") + strlen(p_cfg->psz_name) + strlen(p_cfg->vod.psz_mux);
-    }
+    vlc_memstream_open( &stream );
+    vlc_memstream_puts( &stream, psz_header );
 
-    for( i = 0; i < vlm->i_schedule; i++ )
-    {
-        vlm_schedule_sys_t *schedule = vlm->schedule[i];
-
-        i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
-
-        if( schedule->b_enabled )
-        {
-            i_length += strlen( "date //-:: enabled\n" ) + 14;
-        }
-        else
-        {
-            i_length += strlen( "date //-:: disabled\n" ) + 14;
-        }
-
-
-        if( schedule->period != 0 )
-        {
-            i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
-                strlen( "period //-::\n" ) + 14;
-        }
-
-        if( schedule->i_repeat >= 0 )
-        {
-            char buffer[12];
-
-            sprintf( buffer, "%d", schedule->i_repeat );
-            i_length += strlen( "setup  repeat \n" ) +
-                strlen( schedule->psz_name ) + strlen( buffer );
-        }
-        else
-        {
-            i_length++;
-        }
-
-        for( j = 0; j < schedule->i_command; j++ )
-        {
-            i_length += strlen( "setup  append \n" ) +
-                strlen( schedule->psz_name ) + strlen( schedule->command[j] );
-        }
-
-    }
-
-    /* Don't forget the '\0' */
-    i_length++;
-    /* now we have the length of save */
-
-    p = save = malloc( i_length );
-    if( !save ) return NULL;
-    *save = '\0';
-
-    p += sprintf( p, "%s", psz_header );
-
-    /* finally we can write in it */
-    for( i = 0; i < vlm->i_media; i++ )
+    for( int i = 0; i < vlm->i_media; i++ )
     {
         vlm_media_sys_t *media = vlm->media[i];
         vlm_media_t *p_cfg = &media->cfg;
 
-        if( p_cfg->b_vod )
-            p += sprintf( p, "new %s vod ", p_cfg->psz_name );
-        else
-            p += sprintf( p, "new %s broadcast ", p_cfg->psz_name );
-
-        if( p_cfg->b_enabled )
-            p += sprintf( p, "enabled" );
-        else
-            p += sprintf( p, "disabled" );
+        vlc_memstream_printf( &stream, "new %s %s %sabled", p_cfg->psz_name,
+                              p_cfg->b_vod ? "vod" : "broadcast",
+                              p_cfg->b_enabled ? "en" : "dis" );
 
         if( !p_cfg->b_vod && p_cfg->broadcast.b_loop )
-            p += sprintf( p, " loop\n" );
-        else
-            p += sprintf( p, "\n" );
+            vlc_memstream_puts( &stream, " loop" );
+        vlc_memstream_putc( &stream, '\n' );
 
-        for( j = 0; j < p_cfg->i_input; j++ )
-            p += sprintf( p, "setup %s input \"%s\"\n", p_cfg->psz_name, p_cfg->ppsz_input[j] );
+        for( int j = 0; j < p_cfg->i_input; j++ )
+            vlc_memstream_printf( &stream, "setup %s input \"%s\"\n",
+                                  p_cfg->psz_name, p_cfg->ppsz_input[j] );
 
         if( p_cfg->psz_output )
-            p += sprintf( p, "setup %s output %s\n", p_cfg->psz_name, p_cfg->psz_output );
+            vlc_memstream_printf( &stream, "setup %s output %s\n",
+                                  p_cfg->psz_name, p_cfg->psz_output );
 
-        for( j = 0; j < p_cfg->i_option; j++ )
-            p += sprintf( p, "setup %s option %s\n", p_cfg->psz_name, p_cfg->ppsz_option[j] );
+        for( int j = 0; j < p_cfg->i_option; j++ )
+            vlc_memstream_printf( &stream, "setup %s option %s\n",
+                                  p_cfg->psz_name, p_cfg->ppsz_option[j] );
 
         if( p_cfg->b_vod && p_cfg->vod.psz_mux )
-            p += sprintf( p, "setup %s mux %s\n", p_cfg->psz_name, p_cfg->vod.psz_mux );
+            vlc_memstream_printf( &stream, "setup %s mux %s\n",
+                                  p_cfg->psz_name, p_cfg->vod.psz_mux );
     }
 
     /* and now, the schedule scripts */
-    for( i = 0; i < vlm->i_schedule; i++ )
+    for( int i = 0; i < vlm->i_schedule; i++ )
     {
         vlm_schedule_sys_t *schedule = vlm->schedule[i];
-        struct tm date;
+        struct tm tm;
 
-        localtime_r( &schedule->date, &date);
-        p += sprintf( p, "new %s schedule ", schedule->psz_name);
-
-        if( schedule->b_enabled )
-        {
-            p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
-                          date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
-                          date.tm_hour, date.tm_min, date.tm_sec );
-        }
-        else
-        {
-            p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
-                          date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
-                          date.tm_hour, date.tm_min, date.tm_sec);
-        }
+        localtime_r( &schedule->date, &tm );
+        vlc_memstream_printf( &stream, "new %s schedule date "
+                              "%d/%d/%d-%d:%d:%d %sabled\n",
+                              schedule->psz_name,
+                              tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+                              tm.tm_hour, tm.tm_min, tm.tm_sec,
+                              schedule->b_enabled ? "en" : "dis" );
 
         if( schedule->period != 0 )
         {
             div_t d;
 
-            p += sprintf( p, "setup %s ", schedule->psz_name );
-
             d = div(schedule->period, 60);
-            date.tm_sec = d.rem;
+            tm.tm_sec = d.rem;
             d = div(d.quot, 60);
-            date.tm_min = d.rem;
+            tm.tm_min = d.rem;
             d = div(d.quot, 24);
-            date.tm_hour = d.rem;
+            tm.tm_hour = d.rem;
             d = div(d.quot, 30);
-            date.tm_mday = d.rem;
+            tm.tm_mday = d.rem;
             /* okay, okay, months are not always 30 days long */
             d = div(d.quot, 12);
-            date.tm_mon = d.rem;
-            date.tm_year = d.quot;
-
-            p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
-                          date.tm_year, date.tm_mon, date.tm_mday,
-                          date.tm_hour, date.tm_min, date.tm_sec);
+            tm.tm_mon = d.rem;
+            tm.tm_year = d.quot;
+
+            vlc_memstream_printf( &stream, "setup %s "
+                                  "period %d/%d/%d-%d:%d:%d\n",
+                                  schedule->psz_name,
+                                  tm.tm_year, tm.tm_mon, tm.tm_mday,
+                                  tm.tm_hour, tm.tm_min, tm.tm_sec);
         }
 
         if( schedule->i_repeat >= 0 )
-        {
-            p += sprintf( p, "setup %s repeat %d\n",
-                          schedule->psz_name, schedule->i_repeat );
-        }
-        else
-        {
-            p += sprintf( p, "\n" );
-        }
-
-        for( j = 0; j < schedule->i_command; j++ )
-        {
-            p += sprintf( p, "setup %s append %s\n",
-                          schedule->psz_name, schedule->command[j] );
-        }
+            vlc_memstream_printf( &stream, "setup %s repeat %d",
+                                  schedule->psz_name, schedule->i_repeat );
+        vlc_memstream_putc( &stream, '\n' );
 
+        for( int j = 0; j < schedule->i_command; j++ )
+            vlc_memstream_printf( &stream, "setup %s append %s\n",
+                                  schedule->psz_name, schedule->command[j] );
     }
 
-    return save;
+    if( vlc_memstream_close( &stream ) )
+        return NULL;
+    return stream.ptr;
 }
 
 #endif /* ENABLE_VLM */



More information about the vlc-commits mailing list