Debian Codegraph @

src/hrg/cst_rel_io.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 1999-2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: September 2000 */
35 /*************************************************************************/
36 /* */
37 /* Relations i/o */
38 /* */
39 /*************************************************************************/
40 #include "cst_file.h"
41 #include "cst_tokenstream.h"
42 #include "cst_item.h"
43 #include "cst_relation.h"
44 #include "cst_utterance.h"
45
46 int relation_load(cst_relation *r, const char *filename)
47 {
48 cst_tokenstream *fd;
49 cst_item *item;
50 const char *token=0;
51
52 if ((fd = ts_open(filename,NULL,";","","")) == 0)
53 {
54 cst_errmsg("relation_load: can't open file \"%s\" for reading\n",
55 filename);
56 return CST_ERROR_FORMAT;
57 }
58
59 for ( ; !ts_eof(fd); )
60 {
61 token = ts_get(fd);
62 if (cst_streq("#",token))
63 break;
64 }
65 if (!cst_streq("#",token))
66 {
67 cst_errmsg("relation_load: no end of header marker in \"%s\"\n",
68 filename);
69 ts_close(fd);
70 return CST_ERROR_FORMAT;
71 }
72
73 while (!ts_eof(fd))
74 {
75 token = ts_get(fd);
76 if (cst_streq(token,""))
77 continue;
78 item = relation_append(r,NULL);
79 item_set_float(item,"end",(float)cst_atof(token));
80 token = ts_get(fd);
81 token = ts_get(fd);
82 item_set_string(item,"name",token);
83 }
84
85 ts_close(fd);
86 return CST_OK_FORMAT;
87 }
88
89 int relation_save(cst_relation *r, const char *filename)
90 {
91 cst_file fd;
92 cst_item *item;
93
94 #ifndef UNDER_CE
95 if (cst_streq(filename,"-"))
96 fd = stdout;
97 else
98 #endif
99 if ((fd = cst_fopen(filename,CST_OPEN_WRITE)) == 0)
100 {
101 cst_errmsg("relation_save: can't open file \"%s\" for writing\n",
102 filename);
103 return CST_ERROR_FORMAT;
104 }
105
106 for (item=relation_head(r); item; item=item_next(item))
107 {
108 if (item_feat_present(item,"end"))
109 cst_fprintf(fd,"%f ",item_feat_float(item,"end"));
110 else
111 cst_fprintf(fd,"%f ",0.00);
112 if (item_feat_present(item,"name"))
113 cst_fprintf(fd,"%s ",item_feat_string(item,"name"));
114 else
115 cst_fprintf(fd,"%s ","_");
116 cst_fprintf(fd,"\n");
117 }
118 #ifndef UNDER_CE
119 if (fd != stdout)
120 cst_fclose(fd);
121 #endif
122
123 return CST_OK_FORMAT;
124 }
125