518 lines
16 KiB
C++
518 lines
16 KiB
C++
/*
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
*
|
|
* Copyright (C) 2007-2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
|
* Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
#ifndef RICHIO_H_
|
|
#define RICHIO_H_
|
|
|
|
// 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>
|
|
#include <core/utf8.h>
|
|
|
|
// I really did not want to be dependent on wxWidgets in richio
|
|
// but the errorText needs to be wide char so wxString rules.
|
|
#include <cstdio>
|
|
#include <wx/string.h>
|
|
#include <wx/stream.h>
|
|
|
|
#include <ki_exception.h>
|
|
|
|
|
|
/**
|
|
* This is like sprintf() but the output is appended to a std::string instead of to a
|
|
* character array.
|
|
*
|
|
* @param aResult is the string to append to, previous text is not clear()ed.
|
|
* @param aFormat is a printf() style format string.
|
|
* @return the count of bytes appended to the result string, no terminating nul is included.
|
|
*/
|
|
int
|
|
#if defined(__GNUG__)
|
|
__attribute__ ((format (printf, 2, 3)))
|
|
#endif
|
|
StrPrintf( std::string* aResult, const char* aFormat, ... );
|
|
|
|
|
|
/**
|
|
* This is like sprintf() but the output is returned in a std::string instead of to a
|
|
* character array.
|
|
*
|
|
* @param format is a printf() style format string.
|
|
* @return std::string - the result of the sprintf().
|
|
*/
|
|
std::string
|
|
#if defined(__GNUG__)
|
|
__attribute__ ((format (printf, 1, 2)))
|
|
#endif
|
|
StrPrintf( const char* format, ... );
|
|
|
|
|
|
/**
|
|
* Nominally opens a file and reads it into a string. But unlike other facilities, this handles
|
|
* mis-encded Wine-written files on macOS.
|
|
*
|
|
* @param aFilePath
|
|
* @param aReadType
|
|
* @throw IO_ERROR if the file can't be opened
|
|
* @return the file contents
|
|
*/
|
|
wxString SafeReadFile( const wxString& aFilePath, const wxString& aReadType );
|
|
|
|
|
|
#define LINE_READER_LINE_DEFAULT_MAX 1000000
|
|
#define LINE_READER_LINE_INITIAL_SIZE 5000
|
|
|
|
/**
|
|
* An abstract class from which implementation specific LINE_READERs may be derived to
|
|
* read single lines of text and manage a line number counter.
|
|
*/
|
|
class LINE_READER
|
|
{
|
|
public:
|
|
|
|
/**
|
|
* Build a line reader and fixes the length of the maximum supported line length
|
|
* to @a aMaxLineLength.
|
|
*/
|
|
LINE_READER( unsigned aMaxLineLength = LINE_READER_LINE_DEFAULT_MAX );
|
|
|
|
virtual ~LINE_READER();
|
|
|
|
/**
|
|
* Read a line of text into the buffer and increments the line number counter.
|
|
*
|
|
* If the line is larger than the maximum length passed to the constructor, then an
|
|
* exception is thrown. The line is nul terminated.
|
|
*
|
|
* @return The beginning of the read line, or NULL if EOF.
|
|
* @throw IO_ERROR when a line is too long.
|
|
*/
|
|
virtual char* ReadLine() = 0;
|
|
|
|