2012-01-09 08:35:06 +00:00
|
|
|
#ifndef RICHIO_H_
|
|
|
|
#define RICHIO_H_
|
2009-12-11 04:55:24 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 2007 KiCad Developers, see change_log.txt for contributors.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// This file defines 3 classes useful for working with DSN text files and is named
|
|
|
|
// "richio" after its author, Richard Hollenbeck, aka Dick Hollenbeck.
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
2014-01-02 02:17:07 +00:00
|
|
|
#include <utf8.h>
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
// I really did not want to be dependent on wxWidgets in richio
|
|
|
|
// but the errorText needs to be wide char so wxString rules.
|
|
|
|
#include <wx/wx.h>
|
2011-02-04 02:40:47 +00:00
|
|
|
#include <stdio.h>
|
2010-12-30 22:15:53 +00:00
|
|
|
|
|
|
|
|
2013-09-26 05:29:54 +00:00
|
|
|
/**
|
2013-09-26 15:02:46 +00:00
|
|
|
* Function StrPrintf
|
2013-09-26 05:29:54 +00:00
|
|
|
* is like sprintf() but the output is appended to a std::string instead of to a
|
|
|
|
* character array.
|
2013-09-26 15:02:46 +00:00
|
|
|
* @param aResult is the string to append to, previous text is not clear()ed.
|
|
|
|
* @param aFormat is a printf() style format string.
|
|
|
|
* @return int - the count of bytes appended to the result string, no terminating
|
|
|
|
* nul is included.
|
2013-09-26 05:29:54 +00:00
|
|
|
*/
|
2013-09-26 15:02:46 +00:00
|
|
|
int
|
|
|
|
#if defined(__GNUG__)
|
|
|
|
__attribute__ ((format (printf, 2, 3)))
|
|
|
|
#endif
|
|
|
|
StrPrintf( std::string* aResult, const char* aFormat, ... );
|
2013-09-26 05:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-26 15:02:46 +00:00
|
|
|
* Function StrPrintf
|
2013-09-26 05:29:54 +00:00
|
|
|
* is like sprintf() but the output is returned in a std::string instead of to a
|
|
|
|
* character array.
|
2013-09-26 15:02:46 +00:00
|
|
|
* @param aResult is the string to append to, previous text is not clear()ed.
|
|
|
|
* @param aFormat is a printf() style format string.
|
|
|
|
* @return std::string - the result of the sprintf().
|
2013-09-26 05:29:54 +00:00
|
|
|
*/
|
2013-09-26 15:02:46 +00:00
|
|
|
std::string
|
|
|
|
#if defined(__GNUG__)
|
|
|
|
__attribute__ ((format (printf, 1, 2)))
|
|
|
|
#endif
|
|
|
|
StrPrintf( const char* format, ... );
|
2013-09-26 05:29:54 +00:00
|
|
|
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
/**
|
|
|
|
* @ingroup exception_types
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-12-02 21:56:47 +00:00
|
|
|
#define IO_FORMAT _( "IO_ERROR: %s\nfrom %s : %s" )
|
2013-07-01 06:47:36 +00:00
|
|
|
#define PARSE_FORMAT _( "PARSE_ERROR: %s in input/source\n'%s'\nline %d\noffset %d\nfrom %s : %s" )
|
2010-12-30 22:15:53 +00:00
|
|
|
|
|
|
|
// references:
|
|
|
|
// http://stackoverflow.com/questions/2670816/how-can-i-use-the-compile-time-constant-line-in-a-string
|
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
|
|
|
|
|
|
|
// use one of the following __LOC__ defs, depending on whether your
|
|
|
|
// compiler supports __func__ or not, and how it handles __LINE__
|
2011-12-06 06:19:36 +00:00
|
|
|
#define __LOC__ ((std::string(__FUNCTION__) + "() : line ") + TOSTRING(__LINE__)).c_str()
|
2010-12-30 22:15:53 +00:00
|
|
|
//#define __LOC__ TOSTRING(__LINE__)
|
|
|
|
|
|
|
|
/// macro which captures the "call site" values of __FILE_ & __LOC__
|
|
|
|
#define THROW_IO_ERROR( msg ) throw IO_ERROR( __FILE__, __LOC__, msg )
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Struct IO_ERROR
|
2013-12-09 18:09:58 +00:00
|
|
|
* is a class used to hold an error message and may be used when throwing exceptions
|
2010-12-30 22:15:53 +00:00
|
|
|
* containing meaningful error messages.
|
|
|
|
* @author Dick Hollenbeck
|
|
|
|
*/
|
|
|
|
struct IO_ERROR // : std::exception
|
|
|
|
{
|
|
|
|
wxString errorText;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param aThrowersFile is the __FILE__ preprocessor macro but generated
|
|
|
|
* at the source file of thrower.
|
|
|
|
*
|
|
|
|
* @param aThrowersLoc can be either a function name, such as __func__
|
|
|
|
* or a stringified __LINE__ preprocessor macro but generated
|
|
|
|
* at the source function of the thrower, or concatonation. Use macro
|
|
|
|
* THROW_IO_ERROR() to wrap a call to this constructor at the call site.
|
|
|
|
*
|
|
|
|
* @param aMsg is error text that will be streamed through wxString.Printf()
|
|
|
|
* using the format string IO_FORMAT above.
|
|
|
|
*/
|
2013-10-03 15:12:25 +00:00
|
|
|
explicit IO_ERROR( const char* aThrowersFile,
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* aThrowersLoc,
|
|
|
|
const wxString& aMsg )
|
|
|
|
{
|
|
|
|
init( aThrowersFile, aThrowersLoc, aMsg );
|
|
|
|
}
|
|
|
|
|
2013-10-03 15:12:25 +00:00
|
|
|
explicit IO_ERROR( const char* aThrowersFile,
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* aThrowersLoc,
|
|
|
|
const std::string& aMsg )
|
|
|
|
{
|
|
|
|
init( aThrowersFile, aThrowersLoc, wxString::FromUTF8( aMsg.c_str() ) );
|
|
|
|
}
|
|
|
|
|
2013-10-03 15:12:25 +00:00
|
|
|
explicit IO_ERROR( const char* aThrowersFile,
|
|
|
|
const char* aThrowersLoc,
|
|
|
|
const char* aMsg )
|
|
|
|
{
|
|
|
|
init( aThrowersFile, aThrowersLoc, wxString::FromUTF8( aMsg ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/// handle the case where _() is passed as aMsg.
|
|
|
|
explicit IO_ERROR( const char* aThrowersFile,
|
2010-12-30 22:15:53 +00:00
|
|
|
const char* aThrowersLoc,
|
|
|
|
const wxChar* aMsg )
|
|
|
|
{
|
|
|
|
init( aThrowersFile, aThrowersLoc, wxString( aMsg ) );
|
|
|
|
}
|
|
|
|
|
2012-12-08 23:58:03 +00:00
|
|
|
void init( const char* aThrowersFile, const char* aThrowersLoc, const wxString& aMsg );
|
2010-12-30 22:15:53 +00:00
|
|
|
|
|
|
|
IO_ERROR() {}
|
|
|
|
|
2013-12-09 18:09:58 +00:00
|
|
|
// Destructor is virtual because PARSE_ERROR is derived from it and
|
|
|
|
// boost::ptr_vector lists consisting of both will need a virtual destructor.
|
|
|
|
virtual ~IO_ERROR() throw ( /*none*/ ){}
|
2010-12-30 22:15:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-16 17:40:23 +00:00
|
|
|
* Struct PARSE_ERROR
|
2011-01-01 22:56:07 +00:00
|
|
|
* contains a filename or source description, a problem input line, a line number,
|
|
|
|
* a byte offset, and an error message which contains the the caller's report and his
|
|
|
|
* call site information: CPP source file, function, and line number.
|
2010-12-30 22:15:53 +00:00
|
|
|
* @author Dick Hollenbeck
|
|
|
|
*/
|
|
|
|
struct PARSE_ERROR : public IO_ERROR
|
|
|
|
{
|
|
|
|
// wxString errorText is still public from IO_ERROR
|
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
int lineNumber; ///< at which line number, 1 based index.
|
2012-05-15 13:58:55 +00:00
|
|
|
int byteIndex; ///< at which byte offset within the line, 1 based index
|
2011-01-01 22:56:07 +00:00
|
|
|
|
|
|
|
/// problem line of input [say, from a LINE_READER].
|
|
|
|
/// this is brought up in original byte format rather than wxString form, incase
|
|
|
|
/// there was a problem with the encoding, in which case converting to wxString is
|
|
|
|
/// not reliable in this context.
|
|
|
|
std::string inputLine;
|
2010-12-30 22:15:53 +00:00
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* which is normally called via the macro THROW_PARSE_ERROR so that
|
|
|
|
* __FILE__ and __LOC__ can be captured from the call site.
|
|
|
|
*/
|
2010-12-30 22:15:53 +00:00
|
|
|
PARSE_ERROR( const char* aThrowersFile, const char* aThrowersLoc,
|
|
|
|
const wxString& aMsg, const wxString& aSource,
|
2011-01-01 22:56:07 +00:00
|
|
|
const char* aInputLine,
|
2010-12-30 22:15:53 +00:00
|
|
|
int aLineNumber, int aByteIndex ) :
|
|
|
|
IO_ERROR()
|
|
|
|
{
|
2011-01-01 22:56:07 +00:00
|
|
|
init( aThrowersFile, aThrowersLoc, aMsg, aSource, aInputLine, aLineNumber, aByteIndex );
|
2010-12-30 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init( const char* aThrowersFile, const char* aThrowersLoc,
|
|
|
|
const wxString& aMsg, const wxString& aSource,
|
2011-01-01 22:56:07 +00:00
|
|
|
const char* aInputLine,
|
2012-12-08 23:58:03 +00:00
|
|
|
int aLineNumber, int aByteIndex );
|
2010-12-30 22:15:53 +00:00
|
|
|
|
|
|
|
~PARSE_ERROR() throw ( /*none*/ ){}
|
|
|
|
};
|
|
|
|
|
2011-01-01 22:56:07 +00:00
|
|
|
|
|
|
|
#define THROW_PARSE_ERROR( aMsg, aSource, aInputLine, aLineNumber, aByteIndex ) \
|
|
|
|
throw PARSE_ERROR( __FILE__, __LOC__, aMsg, aSource, aInputLine, aLineNumber, aByteIndex )
|
|
|
|
|
|
|
|
|
2010-12-30 22:15:53 +00:00
|
|
|
/** @} exception_types */
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
|
2010-10-20 22:47:34 +00:00
|
|
|
#define LINE_READER_LINE_DEFAULT_MAX 100000
|
|
|
|
#define LINE_READER_LINE_INITIAL_SIZE 5000
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class LINE_READER
|
2010-10-05 07:15:29 +00:00
|
|
|
* is an abstract class from which implementation specific LINE_READERs may
|
|
|
|
* be derived to read single lines of text and manage a line number counter.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
|
|
|
class LINE_READER
|
|
|
|
{
|
|
|
|
protected:
|
2010-10-20 22:47:34 +00:00
|
|
|
unsigned length; ///< no. bytes in line before trailing nul.
|
2010-12-28 02:44:30 +00:00
|
|
|
unsigned lineNum;
|
2010-10-20 22:47:34 +00:00
|
|
|
|
|
|
|
char* line; ///< the read line of UTF8 text
|
|
|
|
unsigned capacity; ///< no. bytes allocated for line.
|
|
|
|
|
|
|
|
unsigned maxLineLength; ///< maximum allowed capacity using resizing.
|
|
|
|
|
|
|
|
wxString source; ///< origin of text lines, e.g. filename or "clipboard"
|
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
/**
|
|
|
|
* Function expandCapacity
|
2012-12-29 09:54:25 +00:00
|
|
|
* will expand the capacity of @a line up to maxLineLength but not greater, so
|
2011-01-12 04:51:29 +00:00
|
|
|
* be careful about making assumptions of @a capacity after calling this.
|
|
|
|
*/
|
2010-10-20 22:47:34 +00:00
|
|
|
void expandCapacity( unsigned newsize );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
|
2010-03-03 06:13:46 +00:00
|
|
|
public:
|
2010-10-05 07:15:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor LINE_READER
|
|
|
|
* builds a line reader and fixes the length of the maximum supported
|
|
|
|
* line length to @a aMaxLineLength.
|
|
|
|
*/
|
2010-10-20 22:47:34 +00:00
|
|
|
LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
virtual ~LINE_READER();
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function ReadLine
|
|
|
|
* reads a line of text into the buffer and increments the line number
|
2010-10-21 13:21:32 +00:00
|
|
|
* counter. If the line is larger than aMaxLineLength passed to the
|
|
|
|
* constructor, then an exception is thrown. The line is nul terminated.
|
2012-11-27 13:50:01 +00:00
|
|
|
* @return char* - The beginning of the read line, or NULL if EOF.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR when a line is too long.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2012-11-27 13:50:01 +00:00
|
|
|
virtual char* ReadLine() throw( IO_ERROR ) = 0;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
/**
|
|
|
|
* Function GetSource
|
|
|
|
* returns the name of the source of the lines in an abstract sense.
|
|
|
|
* This may be a file or it may be the clipboard or any other source
|
|
|
|
* of lines of text. The returned string is useful for reporting error
|
|
|
|
* messages.
|
|
|
|
*/
|
2011-01-12 04:51:29 +00:00
|
|
|
virtual const wxString& GetSource() const
|
2010-10-05 07:15:29 +00:00
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2011-01-12 04:51:29 +00:00
|
|
|
/**
|
|
|
|
* Function Line
|
|
|
|
* returns a pointer to the last line that was read in.
|
|
|
|
*/
|
2012-11-27 13:50:01 +00:00
|
|
|
char* Line() const
|
2011-01-12 04:51:29 +00:00
|
|
|
{
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
/**
|
|
|
|
* Operator char*
|
|
|
|
* is a casting operator that returns a char* pointer to the start of the
|
|
|
|
* line buffer.
|
|
|
|
*/
|
2010-10-20 22:47:34 +00:00
|
|
|
operator char* () const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
2011-01-12 04:51:29 +00:00
|
|
|
return Line();
|
2009-12-11 04:55:24 +00:00
|
|
|
}
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
/**
|
|
|
|
* Function Line Number
|
|
|
|
* returns the line number of the last line read from this LINE_READER. Lines
|
|
|
|
* start from 1.
|
|
|
|
*/
|
2011-01-12 04:51:29 +00:00
|
|
|
virtual unsigned LineNumber() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
return lineNum;
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:15:29 +00:00
|
|
|
/**
|
|
|
|
* Function Length
|
|
|
|
* returns the number of bytes in the last line read from this LINE_READER.
|
|
|
|
*/
|
2012-11-27 13:50:01 +00:00
|
|
|
unsigned Length() const
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-03 06:13:46 +00:00
|
|
|
/**
|
|
|
|
* Class FILE_LINE_READER
|
|
|
|
* is a LINE_READER that reads from an open file. File must be already open
|
2010-10-05 07:15:29 +00:00
|
|
|
* so that this class can exist without any UI policy.
|
2010-03-03 06:13:46 +00:00
|
|
|
*/
|
|
|
|
class FILE_LINE_READER : public LINE_READER
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
2010-12-28 02:44:30 +00:00
|
|
|
bool iOwn; ///< if I own the file, I'll promise to close it, else not.
|
|
|
|
FILE* fp; ///< I may own this file, but might not.
|
2010-03-03 06:13:46 +00:00
|
|
|
|
|
|
|
public:
|
2010-10-05 07:15:29 +00:00
|
|
|
|
2012-11-27 12:33:12 +00:00
|
|
|
/**
|
|
|
|
* Constructor FILE_LINE_READER
|
|
|
|
* takes @a aFileName and the size of the desired line buffer and opens
|
|
|
|
* the file and assumes the obligation to close it.
|
|
|
|
*
|
|
|
|
* @param aFileName is the name of the file to open and to use for error reporting purposes.
|
|
|
|
*
|
|
|
|
* @param aStartingLineNumber is the initial line number to report on error, and is
|
|
|
|
* accessible here for the case where multiple DSNLEXERs are reading from the
|
|
|
|
* same file in sequence, all from the same open file (with @a doOwn = false).
|
|
|
|
* Internally it is incremented by one after each ReadLine(), so the first
|
|
|
|
* reported line number will always be one greater than what is provided here.
|
|
|
|
*
|
|
|
|
* @param aMaxLineLength is the number of bytes to use in the line buffer.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR if @a aFileName cannot be opened.
|
|
|
|
*/
|
|
|
|
FILE_LINE_READER( const wxString& aFileName,
|
|
|
|
unsigned aStartingLineNumber = 0,
|
|
|
|
unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX ) throw( IO_ERROR );
|
|
|
|
|
2010-03-03 06:13:46 +00:00
|
|
|
/**
|
2010-10-05 07:15:29 +00:00
|
|
|
* Constructor FILE_LINE_READER
|
|
|
|
* takes an open FILE and the size of the desired line buffer and takes
|
|
|
|
* ownership of the open file, i.e. assumes the obligation to close it.
|
|
|
|
*
|
|
|
|
* @param aFile is an open file.
|
|
|
|
* @param aFileName is the name of the file for error reporting purposes.
|
2010-12-28 02:44:30 +00:00
|
|
|
* @param doOwn if true, means I should close the open file, else not.
|
|
|
|
* @param aStartingLineNumber is the initial line number to report on error, and is
|
|
|
|
* accessible here for the case where multiple DSNLEXERs are reading from the
|
|
|
|
* same file in sequence, all from the same open file (with @a doOwn = false).
|
|
|
|
* Internally it is incremented by one after each ReadLine(), so the first
|
|
|
|
* reported line number will always be one greater than what is provided here.
|
2010-10-05 07:15:29 +00:00
|
|
|
* @param aMaxLineLength is the number of bytes to use in the line buffer.
|
2010-03-03 06:13:46 +00:00
|
|
|
*/
|
2010-12-28 02:44:30 +00:00
|
|
|
FILE_LINE_READER( FILE* aFile, const wxString& aFileName, bool doOwn = true,
|
|
|
|
unsigned aStartingLineNumber = 0,
|
|
|
|
unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
|
2010-10-05 07:15:29 +00:00
|
|
|
|
2010-12-28 02:44:30 +00:00
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
* may or may not close the open file, depending on @a doOwn in constructor.
|
|
|
|
*/
|
|
|
|
~FILE_LINE_READER();
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
|
2010-04-16 16:28:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Rewind
|
2010-10-05 07:15:29 +00:00
|
|
|
* rewinds the file and resets the line number back to zero. Line number
|
|
|
|
* will go to 1 on first ReadLine().
|
2010-04-16 16:28:35 +00:00
|
|
|
*/
|
|
|
|
void Rewind()
|
|
|
|
{
|
|
|
|
rewind( fp );
|
|
|
|
lineNum = 0;
|
|
|
|
}
|
2010-03-03 06:13:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class STRING_LINE_READER
|
|
|
|
* is a LINE_READER that reads from a multiline 8 bit wide std::string
|
|
|
|
*/
|
|
|
|
class STRING_LINE_READER : public LINE_READER
|
|
|
|
{
|
|
|
|
protected:
|
2010-10-05 07:15:29 +00:00
|
|
|
std::string lines;
|
2010-03-03 06:13:46 +00:00
|
|
|
size_t ndx;
|
|
|
|
|
|
|
|
public:
|
2010-03-03 06:19:27 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-21 13:21:32 +00:00
|
|
|
* Constructor STRING_LINE_READER( const std::string&, const wxString& )
|
2010-10-05 07:15:29 +00:00
|
|
|
*
|
2010-03-03 06:19:27 +00:00
|
|
|
* @param aString is a source string consisting of one or more lines
|
|
|
|
* of text, where multiple lines are separated with a '\n' character.
|
|
|
|
* The last line does not necessarily need a trailing '\n'.
|
2010-10-21 13:21:32 +00:00
|
|
|
*
|
2010-10-05 07:15:29 +00:00
|
|
|
* @param aSource describes the source of aString for error reporting purposes
|
2010-12-27 21:22:12 +00:00
|
|
|
* can be anything meaninful, such as wxT( "clipboard" ).
|
2010-03-03 06:19:27 +00:00
|
|
|
*/
|
2010-12-28 02:44:30 +00:00
|
|
|
STRING_LINE_READER( const std::string& aString, const wxString& aSource );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor STRING_LINE_READER( const STRING_LINE_READER& )
|
|
|
|
* allows for a continuation of the reading of a stream started by another
|
|
|
|
* STRING_LINE_READER. Any stream offset and source name are used from
|
|
|
|
* @a aStartingPoint.
|
|
|
|
*/
|
|
|
|
STRING_LINE_READER( const STRING_LINE_READER& aStartingPoint );
|
2010-03-03 06:13:46 +00:00
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
|
2010-03-03 06:13:46 +00:00
|
|
|
};
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2012-10-07 15:37:25 +00:00
|
|
|
/**
|
|
|
|
* Class INPUTSTREAM_LINE_READER
|
|
|
|
* is a LINE_READER that reads from a wxInputStream object.
|
|
|
|
*/
|
|
|
|
class INPUTSTREAM_LINE_READER : public LINE_READER
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
wxInputStream* m_stream; //< The input stream to read. No ownership of this pointer.
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor WXINPUTSTREAM_LINE_READER
|
|
|
|
*
|
|
|
|
* @param aStream A pointer to a wxInputStream object to read.
|
|
|
|
*/
|
|
|
|
INPUTSTREAM_LINE_READER( wxInputStream* aStream );
|
|
|
|
|
2012-11-27 13:50:01 +00:00
|
|
|
char* ReadLine() throw( IO_ERROR ); // see LINE_READER::ReadLine() description
|
2012-10-07 15:37:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-25 15:41:20 +00:00
|
|
|
#define OUTPUTFMTBUFZ 500 ///< default buffer size for any OUTPUT_FORMATTER
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
|
|
|
* Class OUTPUTFORMATTER
|
2012-10-25 15:41:20 +00:00
|
|
|
* is an important interface (abstract class) used to output 8 bit text in
|
2010-11-12 15:26:30 +00:00
|
|
|
* a convenient way. The primary interface is "printf() - like" but
|
|
|
|
* with support for indentation control. The destination of the 8 bit
|
|
|
|
* wide text is up to the implementer.
|
2010-08-08 00:31:07 +00:00
|
|
|
* <p>
|
2010-11-12 15:26:30 +00:00
|
|
|
* The implementer only has to implement the write() function, but can
|
|
|
|
* also optionally re-implement GetQuoteChar().
|
2010-08-08 00:31:07 +00:00
|
|
|
* <p>
|
2011-02-28 18:36:19 +00:00
|
|
|
* If you want to output a wxString, then use TO_UTF8() on it
|
2010-11-12 15:26:30 +00:00
|
|
|
* before passing it as an argument to Print().
|
2009-12-11 04:55:24 +00:00
|
|
|
* <p>
|
2010-11-12 15:26:30 +00:00
|
|
|
* Since this is an abstract interface, only classes derived from
|
|
|
|
* this one may actually be used.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
|
|
|
class OUTPUTFORMATTER
|
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
std::vector<char> buffer;
|
|
|
|
char quoteChar[2];
|
2010-08-08 00:31:07 +00:00
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
int sprint( const char* fmt, ... ) throw( IO_ERROR );
|
|
|
|
int vprint( const char* fmt, va_list ap ) throw( IO_ERROR );
|
2010-08-08 00:31:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
2012-10-25 15:41:20 +00:00
|
|
|
OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
|
2010-08-08 00:31:07 +00:00
|
|
|
buffer( aReserve, '\0' )
|
|
|
|
{
|
2012-10-25 15:41:20 +00:00
|
|
|
quoteChar[0] = aQuoteChar;
|
|
|
|
quoteChar[1] = '\0';
|
2010-08-08 00:31:07 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
virtual ~OUTPUTFORMATTER() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function GetQuoteChar
|
|
|
|
* performs quote character need determination according to the Specctra DSN
|
|
|
|
* specification.
|
|
|
|
|
|
|
|
* @param wrapee A string that might need wrapping on each end.
|
|
|
|
* @param quote_char A single character C string which provides the current
|
|
|
|
* quote character, should it be needed by the wrapee.
|
|
|
|
*
|
|
|
|
* @return const char* - the quote_char as a single character string, or ""
|
|
|
|
* if the wrapee does not need to be wrapped.
|
|
|
|
*/
|
|
|
|
static const char* GetQuoteChar( const char* wrapee, const char* quote_char );
|
2010-08-08 00:31:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function write
|
|
|
|
* should be coded in the interface implementation (derived) classes.
|
|
|
|
*
|
|
|
|
* @param aOutBuf is the start of a byte buffer to write.
|
|
|
|
* @param aCount tells how many bytes to write.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR, if there is a problem outputting, such as a full disk.
|
2010-08-08 00:31:07 +00:00
|
|
|
*/
|
2010-11-09 15:45:11 +00:00
|
|
|
virtual void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) = 0;
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
#if defined(__GNUG__) // The GNU C++ compiler defines this
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
// When used on a C++ function, we must account for the "this" pointer,
|
|
|
|
// so increase the STRING-INDEX and FIRST-TO_CHECK by one.
|
|
|
|
// See http://docs.freebsd.org/info/gcc/gcc.info.Function_Attributes.html
|
|
|
|
// Then to get format checking during the compile, compile with -Wall or -Wformat
|
2009-12-11 04:55:24 +00:00
|
|
|
#define PRINTF_FUNC __attribute__ ((format (printf, 3, 4)))
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define PRINTF_FUNC // nothing
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
//-----<interface functions>------------------------------------------
|
|
|
|
|
2009-12-11 04:55:24 +00:00
|
|
|
/**
|
|
|
|
* Function Print
|
|
|
|
* formats and writes text to the output stream.
|
|
|
|
*
|
2010-11-12 15:26:30 +00:00
|
|
|
* @param nestLevel The multiple of spaces to precede the output with.
|
2009-12-11 04:55:24 +00:00
|
|
|
* @param fmt A printf() style format string.
|
|
|
|
* @param ... a variable list of parameters that will get blended into
|
|
|
|
* the output under control of the format string.
|
|
|
|
* @return int - the number of characters output.
|
2010-11-09 15:45:11 +00:00
|
|
|
* @throw IO_ERROR, if there is a problem outputting, such as a full disk.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2010-11-09 15:45:11 +00:00
|
|
|
int PRINTF_FUNC Print( int nestLevel, const char* fmt, ... ) throw( IO_ERROR );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function GetQuoteChar
|
|
|
|
* performs quote character need determination.
|
|
|
|
* It returns the quote character as a single character string for a given
|
|
|
|
* input wrapee string. If the wrappee does not need to be quoted,
|
|
|
|
* the return value is "" (the null string), such as when there are no
|
|
|
|
* delimiters in the input wrapee string. If you want the quote_char
|
|
|
|
* to be assuredly not "", then pass in "(" as the wrappee.
|
|
|
|
* <p>
|
|
|
|
* Implementations are free to override the default behavior, which is to
|
|
|
|
* call the static function of the same name.
|
|
|
|
|
|
|
|
* @param wrapee A string that might need wrapping on each end.
|
|
|
|
* @return const char* - the quote_char as a single character string, or ""
|
|
|
|
* if the wrapee does not need to be wrapped.
|
|
|
|
*/
|
2012-10-25 15:41:20 +00:00
|
|
|
virtual const char* GetQuoteChar( const char* wrapee );
|
2009-12-11 04:55:24 +00:00
|
|
|
|
|
|
|
/**
|
2011-02-02 19:41:35 +00:00
|
|
|
* Function Quotes
|
2010-08-11 19:52:44 +00:00
|
|
|
* checks \a aWrapee input string for a need to be quoted
|
2010-08-09 02:03:16 +00:00
|
|
|
* (e.g. contains a ')' character or a space), and for \" double quotes
|
2011-02-02 15:31:48 +00:00
|
|
|
* within the string that need to be escaped such that the DSNLEXER
|
2010-08-09 02:03:16 +00:00
|
|
|
* will correctly parse the string from a file later.
|
2009-12-11 04:55:24 +00:00
|
|
|
*
|
2010-08-09 02:03:16 +00:00
|
|
|
* @param aWrapee is a string that might need wraping in double quotes,
|
2011-02-02 15:31:48 +00:00
|
|
|
* and it might need to have its internal content escaped, or not.
|
2010-08-09 02:03:16 +00:00
|
|
|
*
|
2010-12-28 16:55:28 +00:00
|
|
|
* @return std::string - whose c_str() function can be called for passing
|
2011-02-02 15:31:48 +00:00
|
|
|
* to printf() style functions that output UTF8 encoded s-expression streams.
|
|
|
|
*
|
|
|
|
* @throw IO_ERROR, if there is any kind of problem with the input string.
|
2009-12-11 04:55:24 +00:00
|
|
|
*/
|
2011-02-02 19:41:35 +00:00
|
|
|
virtual std::string Quotes( const std::string& aWrapee ) throw( IO_ERROR );
|
2010-08-09 02:03:16 +00:00
|
|
|
|
2011-02-02 19:41:35 +00:00
|
|
|
std::string Quotew( const wxString& aWrapee ) throw( IO_ERROR );
|
2011-02-02 15:31:48 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
//-----</interface functions>-----------------------------------------
|
2009-12-11 04:55:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-08-09 02:03:16 +00:00
|
|
|
* Class STRING_FORMATTER
|
2009-12-11 04:55:24 +00:00
|
|
|
* implements OUTPUTFORMATTER to a memory buffer. After Print()ing the
|
|
|
|
* string is available through GetString()
|
|
|
|
*/
|
2010-08-09 02:03:16 +00:00
|
|
|
class STRING_FORMATTER : public OUTPUTFORMATTER
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
std::string mystring;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2010-08-09 02:03:16 +00:00
|
|
|
* Constructor STRING_FORMATTER
|
2009-12-11 04:55:24 +00:00
|
|
|
* reserves space in the buffer
|
|
|
|
*/
|
2012-10-25 15:41:20 +00:00
|
|
|
STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
|
|
|
|
OUTPUTFORMATTER( aReserve, aQuoteChar )
|
2009-12-11 04:55:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Clear
|
|
|
|
* clears the buffer and empties the internal string.
|
|
|
|
*/
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
mystring.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function StripUseless
|
|
|
|
* removes whitespace, '(', and ')' from the mystring.
|
|
|
|
*/
|
|
|
|
void StripUseless();
|
|
|
|
|
|
|
|
std::string GetString()
|
|
|
|
{
|
|
|
|
return mystring;
|
|
|
|
}
|
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
protected:
|
2012-10-25 15:41:20 +00:00
|
|
|
//-----<OUTPUTFORMATTER>------------------------------------------------
|
2010-11-09 15:45:11 +00:00
|
|
|
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
|
2010-08-08 00:31:07 +00:00
|
|
|
//-----</OUTPUTFORMATTER>-----------------------------------------------
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-25 05:52:55 +00:00
|
|
|
/**
|
|
|
|
* Class FILE_OUTPUTFORMATTER
|
|
|
|
* may be used for text file output. It is about 8 times faster than
|
|
|
|
* STREAM_OUTPUTFORMATTER for file streams.
|
|
|
|
*/
|
|
|
|
class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-10-25 07:10:59 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param aFileName is the full filename to open and save to as a text file.
|
|
|
|
* @param aMode is what you would pass to wxFopen()'s mode, defaults to wxT( "wt" )
|
2012-10-29 21:56:18 +00:00
|
|
|
* for text files that are to be created here and now.
|
2012-12-18 13:54:44 +00:00
|
|
|
* @param aQuoteChar is a char used for quoting problematic strings
|
2012-10-29 21:56:18 +00:00
|
|
|
(with whitespace or special characters in them).
|
2012-10-25 07:10:59 +00:00
|
|
|
* @throw IO_ERROR if the file cannot be opened.
|
|
|
|
*/
|
2012-10-25 15:41:20 +00:00
|
|
|
FILE_OUTPUTFORMATTER( const wxString& aFileName,
|
|
|
|
const wxChar* aMode = wxT( "wt" ),
|
|
|
|
char aQuoteChar = '"' )
|
|
|
|
throw( IO_ERROR );
|
2012-10-25 07:10:59 +00:00
|
|
|
|
|
|
|
~FILE_OUTPUTFORMATTER();
|
2012-10-25 05:52:55 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
//-----<OUTPUTFORMATTER>------------------------------------------------
|
2012-10-25 07:10:59 +00:00
|
|
|
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
|
2012-10-25 05:52:55 +00:00
|
|
|
//-----</OUTPUTFORMATTER>-----------------------------------------------
|
2012-10-25 15:41:20 +00:00
|
|
|
|
|
|
|
FILE* m_fp; ///< takes ownership
|
|
|
|
wxString m_filename;
|
2012-10-25 05:52:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
/**
|
|
|
|
* Class STREAM_OUTPUTFORMATTER
|
|
|
|
* implements OUTPUTFORMATTER to a wxWidgets wxOutputStream. The stream is
|
|
|
|
* neither opened nor closed by this class.
|
|
|
|
*/
|
|
|
|
class STREAM_OUTPUTFORMATTER : public OUTPUTFORMATTER
|
|
|
|
{
|
|
|
|
wxOutputStream& os;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor STREAM_OUTPUTFORMATTER
|
|
|
|
* can take any number of wxOutputStream derivations, so it can write
|
|
|
|
* to a file, socket, or zip file.
|
|
|
|
*/
|
|
|
|
STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
|
2012-10-25 15:41:20 +00:00
|
|
|
OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
|
2010-08-08 00:31:07 +00:00
|
|
|
os( aStream )
|
|
|
|
{
|
|
|
|
}
|
2009-12-11 04:55:24 +00:00
|
|
|
|
2010-08-08 00:31:07 +00:00
|
|
|
protected:
|
2012-10-25 15:41:20 +00:00
|
|
|
//-----<OUTPUTFORMATTER>------------------------------------------------
|
2010-11-09 15:45:11 +00:00
|
|
|
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
|
2009-12-11 04:55:24 +00:00
|
|
|
//-----</OUTPUTFORMATTER>-----------------------------------------------
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // RICHIO_H_
|