source: xtideuniversalbios/tags/Serial_Server_for_v2.0.0_beta1/win32/Win32File.h@ 560

Last change on this file since 560 was 225, checked in by gregli@…, 12 years ago

Serial Server, minor improvements to file handling.

File size: 2.8 KB
Line 
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
13#include <windows.h>
14#include <stdio.h>
15#include "../library/library.h"
16
17class FileAccess
18{
19public:
20 int Create( char *p_name )
21 {
22 fp = CreateFileA( p_name, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
23
24 if( fp == INVALID_HANDLE_VALUE )
25 {
26 if( GetLastError() == ERROR_FILE_EXISTS )
27 {
28 log( 0, "'%s', file already exists", p_name );
29 return( 0 );
30 }
31 else
32 log( -1, "'%s', could not create file", p_name );
33 }
34
35 name = p_name;
36
37 return( 1 );
38 }
39
40 void Open( char *p_name )
41 {
42 fp = CreateFileA( p_name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
43
44 if( fp == INVALID_HANDLE_VALUE )
45 log( -1, "'%s', could not open file", p_name );
46
47 name = p_name;
48 }
49
50 void Close()
51 {
52 if( fp )
53 {
54 if( !CloseHandle( fp ) )
55 log( 0, "'%s', could not close file handle", name ? name : "unknown" );
56 }
57 }
58
59 unsigned long SizeSectors(void)
60 {
61 LARGE_INTEGER li;
62 unsigned long i;
63
64 if( !GetFileSizeEx( fp, &li ) )
65 log( -1, "'%s', could not retrieve file size (error %ul)", name, GetLastError() );
66
67 if( li.LowPart & 0x1ff )
68 log( -1, "'%s', file size is not a multiple of 512 byte sectors", name );
69
70 if( li.HighPart > 0x1f )
71 log( -1, "'%s', file size greater than LBA28 limit of 137,438,952,960 bytes", name );
72
73 i = ((li.HighPart << 23 ) & 0xff800000) | ((li.LowPart >> 9) & 0x7fffff);
74
75 return( (unsigned long) i );
76 }
77
78 void SeekSectors( unsigned long lba )
79 {
80 LARGE_INTEGER dist;
81
82 dist.HighPart = lba >> 23;
83 dist.LowPart = lba << 9;
84
85 if( !SetFilePointerEx( fp, dist, NULL, FILE_BEGIN ) )
86 log( -1, "'%s', Failed to seek to lba=%lu", name, lba );
87 }
88
89 void Read( void *buff, unsigned long len )
90 {
91 unsigned long out_len;
92
93 if( !ReadFile( fp, buff, len, &out_len, NULL ) || len != out_len )
94 log( -1, "'%s', ReadFile failed", name );
95 }
96
97 void Write( void *buff, unsigned long len )
98 {
99 unsigned long out_len;
100
101 if( !WriteFile( fp, buff, len, &out_len, NULL ) || len != out_len )
102 log( -1, "'%s', WriteFile failed", name );
103 }
104
105 FileAccess()
106 {
107 fp = NULL;
108 name = NULL;
109 }
110
111 // LBA 28 limit - 28-bits (could be 1 more, but not worth pushing it)
112 const static unsigned long MaxSectors = 0xfffffff;
113#define USAGE_MAXSECTORS "137438 MB (LBA28 limit)"
114
115private:
116 HANDLE fp;
117 char *name;
118};
119
Note: See TracBrowser for help on using the repository browser.