1 | //======================================================================
|
---|
2 | //
|
---|
3 | // Project: XTIDE Universal BIOS, Serial Port Server
|
---|
4 | //
|
---|
5 | // File: file.h - File access via standard "stdio.h" routines
|
---|
6 | //
|
---|
7 | // Routines for accessing the file system using generic routines, which
|
---|
8 | // should work on all systems. The issue with using these is that
|
---|
9 | // ftell() and fseek() are limited to 2 GB files (signed 32-bit quantities)
|
---|
10 | // and there is no standard for 64-bit quantities. So, look for a
|
---|
11 | // OS specific version of this file in the distribution, such as
|
---|
12 | // win32/win32file.h which may be in use instead.
|
---|
13 | //
|
---|
14 |
|
---|
15 | #include <stdio.h>
|
---|
16 |
|
---|
17 | class FileAccess
|
---|
18 | {
|
---|
19 | public:
|
---|
20 | void Create( char *p_name )
|
---|
21 | {
|
---|
22 | fp = fopen( p_name, "r" );
|
---|
23 |
|
---|
24 | if( fp )
|
---|
25 | log( -1, "Create Failure: '%s' already exists", p_name );
|
---|
26 |
|
---|
27 | if( !(fp = fopen( p_name, "w" )) )
|
---|
28 | log( -1, "Could not create file '%s'", p_name );
|
---|
29 |
|
---|
30 | name = p_name;
|
---|
31 | }
|
---|
32 |
|
---|
33 | void Open( char *p_name )
|
---|
34 | {
|
---|
35 | fp = fopen( p_name, "r+" );
|
---|
36 | if( !fp )
|
---|
37 | log( -1, "Could not Open '%s'", p_name );
|
---|
38 | name = p_name;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void Close()
|
---|
42 | {
|
---|
43 | if( fp )
|
---|
44 | fclose( fp );
|
---|
45 | fp = NULL;
|
---|
46 | }
|
---|
47 |
|
---|
48 | unsigned long SizeSectors(void)
|
---|
49 | {
|
---|
50 | long filesize;
|
---|
51 |
|
---|
52 | fseek( fp, 0, SEEK_END );
|
---|
53 | filesize = ftell( fp );
|
---|
54 |
|
---|
55 | if( filesize == 0 || filesize == -1L )
|
---|
56 | log( -1, "Could not get file size for '%s', file possibly larger than 2 GB", name );
|
---|
57 |
|
---|
58 | if( filesize & 0x1ff )
|
---|
59 | log( -1, "'%s' not made up of 512 byte sectors", name );
|
---|
60 |
|
---|
61 | return( filesize >> 9 ); // 512 bytes per sector
|
---|
62 | }
|
---|
63 |
|
---|
64 | void SeekSectors( unsigned long lba )
|
---|
65 | {
|
---|
66 | if( fseek( fp, lba * 512, SEEK_SET ) )
|
---|
67 | log( -1, "'%s', Failed to seek to lba=%lu", name, lba );
|
---|
68 | }
|
---|
69 |
|
---|
70 | void Read( void *buff, unsigned long len )
|
---|
71 | {
|
---|
72 | if( fread( buff, 1, 512, fp ) != 512 )
|
---|
73 | log( -1, "'%s', Failed to read sector", name );
|
---|
74 | }
|
---|
75 |
|
---|
76 | void Write( void *buff, unsigned long len )
|
---|
77 | {
|
---|
78 | if( fwrite( buff, 1, 512, fp ) != 512 )
|
---|
79 | log( -1, "'%s', Failed to write sector", name );
|
---|
80 | }
|
---|
81 |
|
---|
82 | FileAccess()
|
---|
83 | {
|
---|
84 | fp = NULL;
|
---|
85 | name = NULL;
|
---|
86 | }
|
---|
87 |
|
---|
88 | const static unsigned long MaxSectors = 4194303; // limited by signed 32-bit file sizes
|
---|
89 | #define USAGE_MAXSECTORS "2048 MB (signed 32-bit file size limit)"
|
---|
90 |
|
---|
91 | private:
|
---|
92 | FILE *fp;
|
---|
93 | char *name;
|
---|
94 | };
|
---|
95 |
|
---|