From 5e9f27ae4aef1da9256fd2397664d0fe5fa778e1 Mon Sep 17 00:00:00 2001 From: zvon Date: Wed, 1 Apr 2020 13:42:00 +0200 Subject: [PATCH] Functions: added vargs to LMsg --- functions.cpp | 35 +++++++++++++++++++++++++++-------- functions.hpp | 4 ++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/functions.cpp b/functions.cpp index 9e05759..4ec0629 100644 --- a/functions.cpp +++ b/functions.cpp @@ -14,6 +14,7 @@ #include #include +#include "resources_windows.h" #define cout std::wcout #define cerr std::wcerr @@ -51,14 +52,31 @@ std::wstring utf8_to_wstring( const std::string &utf8 ) { return wconv.from_bytes( utf8 ); } -wchar_t *LMsg( int ID ) { - // TODO better max value perhaps? +std::wstring LMsg( int id, ... ) { static wchar_t local[MAX_PATH]; auto hInstance = GetModuleHandle( NULL ); - LoadString( hInstance, ID, local, MAX_PATH ); - // TODO add vargs + LoadString( hInstance, id, local, MAX_PATH ); - return local; + va_list args; + va_start( args, id ); + int count = 0; + const wchar_t *p; + while ( ( p = va_arg( args, const wchar_t * ) ) != NULL ) + count++; + va_end( args ); + + if ( count == 0 ) + return local; + + va_start( args, id ); + int len = vswprintf( nullptr, 0, local, args ); + va_end( args ); + wchar_t *text = new wchar_t[len + 1]; + va_start( args, id ); + vswprintf( text, len + 1, local, args ); + std::wstring ret = text; + delete[] text; + return ret; } #else @@ -256,7 +274,8 @@ string userHome() { return dir_s; } CoTaskMemFree( dir ); - throw std::runtime_error( _( APPDATA_NOT_FOUND ) ); + // runtime_error doesn't support wstring + throw std::runtime_error( "Couldn't find user's appdata" ); } #else // UNIX @@ -592,7 +611,7 @@ void refreshDB( bool linux, void *progress_ptr ) { #ifndef GUI cleanUpLine(); #endif - p.print( _( REFRESHING ) + string( " " ) + show[TEXT( "SHOW" )] ); + p.print( _( REFRESHING ) + string( 1, ' ' ) + show[TEXT( "SHOW" )] ); p.print( 0 ); std::map< int, std::map< int, string > > seasons; @@ -875,7 +894,7 @@ void refreshSelectDB( std::unordered_set< size_t > indexes, bool linux, ProgressBar p( progress_ptr ); for ( auto &show : shows ) { - p.print( _( REFRESHING ) + string( ' ', 1 ) + show[TEXT( "SHOW" )] ); + p.print( _( REFRESHING ) + string( 1, ' ' ) + show[TEXT( "SHOW" )] ); if ( FSLib::exists( show[TEXT( "PATH" )] ) ) { p.print( 0 ); std::map< int, std::map< int, string > > seasons; diff --git a/functions.hpp b/functions.hpp index 4d8bea0..437f785 100644 --- a/functions.hpp +++ b/functions.hpp @@ -16,9 +16,9 @@ using string = std::wstring; using char_t = wchar_t; std::wstring utf8_to_wstring( const std::string &utf8 ); -wchar_t *LMsg( int ID ); +std::wstring LMsg( int id, ... ); -#define _( x, ... ) LMsg( ID, ##__VA_ARGS__ ) +#define _( x, ... ) LMsg( x, ##__VA_ARGS__ ).c_str() #else