HFST - Helsinki Finite-State Transducer Technology - C++ API  version 3.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HfstOutputStream.h
Go to the documentation of this file.
1 // Copyright (c) 2016 University of Helsinki
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 3 of the License, or (at your option) any later version.
7 // See the file COPYING included with this distribution for more
8 // information.
9 
10 #ifndef _HFST_OUTPUTSTREAM_H_
11 #define _HFST_OUTPUTSTREAM_H_
12 
13 #if HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16 
17 #include "HfstDataTypes.h"
18 
19 #include "hfstdll.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_XFSM
41  class XfsmOutputStream;
42 #endif
43 #if HAVE_MY_TRANSDUCER_LIBRARY
44  class MyTransducerLibraryOutputStream;
45 #endif
46  class HfstOlOutputStream;
47  }
48 
49 
71  {
72  protected:
73  union StreamImplementation
74  {
75 #if HAVE_OPENFST
76 #if HAVE_OPENFST_LOG
77  hfst::implementations::LogWeightOutputStream * log_ofst;
78 #endif
79  hfst::implementations::TropicalWeightOutputStream * tropical_ofst;
80 #endif
81 #if HAVE_SFST
82  hfst::implementations::SfstOutputStream * sfst;
83 #endif
84 #if HAVE_FOMA
85  hfst::implementations::FomaOutputStream * foma;
86 #endif
87 #if HAVE_XFSM
88  hfst::implementations::XfsmOutputStream * xfsm;
89 #endif
90 
91 #if HAVE_MY_TRANSDUCER_LIBRARY
93  my_transducer_library;
94 #endif
95 
96  hfst::implementations::HfstOlOutputStream * hfst_ol;
97  };
98  ImplementationType type; // type of the stream implementation
99  // whether an hfst header is written before every transducer
100  bool hfst_format;
101  StreamImplementation implementation; // backend implementation
102 
103  // write data to stream
104  void write(const std::string &s);
105  void write(const std::vector<char> &s);
106  void write(const char &c);
107 
108  // if file is open
109  bool is_open;
110 
111  // append string s to vector str and a '\0'
112  static void append(std::vector<char> &str, const std::string &s);
113 
114  // append obligatory HFST header data to \a header
115  void append_hfst_header_data(std::vector<char> &header);
116  /* append implementation-specific header data collected from
117  \a transducer to \a header */
118  void append_implementation_specific_header_data
119  (std::vector<char> &header, HfstTransducer &transducer);
120 
121 
122  public:
123 
129  HFSTDLL HfstOutputStream(ImplementationType type, bool hfst_format=true);
130 
138  HFSTDLL HfstOutputStream(const std::string &filename, ImplementationType type, bool hfst_format=true);
139 
141  HFSTDLL ~HfstOutputStream(void);
142 
147  HFSTDLL HfstOutputStream &flush();
148 
160  HFSTDLL HfstOutputStream &operator<< (HfstTransducer &transducer);
161 
165  HFSTDLL HfstOutputStream& redirect (HfstTransducer &transducer);
166 
170  HFSTDLL void close(void);
171  };
172 
173 
174 
175 
176 }
177 
178 
179 
180 #endif
HFSTDLL ~HfstOutputStream(void)
Destructor.
Definition: HfstOutputStream.cc:153
A synchronous finite-state transducer.
Definition: HfstTransducer.h:253
HFSTDLL HfstOutputStream & flush()
Flush the stream.
Definition: HfstOutputStream.cc:338
A skeleton class for writing a new type of binary transducers to a stream.
Definition: MyTransducerLibraryTransducer.h:112
ImplementationType
The type of an HfstTransducer.
Definition: HfstDataTypes.h:41
A stream for writing binary transducers.
Definition: HfstOutputStream.h:70
HFSTDLL 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:21
Datatypes that are needed when using the HFST API.
HFSTDLL void close(void)
Close the stream.
Definition: HfstOutputStream.cc:497
HFSTDLL HfstOutputStream & redirect(HfstTransducer &transducer)
An alias for operator<<.
Definition: HfstOutputStream.cc:351
HFSTDLL HfstOutputStream & operator<<(HfstTransducer &transducer)
Write the transducer transducer in binary format to the stream.
Definition: HfstOutputStream.cc:356