file.h File Reference
File base class.
More...
#include <stdio.h>
Go to the source code of this file.
Classes
Typedefs
File base class.
Enumerations
enum {
MDIO_ERROR = -1,
MDIO_LENGTH_ERRMSG = 240
}
enum {
MDIO_ERROR_NONE,
MDIO_ERROR_WARN,
MDIO_ERROR_BADVAL,
MDIO_ERROR_NOMEM,
MDIO_ERROR_OPEN,
MDIO_ERROR_CLOSE,
MDIO_ERROR_READ,
MDIO_ERROR_WRITE,
MDIO_ERROR_SEEK,
MDIO_ERROR_SYNTAX,
MDIO_ERROR_UNXEOF,
MDIO_LENGTH_ERRORLIST
}
Functions
Constructor.
Destructor.
Open file.
Close file.
Read entire line from text file.
Read from text file.
Write to text file.
Read from binary file.
Write to binary file.
Seek position within binary file.
Return nonzero if end-of-file has been reached, otherwise return 0.
Set error status on file.
Set error status on file with customized message.
Set error handler routine.
Returns the name of the file.
Returns the type of the file.
Returns the current error number set for the file.
Returns the current byte position for the file.
Returns the current line number for the file.
Returns the current diagnostic error message for the file.
Resets the error status for the file.
Alternative constructor.
Alternative destructor.
Detailed Description
File base class.
- Author:
- David J. Hardy
- Date:
- 2003-2005
The
mdio_File class performs the actual I/O along with diagnostic and error handling capabilities. It is the base class for the specialized reader and writer classes. For accessing supported types of MD files, instead use the higher level "derived" classes.
The file I/O routines are a somewhat simplified version of the C standard I/O library function calls. The fact that the actual I/O calls are hidden behind this layer means that the implementation could depend on the file type, for example, use of mmap() for large trajectory files.
Typedef Documentation
File base class.
Members should be treated as private.
Enumeration Type Documentation
- Enumerator:
-
MDIO_ERROR
Return value from failed function call.
File type flags.
- Enumerator:
-
MDIO_FILE_TEXT
Flag indicating text file.
MDIO_FILE_BINARY
Flag indicating binary file.
MDIO_FILE_READ
Flag indicating reading from a file.
MDIO_FILE_WRITE
Flag indicating writing to a file.
Error numbers.
- Enumerator:
-
MDIO_ERROR_NONE
No error.
MDIO_ERROR_WARN
A warning.
MDIO_ERROR_BADVAL
An illegal value pertaining to file.
MDIO_ERROR_NOMEM
Memory cannot be allocated.
MDIO_ERROR_OPEN
Cannot open a file.
MDIO_ERROR_CLOSE
Cannot close a file.
MDIO_ERROR_READ
Unable to read from file.
MDIO_ERROR_WRITE
Unable to write to file.
MDIO_ERROR_SEEK
Unable to perform a byte seek within file.
MDIO_ERROR_SYNTAX
Syntax error occurred with file.
MDIO_ERROR_UNXEOF
The end-of-file marker occurred unexpectedly.
- Enumerator:
-
MDIO_EOF
End-of-file constant for MDIO.
MDIO_SEEK_SET
Byte offset wrt beginning of file.
MDIO_SEEK_CUR
Byte offset wrt current position.
MDIO_SEEK_END
Byte offset wrt end of file.
Function Documentation
Alternative destructor.
Use to destroy a preallocated file object (i.e. one constructed using mdio_initializeFile() ).
Close file.
- Returns:
- 0 on success or
MDIO_ERROR on failure.
Constructor.
Creates dynamically allocated file object.
- Returns:
- Pointer to file object or
NULL on failure.
Destructor.
Frees dynamically allocated file object.
Alternative constructor.
Use to construct a preallocated file object. See mdio_createFile() for a description of expected arguments.
const char *
name,
int
filetype
)
Open file.
- Parameters:
-
[in] name The file name, nil-terminated string.
[in] filetype Bitwise OR'ed combination of file type flags.
The
name string is expected to persist until the file is closed with
mdio_closeFile() .
For filetype, must choose either MDIO_FILE_TEXT or MDIO_FILE_BINARY combined (using bitwise OR operator) with either MDIO_FILE_READ or MDIO_FILE_WRITE.
- Returns:
- 0 on success or
MDIO_ERROR on failure.
void *
buffer,
int
elemsz,
int
n
)
Read from binary file.
- Parameters:
-
[out] buffer Points to memory buffer.
[in] elemsz Size in bytes of each element.
[in] n The number of elements to be read.
Read
n elements each of size
elemsz bytes into the memory buffer pointed to by
buffer. Similar to
fread().
- Returns:
- The number of elements read. If it is less than
n, then either the end-of-file was reached or and error occurred.
char **
pbuf,
int *
pbuflen
)
Read entire line from text file.
- Parameters:
-
[in,out] pbuf Address of the char buffer pointer.
[in,out] pbuflen Address of the char buffer length variable.
Read text file up to and including the next newline character, storing the characters into the buffer. The
char buffer is nil-terminated. The address
pbuf needs to point to memory that can be managed using
realloc() so that the buffer can be expanded arbitrarily to contain the entire line, with the updated length stored back to
pbuflen. The caller is responsible for later freeing the memory allocation.
- Returns:
- The number of characters read into the buffer. End-of-file is indicated by returning 0 and storing the empty string into the
pbuf buffer. A blank line is indicated by returning 1 and storing "\n" into the pbuf buffer. An error is indicated by returning MDIO_ERROR.
Read from text file.
- Parameters:
-
[in,out] buf Points to the char buffer.
[in] buflen The length of the char buffer.
Read up to and including the next newline character, but no more than
buflen - 1 characters. Similar to
fgets().
- Returns:
- The number of characters read into the buffer. End-of-file is indicated by returning 0 and storing the empty string into the
pbuf buffer. A blank line is indicated by returning 1 and storing "\n" into the pbuf buffer. An error is indicated by returning MDIO_ERROR.
long
offset,
int
whence
)
Seek position within binary file.
- Parameters:
-
[in] offset Byte offset relative to some location.
[in] whence Flag to indicate the byte offset location.
The seek offset in a binary file may be measured from one of the following locations:
MDIO_SEEK_SET - beginning of file
MDIO_SEEK_CUR - current location in file
MDIO_SEEK_END - end of file
The current file position is then changed to the indicated location.
- Returns:
- 0 on success or
MDIO_ERROR on failure.
Set error status on file.
- Parameters:
-
[in] errnum MDIO error number.
The error status on the file is set to the indicated error number and a corresponding diagnostic message. The message might include information on the name and location within the file where the error occurred, depending on the type of error.
An illegal value for errnum will terminate the program.
If the error handler has been set (see mdio_setErrorHandlerFile() ), then that routine will be invoked before returning.
- Returns:
- The value from the error handler routine or
errnum if the error handler has been set to NULL.
void mdio_setErrorHandlerFile
(
mdio_File *
,
)
int
errnum,
const char *
msg
)
Set error status on file with customized message.
- Parameters:
-
[in] errnum MDIO error number.
[in] msg A nil-terminated string.
This routine is identical to
mdio_setErrorFile() with the addition that the
msg string will be appended to the diagnostic message.
const void *
buffer,
int
elemsz,
int
n
)
Write to binary file.
- Parameters:
-
[in] buffer Points to memory buffer.
[in] elemsz Size in bytes of each element.
[in] n The number of elements to be written.
Write the
n $ \times $
elemsz bytes stored in
buffer to the file. Similar to
fwrite().
- Returns:
- The number of elements written. If it is less than
n, then an error has occurred.
Write to text file.
- Parameters:
-
[in] buf Points to a nil-terminated string.
Write the string, not including the nil-terminator, to the file. Newline characters must be explicitly included in the string. Similar to
fputs().
- Returns:
- The number of characters written or
MDIO_ERROR on error.
Generated on Mon Sep 26 10:55:19 2005 for MDX by
doxygen 1.4.4