~ubuntu-branches/ubuntu/raring/sphinxtrain/raring-proposed

« back to all changes in this revision

Viewing changes to src/libs/libio/s3lamb_io.c

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ====================================================================
 
2
 * Copyright (c) 1996-2000 Carnegie Mellon University.  All rights 
 
3
 * reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer. 
 
11
 *
 
12
 * 2. Redistributions in binary form must reproduce the above copyright
 
13
 *    notice, this list of conditions and the following disclaimer in
 
14
 *    the documentation and/or other materials provided with the
 
15
 *    distribution.
 
16
 *
 
17
 * This work was supported in part by funding from the Defense Advanced 
 
18
 * Research Projects Agency and the National Science Foundation of the 
 
19
 * United States of America, and the CMU Sphinx Speech Consortium.
 
20
 *
 
21
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
 
22
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
 
25
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 
28
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 
29
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 
30
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 
31
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
 *
 
33
 * ====================================================================
 
34
 *
 
35
 */
 
36
/*********************************************************************
 
37
 *
 
38
 * File: s3lamb_io.c
 
39
 * 
 
40
 * Description: 
 
41
 *    Read/write interpolation lambda files
 
42
 *
 
43
 * Author: 
 
44
 *    Eric Thayer (eht@cs.cmu.edu)
 
45
 *********************************************************************/
 
46
 
 
47
#include <s3/s3lamb_io.h>
 
48
#include <s3/s3io.h>
 
49
#include <s3/s3.h>
 
50
 
 
51
#include <string.h>
 
52
 
 
53
int
 
54
s3lamb_read(const char *fn,
 
55
            float32 **out_lambda,
 
56
            uint32 *out_n_lambda)
 
57
{
 
58
    FILE *fp;
 
59
    const char *ver;
 
60
    const char *do_chk;
 
61
    uint32 rd_chksum = 0, sv_chksum, ignore;
 
62
    float32 *lambda;
 
63
    uint32 n_lambda;
 
64
    uint32 swap;
 
65
    
 
66
    fp = s3open(fn, "rb", &swap);
 
67
    if (fp == NULL)
 
68
        return S3_ERROR;
 
69
 
 
70
    /* check version io */
 
71
    ver = s3get_gvn_fattr("version");
 
72
    if (ver) {
 
73
        if (strcmp(ver, LAMBDA_FILE_VERSION) != 0) {
 
74
            E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
 
75
                    fn, ver, LAMBDA_FILE_VERSION);
 
76
        }
 
77
    }
 
78
    else {
 
79
        E_FATAL("No version attribute for %s\n", fn);
 
80
    }
 
81
    
 
82
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
83
    do_chk = s3get_gvn_fattr("chksum0");
 
84
 
 
85
    if (s3read_1d((void **)&lambda,
 
86
                  sizeof(float32),
 
87
                  &n_lambda,
 
88
                  fp,
 
89
                  swap,
 
90
                  &rd_chksum) != S3_SUCCESS)
 
91
        return S3_ERROR;
 
92
 
 
93
    if (do_chk) {
 
94
        /* See if the checksum in the file matches that which
 
95
           was computed from the read data */
 
96
 
 
97
        if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
 
98
            s3close(fp);
 
99
 
 
100
            return S3_ERROR;
 
101
        }
 
102
 
 
103
        if (sv_chksum != rd_chksum) {
 
104
            E_FATAL("Checksum error; read corrupt data.\n");
 
105
        }
 
106
    }
 
107
 
 
108
    s3close(fp);
 
109
 
 
110
    *out_lambda = lambda;
 
111
    *out_n_lambda = n_lambda;
 
112
 
 
113
    E_INFO("Read %s [%u array]\n", fn, n_lambda);
 
114
 
 
115
    return S3_SUCCESS;
 
116
}
 
117
 
 
118
int
 
119
s3lamb_write(const char *fn,
 
120
             const float32 *lambda,
 
121
             uint32 n_lambda)
 
122
{
 
123
    FILE *fp;
 
124
    uint32 chksum = 0;
 
125
    uint32 ignore = 0;
 
126
 
 
127
    s3clr_fattr();
 
128
    s3add_fattr("version", LAMBDA_FILE_VERSION, TRUE);
 
129
    s3add_fattr("chksum0", "yes", TRUE);
 
130
 
 
131
 
 
132
    fp = s3open(fn, "wb", NULL);
 
133
    if (fp == NULL)
 
134
        return S3_ERROR;
 
135
 
 
136
    if (s3write_1d((void *)lambda, sizeof(float32), n_lambda, fp, &chksum) != S3_SUCCESS) {
 
137
        s3close(fp);
 
138
        return S3_ERROR;
 
139
    }
 
140
 
 
141
    if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
 
142
        s3close(fp);
 
143
 
 
144
        return S3_ERROR;
 
145
    }
 
146
 
 
147
    s3close(fp);
 
148
 
 
149
    E_INFO("Wrote %s [%u array]\n", fn, n_lambda);
 
150
 
 
151
    return S3_SUCCESS;
 
152
}
 
153
 
 
154
int
 
155
s3lambcnt_read(const char *fn,
 
156
               uint32 **out_lambda_cnt,
 
157
               uint32 *out_n_lambda_cnt)
 
158
{
 
159
    FILE *fp;
 
160
    const char *ver;
 
161
    const char *do_chk;
 
162
    uint32 rd_chksum = 0, sv_chksum, ignore;
 
163
    uint32 *lambda_cnt;
 
164
    uint32 n_lambda_cnt;
 
165
    uint32 swap;
 
166
 
 
167
    fp = s3open(fn, "rb", &swap);
 
168
    if (fp == NULL)
 
169
        return S3_ERROR;
 
170
 
 
171
    /* check version io */
 
172
    ver = s3get_gvn_fattr("version");
 
173
    if (ver) {
 
174
        if (strcmp(ver, LAMBDACNT_FILE_VERSION) != 0) {
 
175
            E_FATAL("Version mismatch for %s, file ver: %s != reader ver: %s\n",
 
176
                    fn, ver, LAMBDACNT_FILE_VERSION);
 
177
        }
 
178
    }
 
179
    else {
 
180
        E_FATAL("No version attribute for %s\n", fn);
 
181
    }
 
182
    
 
183
    /* if do_chk is non-NULL, there is a checksum after the data in the file */
 
184
    do_chk = s3get_gvn_fattr("chksum0");
 
185
 
 
186
    if (s3read_1d((void **)&lambda_cnt,
 
187
                  sizeof(uint32),
 
188
                  &n_lambda_cnt,
 
189
                  fp,
 
190
                  swap,
 
191
                  &rd_chksum) != S3_SUCCESS)
 
192
        return S3_ERROR;
 
193
 
 
194
    if (do_chk) {
 
195
        /* See if the checksum in the file matches that which
 
196
           was computed from the read data */
 
197
 
 
198
        if (s3read(&sv_chksum, sizeof(uint32), 1, fp, swap, &ignore) != 1) {
 
199
            s3close(fp);
 
200
 
 
201
            return S3_ERROR;
 
202
        }
 
203
 
 
204
        if (sv_chksum != rd_chksum) {
 
205
            E_FATAL("Checksum error; read corrupt data.\n");
 
206
        }
 
207
    }
 
208
 
 
209
    s3close(fp);
 
210
 
 
211
    *out_lambda_cnt = lambda_cnt;
 
212
    *out_n_lambda_cnt = n_lambda_cnt;
 
213
 
 
214
    E_INFO("Read %s [%u array]\n", fn, n_lambda_cnt);
 
215
 
 
216
    return S3_SUCCESS;
 
217
}
 
218
 
 
219
int
 
220
s3lambcnt_write(const char *fn,
 
221
                const uint32 *lambda_cnt,
 
222
                uint32 n_lambda_cnt)
 
223
{
 
224
    FILE *fp;
 
225
    uint32 chksum = 0;
 
226
    uint32 ignore = 0;
 
227
 
 
228
    s3clr_fattr();
 
229
    s3add_fattr("version", LAMBDACNT_FILE_VERSION, TRUE);
 
230
    s3add_fattr("chksum0", "yes", TRUE);
 
231
 
 
232
 
 
233
    fp = s3open(fn, "wb", NULL);
 
234
    if (fp == NULL)
 
235
        return S3_ERROR;
 
236
 
 
237
    if (s3write_1d((void *)lambda_cnt, sizeof(uint32), n_lambda_cnt, fp, &chksum) != S3_SUCCESS) {
 
238
        s3close(fp);
 
239
        return S3_ERROR;
 
240
    }
 
241
 
 
242
    if (s3write(&chksum, sizeof(uint32), 1, fp, &ignore) != 1) {
 
243
        s3close(fp);
 
244
 
 
245
        return S3_ERROR;
 
246
    }
 
247
 
 
248
    s3close(fp);
 
249
 
 
250
    E_INFO("Wrote %s [%u array]\n", fn, n_lambda_cnt);
 
251
 
 
252
    return S3_SUCCESS;
 
253
}
 
254
 
 
255
/*
 
256
 * Log record.  Maintained by RCS.
 
257
 *
 
258
 * $Log$
 
259
 * Revision 1.4  2004/07/21  18:05:40  egouvea
 
260
 * Changed the license terms to make it the same as sphinx2 and sphinx3.
 
261
 * 
 
262
 * Revision 1.3  2001/04/05 20:02:31  awb
 
263
 * *** empty log message ***
 
264
 *
 
265
 * Revision 1.2  2000/09/29 22:35:13  awb
 
266
 * *** empty log message ***
 
267
 *
 
268
 * Revision 1.1  2000/09/24 21:38:31  awb
 
269
 * *** empty log message ***
 
270
 *
 
271
 * Revision 1.1  97/03/17  15:01:49  eht
 
272
 * Initial revision
 
273
 * 
 
274
 *
 
275
 */