Functions: added vargs to LMsg

This commit is contained in:
zvon 2020-04-01 13:42:00 +02:00
parent 9ae1896b22
commit 5e9f27ae4a
2 changed files with 29 additions and 10 deletions

View File

@ -14,6 +14,7 @@
#include <codecvt> #include <codecvt>
#include <shlobj.h> #include <shlobj.h>
#include "resources_windows.h"
#define cout std::wcout #define cout std::wcout
#define cerr std::wcerr #define cerr std::wcerr
@ -51,14 +52,31 @@ std::wstring utf8_to_wstring( const std::string &utf8 ) {
return wconv.from_bytes( utf8 ); return wconv.from_bytes( utf8 );
} }
wchar_t *LMsg( int ID ) { std::wstring LMsg( int id, ... ) {
// TODO better max value perhaps?
static wchar_t local[MAX_PATH]; static wchar_t local[MAX_PATH];
auto hInstance = GetModuleHandle( NULL ); auto hInstance = GetModuleHandle( NULL );
LoadString( hInstance, ID, local, MAX_PATH ); LoadString( hInstance, id, local, MAX_PATH );
// TODO add vargs
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 #else
@ -256,7 +274,8 @@ string userHome() {
return dir_s; return dir_s;
} }
CoTaskMemFree( dir ); 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 #else // UNIX
@ -592,7 +611,7 @@ void refreshDB( bool linux, void *progress_ptr ) {
#ifndef GUI #ifndef GUI
cleanUpLine(); cleanUpLine();
#endif #endif
p.print( _( REFRESHING ) + string( " " ) + show[TEXT( "SHOW" )] ); p.print( _( REFRESHING ) + string( 1, ' ' ) + show[TEXT( "SHOW" )] );
p.print( 0 ); p.print( 0 );
std::map< int, std::map< int, string > > seasons; 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 ); ProgressBar p( progress_ptr );
for ( auto &show : shows ) { 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" )] ) ) { if ( FSLib::exists( show[TEXT( "PATH" )] ) ) {
p.print( 0 ); p.print( 0 );
std::map< int, std::map< int, string > > seasons; std::map< int, std::map< int, string > > seasons;

View File

@ -16,9 +16,9 @@ using string = std::wstring;
using char_t = wchar_t; using char_t = wchar_t;
std::wstring utf8_to_wstring( const std::string &utf8 ); 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 #else