source: xtideuniversalbios/trunk/Serial_Server/library/File.h@ 330

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

Serial Server, minor improvements to file handling.

File size: 2.3 KB
Line 
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
17class FileAccess
18{
19public:
20 int Create( char *p_name )
21 {
22 fp = fopen( p_name, "r" );
23
24 if( fp )
25 {
26 log( 0, "'%s' file already exists", p_name );
27 fclose( fp );
28 return( 0 );
29 }
30
31 if( !(fp = fopen( p_name, "w" )) )
32 log( -1, "Could not create file '%s'", p_name );
33
34 name = p_name;
35
36 return( 1 );
37 }
38
39 void Open( char *p_name )
40 {
41 fp = fopen( p_name, "r+" );
42 if( !fp )
43 log( -1, "Could not Open '%s'", p_name );
44 name = p_name;
45 }
46
47 void Close()
48 {
49 if( fp )
50 fclose( fp );
51 fp = NULL;
52 }
53
54 unsigned long SizeSectors(void)
55 {
56 long filesize;
57
58 fseek( fp, 0, SEEK_END );
59 filesize = ftell( fp );
60
61 if( filesize == 0 || filesize == -1L )
62 log( -1, "Could not get file size for '%s', file possibly larger than 2 GB", name );
63
64 if( filesize & 0x1ff )
65 log( -1, "'%s' not made up of 512 byte sectors", name );
66
67 return( filesize >> 9 ); // 512 bytes per sector
68 }
69
70 void SeekSectors( unsigned long lba )
71 {
72 if( fseek( fp, lba * 512, SEEK_SET ) )
73 log( -1, "'%s', Failed to seek to lba=%lu", name, lba );
74 }
75
76 void Read( void *buff, unsigned long len )
77 {
78 if( fread( buff, 1, 512, fp ) != 512 )
79 log( -1, "'%s', Failed to read sector", name );
80 }
81
82 void Write( void *buff, unsigned long len )
83 {
84 if( fwrite( buff, 1, 512, fp ) != 512 )
85 log( -1, "'%s', Failed to write sector", name );
86 }
87
88 FileAccess()
89 {
90 fp = NULL;
91 name = NULL;
92 }
93
94 const static unsigned long MaxSectors = 4194303; // limited by signed 32-bit file sizes
95#define USAGE_MAXSECTORS "2048 MB (signed 32-bit file size limit)"
96
97private:
98 FILE *fp;
99 char *name;
100};
101
Note: See TracBrowser for help on using the repository browser.