]> granicus.if.org Git - re2c/commitdiff
- Use stream_lc for reading too (better use same structure for in and out)
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Wed, 25 Jan 2006 23:45:49 +0000 (23:45 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Wed, 25 Jan 2006 23:45:49 +0000 (23:45 +0000)
main.cc
stream_lc.h

diff --git a/main.cc b/main.cc
index 875474d4b0fb9bb8d8be8274ec4dc8fd9b0e9182..2ea0ca884ed6c649f5859d9e6a9a493e12611bdb 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -108,9 +108,8 @@ using namespace re2c;
 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)
        {
@@ -201,7 +200,7 @@ int main(int argc, char *argv[])
        }
        else if (argc == opt_ind + 1)
        {
-               fileName = argv[opt_ind];
+               sourceFileName = argv[opt_ind];
        }
        else
        {
@@ -209,54 +208,37 @@ int main(int argc, char *argv[])
                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;
 }
index 37e89d4321d3a150ee29d29c3b734e3aee50a739..4d14925e6ac6d9feba9e7297944b0be89f63efb8 100755 (executable)
@@ -83,7 +83,7 @@ public:
                : _Mybase()
                , fp(_fp)
                , must_close(false)
-               , oline(1)
+               , fline(1)
        {
        }
 
@@ -98,7 +98,7 @@ public:
 
        uint get_line() const
        {
-               return oline + 1;
+               return fline + 1;
        }
 
        bool is_open() const
@@ -113,8 +113,8 @@ public:
                        return 0;
                }
                const char * fmode = (mode & std::ios_base::out)
-                                  ? "w"
-                                  : "r";
+                                  ? "wt"
+                                  : "rt";
                if ((fp = fopen(filename, fmode)) == 0)
                {
                        return 0;
@@ -157,7 +157,7 @@ protected:
        {
                if (c == '\n')
                {
-                       ++oline;
+                       ++fline;
                }
                if (_Tr::eq_int_type(_Tr::eof(), c))
                {
@@ -172,35 +172,42 @@ protected:
 
        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)
@@ -227,7 +234,7 @@ protected:
        {
                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);
        }
 
@@ -235,29 +242,34 @@ private:
 
        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()
        {
        }
 
@@ -266,20 +278,22 @@ public:
                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()
@@ -299,8 +313,22 @@ protected:
        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: