HFST - Helsinki Finite-State Transducer Technology API  version 3.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HfstOutputStream.h
Go to the documentation of this file.
1 // This program is free software: you can redistribute it and/or modify
2 // it under the terms of the GNU General Public License as published by
3 // the Free Software Foundation, version 3 of the License.
4 //
5 // This program is distributed in the hope that it will be useful,
6 // but WITHOUT ANY WARRANTY; without even the implied warranty of
7 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 // GNU General Public License for more details.
9 //
10 // You should have received a copy of the GNU General Public License
11 // along with this program. If not, see <http://www.gnu.org/licenses/>.
12 #ifndef _HFST_OUTPUTSTREAM_H_
13 #define _HFST_OUTPUTSTREAM_H_
14 
15 #if HAVE_CONFIG_H
16 # include <config.h>
17 #endif
18 
19 #include "HfstDataTypes.h"
20 
24 namespace hfst
25 {
26 
27  namespace implementations {
28 #if HAVE_OPENFST
29 #if HAVE_OPENFST_LOG
30  class LogWeightOutputStream;
31 #endif
32  class TropicalWeightOutputStream;
33 #endif
34 #if HAVE_SFST
35  class SfstOutputStream;
36 #endif
37 #if HAVE_FOMA
38  class FomaOutputStream;
39 #endif
40 #if HAVE_MY_TRANSDUCER_LIBRARY
41  class MyTransducerLibraryOutputStream;
42 #endif
43  class HfstOlOutputStream;
44  }
45 
46 
68  {
69  protected:
70  union StreamImplementation
71  {
72 #if HAVE_OPENFST
73 #if HAVE_OPENFST_LOG
74  hfst::implementations::LogWeightOutputStream * log_ofst;
75 #endif
76  hfst::implementations::TropicalWeightOutputStream * tropical_ofst;
77 #endif
78 #if HAVE_SFST
79  hfst::implementations::SfstOutputStream * sfst;
80 #endif
81 #if HAVE_FOMA
82  hfst::implementations::FomaOutputStream * foma;
83 #endif
84 
85 #if HAVE_MY_TRANSDUCER_LIBRARY
87  my_transducer_library;
88 #endif
89 
90  hfst::implementations::HfstOlOutputStream * hfst_ol;
91  };
92  ImplementationType type; // type of the stream implementation
93  // whether an hfst header is written before every transducer
94  bool hfst_format;
95  StreamImplementation implementation; // backend implementation
96 
97  // write data to stream
98  void write(const std::string &s);
99  void write(const std::vector<char> &s);
100  void write(const char &c);
101 
102  // if file is open
103  bool is_open;
104 
105  // append string s to vector str and a '\0'
106  static void append(std::vector<char> &str, const std::string &s);
107 
108  // append obligatory HFST header data to \a header
109  void append_hfst_header_data(std::vector<char> &header);
110  /* append implementation-specific header data collected from
111  \a transducer to \a header */
112  void append_implementation_specific_header_data
113  (std::vector<char> &header, HfstTransducer &transducer);
114 
115 
116  public:
117 
123  HfstOutputStream(ImplementationType type, bool hfst_format=true);
124 
132  HfstOutputStream(const std::string &filename, ImplementationType type, bool hfst_format=true);
133 
135  ~HfstOutputStream(void);
136 
146 
151 
155  void close(void);
156  };
157 
158 
159 
160 
161 }
162 
163 
164 
165 #endif
~HfstOutputStream(void)
Destructor.
Definition: HfstOutputStream.cc:140
A synchronous finite-state transducer.
Definition: HfstTransducer.h:227
A skeleton class for writing a new type of binary transducers to a stream.
Definition: MyTransducerLibraryTransducer.h:115
ImplementationType
The type of an HfstTransducer.
Definition: HfstDataTypes.h:43
A stream for writing binary transducers.
Definition: HfstOutputStream.h:67
HfstOutputStream(ImplementationType type, bool hfst_format=true)
Create a stream to standard output for writing binary transducers of type type. hfst_format defines w...
Definition: HfstOutputStream.cc:24
Datatypes that are needed when using the HFST API.
void close(void)
Close the stream.
Definition: HfstOutputStream.cc:444
HfstOutputStream & redirect(HfstTransducer &transducer)
An alias for operator<<.
Definition: HfstOutputStream.cc:310
HfstOutputStream & operator<<(HfstTransducer &transducer)
Write the transducer transducer in binary format to the stream.
Definition: HfstOutputStream.cc:315