ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/util_windows.cpp
Revision: 1.1
Committed: 2004-12-03T22:01:47Z (19 years, 11 months ago) by gbeauche
Branch: MAIN
Log Message:
Merge util_windows.cpp (exists, get_file_size, create_file) from original
Basilisk II for Windows

File Contents

# Content
1 /*
2 * util_windows.cpp - Miscellaneous utilities for Win32
3 *
4 * Basilisk II (C) 1997-2004 Christian Bauer
5 *
6 * Windows platform specific code copyright (C) Lauri Pesonen
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "sysdeps.h"
24 #include "util_windows.h"
25
26 BOOL exists( const char *path )
27 {
28 HFILE h;
29 bool ret = false;
30
31 h = _lopen( path, OF_READ );
32 if(h != HFILE_ERROR) {
33 ret = true;
34 _lclose(h);
35 }
36 return(ret);
37 }
38
39 BOOL create_file( const char *path, DWORD size )
40 {
41 HANDLE h;
42 bool ok = false;
43
44 h = CreateFile( path,
45 GENERIC_READ | GENERIC_WRITE,
46 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL
47 );
48 if(h != INVALID_HANDLE_VALUE) {
49 if(size == 0) {
50 ok = true;
51 } else if(SetFilePointer( h, size, NULL, FILE_BEGIN) != 0xFFFFFFFF) {
52 if(SetEndOfFile(h)) {
53 ok = true;
54 if(SetFilePointer( h, 0, NULL, FILE_BEGIN) != 0xFFFFFFFF) {
55 DWORD written, zeroed_size = min(1024*1024,size);
56 char *b = (char *)malloc(zeroed_size);
57 if(b) {
58 memset( b, 0, zeroed_size );
59 WriteFile( h, b, zeroed_size, &written, NULL );
60 free(b);
61 }
62 }
63 }
64 }
65 CloseHandle(h);
66 }
67 if(!ok) DeleteFile(path);
68 return(ok);
69 }
70
71 int32 get_file_size( const char *path )
72 {
73 HANDLE h;
74 DWORD size = 0;
75
76 h = CreateFile( path,
77 GENERIC_READ,
78 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
79 );
80 if(h != INVALID_HANDLE_VALUE) {
81 size = GetFileSize( h, NULL );
82 CloseHandle(h);
83 }
84 return(size);
85 }