diff options
Diffstat (limited to '')
-rw-r--r-- | src/file.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/file.cpp b/src/file.cpp new file mode 100644 index 0000000..1a8bd08 --- /dev/null +++ b/src/file.cpp | |||
@@ -0,0 +1,158 @@ | |||
1 | #include "file.h" | ||
2 | #include "exceptions.h" | ||
3 | #include <errno.h> | ||
4 | #include <sys/types.h> | ||
5 | #include <sys/stat.h> | ||
6 | |||
7 | Bu::File::File( const char *sName, const char *sFlags ) | ||
8 | { | ||
9 | fh = fopen( sName, sFlags ); | ||
10 | if( fh == NULL ) | ||
11 | { | ||
12 | throw Bu::FileException( errno, strerror(errno) ); | ||
13 | } | ||
14 | } | ||
15 | |||
16 | Bu::File::File( const Bu::FString &sName, const char *sFlags ) | ||
17 | { | ||
18 | fh = fopen( sName.getStr(), sFlags ); | ||
19 | if( fh == NULL ) | ||
20 | { | ||
21 | throw Bu::FileException( errno, strerror(errno) ); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | Bu::File::File( int fd, const char *sFlags ) | ||
26 | { | ||
27 | fh = fdopen( fd, sFlags ); | ||
28 | } | ||
29 | |||
30 | Bu::File::~File() | ||
31 | { | ||
32 | close(); | ||
33 | } | ||
34 | |||
35 | void Bu::File::close() | ||
36 | { | ||
37 | if( fh ) | ||
38 | { | ||
39 | fclose( fh ); | ||
40 | fh = NULL; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | size_t Bu::File::read( void *pBuf, size_t nBytes ) | ||
45 | { | ||
46 | if( !fh ) | ||
47 | throw FileException("File not open."); | ||
48 | |||
49 | int nAmnt = fread( pBuf, 1, nBytes, fh ); | ||
50 | |||
51 | //if( nAmnt == 0 ) | ||
52 | // throw FileException("End of file."); | ||
53 | |||
54 | return nAmnt; | ||
55 | } | ||
56 | |||
57 | size_t Bu::File::write( const void *pBuf, size_t nBytes ) | ||
58 | { | ||
59 | if( !fh ) | ||
60 | throw FileException("File not open."); | ||
61 | |||
62 | return fwrite( pBuf, 1, nBytes, fh ); | ||
63 | } | ||
64 | |||
65 | long Bu::File::tell() | ||
66 | { | ||
67 | if( !fh ) | ||
68 | throw FileException("File not open."); | ||
69 | |||
70 | return ftell( fh ); | ||
71 | } | ||
72 | |||
73 | void Bu::File::seek( long offset ) | ||
74 | { | ||
75 | if( !fh ) | ||
76 | throw FileException("File not open."); | ||
77 | |||
78 | fseek( fh, offset, SEEK_CUR ); | ||
79 | } | ||
80 | |||
81 | void Bu::File::setPos( long pos ) | ||
82 | { | ||
83 | if( !fh ) | ||
84 | throw FileException("File not open."); | ||
85 | |||
86 | fseek( fh, pos, SEEK_SET ); | ||
87 | } | ||
88 | |||
89 | void Bu::File::setPosEnd( long pos ) | ||
90 | { | ||
91 | if( !fh ) | ||
92 | throw FileException("File not open."); | ||
93 | |||
94 | fseek( fh, pos, SEEK_END ); | ||
95 | } | ||
96 | |||
97 | bool Bu::File::isEOS() | ||
98 | { | ||
99 | return feof( fh ); | ||
100 | } | ||
101 | |||
102 | bool Bu::File::canRead() | ||
103 | { | ||
104 | return true; | ||
105 | } | ||
106 | |||
107 | bool Bu::File::canWrite() | ||
108 | { | ||
109 | return true; | ||
110 | } | ||
111 | |||
112 | bool Bu::File::isReadable() | ||
113 | { | ||
114 | return true; | ||
115 | } | ||
116 | |||
117 | bool Bu::File::isWritable() | ||
118 | { | ||
119 | return true; | ||
120 | } | ||
121 | |||
122 | bool Bu::File::isSeekable() | ||
123 | { | ||
124 | return true; | ||
125 | } | ||
126 | |||
127 | bool Bu::File::isBlocking() | ||
128 | { | ||
129 | return true; | ||
130 | } | ||
131 | |||
132 | void Bu::File::setBlocking( bool bBlocking ) | ||
133 | { | ||
134 | return; | ||
135 | } | ||
136 | |||
137 | #ifndef WIN32 | ||
138 | void Bu::File::truncate( long nSize ) | ||
139 | { | ||
140 | ftruncate( fileno( fh ), nSize ); | ||
141 | } | ||
142 | |||
143 | void Bu::File::chmod( mode_t t ) | ||
144 | { | ||
145 | fchmod( fileno( fh ), t ); | ||
146 | } | ||
147 | #endif | ||
148 | |||
149 | void Bu::File::flush() | ||
150 | { | ||
151 | fflush( fh ); | ||
152 | } | ||
153 | |||
154 | bool Bu::File::isOpen() | ||
155 | { | ||
156 | return (fh != NULL); | ||
157 | } | ||
158 | |||