]> git.lyx.org Git - features.git/blob - src/TexStream.cpp
some renamimg
[features.git] / src / TexStream.cpp
1 #include "LaTeXStream.h"
2
3 #include <iostream>
4 #include <streambuf>
5
6 namespace lyx {
7
8 ////////////////////////////////////////////////////////////////
9 //
10 // LaTeXStreamBuffer
11 //
12 ////////////////////////////////////////////////////////////////
13
14
15 class LaTeXStreamBuffer : public std::streambuf
16 {
17 public:
18   explicit LaTeXStreamBuffer(std::streambuf * sbuf);
19         int line() const { return line_; }
20
21 protected:
22   int overflow(int);
23   int sync();
24
25 private:
26   std::streambuf * sbuf_; 
27         int line_;
28 };
29
30
31 LaTeXStreamBuffer::LaTeXStreamBuffer(std::streambuf *sb)
32   : sbuf_(sb), line_(0)
33 {
34   setp(0, 0);
35   setg(0, 0, 0);
36 }
37
38 int LaTeXStreamBuffer::overflow(int c)
39 {
40         if (c == '\n')
41                 ++line_;
42         return c;
43 }
44
45
46 int LaTeXStreamBuffer::sync()
47 {
48   sbuf_->pubsync();
49   return 0;
50 }
51
52   
53 ////////////////////////////////////////////////////////////////
54 //
55 // LaTeXStream
56 //
57 ////////////////////////////////////////////////////////////////
58
59 LaTeXStream::LaTeXStream(std::streambuf * sbuf)
60                 : std::ostream(sbuf_ = new LaTeXStreamBuffer(sbuf))
61 {}
62
63
64 LaTeXStream::~LaTeXStream()
65 {
66         delete sbuf_;
67 }
68
69
70 int LaTeXStream::line() const
71 {
72         return sbuf_->line();
73 }
74
75
76 ////////////////////////////////////////////////////////////////
77 //
78 // Test
79 //
80 ////////////////////////////////////////////////////////////////
81
82 #if 0
83
84 int main(int argc, char *argv[])
85 {
86         LaTeXStream out(std::cout.rdbuf());
87         char c;
88         while (std::cin) {
89                 if (std::cin.get(c))
90                         out.put(c);
91         }
92         std::cout << "line count: " << out.line() << std::endl;
93
94   return 0;
95 }
96
97 #endif
98
99 }