aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/gfsmAutomatonIO.h
blob: be9848ce75840374f2fb703abfe29db8f3f75800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

/*=============================================================================*\
 * File: gfsmAutomatonIO.h
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state machine library: automata: I/O
 *
 * Copyright (c) 2004-2007 Bryan Jurish.
 *
 * For information on usage and redistribution, and for a DISCLAIMER
 * OF ALL WARRANTIES, see the file "COPYING" in this distribution.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *=============================================================================*/

/** \file gfsmAutomatonIO.h
 *  \brief Librarian routines for automata.
 */

#ifndef _GFSM_AUTOMATON_IO_H
#define _GFSM_AUTOMATON_IO_H

#include <gfsmAutomaton.h>
#include <gfsmVersion.h>

/*======================================================================
 * Types
 */
/// Header info for binary files
typedef struct {
  gchar              magic[16];    /**< magic header string "gfsm_automaton" */
  gfsmVersionInfo    version;      /**< gfsm version which created the stored file */
  gfsmVersionInfo    version_min;  /**< minimum gfsm version required to load the file */
  gfsmAutomatonFlags flags;        /**< automaton flags */
  gfsmStateId        root_id;      /**< Id of root node */
  gfsmStateId        n_states;     /**< number of stored states */
  gfsmStateId        n_arcs_007;   /**< number of stored arcs (v0.0.2 .. v0.0.7) */
  guint32            srtype;       /**< semiring type (cast to gfsmSRType) */
  guint32            unused1;      /**< reserved */
  guint32            unused2;      /**< reserved */
  guint32            unused3;      /**< reserved */
} gfsmAutomatonHeader;

/// Type for a stored state
typedef struct {
  guint32  is_valid : 1;  /**< valid flag */
  guint32  is_final : 1;  /**< final flag */
  guint32  unused   : 30; /**< reserved */
  guint32  n_arcs;        /**< number of stored arcs for this state */
} gfsmStoredState;


/// Type for a stored arc (no 'source' field)
//typedef gfsmArc gfsmStoredArc;
typedef struct {
  gfsmStateId       target;  /**< ID of target node */
  gfsmLabelId       lower;   /**< Lower label */
  gfsmLabelId       upper;   /**< Upper label */
  gfsmWeight        weight;  /**< arc weight */
} gfsmStoredArc;

/*======================================================================
 * Constants
 */
/* Scanner config for gfsm_automaton_compile() */
//extern const GScannerConfig gfsm_automaton_scanner_config;

/** Magic header string for stored gfsm files */
extern const gchar gfsm_header_magic[16];

/** Minimum libgfsm version required for loading files stored by this version of libgfsm */
extern const gfsmVersionInfo gfsm_version_bincompat_min_store;

/** Minimum libgfsm version whose binary files this version of libgfsm can read */
extern const gfsmVersionInfo gfsm_version_bincompat_min_check;

/*======================================================================
 * Methods: Binary I/O
 */
/// \name Automaton Methods: Binary I/O
//@{
/** Load an automaton header from a stored binary file.
 *  Returns TRUE iff the header looks valid. */
gboolean gfsm_automaton_load_header(gfsmAutomatonHeader *hdr, gfsmIOHandle *ioh, gfsmError **errp);

/** Load an automaton from a named binary file (implicitly clear()s \a fsm) */
gboolean gfsm_automaton_load_bin_handle(gfsmAutomaton *fsm, gfsmIOHandle *ioh, gfsmError **errp);

/** Load an automaton from a stored binary file (implicitly clear()s \a fsm) */
gboolean gfsm_automaton_load_bin_file(gfsmAutomaton *fsm, FILE *f, gfsmError **errp);

/** Load an automaton from a named binary file (implicitly clear()s \a fsm) */
gboolean gfsm_automaton_load_bin_filename(gfsmAutomaton *fsm, const gchar *filename, gfsmError **errp);

/** Load an automaton from an in-memory buffer */
gboolean gfsm_automaton_load_bin_gstring(gfsmAutomaton *fsm, GString *gs, gfsmError **errp);


/** Store an automaton in binary form to a gfsmIOHandle* */
gboolean gfsm_automaton_save_bin_handle(gfsmAutomaton *fsm, gfsmIOHandle *ioh, gfsmError **errp);

/** Store an automaton in binary form to a file */
gboolean gfsm_automaton_save_bin_file(gfsmAutomaton *fsm, FILE *f, gfsmError **errp);

/** Store an automaton to a named binary file (no compression) */
gboolean gfsm_automaton_save_bin_filename_nc(gfsmAutomaton *fsm, const gchar *filename, gfsmError **errp);

/** Store an automaton to a named binary file, possibly compressing.
 *  Set \a zlevel=-1 for default compression, and
 *  set \a zlevel=0  for no compression, otherwise should be as for zlib (1 <= zlevel <= 9)
 */
gboolean gfsm_automaton_save_bin_filename(gfsmAutomaton *fsm, const gchar *filename, int zlevel, gfsmError **errp);

/** Append an uncompressed binary automaton to an in-memory buffer */
gboolean gfsm_automaton_save_bin_gstring(gfsmAutomaton *fsm, GString *gs, gfsmError **errp);

//@}

/*======================================================================
 * Automaton Methods: Text I/O
 */
/// \name Automaton Methods: Text I/O
//@{

/** Load an automaton in Ma-Bell-compatible text-format from a gfsmIOHandle*  */
gboolean gfsm_automaton_compile_handle (gfsmAutomaton *fsm,
					gfsmIOHandle  *ioh,
					gfsmAlphabet  *lo_alphabet,
					gfsmAlphabet  *hi_alphabet,
					gfsmAlphabet  *state_alphabet,
					gfsmError     **errp);


/** Load an automaton in Ma-Bell-compatible text-format from a FILE*  */
gboolean gfsm_automaton_compile_file_full (gfsmAutomaton *fsm,
					   FILE          *f,
					   gfsmAlphabet  *lo_alphabet,
					   gfsmAlphabet  *hi_alphabet,
					   gfsmAlphabet  *state_alphabet,
					   gfsmError     **errp);

/** Convenience macro for compiling all-numeric-id text streams */
#define gfsm_automaton_compile_file(fsm,filep,errp) \
   gfsm_automaton_compile_file_full((fsm),(filep),NULL,NULL,NULL,(errp))

/** Load an automaton in Ma-Bell-compatible text-format from a named file, possibly compressed. */
gboolean gfsm_automaton_compile_filename_full (gfsmAutomaton *fsm,
					       const gchar   *filename,
					       gfsmAlphabet  *lo_alphabet,
					       gfsmAlphabet  *hi_alphabet,
					       gfsmAlphabet  *state_alphabet,
					       gfsmError     **errp);

/** Convenience macro for compiling all-numeric-id named text files */
#define gfsm_automaton_compile_filename(fsm,filename,errp) \
   gfsm_automaton_compile_filename_full((fsm),(filename),NULL,NULL,NULL,(errp))

/** Load an automaton in Ma-Bell-compatible text-format from an in-memory buffer */
gboolean gfsm_automaton_compile_gstring_full (gfsmAutomaton *fsm,
					      GString       *gs,
					      gfsmAlphabet  *lo_alphabet,
					      gfsmAlphabet  *hi_alphabet,
					      gfsmAlphabet  *state_alphabet,
					      gfsmError     **errp);


/*-----------------------*/

/** Print an automaton in Ma-Bell-compatible text-format to a gfsmIOHandle* */
gboolean gfsm_automaton_print_handle (gfsmAutomaton *fsm,
				      gfsmIOHandle  *ioh,
				      gfsmAlphabet  *lo_alphabet,
				      gfsmAlphabet  *hi_alphabet,
				      gfsmAlphabet  *state_alphabet,
				      gfsmError     **errp);


/** Print an automaton in Ma-Bell-compatible text-format to a FILE*  */
gboolean gfsm_automaton_print_file_full (gfsmAutomaton *fsm,
					 FILE          *f,
					 gfsmAlphabet  *lo_alphabet,
					 gfsmAlphabet  *hi_alphabet,
					 gfsmAlphabet  *state_alphabet,
					 int            zlevel,
					 gfsmError     **errp);

/** Convenience macro for printing to uncompresed all-numeric-id text streams  */
#define gfsm_automaton_print_file(fsm,filep,errp) \
  gfsm_automaton_print_file_full((fsm),(filep),NULL,NULL,NULL,0,(errp))

/** Print an automaton in Ma-Bell-compatible text-format to a named file */
gboolean gfsm_automaton_print_filename_full (gfsmAutomaton *fsm,
					     const gchar   *filename,
					     gfsmAlphabet  *lo_alphabet,
					     gfsmAlphabet  *hi_alphabet,
					     gfsmAlphabet  *state_alphabet,
					     int            zlevel,
					     gfsmError     **errp);

/** Convenience macro for printing to uncompressed all-numeric-id named text files */
#define gfsm_automaton_print_filename(fsm,filep,errp) \
  gfsm_automaton_print_filename_full((fsm),(filep),NULL,NULL,NULL,0,(errp))

/** Print an automaton in Ma-Bell-compatible text-format to an in-memory buffer */
gboolean gfsm_automaton_print_gstring_full (gfsmAutomaton *fsm,
					    GString       *gs,
					    gfsmAlphabet  *lo_alphabet,
					    gfsmAlphabet  *hi_alphabet,
					    gfsmAlphabet  *state_alphabet,
					    gfsmError     **errp);

//@}

#endif /* _GFSM_AUTOMATON_IO_H */