[MPlayer-dev-eng] [PATCH] Add mp_strings.c with mp_asprintf function.
Clément Bœsch
ubitux at gmail.com
Sat Feb 19 14:53:27 CET 2011
On Sat, Feb 05, 2011 at 10:45:57PM +0100, Reimar Döffinger wrote:
> > [...]
> It's fine by me in principle, except that I am still somewhat suspicious of
> the va_* stuff (that used to be quite tricky/buggy at some points in time)
> but if you think it really is useful I won't stop you.
I think it could be really useful in various places. As an example I
attached to this mail two patches which use it. Also re-attached
mp_asprintf since I shamefully forgot a +1.
If anyone thinks those examples are a valid reason to add mp_asprintf I'll
commit it, and will wait a review for those two next patches.
--
Clément B.
-------------- next part --------------
From acaf7480a5f145ba73da6aef9683dbc7a1ee5a1d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Wed, 12 Jan 2011 23:37:11 +0100
Subject: [PATCH 1/3] Add mp_strings.c with mp_asprintf function.
---
Makefile | 1 +
mp_strings.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
mp_strings.h | 26 ++++++++++++++++++++++++++
3 files changed, 76 insertions(+), 0 deletions(-)
create mode 100644 mp_strings.c
create mode 100644 mp_strings.h
diff --git a/Makefile b/Makefile
index 5e5ef6d..2996392 100644
--- a/Makefile
+++ b/Makefile
@@ -301,6 +301,7 @@ SRCS_COMMON = asxparser.c \
m_option.c \
m_struct.c \
mp_msg.c \
+ mp_strings.c \
mpcommon.c \
parser-cfg.c \
path.c \
diff --git a/mp_strings.c b/mp_strings.c
new file mode 100644
index 0000000..0c8a577
--- /dev/null
+++ b/mp_strings.c
@@ -0,0 +1,49 @@
+/*
+ * Strings utilities
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include "mp_strings.h"
+
+int mp_asprintf(char **strp, const char *fmt, ...)
+{
+ va_list va, va_bak;
+ int len;
+
+ va_start(va, fmt);
+ va_copy(va_bak, va);
+
+ len = vsnprintf(NULL, 0, fmt, va);
+ if (len < 0)
+ goto end;
+
+ *strp = malloc(len + 1);
+ if (!*strp) {
+ len = -1;
+ goto end;
+ }
+
+ len = vsnprintf(*strp, len + 1, fmt, va_bak);
+
+end:
+ va_end(va);
+ return len;
+}
diff --git a/mp_strings.h b/mp_strings.h
new file mode 100644
index 0000000..3990322
--- /dev/null
+++ b/mp_strings.h
@@ -0,0 +1,26 @@
+/*
+ * Strings utilities
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef MPLAYER_MP_STRINGS_H
+#define MPLAYER_MP_STRINGS_H
+
+int mp_asprintf(char **strp, const char *fmt, ...);
+
+#endif /* !MPLAYER_MP_STRINGS_H */
--
1.7.4.1
-------------- next part --------------
From 6fa3b312b08818ce9c4ec0e5af0a24dc3cd8242d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sun, 13 Feb 2011 12:16:01 +0100
Subject: [PATCH 2/3] Replace make_noauth_url and make_http_proxy_url with mp_asprintf calls.
---
stream/network.c | 15 +++++++++++----
stream/url.c | 38 ++++++++------------------------------
stream/url.h | 2 --
3 files changed, 19 insertions(+), 36 deletions(-)
diff --git a/stream/network.c b/stream/network.c
index 1f1d382..d1d6faf 100644
--- a/stream/network.c
+++ b/stream/network.c
@@ -42,6 +42,7 @@
#include "libmpdemux/demuxer.h"
#include "m_config.h"
#include "mpcommon.h"
+#include "mp_strings.h"
#include "network.h"
#include "tcp.h"
#include "http.h"
@@ -143,7 +144,6 @@ check4proxies( URL_t *url ) {
proxy = getenv("http_proxy");
if( proxy!=NULL ) {
// We got a proxy, build the URL to use it
- int len;
char *new_url;
URL_t *tmp_url;
URL_t *proxy_url = url_new( proxy );
@@ -164,14 +164,21 @@ check4proxies( URL_t *url ) {
#endif
mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
- len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
- new_url = malloc(len);
+
+ if (proxy_url->username)
+ mp_asprintf(&new_url, "http_proxy://%s:%s@%s:%d/%s",
+ proxy_url->username,
+ proxy_url->password ? proxy_url->password : "",
+ proxy_url->hostname, proxy_url->port, url->url);
+ else
+ mp_asprintf(&new_url, "http_proxy://%s:%d/%s",
+ proxy_url->hostname, proxy_url->port, url->url);
+
if( new_url==NULL ) {
mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
url_free(proxy_url);
return url_out;
}
- make_http_proxy_url(proxy_url, url->url, new_url, len);
tmp_url = url_new( new_url );
if( tmp_url==NULL ) {
free( new_url );
diff --git a/stream/url.c b/stream/url.c
index af530f0..7769e91 100644
--- a/stream/url.c
+++ b/stream/url.c
@@ -28,6 +28,7 @@
#include "url.h"
#include "mp_msg.h"
+#include "mp_strings.h"
#include "help_mp.h"
#ifndef SIZE_MAX
@@ -58,32 +59,9 @@ URL_t *url_redirect(URL_t **url, const char *redir) {
return res;
}
-static int make_noauth_url(URL_t *url, char *dst, int dst_size)
-{
- if (url->port)
- return snprintf(dst, dst_size, "%s://%s:%d%s", url->protocol,
- url->hostname, url->port, url->file);
- else
- return snprintf(dst, dst_size, "%s://%s%s", url->protocol,
- url->hostname, url->file);
-}
-
-int make_http_proxy_url(URL_t *proxy, const char *host_url, char *dst,
- int dst_size)
-{
- if (proxy->username)
- return snprintf(dst, dst_size, "http_proxy://%s:%s@%s:%d/%s",
- proxy->username,
- proxy->password ? proxy->password : "",
- proxy->hostname, proxy->port, host_url);
- else
- return snprintf(dst, dst_size, "http_proxy://%s:%d/%s",
- proxy->hostname, proxy->port, host_url);
-}
-
URL_t*
url_new(const char* url) {
- int pos1, pos2,v6addr = 0, noauth_len;
+ int pos1, pos2,v6addr = 0;
URL_t* Curl = NULL;
char *escfilename=NULL;
char *ptr1=NULL, *ptr2=NULL, *ptr3=NULL, *ptr4=NULL;
@@ -252,16 +230,16 @@ url_new(const char* url) {
strcpy(Curl->file, "/");
}
- noauth_len = make_noauth_url(Curl, NULL, 0);
- if (noauth_len > 0) {
- noauth_len++;
- Curl->noauth_url = malloc(noauth_len);
+ if (Curl->port)
+ mp_asprintf(&Curl->noauth_url, "%s://%s:%d%s", Curl->protocol,
+ Curl->hostname, Curl->port, Curl->file);
+ else
+ mp_asprintf(&Curl->noauth_url, "%s://%s%s", Curl->protocol,
+ Curl->hostname, Curl->file);
if (!Curl->noauth_url) {
mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed);
goto err_out;
}
- make_noauth_url(Curl, Curl->noauth_url, noauth_len);
- }
free(escfilename);
return Curl;
diff --git a/stream/url.h b/stream/url.h
index b75978a..1ab67a0 100644
--- a/stream/url.h
+++ b/stream/url.h
@@ -38,8 +38,6 @@ typedef struct {
URL_t *url_redirect(URL_t **url, const char *redir);
-int make_http_proxy_url(URL_t *proxy, const char *host_url, char *dst, int dst_size);
-
URL_t* url_new(const char* url);
void url_free(URL_t* url);
--
1.7.4.1
-------------- next part --------------
From 6571b23452d9595008f3081f237ba4253d5be500 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sun, 13 Feb 2011 12:24:48 +0100
Subject: [PATCH 3/3] Use mp_asprintf in get_metadata instead of limited stack buffer.
---
mplayer.c | 24 ++++++++++++------------
1 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/mplayer.c b/mplayer.c
index b03b5d8..f675752 100644
--- a/mplayer.c
+++ b/mplayer.c
@@ -438,7 +438,7 @@ static char *get_demuxer_info (char *tag) {
}
char *get_metadata (metadata_t type) {
- char meta[128];
+ char *meta;
sh_audio_t * const sh_audio = mpctx->sh_audio;
sh_video_t * const sh_video = mpctx->sh_video;
@@ -460,18 +460,18 @@ char *get_metadata (metadata_t type) {
else if (sh_video->format == 0x10000005)
return strdup("h264");
else if (sh_video->format >= 0x20202020)
- snprintf(meta, sizeof(meta), "%.4s", (char *) &sh_video->format);
+ mp_asprintf(&meta, "%.4s", (char *) &sh_video->format);
else
- snprintf(meta, sizeof(meta), "0x%08X", sh_video->format);
- return strdup(meta);
+ mp_asprintf(&meta, "0x%08X", sh_video->format);
+ return meta;
case META_VIDEO_BITRATE:
- snprintf(meta, sizeof(meta), "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
- return strdup(meta);
+ mp_asprintf(&meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
+ return meta;
case META_VIDEO_RESOLUTION:
- snprintf(meta, sizeof(meta), "%d x %d", sh_video->disp_w, sh_video->disp_h);
- return strdup(meta);
+ mp_asprintf(&meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
+ return meta;
case META_AUDIO_CODEC:
if (sh_audio->codec && sh_audio->codec->name)
@@ -479,12 +479,12 @@ char *get_metadata (metadata_t type) {
break;
case META_AUDIO_BITRATE:
- snprintf(meta, sizeof(meta), "%d kbps", (int)(sh_audio->i_bps * 8 / 1000));
- return strdup(meta);
+ mp_asprintf(&meta, "%d kbps", (int)(sh_audio->i_bps * 8 / 1000));
+ return meta;
case META_AUDIO_SAMPLES:
- snprintf(meta, sizeof(meta), "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
- return strdup(meta);
+ mp_asprintf(&meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
+ return meta;
/* check for valid demuxer */
case META_INFO_TITLE:
--
1.7.4.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/attachments/20110219/0dbeb836/attachment.pgp>
More information about the MPlayer-dev-eng
mailing list