edelib  2.1.0
Public Member Functions | Related Functions | List of all members
File Class Reference

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)
 

Detailed Description

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
*
Todo:
error checks during stream read/write (ferror)

Constructor & Destructor Documentation

File ( )

Creates empty file object.

File ( const char *  n,
int  m 
)

Open stream for reading or writing

Parameters
nfile name to open
mhow to open file; default is read-only
~File ( )

Cleans reserved data, and closes all possible opened descriptors.

Member Function Documentation

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.

Returns
true if end of file is reached.
int getch ( void  )

Read an character from stream.

Returns
character or EOF.
const char* name ( void  ) const

Returns name of curent opened stream. If is called close() before, it will return NULL.

Returns
name of curent opened stream
bool open ( const char *  fname,
int  mode = FIO_READ 
)

Open file in given mode

Parameters
fnamefile name to open
modehow to open file; default is read-only
int printf ( const char *  fmt,
  ... 
)

printf function on the stream.

Returns
size of writen data
int putch ( int  c)

Write character to the stream.

Returns
a character written, or EOF in case of error.
int read ( void *  buff,
int  typesz,
int  buffsz 
)

Read data from the stream, with specified size.

Returns
number of read data
Parameters
buffwhere to place data
typeszsize of each item
buffszsize 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.

Todo:
it does not return 1 when line contains only '\n' char. Should return?
Returns
size of readed data or EOF for end.
Parameters
buffwhere to place content
buffszsize of buffer
int write ( const void *  buff,
int  typesz,
int  buffsz 
)

Write data to the stream, with specified size.

Returns
number of written data
Parameters
buffdata to write
typeszsize of each item
buffszsize of buffer
int write ( const char *  buff,
unsigned int  buffsz 
)

Write char data to the stream.

Returns
number of written data
Parameters
buffdata to write
buffszsize of buffer
int write ( const char *  buff)

Same as write(buff, strlen(buff))

Friends And Related Function Documentation

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.

Note
src must exists and checking if dest exists is not done. This means if dest exists, it will be overwritten. Use file_exists() to check does it really exists.

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.

Returns
true if is able to open src and able to write in dest, or false if failed in both (or setting permissions/time if exact paramter is used)
Parameters
srcis source file to be copied
destis destination
exactwill try to set exact src info (timestamp,owner,group,...) to dest
bool file_executable ( const char *  name)
related

Check if file is executable

Deprecated:
with file_test()
bool file_exists ( const char *  name)
related

Check if file exists and is regular file

Deprecated:
with file_test()
String file_path ( const char *  fname,
bool  skip_link = false 
)
related

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.

Returns
full path if file exists or empty string as noted above
Parameters
fnamefile to look for
skip_linkif true symbolic links are ignored
bool file_readable ( const char *  name)
related

Check if file is readable

Deprecated:
with file_test()
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).

Returns
true if operation was succesfull, or false if is unable to proceed
Parameters
nameis 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.)

Returns
true if succeded or false if not
Parameters
fromis what file to rename
tois new name
bool file_writeable ( const char *  name)
related

Check if file is writeable

Deprecated:
with file_test()

The documentation for this class was generated from the following file: