[PATCH] Export mp_basename in a function

Clément Bœsch ubitux at gmail.com
Thu Nov 11 17:30:36 CET 2010


---
 gui/interface.c   |    2 --
 libmenu/menu_pt.c |   17 +++++++++++------
 mplayer.c         |   11 ++++-------
 path.c            |   13 +++++++++++++
 path.h            |    1 +
 5 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/gui/interface.c b/gui/interface.c
index a03281e..a83aaa2 100644
--- a/gui/interface.c
+++ b/gui/interface.c
@@ -1135,8 +1135,6 @@ void * gtkSet( int cmd,float fparam, void * vparam )
  return NULL;
 }
 
-#define mp_basename(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
-
 #include "playtree.h"
 
 //This function adds/inserts one file into the gui playlist
diff --git a/libmenu/menu_pt.c b/libmenu/menu_pt.c
index 80d9cf7..984db1a 100644
--- a/libmenu/menu_pt.c
+++ b/libmenu/menu_pt.c
@@ -24,6 +24,7 @@
 #include "config.h"
 #include "mp_msg.h"
 #include "help_mp.h"
+#include "path.h"
 
 #include "libmpcodecs/img_format.h"
 #include "libmpcodecs/mp_image.h"
@@ -38,8 +39,6 @@
 #include "input/input.h"
 #include "access_mpcontext.h"
 
-#define mp_basename(s) (strrchr((s),'/')==NULL?(char*)(s):(strrchr((s),'/')+1))
-
 struct list_entry_s {
   struct list_entry p;
   play_tree_t* pt;
@@ -124,8 +123,14 @@ static int read_key(menu_t* menu,int c){
   return menu_list_jump_to_key(menu, c);
 }
 
+static void free_entry(list_entry_t* entry)
+{
+  free(entry->p.txt);
+  free(entry);
+}
+
 static void close_menu(menu_t* menu) {
-  menu_list_uninit(menu,NULL);
+  menu_list_uninit(menu, free_entry);
 }
 
 static int op(menu_t* menu, char* args) {
@@ -146,7 +151,7 @@ static int op(menu_t* menu, char* args) {
 
   if(playtree_iter->tree->parent != playtree_iter->root) {
     e = calloc(1,sizeof(list_entry_t));
-    e->p.txt = "..";
+    e->p.txt = strdup("..");
     e->pt = playtree_iter->tree->parent;
     menu_list_add_entry(menu,e);
   }
@@ -156,9 +161,9 @@ static int op(menu_t* menu, char* args) {
   for( ; i != NULL ; i = i->next ) {
     e = calloc(1,sizeof(list_entry_t));
     if(i->files)
-      e->p.txt = mp_basename(i->files[0]);
+      e->p.txt = strdup(mp_basename(i->files[0]));
     else
-      e->p.txt = "Group ...";
+      e->p.txt = strdup("Group ...");
     e->pt = i;
     menu_list_add_entry(menu,e);
   }
diff --git a/mplayer.c b/mplayer.c
index e03bfe4..3c0427e 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -349,8 +349,6 @@ static int crash_debug = 0;
 #include "cfg-mplayer.h"
 
 
-#define mp_basename2(s) (strrchr(s,'/')==NULL?(char*)s:(strrchr(s,'/')+1))
-
 const void *mpctx_get_video_out(MPContext *mpctx)
 {
     return mpctx->video_out;
@@ -456,7 +454,7 @@ char *get_metadata (metadata_t type) {
   {
   case META_NAME:
   {
-    return strdup (mp_basename2 (filename));
+    return strdup(mp_basename(filename));
   }
 
   case META_VIDEO_CODEC:
@@ -1046,8 +1044,6 @@ static int libmpdemux_was_interrupted(int eof) {
   return eof;
 }
 
-#define mp_basename(s) (strrchr(s,'\\')==NULL?(mp_basename2(s)):(strrchr(s,'\\')+1))
-
 static int playtree_add_playlist(play_tree_t* entry)
 {
   play_tree_add_bpf(entry,filename);
@@ -3175,7 +3171,7 @@ while (player_idle_mode && !filename) {
 	mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_Playing,
 		filename_recode(filename));
         if(use_filename_title && vo_wintitle == NULL)
-            vo_wintitle = strdup ( mp_basename2 (filename));
+            vo_wintitle = strdup(mp_basename(filename));
     }
 
     edl_loadfile();
@@ -3369,7 +3365,8 @@ if (mpctx->demuxer && mpctx->demuxer->type==DEMUXER_TYPE_PLAYLIST)
   current_module="handle_demux_playlist";
   while (ds_get_packet(mpctx->demuxer->video,&playlist_entry)>0)
   {
-    char *temp, *bname;
+    char *temp;
+    const char *bname;
 
     mp_msg(MSGT_CPLAYER,MSGL_V,"Adding file %s to element entry.\n",
 	    filename_recode(playlist_entry));
diff --git a/path.c b/path.c
index 2a1d504..cb41545 100644
--- a/path.c
+++ b/path.c
@@ -193,3 +193,16 @@ void set_codec_path(const char *path)
     strcpy(codec_path, path);
     needs_free = 1;
 }
+
+const char *mp_basename(const char *path)
+{
+    char *s;
+
+#if HAVE_DOS_PATHS
+    s = strrchr(path, '\\');
+    if (s)
+        return s + 1;
+#endif
+    s = strrchr(path, '/');
+    return s ? s + 1 : s;
+}
diff --git a/path.h b/path.h
index 3e71120..349f55e 100644
--- a/path.h
+++ b/path.h
@@ -26,5 +26,6 @@ extern char *codec_path;
 char *get_path(const char *filename);
 void set_path_env(void);
 void set_codec_path(const char *path);
+const char *mp_basename(const char *path);
 
 #endif /* MPLAYER_PATH_H */
-- 
1.7.3.2


--AKkMM/tm2Mk6Yn/s--


More information about the MPlayer-dev-eng mailing list