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

Last change on this file since 537 was 526, checked in by krille_n_@…, 12 years ago

Changes:

  • Update of the copyright notices to include the year 2013.
File size: 3.5 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,
[505]9// in particular ftell() and fseek(), are limited to signed 32-bits (2 GB).
[219]10// These are also likely faster since they are more direct.
[505]11//
[219]12
[376]13//
[505]14// XTIDE Universal BIOS and Associated Tools
[526]15// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
[376]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.
[505]21//
[376]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
[505]25// GNU General Public License for more details.
[376]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 ) )
[505]71 log( 0, "'%s', could not close file handle", name ? name : "unknown" );
[219]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)
[505]128 const static unsigned long MaxSectors = 0xfffffff;
[219]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.