contents up previous next

wxtextfile

the wxtextfile is a simple class which allows to work with text files on line by line basis. it also understands the differences in line termination characters under different platforms and will not do anything bad to files with "non native" line termination sequences - in fact, it can be also used to modify the text files and change the line termination characters from one type (say dos) to another (say unix).

one word of warning: the class is not at all optimized for big files and thus it will load the file entirely into memory when opened. of course, you should not work in this way with large files (as an estimation, anything over 1 megabyte is surely too big for this class). on the other hand, it is not a serious limitation for small files like configuration files or program sources which are well handled by wxtextfile.

the typical things you may do with wxtextfile in order are:

derived from

no base class

include files

<wx/textfile.h>

data structures

the following constants identify the line termination type:

enum wxtextfiletype
{
    wxtextfiletype_none,  // incomplete (the last line of the file only)
    wxtextfiletype_unix,  // line is terminated with 'lf' = 0xa = 10 = '\n'
    wxtextfiletype_dos,   //                         'cr' 'lf'
    wxtextfiletype_mac    //                         'cr' = 0xd = 13 = '\r'
};
see also

wxfile

members

wxtextfile::wxtextfile
wxtextfile::wxtextfile
wxtextfile::~wxtextfile
wxtextfile::addline
wxtextfile::close
wxtextfile::create
wxtextfile::exists
wxtextfile::isopened
wxtextfile::getlinecount
wxtextfile::getline
wxtextfile::operator[]
wxtextfile::getcurrentline
wxtextfile::gotoline
wxtextfile::eof
wxtextfile::geteol
wxtextfile::getfirstline
wxtextfile::getnextline
wxtextfile::getprevline
wxtextfile::getlastline
wxtextfile::getlinetype
wxtextfile::guesstype
wxtextfile::getname
wxtextfile::insertline
wxtextfile::open
wxtextfile::removeline
wxtextfile::clear
wxtextfile::write


wxtextfile::wxtextfile

wxtextfile() const

default constructor, use create or open with a file name parameter to initialize the object.


wxtextfile::wxtextfile

wxtextfile(const wxstring& strfile) const

constructor does not load the file into memory, use open() to do it.


wxtextfile::~wxtextfile

~wxtextfile() const

destructor does nothing.


wxtextfile::addline

void addline(const wxstring& str, wxtextfiletype type = typedefault) const

adds a line to the end of file.


wxtextfile::close

bool close() const

closes the file and frees memory, losing all changes. use write() if you want to save them.


wxtextfile::create

bool create() const

bool create(const wxstring& strfile) const

creates the file with the given name or the name which was given in the constructor. the array of file lines is initially empty.

it will fail if the file already exists, open should be used in this case.


wxtextfile::exists

bool exists() const

return true if file exists - the name of the file should have been specified in the constructor before calling exists().


wxtextfile::isopened

bool isopened() const

returns true if the file is currently opened.


wxtextfile::getlinecount

size_t getlinecount() const

get the number of lines in the file.


wxtextfile::getline

wxstring& getline(size_t n) const

retrieves the line number n from the file. the returned line may be modified but you shouldn't add line terminator at the end - this will be done by wxtextfile.


wxtextfile::operator[]

wxstring& operator[](size_t n) const

the same as getline.


wxtextfile::getcurrentline

size_t getcurrentline() const

returns the current line: it has meaning only when you're using getfirstline()/getnextline() functions, it doesn't get updated when you're using "direct access" functions like getline(). getfirstline() and getlastline() also change the value of the current line, as well as gotoline().


wxtextfile::gotoline

void gotoline(size_t n) const

changes the value returned by getcurrentline and used by getfirstline()/getnextline().


wxtextfile::eof

bool eof() const

returns true if the current line is the last one.


wxtextfile::geteol

static const char* geteol(wxtextfiletype type = typedefault) const

get the line termination string corresponding to given constant. typedefault is the value defined during the compilation and corresponds to the native format of the platform, i.e. it will be wxtextfiletype_dos under windows, wxtextfiletype_unix under unix (including mac os x when compiling with the apple developer tools) and wxtextfiletype_mac under mac os (including mac os x when compiling with codewarrior).


wxtextfile::getfirstline

wxstring& getfirstline() const

this method together with getnextline() allows more "iterator-like" traversal of the list of lines, i.e. you may write something like:

wxtextfile file;
...
for ( str = file.getfirstline(); !file.eof(); str = file.getnextline() )
{
    // do something with the current line in str
}
// do something with the last line in str

wxtextfile::getnextline

wxstring& getnextline()

gets the next line (see getfirstline for the example).


wxtextfile::getprevline

wxstring& getprevline()

gets the previous line in the file.


wxtextfile::getlastline

wxstring& getlastline()

gets the last line of the file. together with getprevline it allows to enumerate the lines in the file from the end to the beginning like this:

wxtextfile file;
...
for ( str = file.getlastline();
      file.getcurrentline() > 0;
      str = file.getprevline() )
{
    // do something with the current line in str
}
// do something with the first line in str

wxtextfile::getlinetype

wxtextfiletype getlinetype(size_t n) const

get the type of the line (see also geteol)


wxtextfile::guesstype

wxtextfiletype guesstype() const

guess the type of file (which is supposed to be opened). if sufficiently many lines of the file are in dos/unix/mac format, the corresponding value will be returned. if the detection mechanism fails wxtextfiletype_none is returned.


wxtextfile::getname

const char* getname() const

get the name of the file.


wxtextfile::insertline

void insertline(const wxstring& str, size_t n, wxtextfiletype type = typedefault) const

insert a line before the line number n.


wxtextfile::open

bool open(wxmbconv& conv = wxconvutf8) const

bool open(const wxstring& strfile, wxmbconv& conv = wxconvutf8) const

open() opens the file with the given name or the name which was given in the constructor and also loads file in memory on success. it will fail if the file does not exist, create should be used in this case.

the conv argument is only meaningful in unicode build of wxwidgets when it is used to convert the file to wide character representation.


wxtextfile::removeline

void removeline(size_t n) const

delete line number n from the file.


wxtextfile::clear

void clear() const

delete all lines from the file, set current line number to 0.


wxtextfile::write

bool write(wxtextfiletype typenew = wxtextfiletype_none, wxmbconv& conv = wxconvutf8) const

change the file on disk. the typenew parameter allows you to change the file format (default argument means "don't change type") and may be used to convert, for example, dos files to unix.

the conv argument is only meaningful in unicode build of wxwidgets when it is used to convert all lines to multibyte representation before writing them them to physical file.

returns true if operation succeeded, false if it failed.