]> git.lyx.org Git - lyx.git/blob - src/xtl/text.h
Two little things:
[lyx.git] / src / xtl / text.h
1 /*
2  * Text format driver for XTL
3  *
4  * Copyright (C) 1998-2000 Jose' Orlando Pereira, jop@di.uminho.pt
5  * Copyright (C) 2000 Angus Leeming, a.leeming@ic.ac.uk
6  */
7 /* XTL - eXternalization Template Library - http://gsd.di.uminho.pt/~jop/xtl
8  * Copyright (C) 1998-2000 Jose' Orlando Pereira, Universidade do Minho
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  * 
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  * 
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA
24  *
25  * Id: text.h 1.14 Fri, 05 May 2000 18:57:58 +0100 jop 
26  */
27
28 #ifndef __XTL_TEXT
29 #define __XTL_TEXT
30
31 #include <strstream>
32
33 #define def_simple_output(type) \
34         void output_simple(type const& data) { \
35                 space(); \
36                 std::ostrstream os((char*)desire(20), 20); \
37                 os << data; \
38                 undesire(20-os.pcount()); \
39         }
40
41 // Required to compile "long long" with DEC cxx -std strict_ansi
42 #define def_simple_output_ll(type) \
43         void output_simple(type const& data) { \
44                 space(); \
45                 std::ostrstream os((char*)desire(20), 20); \
46                 os << static_cast<long>( data ); \
47                 undesire(20-os.pcount()); \
48         }
49
50
51 template <class Buffer>
52 class text_format: public generic_format<Buffer> {
53 #if 0
54         // Not yet...
55  private:
56         void gchar(int c)       {}
57  public:
58         void input_start_composite() {gchar('{');}
59         void input_end_composite() {gchar('}');}
60
61         void input_start_vector() {gchar('[');}
62         void input_end_vector() {gchar(']');}
63
64         void input_start_array(int& n) {gchar('<');}
65         bool input_end_array(int& n) {gchar('>');}
66
67         void input_simple(int& data)
68                 {data=69;return *this;}
69 #endif
70
71  private:
72         bool need;
73
74         void space() {if (need) pchar(' ');need=true;}
75         void pchar(int c)       {*(char*)desire(1)=c;}
76  public:
77         typedef Buffer buffer;
78
79         text_format(Buffer& buf):generic_format<Buffer>(buf),need(false) {}
80
81         void output_start_composite() {space();pchar('{');need=false;}
82         void output_end_composite() {pchar('}');need=true;}
83
84         void output_start_vector() {space();pchar('[');need=false;}
85         void output_end_vector() {pchar(']');need=true;}
86
87         void output_start_array(int n) {space();pchar('<');need=false;}
88         void output_end_array() {pchar('>');need=true;}
89
90         def_simple_output(bool)
91         def_simple_output(char)
92         def_simple_output(unsigned char)
93         def_simple_output(short)
94         def_simple_output(unsigned short)
95         def_simple_output(int)
96         def_simple_output(unsigned int)
97         def_simple_output(long)
98         def_simple_output(unsigned long)
99         def_simple_output_ll(longlong)
100         def_simple_output_ll(unsignedlonglong)
101         def_simple_output(float)
102         def_simple_output(double)
103
104         void output_chars(char const* data, int size) {
105                 output_simple('"');
106                 write(data, size);
107                 need=false;
108                 output_simple('"');
109         }
110
111         void output_raw(char const* data, int size) {
112                 output_chars(data, size);
113         }
114 };
115
116 #undef def_simple_output
117 #undef def_simple_output_ll
118
119 #endif