Skip to content

Commit feca777

Browse files
committedApr 24, 2014
Added missing _win32
1 parent ebc48e7 commit feca777

8 files changed

+1726
-0
lines changed
 

‎libplatform/io/FileSystem_win32.cpp

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include "src/impl.h"
2+
#include "libplatform/impl.h" /* for platform_win32_impl.h which declares Utf8ToFilename */
3+
#include <windows.h>
4+
5+
namespace mp4v2 {
6+
using namespace impl;
7+
}
8+
9+
namespace mp4v2 { namespace platform { namespace io {
10+
11+
///////////////////////////////////////////////////////////////////////////////
12+
13+
static DWORD getAttributes ( string path_ );
14+
15+
/**
16+
* Call GetFileAttributesW throw exceptions for errors
17+
*
18+
* @param path_ the path to get attributes for
19+
*
20+
* @retval INVALID_FILE_ATTRIBUTES @p path_ doesn't exist
21+
* @retval anything else the attributes of @p path_
22+
*/
23+
static DWORD
24+
getAttributes ( string path_ )
25+
{
26+
win32::Utf8ToFilename filename(path_);
27+
28+
if (!filename.IsUTF16Valid())
29+
{
30+
// throw an exception to avoid changing the
31+
// signature of this function and dealing with all
32+
// the places it's called.
33+
ostringstream msg;
34+
msg << "can't convert file to UTF-16(" << filename.utf8 << ")";
35+
throw new Exception(msg.str(),__FILE__,__LINE__,__FUNCTION__);
36+
}
37+
38+
DWORD attributes = ::GetFileAttributesW(filename);
39+
if( attributes == INVALID_FILE_ATTRIBUTES )
40+
{
41+
DWORD last_err = GetLastError();
42+
43+
// Distinguish between an error and the path not existing
44+
if ((last_err == ERROR_FILE_NOT_FOUND) || (last_err == ERROR_PATH_NOT_FOUND))
45+
{
46+
return attributes;
47+
}
48+
49+
// Anything else is an error
50+
ostringstream msg;
51+
msg << "GetFileAttributes(" << filename.utf8 << ") failed (" << last_err << ")";
52+
throw new Exception(msg.str(),__FILE__,__LINE__,__FUNCTION__);
53+
}
54+
55+
// path exists so return its attributes
56+
return attributes;
57+
}
58+
59+
bool
60+
FileSystem::exists( string path_ )
61+
{
62+
return( getAttributes(path_) != INVALID_FILE_ATTRIBUTES );
63+
}
64+
65+
///////////////////////////////////////////////////////////////////////////////
66+
67+
bool
68+
FileSystem::isDirectory( string path_ )
69+
{
70+
DWORD attributes = getAttributes( path_ );
71+
if( attributes == INVALID_FILE_ATTRIBUTES )
72+
return false;
73+
74+
return ( ( attributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY );
75+
}
76+
77+
///////////////////////////////////////////////////////////////////////////////
78+
79+
bool
80+
FileSystem::isFile( string path_ )
81+
{
82+
DWORD attributes = getAttributes( path_ );
83+
if( attributes == INVALID_FILE_ATTRIBUTES )
84+
return false;
85+
86+
return ( ( attributes & FILE_ATTRIBUTE_DIRECTORY ) != FILE_ATTRIBUTE_DIRECTORY );
87+
}
88+
89+
///////////////////////////////////////////////////////////////////////////////
90+
91+
bool
92+
FileSystem::getFileSize( string path_, File::Size& size_ )
93+
{
94+
win32::Utf8ToFilename filename(path_);
95+
96+
if (!filename.IsUTF16Valid())
97+
{
98+
// The logging is done
99+
return true;
100+
}
101+
102+
size_ = 0;
103+
WIN32_FILE_ATTRIBUTE_DATA data = {0};
104+
if( !GetFileAttributesExW( filename, GetFileExInfoStandard, (LPVOID)&data ) )
105+
{
106+
log.errorf("%s: GetFileAttributesExW(%s) failed (%d)",__FUNCTION__,filename.utf8.c_str(),
107+
GetLastError());
108+
return true;
109+
}
110+
111+
size_ = ( (File::Size)data.nFileSizeHigh << 32 ) | data.nFileSizeLow;
112+
return false;
113+
}
114+
115+
///////////////////////////////////////////////////////////////////////////////
116+
117+
bool
118+
FileSystem::rename( string from, string to )
119+
{
120+
win32::Utf8ToFilename from_file(from);
121+
win32::Utf8ToFilename to_file(to);
122+
123+
if (!from_file.IsUTF16Valid() || !to_file.IsUTF16Valid())
124+
{
125+
return true;
126+
}
127+
128+
if (!::MoveFileExW( from_file, to_file,
129+
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH ) )
130+
{
131+
log.errorf("%s: MoveFileExW(%s,%s) failed (%d)",__FUNCTION__,from_file.utf8.c_str(),to_file.utf8.c_str(),
132+
GetLastError());
133+
return true;
134+
}
135+
136+
return false;
137+
}
138+
139+
///////////////////////////////////////////////////////////////////////////////
140+
141+
string FileSystem::DIR_SEPARATOR = "\\";
142+
string FileSystem::PATH_SEPARATOR = ";";
143+
144+
///////////////////////////////////////////////////////////////////////////////
145+
146+
}}} // namespace mp4v2::platform::io

‎libplatform/io/File_win32.cpp

+267
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#include "src/impl.h"
2+
#include "libplatform/impl.h" /* for platform_win32_impl.h which declares Utf8ToFilename */
3+
#include <windows.h>
4+
5+
namespace mp4v2 {
6+
using namespace impl;
7+
}
8+
9+
/**
10+
* Set this to 1 to compile in extra debugging
11+
*/
12+
#define EXTRA_DEBUG 0
13+
14+
/**
15+
* @def LOG_PRINTF
16+
*
17+
* call log.printf if EXTRA_DEBUG is defined to 1. Do
18+
* nothing otherwise
19+
*/
20+
#if EXTRA_DEBUG
21+
#define LOG_PRINTF(X) log.printf X
22+
#else
23+
#define LOG_PRINTF(X)
24+
#endif
25+
26+
namespace mp4v2 { namespace platform { namespace io {
27+
28+
///////////////////////////////////////////////////////////////////////////////
29+
30+
class StandardFileProvider : public FileProvider
31+
{
32+
public:
33+
StandardFileProvider();
34+
35+
bool open( std::string name, Mode mode );
36+
bool seek( Size pos );
37+
bool read( void* buffer, Size size, Size& nin, Size maxChunkSize );
38+
bool write( const void* buffer, Size size, Size& nout, Size maxChunkSize );
39+
bool close();
40+
41+
int64_t getSize();
42+
43+
private:
44+
HANDLE _handle;
45+
46+
/**
47+
* The UTF-8 encoded file name
48+
*/
49+
std::string _name;
50+
};
51+
52+
///////////////////////////////////////////////////////////////////////////////
53+
54+
StandardFileProvider::StandardFileProvider()
55+
: _handle( INVALID_HANDLE_VALUE )
56+
{
57+
}
58+
59+
/**
60+
* Open a file
61+
*
62+
* @param name the name of a file to open
63+
* @param mode the mode to open @p name
64+
*
65+
* @retval false successfully opened @p name
66+
* @retval true error opening @p name
67+
*/
68+
bool
69+
StandardFileProvider::open( std::string name, Mode mode )
70+
{
71+
DWORD access = 0;
72+
DWORD share = 0;
73+
DWORD crdisp = 0;
74+
DWORD flags = FILE_ATTRIBUTE_NORMAL;
75+
76+
switch( mode ) {
77+
case MODE_UNDEFINED:
78+
case MODE_READ:
79+
default:
80+
access |= GENERIC_READ;
81+
share |= FILE_SHARE_READ;
82+
crdisp |= OPEN_EXISTING;
83+
break;
84+
85+
case MODE_MODIFY:
86+
access |= GENERIC_READ | GENERIC_WRITE;
87+
share |= FILE_SHARE_READ;
88+
crdisp |= OPEN_EXISTING;
89+
break;
90+
91+
case MODE_CREATE:
92+
access |= GENERIC_READ | GENERIC_WRITE;
93+
share |= FILE_SHARE_READ;
94+
crdisp |= CREATE_ALWAYS;
95+
break;
96+
}
97+
98+
win32::Utf8ToFilename filename(name);
99+
100+
if (!filename.IsUTF16Valid())
101+
{
102+
// The logging is done
103+
return true;
104+
}
105+
106+
ASSERT(LPCWSTR(filename));
107+
_handle = CreateFileW( filename, access, share, NULL, crdisp, flags, NULL );
108+
if (_handle == INVALID_HANDLE_VALUE)
109+
{
110+
log.errorf("%s: CreateFileW(%s) failed (%d)",__FUNCTION__,filename.utf8.c_str(),GetLastError());
111+
return true;
112+
}
113+
114+
/*
115+
** Make a copy of the name for future log messages, etc.
116+
*/
117+
log.verbose2f("%s: CreateFileW(%s) succeeded",__FUNCTION__,filename.utf8.c_str());
118+
119+
_name = name;
120+
return false;
121+
}
122+
123+
/**
124+
* Seek to an offset in the file
125+
*
126+
* @param pos the offset from the beginning of the file to
127+
* seek to
128+
*
129+
* @retval false successfully seeked to @p pos
130+
* @retval true error seeking to @p pos
131+
*/
132+
bool
133+
StandardFileProvider::seek( Size pos )
134+
{
135+
LARGE_INTEGER n;
136+
137+
ASSERT(_handle != INVALID_HANDLE_VALUE);
138+
139+
n.QuadPart = pos;
140+
if (!SetFilePointerEx( _handle, n, NULL, FILE_BEGIN ))
141+
{
142+
log.errorf("%s: SetFilePointerEx(%s,%" PRId64 ") failed (%d)",__FUNCTION__,_name.c_str(),
143+
pos,GetLastError());
144+
return true;
145+
}
146+
147+
return false;
148+
}
149+
150+
/**
151+
* Read from the file
152+
*
153+
* @param buffer populated with at most @p size bytes from
154+
* the file
155+
*
156+
* @param size the maximum number of bytes to read
157+
*
158+
* @param nin the
159+
*
160+
* @retval false successfully read from the file
161+
* @retval true error reading from the file
162+
*/
163+
bool
164+
StandardFileProvider::read( void* buffer, Size size, Size& nin, Size maxChunkSize )
165+
{
166+
DWORD nread = 0;
167+
168+
ASSERT(_handle != INVALID_HANDLE_VALUE);
169+
170+
// ReadFile takes a DWORD for number of bytes to read so
171+
// make sure we're not asking for more than fits.
172+
// MAXDWORD from WinNT.h.
173+
ASSERT(size <= MAXDWORD);
174+
if( ReadFile( _handle, buffer, (DWORD)(size & MAXDWORD), &nread, NULL ) == 0 )
175+
{
176+
log.errorf("%s: ReadFile(%s,%d) failed (%d)",__FUNCTION__,_name.c_str(),
177+
(DWORD)(size & MAXDWORD),GetLastError());
178+
return true;
179+
}
180+
LOG_PRINTF((MP4_LOG_VERBOSE3,"%s: ReadFile(%s,%d) succeeded: read %d byte(s)",__FUNCTION__,
181+
_name.c_str(),(DWORD)(size & MAXDWORD),nread));
182+
nin = nread;
183+
return false;
184+
}
185+
186+
/**
187+
* Write to the file
188+
*
189+
* @param buffer the data to write
190+
*
191+
* @param size the number of bytes of @p buffer to write
192+
*
193+
* @param nout populated with the number of bytes actually
194+
* written if the function succeeds
195+
*
196+
* @retval false successfully wrote to the file
197+
* @retval true error writing to the file
198+
*/
199+
bool
200+
StandardFileProvider::write( const void* buffer, Size size, Size& nout, Size maxChunkSize )
201+
{
202+
DWORD nwrote = 0;
203+
204+
ASSERT(_handle != INVALID_HANDLE_VALUE);
205+
206+
// ReadFile takes a DWORD for number of bytes to read so
207+
// make sure we're not asking for more than fits.
208+
// MAXDWORD from WinNT.h.
209+
ASSERT(size <= MAXDWORD);
210+
if( WriteFile( _handle, buffer, (DWORD)(size & MAXDWORD), &nwrote, NULL ) == 0 )
211+
{
212+
log.errorf("%s: WriteFile(%s,%d) failed (%d)",__FUNCTION__,_name.c_str(),
213+
(DWORD)(size & MAXDWORD),GetLastError());
214+
return true;
215+
}
216+
log.verbose2f("%s: WriteFile(%s,%d) succeeded: wrote %d byte(s)",__FUNCTION__,
217+
_name.c_str(),(DWORD)(size & MAXDWORD),nwrote);
218+
nout = nwrote;
219+
return false;
220+
}
221+
222+
/**
223+
* Close the file
224+
*
225+
* @retval false successfully closed the file
226+
* @retval true error closing the file
227+
*/
228+
bool
229+
StandardFileProvider::close()
230+
{
231+
BOOL retval;
232+
233+
retval = CloseHandle( _handle );
234+
if (!retval)
235+
{
236+
log.errorf("%s: CloseHandle(%s) failed (%d)",__FUNCTION__,
237+
_name.c_str(),GetLastError());
238+
}
239+
240+
// Whether we succeeded or not, clear the handle and
241+
// forget the name
242+
_handle = INVALID_HANDLE_VALUE;
243+
_name.clear();
244+
245+
// CloseHandle return 0/false to indicate failure, but
246+
// we return 0/false to indicate success, so negate.
247+
return !retval;
248+
}
249+
250+
int64_t StandardFileProvider::getSize()
251+
{
252+
int64_t retSize = 0;
253+
FileSystem::getFileSize( _name, retSize );
254+
return retSize;
255+
}
256+
257+
///////////////////////////////////////////////////////////////////////////////
258+
259+
FileProvider&
260+
FileProvider::standard()
261+
{
262+
return *new StandardFileProvider();
263+
}
264+
265+
///////////////////////////////////////////////////////////////////////////////
266+
267+
}}} // namespace mp4v2::platform::io

‎libplatform/number/random_win32.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "libplatform/impl.h"
2+
#include <stdlib.h>
3+
4+
namespace mp4v2 { namespace platform { namespace number {
5+
6+
///////////////////////////////////////////////////////////////////////////////
7+
8+
uint32_t
9+
random32()
10+
{
11+
return uint32_t( ::rand() << 16 | ::rand() );
12+
}
13+
14+
///////////////////////////////////////////////////////////////////////////////
15+
16+
void
17+
srandom( uint32_t seed )
18+
{
19+
::srand( seed );
20+
}
21+
22+
///////////////////////////////////////////////////////////////////////////////
23+
24+
}}} // namespace mp4v2::platform::time

‎libplatform/platform_win32.cpp

+1,092
Large diffs are not rendered by default.

‎libplatform/platform_win32.h

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef MP4V2_PLATFORM_WIN32_H
2+
#define MP4V2_PLATFORM_WIN32_H
3+
4+
///////////////////////////////////////////////////////////////////////////////
5+
6+
// mingw needs this to enable some newer 64-bit functions
7+
#ifdef __MINGW32__
8+
# undef __MSVCRT_VERSION__
9+
# define __MSVCRT_VERSION__ 0x800
10+
// JAN: see http://code.google.com/p/mp4v2/issues/detail?id=132
11+
# define _USE_32BIT_TIME_T
12+
#endif
13+
14+
// set minimum win32 API requirement to Windows 2000 or higher
15+
#ifndef _WIN32_WINNT
16+
# define _WIN32_WINNT 0x0500
17+
#endif
18+
#ifndef WINVER
19+
# define WINVER 0x0500
20+
#endif
21+
22+
///////////////////////////////////////////////////////////////////////////////
23+
24+
#include "libplatform/platform_base.h"
25+
#include <mp4v2/mp4v2.h>
26+
27+
///////////////////////////////////////////////////////////////////////////////
28+
29+
namespace mp4v2 { namespace platform {
30+
using namespace std;
31+
32+
using ::int8_t;
33+
using ::int16_t;
34+
using ::int32_t;
35+
using ::int64_t;
36+
37+
using ::uint8_t;
38+
using ::uint16_t;
39+
using ::uint32_t;
40+
using ::uint64_t;
41+
}} // namespace mp4v2::platform
42+
43+
///////////////////////////////////////////////////////////////////////////////
44+
45+
// fprintf macros for unsigned types - mingw32 is a good source if more needed
46+
#define PRId8 "d"
47+
#define PRId16 "d"
48+
#define PRId32 "d"
49+
#define PRId64 "I64d"
50+
51+
#define PRIu8 "u"
52+
#define PRIu16 "u"
53+
#define PRIu32 "u"
54+
#define PRIu64 "I64u"
55+
56+
#define PRIx8 "x"
57+
#define PRIx16 "x"
58+
#define PRIx32 "x"
59+
#define PRIx64 "I64x"
60+
61+
///////////////////////////////////////////////////////////////////////////////
62+
63+
// some macros for constant expressions
64+
#define INT8_C(x) x
65+
#define INT16_C(x) x
66+
#define INT32_C(x) x ## L
67+
#define INT64_C(x) x ## LL
68+
69+
#define UINT8_C(x) x
70+
#define UINT16_C(x) x
71+
#define UINT32_C(x) x ## UL
72+
#define UINT64_C(x) x ## ULL
73+
74+
///////////////////////////////////////////////////////////////////////////////
75+
76+
#ifdef min
77+
# undef min
78+
#endif
79+
80+
#ifdef max
81+
# undef max
82+
#endif
83+
84+
///////////////////////////////////////////////////////////////////////////////
85+
86+
#define snprintf(s,n,...) _snprintf(s,n,__VA_ARGS__)
87+
#define strcasecmp(s1,s2) _stricmp(s1,s2)
88+
#define strdup(s) _strdup(s)
89+
90+
///////////////////////////////////////////////////////////////////////////////
91+
92+
// macro clashes with symbol
93+
#undef LC_NONE
94+
95+
#endif // MP4V2_PLATFORM_WIN32_H

‎libplatform/platform_win32_impl.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Note that we have a separate platform_win32_impl.h to deal with the fact that windows.h defines a macro
2+
// called FindAtom, which mp4v2 also defines. In older versions of visual studio, this actually causes
3+
// some pretty seriously issues with naming collisions and the defined macros (think infamous min/max macro
4+
// of windows.h vs stdc++'s min/max template functions)
5+
#include <windows.h>
6+
7+
///////////////////////////////////////////////////////////////////////////////
8+
9+
namespace mp4v2 { namespace platform { namespace win32 {
10+
11+
class Utf8ToFilename
12+
{
13+
public:
14+
Utf8ToFilename( const string &utf8string );
15+
~Utf8ToFilename( );
16+
17+
bool IsUTF16Valid( ) const;
18+
19+
operator LPCWSTR( ) const { return _wideCharString; }
20+
operator LPWSTR( ) const { return _wideCharString; }
21+
22+
private:
23+
Utf8ToFilename ( const Utf8ToFilename &src );
24+
Utf8ToFilename &operator= ( const Utf8ToFilename &src );
25+
26+
wchar_t *ConvertToUTF16 ( const string &utf8 );
27+
28+
static int ConvertToUTF16Buf ( const char *utf8,
29+
wchar_t *utf16_buf,
30+
size_t num_bytes );
31+
static int GetPrefixLen ( const string &utf8string );
32+
33+
static int IsAbsolute ( const string &utf8string );
34+
35+
static int IsPathSeparator ( char c );
36+
37+
static int IsUncPath ( const string &utf8string );
38+
39+
static const UINT8 *Utf8DecodeChar (
40+
const UINT8 *utf8_char,
41+
size_t num_bytes,
42+
wchar_t *utf16,
43+
int *invalid
44+
);
45+
46+
static size_t Utf8LenFromUcs4 ( UINT32 ucs4 );
47+
48+
static UINT8 Utf8NumOctets ( UINT8 utf8_first_byte );
49+
50+
/**
51+
* The UTF-8 encoding of the filename actually used
52+
*/
53+
string _utf8;
54+
55+
/**
56+
* The UTF-16 encoding of the filename actually used
57+
*/
58+
wchar_t* _wideCharString;
59+
60+
public:
61+
62+
/**
63+
* Accessor for @p _utf8
64+
*/
65+
const string& utf8;
66+
};
67+
68+
}}} // namespace mp4v2::platform::win32

‎libplatform/process/process_win32.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "libplatform/impl.h"
2+
#include <process.h>
3+
4+
namespace mp4v2 { namespace platform { namespace process {
5+
6+
///////////////////////////////////////////////////////////////////////////////
7+
8+
int32_t
9+
getpid()
10+
{
11+
return ::_getpid();
12+
}
13+
14+
///////////////////////////////////////////////////////////////////////////////
15+
16+
}}} // namespace mp4v2::platform::process

‎libplatform/time/time_win32.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "libplatform/impl.h"
2+
#include <sys/timeb.h>
3+
4+
namespace mp4v2 { namespace platform { namespace time {
5+
6+
///////////////////////////////////////////////////////////////////////////////
7+
8+
milliseconds_t
9+
getLocalTimeMilliseconds()
10+
{
11+
__timeb64 buf;
12+
_ftime64( &buf );
13+
return milliseconds_t( buf.time ) * 1000 + buf.millitm;
14+
}
15+
16+
///////////////////////////////////////////////////////////////////////////////
17+
18+
}}} // namespace mp4v2::platform::time

0 commit comments

Comments
 (0)
Please sign in to comment.