Fixed initialization of FileStream

This commit is contained in:
Tsuda Kageyu 2013-05-19 11:09:43 +09:00
parent 7060d53cf3
commit 5c3f096fe4
4 changed files with 27 additions and 32 deletions

View File

@ -29,21 +29,14 @@
#include "tdebug.h"
#include "tpropertymap.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <wchar.h>
# include <windows.h>
# include <io.h>
# define ftruncate _chsize
#else
# include <stdio.h>
# include <unistd.h>
#endif
#include <stdlib.h>
#ifndef R_OK
# define R_OK 4
#endif

View File

@ -213,7 +213,7 @@ namespace TagLib {
bool isOpen() const;
/*!
* Returns true if the file is open and readble.
* Returns true if the file is open and readable.
*/
bool isValid() const;

View File

@ -27,20 +27,13 @@
#include "tstring.h"
#include "tdebug.h"
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <wchar.h>
# include <windows.h>
# include <io.h>
#else
# include <stdio.h>
# include <unistd.h>
#endif
#include <stdlib.h>
using namespace TagLib;
namespace
@ -89,11 +82,12 @@ namespace
return 0;
}
# ifndef NDEBUG
// Convert a string in a local encoding into a UTF-16 string.
// This function should only be used to generate an error message.
// In actual use, file names in local encodings are passed to CreateFileA()
// without any conversions.
// Debugging use only. In actual use, file names in local encodings are passed to
// CreateFileA() without any conversions.
String fileNameToString(const FileName &name)
{
@ -115,7 +109,9 @@ namespace
}
}
#else
# endif
#else // _WIN32
struct FileNameHandle : public std::string
{
@ -146,16 +142,16 @@ namespace
return fwrite(buffer.data(), sizeof(char), buffer.size(), file);
}
#endif
#endif // _WIN32
}
class FileStream::FileStreamPrivate
{
public:
FileStreamPrivate(const FileName &fileName, bool openReadOnly)
FileStreamPrivate(const FileName &fileName)
: file(InvalidFileHandle)
, name(fileName)
, readOnly(openReadOnly)
, readOnly(true)
, size(0)
{
}
@ -172,23 +168,23 @@ public:
// public members
////////////////////////////////////////////////////////////////////////////////
FileStream::FileStream(FileName file, bool openReadOnly)
: d(new FileStreamPrivate(file, openReadOnly))
FileStream::FileStream(FileName fileName, bool openReadOnly)
: d(new FileStreamPrivate(fileName))
{
// First try with read / write mode, if that fails, fall back to read only.
if(!openReadOnly)
d->file = openFile(file, false);
d->file = openFile(fileName, false);
if(d->file != InvalidFileHandle)
d->readOnly = false;
else
d->file = openFile(d->name, true);
d->file = openFile(fileName, true);
if(d->file == InvalidFileHandle)
{
# ifdef _WIN32
debug("Could not open file " + fileNameToString(d->name));
debug("Could not open file " + fileNameToString(fileName));
# else
debug("Could not open file " + String(static_cast<const char *>(d->name)));
# endif

View File

@ -29,6 +29,8 @@ using namespace TagLib;
#ifdef _WIN32
# include "tstring.h"
# include "tdebug.h"
# include <windows.h>
namespace
@ -50,14 +52,18 @@ namespace
// This function should only be used in Windows9x systems which don't support
// Unicode file names.
std::string unicodeToAnsi(const std::wstring &wstr)
std::string unicodeToAnsi(const wchar_t *wstr)
{
const int len = WideCharToMultiByte(CP_ACP, 0, &wstr[0], -1, NULL, 0, NULL, NULL);
if(SystemSupportsUnicode) {
debug("unicodeToAnsi() - Should not be used on WinNT systems.");
}
const int len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
if(len == 0)
return std::string();
std::string str(len, '\0');
WideCharToMultiByte(CP_ACP, 0, &wstr[0], -1, &str[0], len, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, &str[0], len, NULL, NULL);
return str;
}