1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gfile_win32.cpp
19 //
20
21 #include "gdef.h"
22 #include "gfile.h"
23 #include <sys/stat.h>
24 #include <direct.h>
25 #include <iomanip>
26 #include <sstream>
27
mkdir(const Path & dir,const NoThrow &)28 bool G::File::mkdir( const Path & dir , const NoThrow & )
29 {
30 return 0 == ::_mkdir( dir.str().c_str() ) ;
31 }
32
executable(const Path & path)33 bool G::File::executable( const Path & path )
34 {
35 return exists( path , NoThrow() ) ;
36 }
37
sizeString(const Path & path)38 std::string G::File::sizeString( const Path & path )
39 {
40 WIN32_FIND_DATAA info ;
41 HANDLE h = ::FindFirstFileA( path.str().c_str() , &info ) ;
42 if( h == INVALID_HANDLE_VALUE )
43 return std::string() ;
44
45 const DWORD & hi = info.nFileSizeHigh ;
46 const DWORD & lo = info.nFileSizeLow ;
47
48 ::FindClose( h ) ;
49
50 return sizeString( hi , lo ) ;
51 }
52
sizeString(g_uint32_t hi,g_uint32_t lo)53 std::string G::File::sizeString( g_uint32_t hi , g_uint32_t lo )
54 {
55 __int64 n = hi ;
56 n <<= 32U ;
57 n |= lo ;
58 if( n < 0 )
59 throw SizeOverflow() ;
60
61 if( n == 0 )
62 return std::string("0") ;
63
64 std::string s ;
65 while( n != 0 )
66 {
67 int i = static_cast<int>( n % 10 ) ;
68 char c = static_cast<char>( '0' + i ) ;
69 s.insert( 0U , 1U , c ) ;
70 n /= 10 ;
71 }
72 return s ;
73 }
74
exists(const char * path,bool & enoent)75 bool G::File::exists( const char * path , bool & enoent )
76 {
77 struct _stat statbuf ;
78 bool ok = 0 == ::_stat( path , &statbuf ) ;
79 enoent = !ok ;
80 return ok ;
81 }
82
time(const Path & path)83 G::File::time_type G::File::time( const Path & path )
84 {
85 struct _stat statbuf ;
86 if( 0 != ::_stat( path.str().c_str() , &statbuf ) )
87 throw TimeError( path.str() ) ;
88 return statbuf.st_mtime ;
89 }
90
time(const Path & path,const NoThrow &)91 G::File::time_type G::File::time( const Path & path , const NoThrow & )
92 {
93 struct _stat statbuf ;
94 return ::_stat( path.str().c_str() , &statbuf ) == 0 ? statbuf.st_mtime : 0 ;
95 }
96
chmodx(const Path &,bool)97 bool G::File::chmodx( const Path & , bool )
98 {
99 return true ; // no-op
100 }
101
link(const Path &,const Path & new_link)102 void G::File::link( const Path & , const Path & new_link )
103 {
104 CannotLink e( new_link.str() ) ;
105 e.append( "not supported" ) ;
106 throw e ;
107 }
108
link(const Path &,const Path &,const NoThrow &)109 bool G::File::link( const Path & , const Path & , const NoThrow & )
110 {
111 return false ; // not supported
112 }
113
114 /// \file gfile_win32.cpp
115