int main(int argc, char *argv[])
{
int c;
- const char *fileName = 0;
+ const char *sourceFileName = 0;
const char *outputFileName = 0;
- re2c::ofstream_lc output;
if (argc == 1)
{
}
else if (argc == opt_ind + 1)
{
- fileName = argv[opt_ind];
+ sourceFileName = argv[opt_ind];
}
else
{
return 2;
}
- // set up the input stream
- istream* input = 0;
-
- ifstream inputFile;
+ // set up the source stream
+ re2c::ifstream_lc source;
- if (fileName[0] == '-' && fileName[1] == '\0')
+ if (sourceFileName[0] == '-' && sourceFileName[1] == '\0')
{
- fileName = "<stdin>";
- input = &cin;
+ sourceFileName = "<stdin>";
+ source.open(stdin);
}
- else
+ else if (!source.open(sourceFileName).is_open())
{
- inputFile.open(fileName);
-
- if (!inputFile)
- {
- cerr << "can't open " << fileName << "\n";
- return 1;
- }
-
- input = &inputFile;
+ cerr << "re2c: error: cannot open " << sourceFileName << "\n";
+ return 1;
}
// set up the output stream
- if (outputFileName == 0 || (fileName[0] == '-' && fileName[1] == '\0'))
+ re2c::ofstream_lc output;
+
+ if (outputFileName == 0 || (sourceFileName[0] == '-' && sourceFileName[1] == '\0'))
{
outputFileName = "<stdout>";
output.open(stdout);
}
- else
- {
- output.open(outputFileName);
-
- if (!output.is_open())
- {
- cerr << "can't open " << outputFileName << "\n";
- return 1;
- }
- }
-
- Scanner in(*input, output);
-
- if (!iFlag)
+ else if (!output.open(outputFileName).is_open())
{
- sourceFileInfo = file_info(fileName, &in);
- outputFileInfo = file_info(outputFileName, &output);
+ cerr << "re2c: error: cannot open " << outputFileName << "\n";
+ return 1;
}
+ Scanner scanner(source, output);
+ sourceFileInfo = file_info(sourceFileName, &scanner);
+ outputFileInfo = file_info(outputFileName, &output);
- parse(in, output);
+ parse(scanner, output);
return 0;
}
: _Mybase()
, fp(_fp)
, must_close(false)
- , oline(1)
+ , fline(1)
{
}
uint get_line() const
{
- return oline + 1;
+ return fline + 1;
}
bool is_open() const
return 0;
}
const char * fmode = (mode & std::ios_base::out)
- ? "w"
- : "r";
+ ? "wt"
+ : "rt";
if ((fp = fopen(filename, fmode)) == 0)
{
return 0;
{
if (c == '\n')
{
- ++oline;
+ ++fline;
}
if (_Tr::eq_int_type(_Tr::eof(), c))
{
virtual int_type pbackfail(int_type c = _Tr::eof())
{
- c = 0;
assert(0);
- return 0;
+ c = 0;
+ return _Tr::eof();
}
- virtual int_type underflow()
+ virtual int_type underflow() // don't point past it
{
assert(0);
- return 0;
+ return _Tr::eof();
}
- virtual int_type uflow()
+ virtual int_type uflow() // point past it
{
- assert(0);
- return 0;
+ int_type c;
+
+ if (fp == 0 || (_Tr::eq_int_type(_Tr::eof(), (c = fgetc(fp)))))
+ {
+ return _Tr::eof();
+ }
+ else if (c == '\n')
+ {
+ ++fline;
+ }
+ return c;
}
- virtual pos_type seekoff(off_type, std::ios_base::seekdir,
+ virtual pos_type seekoff(off_type off, std::ios_base::seekdir whence,
std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
{
- assert(0);
- return pos_type(~0);
+ return fseek(fp, off, whence);
}
- virtual pos_type seekpos(pos_type,
+ virtual pos_type seekpos(pos_type fpos,
std::ios_base::openmode = (std::ios_base::openmode)(std::ios_base::in | std::ios_base::out))
{
- assert(0);
- return pos_type(~0);
+ return fseek(fp, fpos, SEEK_SET);
}
virtual _Mybase * setbuf(_E *, std::streamsize)
{
fwrite(buffer.c_str(), sizeof(_E), buffer.length(), fp);
buffer.clear();
- oline += std::count(buf, buf + cnt, '\n');
+ fline += std::count(buf, buf + cnt, '\n');
return fwrite(buf, sizeof(_E), cnt, fp);
}
FILE * fp;
bool must_close;
- uint oline;
+ uint fline;
+ _E pbchar;
std::basic_string<_E, _Tr> buffer;
};
typedef basic_filebuf_lc<char> filebuf_lc;
-template<class _E, class _Tr = std::char_traits<_E> >
-class basic_ofstream_lc
- : public std::basic_ostream<_E, _Tr>
+template<
+ class _E,
+ class _BaseStream,
+ std::ios_base::openmode _DefOpenMode,
+ class _Tr = std::char_traits<_E> >
+class basic_fstream_lc
+ : public _BaseStream
, public line_number
{
public:
- typedef std::basic_ios< _E, _Tr> _Myios;
- typedef std::basic_ostream<_E, _Tr> _Mybase;
- typedef basic_ofstream_lc< _E, _Tr> _Myt;
+ typedef basic_fstream_lc< _E, _BaseStream, _DefOpenMode, _Tr> _Myt;
+ typedef std::basic_ios<_E, _Tr> _Myios;
+ typedef _BaseStream _Mybase;
typedef basic_filebuf_lc< _E, _Tr> _Mybuf;
- basic_ofstream_lc()
+ basic_fstream_lc()
: _Mybase(&mybuf)
{
}
- virtual ~basic_ofstream_lc()
+ virtual ~basic_fstream_lc()
{
}
return mybuf.is_open();
}
- void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out)
+ _Myt& open(const char * filename, std::ios_base::openmode mode = _DefOpenMode)
{
- if ((mode & std::ios_base::out) == 0 || mybuf.open(filename, mode) == 0)
+ if ((mode & _DefOpenMode) == 0 || mybuf.open(filename, mode) == 0)
{
_Myios::setstate(std::ios_base::failbit);
}
+ return *this;
}
- void open(FILE *fp)
+ _Myt& open(FILE *fp)
{
if (mybuf.open(fp) == 0)
{
_Myios::setstate(std::ios_base::failbit);
}
+ return *this;
}
void close()
mutable _Mybuf mybuf;
};
+template<class _E, class _Tr = std::char_traits<_E> >
+class basic_ofstream_lc
+ : public basic_fstream_lc<_E, std::basic_ostream<_E, _Tr>, std::ios_base::out, _Tr>
+{
+};
+
typedef basic_ofstream_lc<char> ofstream_lc;
+template<class _E, class _Tr = std::char_traits<_E> >
+class basic_ifstream_lc
+ : public basic_fstream_lc<_E, std::basic_istream<_E, _Tr>, std::ios_base::in, _Tr>
+{
+};
+
+typedef basic_ifstream_lc<char> ifstream_lc;
+
class file_info
{
public: