File: util_io.cc

package info (click to toggle)
speech-tools 1.1.0-1
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 5,280 kB
  • ctags: 6,800
  • sloc: cpp: 53,460; ansic: 2,936; java: 1,405; makefile: 830; perl: 728; sh: 538; awk: 49; pascal: 14
file content (467 lines) | stat: -rw-r--r-- 11,295 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                    Copyright (c) 1994,1995,1996                       */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission to use, copy, modify, distribute this software and its    */
/*  documentation for research, educational and individual use only, is  */
/*  hereby granted without fee, subject to the following conditions:     */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*  This software may not be used for commercial purposes without        */
/*  specific prior written permission from the authors.                  */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                      Author :  Paul Taylor                            */
/*                      Date   :  May 1994                               */
/*-----------------------------------------------------------------------*/
/*                     File i/o utility functions                        */
/*                                                                       */
/*=======================================================================*/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <iostream.h>
#include "EST_types.h"
#include "EST_String.h"
#include "EST_Pathname.h"
#include "EST_io_aux.h"
#include "EST_string_aux.h"
#include "EST_cutils.h"
#include "EST_Token.h"

/* For endian tests */
int est_endian_loc = 1;

EST_String make_tmp_filename()
{
    // returns tmp filename
    char *tname = cmake_tmp_filename();
    EST_String cname = tname;
    wfree(tname);
    return cname;
}

int readable_file(char *filename)
{
    // Returns TRUE if this is file is readable, FALSE otherwise

    if (streq(filename,"-"))
	return TRUE;
    else if (access(filename,R_OK) == 0)
	return TRUE;
    else
	return FALSE;
}

int writable_file(char *filename)
{
    // Returns TRUE if this is afile is writable or creatable, FALSE
    // otherwise
    // Note this is *not* guaranteed to work, if the file doesn't
    // exist the directory is checked if its writable but it can
    // lie, esp. with ro file systems and NFS.

    if (streq(filename,"-"))
	return TRUE;
    else if (access(filename,W_OK) == 0)
	return TRUE;
    else if ((access(filename,F_OK) == -1) &&  // doesn't exists
	     (access(EST_Pathname(filename).directory(),W_OK) == 0))
	return TRUE;  // probably;
    else
	return FALSE;
}

EST_String stdin_to_file()
{
    /* Copy stding to a file and return the name of that tmpfile */
    EST_String tmpname = make_tmp_filename();
    char buff[1024];
    FILE *fd;
    unsigned int n;

    if ((fd = fopen(tmpname,"wb")) == NULL)
    {
	cerr << "Write access failed for temporary file\n";
	return tmpname;
    }	    
    while ((n=fread(buff,1,1024,stdin)) > 0)
	if (fwrite(buff,1,n,fd) != n)
	{
	    cerr << "Write error on temporary file";
	    return tmpname;
	}
    fclose(fd);
    return tmpname;
}

int Stringtoi(EST_String s, int success)
{
    char *a;
    int d;

    d = strtol(s, &a, 0);
    success = (*a == '\0') ? 0: 1;

    return d;
}

EST_String itoString(int n)
{
    char tmp[1000];
    
    sprintf(tmp, "%d", n);
    return EST_String(tmp);
}

EST_String ftoString(float n, int pres, int width, int right_justify)
{
    (void)right_justify;
    EST_String val;
    char tmp[1000];
    char spec[10];
    strcpy(spec, "%");
    if (width != 0)
	strcat(spec, itoString(width));
    strcat(spec, ".");
    strcat(spec, itoString(pres));
    strcat(spec, "f");
    
    sprintf(tmp, spec, n);
    val = tmp;
    return val;
}

int StrListtoFList(EST_StrList &s, EST_FList &f)
{
    EST_TBI *p;

    for (p = s.head(); p; p = next(p))
	if (!s(p).matches(RXdouble))
	{
	    cout << 
		"Expecting a floating point value in StrListtoFlist(): got "
		    << s(p) << endl;
	    return -1;
	}
	else
	    f.append(atof(s(p)));

    return 0;
}

int StrListtoIList(EST_StrList &s, EST_IList &il)
{
    EST_TBI *p;

    for (p = s.head(); p; p = next(p))
	if (!s(p).matches(RXint))
	{
	    cout << 
		"Expecting a integer value in StrListtoIlist(): got "
		    << s(p) << endl;
	    return -1;
	}
	else
	    il.append(atoi(s(p)));

    return 0;
}

// read string list eclosed in brackets. Simply a place holder for
// future use with more complicate lists.
void BracketStringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
{
    s.gsub("(", "");
    s.gsub(")", "");
    StringtoStrList(s, l, sep);
}
    
void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
{
    EST_TokenStream ts;
    EST_String tmp;

    ts.open_string(s);

    (void)sep;
    if (sep != "")  // default is standard white space
	ts.set_WhiteSpaceChars(sep);
    ts.set_SingleCharSymbols(";");

    // modified by simonk - was appending an empty
    // string at end of list. 
    // unmodified back again by pault
    while (!ts.eof())
	l.append(ts.get().string());
        
    ts.close();
    return;
}
    
void StrListtoString(EST_StrList &l, EST_String &s, EST_String sep)
{
    for (EST_Litem *p = l.head(); p; p = next(p))
	s += l(p) + sep;
}
    
// Carry out equivalent of Bourne shell basename command, i.e. strip of
// leading path and optionally remove extension. It assumes directories
// are separated by a forward "/".  This wont work on deviant OSs.
EST_String basename(EST_String full, EST_String ext)
{
    if (full.contains("/"))
    {
	full= full.after(full.index("/", -1));
//	full= full.after("/"); //- don't know why this was here
    }

    if (ext == "*") 
    {
	if (full.contains("."))
	    full = full.before(".", -1); // everything apart from last extension
    }
    else if (ext == "?")
    {
	if (full.contains("."))
	    full = full.before("."); // everything up to first extension
    }
    else if (ext != "")
	full = full.before(ext);
    
    return full;
}

EST_read_status load_StrList(EST_String filename, EST_StrList &l)
{
    EST_TokenStream ts;
    EST_String s;

    if(ts.open(filename) != 0){
	cerr << "Can't open EST_StrList file " << filename << endl;
	return misc_read_error;
    }
    
    ts.set_SingleCharSymbols("");
    ts.set_PunctuationSymbols("");
    
    while (!ts.eof())
	l.append(ts.get().string());
    
    ts.close();
    return format_ok;
}

EST_write_status save_StrList(EST_String filename, EST_StrList &l, 
			      EST_String style)
{
    ostream *outf;
    EST_TBI *p;
    if (filename == "-")
	outf = &cout;
    else
	outf = new ofstream(filename);
    
    if (!(*outf))
	return write_fail;

    if (style == "words")
    {
	for (p = l.head(); p; p = next(p))
	{
	    *outf << l(p);
	    if (next(p) != 0)
		*outf << " ";
	}
	*outf << endl;
    }

    else if (style == "lines")
	for (p = l.head(); p; p = next(p))
	    *outf << l(p) << endl;
    else
    {
	cerr << "Unknown style for writing StrLists: " << style << endl;
	return misc_write_error;
    }

    delete outf;

    return write_ok;
}

void strip_quotes(EST_String &s, const EST_String quote_char)
{
    // if s is has quote_char as first and/or last char, strip them
    if (s == "")
	return;

    if (quote_char(0) == s(0))
	s = s.after(0);
    if (quote_char(0) == s(s.length()-1))
	s = s.before((int)(s.length()-1));
}

// uncompression via temporary file
EST_String
uncompress_file_to_temporary(const EST_String &filename, const EST_String &prog_name)
{
    
    EST_String new_filename = make_tmp_filename();
    EST_String sysstr = prog_name + " " + filename + " > " + new_filename;
    
    //cerr << "Uncompressing file : " << sysstr << endl;
    int stat = system(sysstr);

    if(stat != 0)
    {
	(void)delete_file(new_filename);
	new_filename = "";
    }

    return new_filename;
}

int compress_file_in_place(const EST_String &filename, 
			   const EST_String &prog_name)
{
    return system(prog_name + " " + filename);
}

int compress_file(const EST_String &filename,
		  const EST_String &new_filename, 
		  const EST_String &prog_name)
{

    EST_String sysstr;
    if(new_filename == "-")
	sysstr = prog_name + " " + filename;
    else
	sysstr = prog_name + " " + filename + " > " + new_filename;
    return system(sysstr);
}


EST_read_status load_TList_of_StrVector(EST_TList<EST_StrVector> &w,
					const EST_String &filename,
					const int vec_len)
{

    EST_TokenStream ts;
    EST_String s;
    EST_StrVector v;
    int c;

    if(ts.open(filename) != 0){
	cerr << "Can't open EST_TList<EST_StrVector> file " << filename << endl;
	return misc_read_error;
    }
    
    v.resize(vec_len);
//    ts.set_SingleCharSymbols("");
//    ts.set_PunctuationSymbols("");

    c=0;
    while (!ts.eof())
    {

	s = ts.get().string();
	if(s != "")
	{
	    if(c == vec_len)
	    {
		cerr << "Too many points in line - expected " << vec_len << endl;
		return wrong_format;
	    }
	    else
		v[c++] = s;
	}

	if(ts.eoln())
	{
	    if(c != vec_len)
	    {
		cerr << "Too few points in line - got "
		    << c << ", expected " << vec_len << endl;
		return wrong_format;
	    }
	    else
	    {
		w.append(v);
		c=0;
	    }
	}
    }

    ts.close();
    return format_ok;

}


int ilist_member(const EST_IList &l,int i)
{
    EST_TBI *p;
    for (p = l.head(); p != 0; p = next(p))
	if (l.item(p) == i)
	    return TRUE;

    return FALSE;
}

int ilist_index(const EST_IList &l,int i)
{
    EST_TBI *p;
    int j=0;
    for (p = l.head(); p != 0; p = next(p))
    {
	if (l.item(p) == i)
	    return j;
	j++;
    }

    return -1;
}

int strlist_member(const EST_StrList &l,const EST_String &s)
{
    EST_TBI *p;
    for (p = l.head(); p != 0; p = next(p))
	if (l.item(p) == s)
	    return TRUE;

    return FALSE;
}

int strlist_index(const EST_StrList &l,const EST_String &s)
{
    EST_TBI *p;
    int j=0;
    for (p = l.head(); p != 0; p = next(p))
    {
	if (l.item(p) == s)
	    return j;
	j++;
    }

    return -1;
}