Commit 841b9581 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

fs: win32: Deal with unicode VS wide chars

parent 9f4fdab0
......@@ -164,6 +164,9 @@ libmedialibrary_la_SOURCES += \
src/filesystem/win32/File.cpp \
src/filesystem/win32/Device.cpp \
$(NULL)
noinst_HEADERS += \
src/utils/Charsets.h \
$(NULL)
else
libmedialibrary_la_SOURCES += \
src/filesystem/unix/Directory.cpp \
......
......@@ -25,6 +25,9 @@
#endif
#include "Directory.h"
#include "utils/Charsets.h"
#include "factory/IFileSystem.h"
#include "File.h"
#include <sys/types.h>
#include <sys/stat.h>
......@@ -41,10 +44,11 @@ Directory::Directory(const std::string& path , factory::IFileSystem& fsFactory)
{
}
void Directory::read()
void Directory::read() const
{
WIN32_FIND_DATA f;
auto h = FindFirstFile( m_path.c_str(), &f );
auto wpath = charset::ToWide( m_path.c_str() );
auto h = FindFirstFile( wpath.get(), &f );
if ( h == INVALID_HANDLE_VALUE )
throw std::runtime_error( "Failed to browse through " + m_path );
try
......@@ -53,11 +57,11 @@ void Directory::read()
{
if ( ( f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0 )
{
m_dirs.emplace_back( f.cFileName );
m_dirs.emplace_back( m_fsFactory.createDirectory( charset::FromWide( f.cFileName ).get() ) );
}
else
{
m_dirs.emplace_back( f.cFileName );
m_files.emplace_back( std::make_shared<File>( charset::FromWide( f.cFileName ).get() ) );
}
} while ( FindNextFile( h, &f ) != 0 );
}
......@@ -69,12 +73,14 @@ void Directory::read()
std::string Directory::toAbsolute( const std::string& path )
{
TCHAR buff[MAX_PATH];
if ( GetFullPathName( path.c_str(), MAX_PATH, buff, nullptr ) == 0 )
auto wpath = charset::ToWide( path.c_str() );
if ( GetFullPathName( wpath.get(), MAX_PATH, buff, nullptr ) == 0 )
{
throw std::runtime_error("Failed to convert " + path + " to absolute path (" +
std::to_string( GetLastError() ) + ")" );
}
return std::string( buff );
auto upath = charset::FromWide( buff );
return std::string( upath.get() );
}
}
......
/*****************************************************************************
* Media Library
*****************************************************************************
* Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
*
* Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#pragma once
#ifdef _WIN32
#include <windows.h>
#include <memory>
namespace medialibrary
{
namespace charset
{
static inline std::unique_ptr<char[]> FromWide( const wchar_t *wide )
{
size_t len = WideCharToMultiByte( CP_UTF8, 0, wide, -1, nullptr, 0, nullptr, nullptr );
if (len == 0)
return nullptr;
char *out = new char[len];
WideCharToMultiByte( CP_UTF8, 0, wide, -1, out, len, nullptr, nullptr );
return std::unique_ptr<char[]>( out );
}
static inline std::unique_ptr<wchar_t[]> ToWide( const char *utf8 )
{
int len = MultiByteToWideChar( CP_UTF8, 0, utf8, -1, nullptr, 0);
if (len == 0)
return nullptr;
wchar_t *out = new wchar_t[len];
MultiByteToWideChar( CP_UTF8, 0, utf8, -1, out, len );
return std::unique_ptr<wchar_t[]>( out );
}
}
}
#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