2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: textfile.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxTextFile
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-10-04 07:01:50 -04:00
|
|
|
|
2008-11-22 08:56:16 -05:00
|
|
|
/**
|
|
|
|
The line termination type (kept wxTextFileType name for compability).
|
|
|
|
*/
|
2008-10-04 07:01:50 -04:00
|
|
|
enum wxTextFileType
|
|
|
|
{
|
|
|
|
wxTextFileType_None, //!< incomplete (the last line of the file only)
|
2008-10-04 10:30:56 -04:00
|
|
|
wxTextFileType_Unix, //!< line is terminated with 'LF' = 0xA = 10 = '\\n'
|
2008-10-04 07:01:50 -04:00
|
|
|
wxTextFileType_Dos, //!< line is terminated with 'CR' 'LF'
|
2008-10-04 10:30:56 -04:00
|
|
|
wxTextFileType_Mac, //!< line is terminated with 'CR' = 0xD = 13 = '\\r'
|
2008-10-04 07:01:50 -04:00
|
|
|
wxTextFileType_Os2 //!< line is terminated with 'CR' 'LF'
|
|
|
|
};
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxTextFile
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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).
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
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
|
2008-10-04 07:01:50 -04:00
|
|
|
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
|
2008-03-08 08:52:38 -05:00
|
|
|
limitation for small files like configuration files or program sources
|
|
|
|
which are well handled by wxTextFile.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
The typical things you may do with wxTextFile in order are:
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-10-04 07:01:50 -04:00
|
|
|
- Create and open it: this is done with either wxTextFile::Create or wxTextFile::Open
|
|
|
|
function which opens the file (name may be specified either as the argument to
|
|
|
|
these functions or in the constructor), reads its contents in memory (in the
|
2008-11-22 08:56:16 -05:00
|
|
|
case of wxTextFile::Open()) and closes it.
|
2008-10-04 07:01:50 -04:00
|
|
|
- Work with the lines in the file: this may be done either with "direct
|
|
|
|
access" functions like wxTextFile::GetLineCount and wxTextFile::GetLine
|
|
|
|
(@e operator[] does exactly the same but looks more like array addressing)
|
|
|
|
or with "sequential access" functions which include wxTextFile::GetFirstLine,
|
|
|
|
wxTextFile::GetNextLine and also wxTextFile::GetLastLine, wxTextFile::GetPrevLine.
|
|
|
|
For the sequential access functions the current line number is maintained: it is
|
|
|
|
returned by wxTextFile::GetCurrentLine and may be changed with wxTextFile::GoToLine.
|
|
|
|
- Add/remove lines to the file: wxTextFile::AddLine and wxTextFile::InsertLine
|
|
|
|
add new lines while wxTextFile::RemoveLine deletes the existing ones.
|
|
|
|
wxTextFile::Clear resets the file to empty.
|
|
|
|
- Save your changes: notice that the changes you make to the file will @b not be
|
|
|
|
saved automatically; calling wxTextFile::Close or doing nothing discards them!
|
|
|
|
To save the changes you must explicitly callwxTextFile::Write - here, you may
|
|
|
|
also change the line termination type if you wish.
|
2008-03-08 09:43:31 -05:00
|
|
|
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{file}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxFile
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxTextFile
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-10-04 07:01:50 -04:00
|
|
|
/**
|
|
|
|
Default constructor, use Create() or Open() with a file name parameter to
|
|
|
|
initialize the object.
|
|
|
|
*/
|
|
|
|
wxTextFile();
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
Constructor does not load the file into memory, use Open() to do it.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
wxTextFile(const wxString& strFile);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor does nothing.
|
|
|
|
*/
|
2008-09-27 07:21:10 -04:00
|
|
|
virtual ~wxTextFile();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Adds a line to the end of file.
|
|
|
|
*/
|
|
|
|
void AddLine(const wxString& str,
|
2008-10-28 11:36:26 -04:00
|
|
|
wxTextFileType type = wxTextBuffer::typeDefault);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Delete all lines from the file, set current line number to 0.
|
|
|
|
*/
|
2008-10-13 07:09:56 -04:00
|
|
|
void Clear();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Closes the file and frees memory, @b "losing all changes".
|
|
|
|
Use Write() if you want to save them.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-13 07:09:56 -04:00
|
|
|
bool Close();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-12-12 11:10:08 -05:00
|
|
|
Creates the file with the name which was given in the
|
|
|
|
wxTextFile(const wxString&) constructor.
|
|
|
|
The array of file lines is initially empty.
|
2008-10-04 07:01:50 -04:00
|
|
|
|
|
|
|
It will fail if the file already exists, Open() should be used in this case.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2009-01-08 11:33:08 -05:00
|
|
|
bool Create();
|
2008-12-12 11:10:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Creates the file with the given name.
|
|
|
|
The array of file lines is initially empty.
|
|
|
|
|
|
|
|
It will fail if the file already exists, Open() should be used in this case.
|
|
|
|
*/
|
2009-01-08 11:33:08 -05:00
|
|
|
bool Create(const wxString& strFile);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the current line is the last one.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool Eof() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Return @true if file exists - the name of the file should have been specified
|
|
|
|
in the constructor before calling Exists().
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool Exists() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current line: it has meaning only when you're using
|
|
|
|
GetFirstLine()/GetNextLine() functions, it doesn't get updated when
|
2008-10-04 07:01:50 -04:00
|
|
|
you're using "direct access" functions like GetLine().
|
|
|
|
GetFirstLine() and GetLastLine() also change the value of the current line,
|
|
|
|
as well as GoToLine().
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
size_t GetCurrentLine() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Get the line termination string corresponding to given constant.
|
|
|
|
|
|
|
|
@e typeDefault is the value defined during the compilation and corresponds
|
|
|
|
to the native format of the platform, i.e. it will be @c wxTextFileType_Dos
|
|
|
|
under Windows, @c wxTextFileType_Unix under Unix (including Mac OS X when
|
|
|
|
compiling with the Apple Developer Tools) and @c wxTextFileType_Mac under
|
|
|
|
Mac OS (including Mac OS X when compiling with CodeWarrior).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2009-01-10 17:42:09 -05:00
|
|
|
static const wxChar* GetEOL(wxTextFileType type = wxTextBuffer::typeDefault);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
This method together with GetNextLine() allows more "iterator-like"
|
|
|
|
traversal of the list of lines, i.e. you may write something like:
|
|
|
|
|
|
|
|
@code
|
|
|
|
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
|
|
|
|
@endcode
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& GetFirstLine();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Gets the last line of the file.
|
|
|
|
|
|
|
|
Together with GetPrevLine() it allows to enumerate the lines
|
2008-03-08 08:52:38 -05:00
|
|
|
in the file from the end to the beginning like this:
|
2008-10-04 07:01:50 -04:00
|
|
|
|
|
|
|
@code
|
|
|
|
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
|
|
|
|
@endcode
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& GetLastLine();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Retrieves the line number @a 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.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& GetLine(size_t n) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the number of lines in the file.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
size_t GetLineCount() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Get the type of the line (see also wxTextFile::GetEOL).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxTextFileType GetLineType(size_t n) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the name of the file.
|
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
const wxString& GetName() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Gets the next line (see GetFirstLine() for the example).
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& GetNextLine();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Gets the previous line in the file.
|
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& GetPrevLine();
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Changes the value returned by GetCurrentLine() and used by GetFirstLine()
|
|
|
|
and GetNextLine().
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-13 07:09:56 -04:00
|
|
|
void GoToLine(size_t n);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
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 @c wxTextFileType_None is returned.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxTextFileType GuessType() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Insert a line before the line number @a n.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void InsertLine(const wxString& str, size_t n,
|
2008-10-28 11:36:26 -04:00
|
|
|
wxTextFileType type = wxTextBuffer::typeDefault);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if the file is currently opened.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOpened() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
Open() opens the file with the given name or the name which was given in the
|
2008-12-12 11:10:08 -05:00
|
|
|
wxTextFile(const wxString&) constructor and also loads file in memory on success.
|
2008-10-04 07:01:50 -04:00
|
|
|
|
|
|
|
It will fail if the file does not exist, Create() should be used in this case.
|
|
|
|
|
|
|
|
The @a conv argument is only meaningful in Unicode build of wxWidgets when
|
2008-03-08 08:52:38 -05:00
|
|
|
it is used to convert the file to wide character representation.
|
|
|
|
*/
|
2008-10-04 07:01:50 -04:00
|
|
|
bool Open(const wxMBConv& conv = wxConvAuto()) const;
|
|
|
|
bool Open(const wxString& strFile, const wxMBConv& conv = wxConvAuto()) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/**
|
2008-03-09 08:33:59 -04:00
|
|
|
Delete line number @a n from the file.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-13 07:09:56 -04:00
|
|
|
void RemoveLine(size_t n);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
2008-10-04 07:01:50 -04:00
|
|
|
Change the file on disk.
|
|
|
|
|
|
|
|
The @a 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 @a conv argument is only meaningful in Unicode build of wxWidgets when
|
2008-10-04 10:30:56 -04:00
|
|
|
it is used to convert all lines to multibyte representation before writing
|
2008-03-08 08:52:38 -05:00
|
|
|
them to physical file.
|
2008-10-04 07:01:50 -04:00
|
|
|
|
|
|
|
@return
|
|
|
|
@true if operation succeeded, @false if it failed.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-04 07:01:50 -04:00
|
|
|
bool Write(wxTextFileType typeNew = wxTextFileType_None,
|
2008-10-27 17:26:54 -04:00
|
|
|
const wxMBConv& conv = wxConvAuto());
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
The same as GetLine().
|
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
wxString& operator[](size_t n) const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|