edelib
2.0.0
|
A system file io stream. More...
#include <edelib/File.h>
Public Member Functions | |
File () | |
File (const char *n, int m) | |
~File () | |
bool | open (const char *fname, int mode=FIO_READ) |
void | close (void) |
const char * | name (void) const |
bool | eof (void) |
int | getch (void) |
int | read (void *buff, int typesz, int buffsz) |
int | readline (char *buff, int buffsz) |
int | putch (int c) |
int | write (const void *buff, int typesz, int buffsz) |
int | write (const char *buff, unsigned int buffsz) |
int | write (const char *buff) |
int | printf (const char *fmt,...) |
Related Functions | |
(Note that these are not member functions.) | |
bool | file_exists (const char *name) |
bool | file_readable (const char *name) |
bool | file_writeable (const char *name) |
bool | file_executable (const char *name) |
bool | file_remove (const char *name) |
bool | file_copy (const char *src, const char *dest, bool exact=false) |
bool | file_rename (const char *from, const char *to) |
String | file_path (const char *fname, bool skip_link=false) |
A system file io stream.
File is portable wrapper for various functions for reading and writing files. Also brings automatic descriptor closing or possible another file openning during object lifetime.
Here is a sample of common usage:
File f("foo.txt"); if(!f) printf("can't open %s\n", f.name()); while(!f.eof()) printf("%s", f.readline()); f.close(); // optional
File | ( | ) |
Creates empty file object.
File | ( | const char * | n, |
int | m | ||
) |
Open stream for reading or writing
n | file name to open |
m | how to open file; default is read-only |
~File | ( | ) |
Cleans reserved data, and closes all possible opened descriptors.
void close | ( | void | ) |
Close current open descriptor, and clean reserved data. If is not opened, function will do nothing, except to clean reserved data if needed.
bool eof | ( | void | ) |
Checks if end of file reached.
int getch | ( | void | ) |
Read an character from stream.
const char* name | ( | void | ) | const |
Returns name of curent opened stream. If is called close() before, it will return NULL.
Open file in given mode
fname | file name to open |
mode | how to open file; default is read-only |
int printf | ( | const char * | fmt, |
... | |||
) |
printf function on the stream.
int putch | ( | int | c | ) |
Write character to the stream.
int read | ( | void * | buff, |
int | typesz, | ||
int | buffsz | ||
) |
Read data from the stream, with specified size.
buff | where to place data |
typesz | size of each item |
buffsz | size of buffer |
int readline | ( | char * | buff, |
int | buffsz | ||
) |
Read line from stream in specified buffer, with given size. If buffer size is less than read line, only given size will be filled. A '\0' is stored as last character in buffer. It will return EOF if end of stream is reached.
buff | where to place content |
buffsz | size of buffer |
int write | ( | const void * | buff, |
int | typesz, | ||
int | buffsz | ||
) |
Write data to the stream, with specified size.
buff | data to write |
typesz | size of each item |
buffsz | size of buffer |
int write | ( | const char * | buff, |
unsigned int | buffsz | ||
) |
Write char data to the stream.
buff | data to write |
buffsz | size of buffer |
int write | ( | const char * | buff | ) |
Same as write(buff, strlen(buff))
bool file_copy | ( | const char * | src, |
const char * | dest, | ||
bool | exact = false |
||
) | [related] |
Copy file from src to dest. This function is implemented via getc() (not like standard cp command) due it's simplicity.
If exact parameter is set to true, it will set the same permissions and access/change time to the dest as src. On other hand, if any of stages (changing permissions or setting time) fails file_copy() will return false.
src | is source file to be copied |
dest | is destination |
exact | will try to set exact src info (timestamp,owner,group,...) to dest |
bool file_executable | ( | const char * | name | ) | [related] |
Check if file is executable
bool file_exists | ( | const char * | name | ) | [related] |
Check if file exists and is regular file
Return full path of given argument, looking at $PATH environment variable. If is unable to read $PATH or file does not exists or resolved path is not file, it will return empty string.
Returned path can be either link to binary or actual binary (depends on how entries in $PATH are placed). For example mv command is often in /bin, and it's link is (often) in /usr/bin. If PATH contains something like /usr/local/bin:/usr/bin:/bin, /usr/bin/mv will be returned.
For this casses, setting skip_link to true will return /bin/mv.
fname | file to look for |
skip_link | if true symbolic links are ignored |
bool file_readable | ( | const char * | name | ) | [related] |
Check if file is readable
bool file_remove | ( | const char * | name | ) | [related] |
Remove file at given path. It will call system's unlink() instead remove() since remove() is not compatible between latest versions of libc and libc4 or libc5 (see man remove).
name | is absolute (or relative) path to the file |
bool file_rename | ( | const char * | from, |
const char * | to | ||
) | [related] |
Rename file. Assumed is that name to be renamed to does not exists (file, directory, link and etc.)
from | is what file to rename |
to | is new name |
bool file_writeable | ( | const char * | name | ) | [related] |
Check if file is writeable