Commit 169ad3f6 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Do not manually handle c strings.

parent 4e18daee
...@@ -201,12 +201,10 @@ public: ...@@ -201,12 +201,10 @@ public:
*/ */
std::string mrl() std::string mrl()
{ {
char* c_result = libvlc_media_get_mrl(*this); auto str = wrapCStr( libvlc_media_get_mrl(*this) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
...@@ -240,12 +238,10 @@ public: ...@@ -240,12 +238,10 @@ public:
*/ */
std::string meta(libvlc_meta_t e_meta) std::string meta(libvlc_meta_t e_meta)
{ {
char* c_result = libvlc_media_get_meta(*this, e_meta); auto str = wrapCStr(libvlc_media_get_meta(*this, e_meta) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
......
...@@ -60,12 +60,10 @@ public: ...@@ -60,12 +60,10 @@ public:
*/ */
std::string localizedName() std::string localizedName()
{ {
char* c_result = libvlc_media_discoverer_localized_name(*this); auto str = wrapCStr( libvlc_media_discoverer_localized_name(*this) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
......
...@@ -1147,12 +1147,10 @@ public: ...@@ -1147,12 +1147,10 @@ public:
*/ */
std::string aspectRatio() std::string aspectRatio()
{ {
char* c_result = libvlc_video_get_aspect_ratio(*this); auto str = wrapCStr( libvlc_video_get_aspect_ratio(*this) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
...@@ -1289,12 +1287,10 @@ public: ...@@ -1289,12 +1287,10 @@ public:
*/ */
std::string cropGeometry() std::string cropGeometry()
{ {
char* c_result = libvlc_video_get_crop_geometry(*this); auto str = wrapCStr( libvlc_video_get_crop_geometry(*this) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
...@@ -1424,12 +1420,10 @@ public: ...@@ -1424,12 +1420,10 @@ public:
*/ */
std::string marqueeString(unsigned option) std::string marqueeString(unsigned option)
{ {
char* c_result = libvlc_video_get_marquee_string(*this, option); auto str = wrapCStr( libvlc_video_get_marquee_string(*this, option) );
if ( c_result == NULL ) if ( str == nullptr )
return std::string(); return {};
std::string result = c_result; return str.get();
libvlc_free(c_result);
return result;
} }
/** /**
......
...@@ -48,6 +48,11 @@ namespace VLC ...@@ -48,6 +48,11 @@ namespace VLC
{ {
return ref.get(); return ref.get();
} }
std::unique_ptr<char, void (*)(void*)> wrapCStr(char* str)
{
return std::unique_ptr<char, void(*)(void*)>( str, [](void* ptr) { libvlc_free(ptr); } );
}
} }
#endif #endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment