file.cpp
来自「由一个古老的BASIC解释器改进而成, 保留了ANSI C固有的艺术美感.」· C++ 代码 · 共 950 行 · 第 1/2 页
CPP
950 行
// file.cpp
#include "blassic.h"
#include "file.h"
#include "trace.h"
#include "error.h"
#include "var.h"
#include "cursor.h"
#include "graphics.h"
#include "util.h"
using util::to_string;
#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <iostream>
using std::cerr;
using std::endl;
#include <cassert>
#define ASSERT assert
#ifndef _Windows
#include <unistd.h> // read, write
#endif
BlFile::BlFile (OpenMode nmode) :
mode (nmode),
cDelimiter (','),
cQuote ('"'),
cEscape ('\0')
{
}
BlFile::~BlFile ()
{
}
bool BlFile::eof ()
{ throw ErrFileMode; }
void BlFile::flush ()
{ throw ErrFileMode; }
void BlFile::getline (std::string &)
{ throw ErrFileMode; }
void BlFile::outstring (const std::string &)
{ throw ErrFileMode; }
void BlFile::outchar (char)
{ throw ErrFileMode; }
#if 0
void BlFile::outnumber (BlNumber n)
{
//outstring (to_string (n) );
std::ostringstream oss;
oss << std::setprecision (16) << n;
outstring (oss.str () );
}
void BlFile::outinteger (BlInteger n)
{
outstring (to_string (n) );
}
void BlFile::outlinenumber (BlLineNumber l)
{
std::ostringstream oss;
oss << std::setw (7) << l;
outstring (oss.str () );
}
#endif
BlFile & operator << (BlFile & bf, const std::string & str)
{
bf.outstring (str);
return bf;
}
BlFile & operator << (BlFile & bf, char c)
{
bf.outchar (c);
return bf;
}
BlFile & operator << (BlFile & bf, BlNumber n)
{
//bf.outnumber (n);
std::ostringstream oss;
oss << std::setprecision (16) << n;
bf.outstring (oss.str () );
return bf;
}
BlFile & operator << (BlFile & bf, BlInteger n)
{
//bf.outinteger (n);
bf.outstring (to_string (n) );
return bf;
}
BlFile & operator << (BlFile & bf, BlLineNumber l)
{
//bf.outlinenumber (l);
std::ostringstream oss;
oss << std::setw (7) << l;
bf.outstring (oss.str () );
return bf;
}
void BlFile::putspaces (size_t n)
{
outstring (std::string (n, ' ') );
}
void BlFile::tab (size_t n)
{
// Provisional
outstring (std::string (n, ' ') );
}
void BlFile::put (size_t)
{ throw ErrFileMode; }
void BlFile::get (size_t)
{ throw ErrFileMode; }
void BlFile::field (const std::vector <field_element> &)
{ throw ErrFileMode; }
// assign doesn't throw because we call it for all open files,
// those that are no random files or does nor have the var in
// their fields just ignore it.
void BlFile::assign (const std::string &, const std::string &, Align)
{ }
std::string BlFile::read (size_t)
{ throw ErrFileMode; }
void BlFile::gotoxy (int, int)
{ throw ErrFileMode; }
void BlFile::setcolor (int)
{ throw ErrFileMode; }
void BlFile::setbackground (int)
{ throw ErrFileMode; }
void BlFile::cls ()
{ throw ErrFileMode; }
//***********************************************
// BlFileConsole
//***********************************************
BlFileConsole::BlFileConsole (std::istream & nin, std::ostream & nout) :
BlFile (OpenMode (Input | Output) ),
in (nin),
out (nout)
{
}
bool BlFileConsole::eof ()
{
int c= in.get ();
if (! in || c == EOF)
return true;
in.unget ();
return false;
}
void BlFileConsole::flush ()
{
out << std::flush;
}
void BlFileConsole::getline (std::string & str)
{
std::getline (in, str);
if (fInterrupted)
{
in.clear ();
str.erase ();
return;
}
#ifdef __WIN32__
size_t l= str.size ();
util::auto_buffer <char> aux (l);
OemToCharBuff (str.data (), aux, l);
str= std::string (aux, l);
#endif
}
std::string BlFileConsole::read (size_t n)
{
util::auto_buffer <char> buf (n);
in.read (buf, n);
return std::string (buf, n);
}
void BlFileConsole::tab (size_t n)
{
if (graphics::ingraphicsmode () )
{
graphics::tab (n);
}
else
{
outstring (std::string (n, ' ') );
}
}
void BlFileConsole::outstring (const std::string & str)
{
if (graphics::ingraphicsmode () )
{
graphics::stringout (str);
return;
}
#ifdef __WIN32__
size_t l= str.size ();
util::auto_buffer <char> aux (l + 1);
CharToOemBuff (str.data (), aux, l);
aux [l]= 0;
out << aux;
#else
out << str;
#endif
if (! out)
{
out.clear ();
//throw std::runtime_error ("Al diablo");
}
}
void BlFileConsole::outchar (char c)
{
if (graphics::ingraphicsmode () )
{
graphics::charout (c);
return;
}
#ifdef __WIN32__
CharToOemBuff (& c, & c, 1);
#endif
if (c == '\n')
out << endl;
else
out << c;
if (! out)
{
out.clear ();
//throw std::runtime_error ("Al diablo");
}
}
#if 0
void BlFileConsole::outnumber (BlNumber n)
{
if (graphics::ingraphicsmode () )
graphics::stringout (to_string (n) );
else
out << n;
}
void BlFileConsole::outinteger (BlInteger n)
{
if (graphics::ingraphicsmode () )
graphics::stringout (to_string (n) );
else
out << n;
}
#endif
void BlFileConsole::gotoxy (int x, int y)
{
::gotoxy (x, y);
}
void BlFileConsole::setcolor (int color)
{
textcolor (color);
}
void BlFileConsole::setbackground (int color)
{
textbackground (color);
}
void BlFileConsole::cls ()
{
::cls ();
}
//***********************************************
// BlFileWindow
//***********************************************
BlFileWindow::BlFileWindow (BlChannel ch) :
BlFile (OpenMode (Input | Output) ),
ch (ch)
{
if (ch != 0)
throw ErrBlassicInternal;
}
BlFileWindow::BlFileWindow (BlChannel ch, int x1, int x2, int y1, int y2) :
BlFile (OpenMode (Input | Output) ),
ch (ch)
{
graphics::definewindow (ch, x1, x2, y1, y2);
}
BlFileWindow::~BlFileWindow ()
{
graphics::undefinewindow (ch);
}
void BlFileWindow::reset (int x1, int x2, int y1, int y2)
{
graphics::definewindow (ch, x1, x2, y1, y2);
}
bool BlFileWindow::eof ()
{
return false;
}
void BlFileWindow::flush ()
{
// Nothing to do
}
void BlFileWindow::getline (std::string &)
{
throw ErrNotImplemented;
}
std::string BlFileWindow::read (size_t)
{
throw ErrNotImplemented;
}
void BlFileWindow::tab (size_t n)
{
graphics::tab (ch, n);
}
void BlFileWindow::gotoxy (int x, int y)
{
graphics::gotoxy (ch, x, y);
}
void BlFileWindow::setcolor (int color)
{
graphics::setcolor (ch, color);
}
void BlFileWindow::setbackground (int color)
{
graphics::setbackground (ch, color);
}
void BlFileWindow::outstring (const std::string & str)
{
graphics::stringout (ch, str);
}
void BlFileWindow::outchar (char c)
{
graphics::charout (ch, c);
}
void BlFileWindow::cls ()
{
graphics::cls (ch);
}
//***********************************************
// BlFileOut
//***********************************************
BlFileOut::BlFileOut () : BlFile (Output)
{ }
BlFileOut::BlFileOut (OpenMode mode) : BlFile (mode)
{ }
void BlFileOut::flush ()
{
ofs () << std::flush;
}
void BlFileOut::outstring (const std::string & str)
{
ofs () << str;
}
void BlFileOut::outchar (char c)
{
ofs () << c;
}
#if 0
void BlFileOut::outnumber (BlNumber n)
{
ofs () << n;
}
void BlFileOut::outinteger (BlInteger n)
{
ofs () << n;
}
void BlFileOut::outlinenumber (BlLineNumber l)
{
ofs () << std::setw (7) << l;
}
#endif
//***********************************************
// BlFileOutString
//***********************************************
BlFileOutString::BlFileOutString ()
{ }
std::string BlFileOutString::str ()
{
return oss.str ();
}
std::ostream & BlFileOutString::ofs ()
{
return oss;
}
//***********************************************
// BlFileOutput
//***********************************************
BlFileOutput::BlFileOutput (std::ostream & os) :
os (os)
{
}
std::ostream & BlFileOutput::ofs ()
{
return os;
}
//***********************************************
// BlFileRegular
//***********************************************
BlFileRegular::BlFileRegular (const std::string & name, OpenMode nmode) :
BlFileOut (nmode)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?