32 #define DEBUG_BYTE_FILER
35 using namespace basis;
52 byte_filer::byte_filer()
53 : _handle(new file_hider),
59 : _handle(new file_hider),
62 {
open(fname, perms); }
65 : _handle(new file_hider),
68 {
open(fname, perms); }
71 : _handle(new file_hider),
73 _auto_close(auto_close)
76 _handle->fp = (FILE *)handle;
90 _filename->
reset(fname);
99 if (_auto_close && _handle->fp) fclose(_handle->fp);
107 if (!_handle->fp)
return 0;
108 long to_return = ::ftell(_handle->fp);
109 if (to_return == -1) {
116 return size_t(to_return);
124 {
return !_handle->fp ? 0 : int(::fread((
char *)buff, 1, size, _handle->fp)); }
127 {
return !_handle->fp ? 0 : int(::fwrite((
char *)buff, 1, size, _handle->fp)); }
131 buff.
reset(desired_size);
132 int to_return =
read(buff.
access(), desired_size);
142 size_t current_posn =
tell();
144 size_t file_size =
tell();
151 s.
pad(desired_size + 2);
167 if (!_handle->fp)
return;
168 ::fflush(_handle->fp);
174 int fnum =
fileno(_handle->fp);
176 return SetEndOfFile((
HANDLE)_get_osfhandle(fnum));
178 size_t posn =
tell();
183 return !ftruncate(fnum, posn);
189 if (!_handle->fp)
return false;
192 case FROM_START: real_origin = SEEK_SET;
break;
193 case FROM_END: real_origin = SEEK_END;
break;
195 default:
return false;
197 int ret = ::fseek(_handle->fp, where, real_origin);
203 if (!_handle->fp)
return 0;
204 char *ret = ::fgets((
char *)buff, desired_size, _handle->fp);
205 return !ret? 0 : int(strlen((
char *)buff)) + 1;
210 buff.
reset(desired_size + 1);
216 buff.
pad(desired_size + 1);
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
const contents * observe() const
Returns a pointer to the underlying C array of data.
contents * access()
A non-constant access of the underlying C-array. BE REALLY CAREFUL.
int length() const
Returns the current reported length of the allocated C array.
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
bool t() const
t() is a shortcut for the string being "true", as in non-empty.
void shrink()
changes all occurrences of "to_replace" into "new_string".
void pad(int length, char padding=' ')
makes the string "length" characters long.
int length() const
Returns the current length of the string.
virtual char * access()
provides access to the actual string held.
virtual const char * observe() const
observes the underlying pointer to the zero-terminated string.
A very common template for a dynamic array of bytes.
int getline(basis::abyte *buffer, int desired_size)
reads a line of text (terminated by a return) into the "buffer".
static size_t file_size_limit()
returns the maximum size that seek and length can support.
int write(const basis::abyte *buffer, int buffer_size)
writes "buffer_size" bytes into the file from "buffer".
void * file_handle()
provides a hook to get at the operating system's file handle.
byte_filer()
constructs an object that doesn't access a file yet.
void close()
shuts down the open file, if any.
@ FROM_START
offset is from the beginning of the file.
@ FROM_CURRENT
offset is from current cursor position.
@ FROM_END
offset is from the end of the file.
bool seek(int where, origins origin=FROM_START)
places the cursor in the file at "where", based on the "origin".
const basis::astring & name() const
returns the file name that the object is operating on.
int read(basis::abyte *buffer, int buffer_size)
reads "buffer_size" bytes from the file into "buffer".
bool eof()
returns true if the cursor is at (or after) the end of the file.
size_t tell()
returns the current position within the file, in terms of bytes.
void flush()
forces any pending writes to actually be saved to the file.
size_t length()
returns the file's total length, in bytes.
bool truncate()
truncates the file after the current position.
bool good()
returns true if the file seems to be in the appropriate desired state.
bool open(const basis::astring &fname, const basis::astring &permissions)
opens a file with "fname" and "permissions" as in the constructor.
Provides operations commonly needed on file names.
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
void reset(const basis::astring &name)
changes the file name held by the object.
#define NULL_POINTER
The value representing a pointer to nothing.
The guards collection helps in testing preconditions and reporting errors.
const int GIGABYTE
Number of bytes in a gigabyte.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
unsigned char abyte
A fairly important unit which is seldom defined...
bool non_negative(const type &a)
non_negative returns true if "a" is greater than or equal to zero.
A platform independent way to obtain the timestamp of a file.
const size_t BTFL_FILE_TELL_LIMIT
Support for unicode builds.
Aids in achievement of platform independence.