source: xtideuniversalbios/trunk/Serial_Server/win32/Win32File.h@ 478

Last change on this file since 478 was 376, checked in by gregli@…, 13 years ago

WIDE checkin... Added copyright and license information to sorce files, as per the GPL instructions for usage.

File size: 3.6 KB
RevLine 
[219]1//======================================================================
2//
3// Project: XTIDE Universal BIOS, Serial Port Server
4//
5// File: Win32File.h - Microsoft Windows file system access.
6//
7// Routines for accessing the file system under Win32. It's important
8// to use these direct Win32 calls for large files, since FILE * routines,
9// in particular ftell() and fseek(), are limites to signed 32-bits (2 GB).
10// These are also likely faster since they are more direct.
11//
12
[376]13//
14// XTIDE Universal BIOS and Associated Tools
15// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
16//
17// This program is free software; you can redistribute it and/or modify
18// it under the terms of the GNU General Public License as published by
19// the Free Software Foundation; either version 2 of the License, or
20// (at your option) any later version.
21//
22// This program is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26// Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
27//
28
[219]29#include <windows.h>
30#include <stdio.h>
31#include "../library/library.h"
32
33class FileAccess
34{
35public:
[225]36 int Create( char *p_name )
[219]37 {
38 fp = CreateFileA( p_name, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
[225]39
40 if( fp == INVALID_HANDLE_VALUE )
41 {
42 if( GetLastError() == ERROR_FILE_EXISTS )
43 {
44 log( 0, "'%s', file already exists", p_name );
45 return( 0 );
46 }
47 else
48 log( -1, "'%s', could not create file", p_name );
49 }
50
[219]51 name = p_name;
[225]52
53 return( 1 );
[219]54 }
55
56 void Open( char *p_name )
57 {
58 fp = CreateFileA( p_name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
[225]59
60 if( fp == INVALID_HANDLE_VALUE )
[219]61 log( -1, "'%s', could not open file", p_name );
[225]62
[219]63 name = p_name;
64 }
65
66 void Close()
67 {
68 if( fp )
69 {
70 if( !CloseHandle( fp ) )
71 log( 0, "'%s', could not close file handle", name ? name : "unknown" );
72 }
73 }
74
75 unsigned long SizeSectors(void)
76 {
77 LARGE_INTEGER li;
78 unsigned long i;
79
80 if( !GetFileSizeEx( fp, &li ) )
81 log( -1, "'%s', could not retrieve file size (error %ul)", name, GetLastError() );
82
83 if( li.LowPart & 0x1ff )
84 log( -1, "'%s', file size is not a multiple of 512 byte sectors", name );
85
86 if( li.HighPart > 0x1f )
87 log( -1, "'%s', file size greater than LBA28 limit of 137,438,952,960 bytes", name );
88
89 i = ((li.HighPart << 23 ) & 0xff800000) | ((li.LowPart >> 9) & 0x7fffff);
90
91 return( (unsigned long) i );
92 }
93
94 void SeekSectors( unsigned long lba )
95 {
96 LARGE_INTEGER dist;
97
98 dist.HighPart = lba >> 23;
99 dist.LowPart = lba << 9;
100
101 if( !SetFilePointerEx( fp, dist, NULL, FILE_BEGIN ) )
102 log( -1, "'%s', Failed to seek to lba=%lu", name, lba );
103 }
104
105 void Read( void *buff, unsigned long len )
106 {
107 unsigned long out_len;
108
109 if( !ReadFile( fp, buff, len, &out_len, NULL ) || len != out_len )
[225]110 log( -1, "'%s', ReadFile failed", name );
[219]111 }
112
113 void Write( void *buff, unsigned long len )
114 {
115 unsigned long out_len;
116
117 if( !WriteFile( fp, buff, len, &out_len, NULL ) || len != out_len )
[225]118 log( -1, "'%s', WriteFile failed", name );
[219]119 }
120
121 FileAccess()
122 {
123 fp = NULL;
124 name = NULL;
125 }
126
127 // LBA 28 limit - 28-bits (could be 1 more, but not worth pushing it)
128 const static unsigned long MaxSectors = 0xfffffff;
129#define USAGE_MAXSECTORS "137438 MB (LBA28 limit)"
130
131private:
132 HANDLE fp;
133 char *name;
134};
135
Note: See TracBrowser for help on using the repository browser.