Edinburgh Speech Tools  2.1-release
util_io.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1994,1995,1996 */
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 /* THE UNIVERSITY OF EDINBURGH 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 THE UNIVERSITY OF EDINBURGH 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 : Paul Taylor */
34 /* Date : May 1994 */
35 /*-----------------------------------------------------------------------*/
36 /* File i/o utility functions */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdio>
41 #include <cctype>
42 #include <cstdlib>
43 #include <string>
44 #include <fstream>
45 #include <iostream>
46 #include "EST_types.h"
47 #include "EST_String.h"
48 #include "EST_Pathname.h"
49 #include "EST_io_aux.h"
50 #include "EST_string_aux.h"
51 #include "EST_cutils.h"
52 #include "EST_Token.h"
53 
54 using namespace std;
55 
57 {
58  // returns tmp filename
59  char *tname = cmake_tmp_filename();
60  EST_String cname = tname;
61  wfree(tname);
62  return cname;
63 }
64 
65 int readable_file(char *filename)
66 {
67  // Returns TRUE if this is file is readable, FALSE otherwise
68 
69  if (streq(filename,"-"))
70  return TRUE;
71  else if (access(filename,R_OK) == 0)
72  return TRUE;
73  else
74  return FALSE;
75 }
76 
77 int writable_file(char *filename)
78 {
79  // Returns TRUE if this is afile is writable or creatable, FALSE
80  // otherwise
81  // Note this is *not* guaranteed to work, if the file doesn't
82  // exist the directory is checked if its writable but it can
83  // lie, esp. with ro file systems and NFS.
84 
85  if (streq(filename,"-"))
86  return TRUE;
87  else if (access(filename,W_OK) == 0)
88  return TRUE;
89  else if ((access(filename,F_OK) == -1) && // doesn't exists
90  (access(EST_Pathname(filename).directory(),W_OK) == 0))
91  return TRUE; // probably;
92  else
93  return FALSE;
94 }
95 
97 {
98  /* Copy stding to a file and return the name of that tmpfile */
99  EST_String tmpname = make_tmp_filename();
100  char buff[1024];
101  FILE *fd;
102  unsigned int n;
103 
104  if ((fd = fopen(tmpname,"wb")) == NULL)
105  {
106  cerr << "Write access failed for temporary file\n";
107  return tmpname;
108  }
109  while ((n=fread(buff,1,1024,stdin)) > 0)
110  if (fwrite(buff,1,n,fd) != n)
111  {
112  cerr << "Write error on temporary file";
113  fclose(fd);
114  return tmpname;
115  }
116  fclose(fd);
117  return tmpname;
118 }
119 
120 int Stringtoi(EST_String s, int &success)
121 {
122  char *a;
123  int d;
124 
125  d = strtol(s, &a, 0);
126  success = (*a == '\0') ? 0: 1;
127 
128  return d;
129 }
130 
132 {
133  char *a;
134  int d;
135 
136  d = strtol(s, &a, 0);
137 
138  return d;
139 }
140 
142 {
143  char tmp[1000];
144 
145  sprintf(tmp, "%d", n);
146  return EST_String(tmp);
147 }
148 
149 EST_String ftoString(float n, int pres, int width, int right_justify)
150 {
151  (void)right_justify;
152  EST_String val;
153  char tmp[10000];
154  char spec[100];
155  if (width != 0)
156  sprintf(spec, "%%%d.%df", width, pres);
157  else
158  sprintf(spec, "%%.%df", pres);
159  sprintf(tmp, spec, n);
160  val = tmp;
161  return val;
162 }
163 
164 // Carry out equivalent of Bourne shell basename command, i.e. strip of
165 // leading path and optionally remove extension. It assumes directories
166 // are separated by a forward "/". This wont work on deviant OSs.
168 {
169  if (full.contains("/"))
170  {
171  full= full.after(full.index("/", -1));
172 // full= full.after("/"); //- don't know why this was here
173  }
174 
175  if (ext == "*")
176  {
177  if (full.contains("."))
178  full = full.before(".", -1); // everything apart from last extension
179  }
180  else if (ext == "?")
181  {
182  if (full.contains("."))
183  full = full.before("."); // everything up to first extension
184  }
185  else if (ext != "")
186  full = full.before(ext);
187 
188  return full;
189 }
190 
191 void strip_quotes(EST_String &s, const EST_String quote_char)
192 {
193  // if s is has quote_char as first and/or last char, strip them
194  if (s == "")
195  return;
196 
197  if (quote_char(0) == s(0))
198  s = s.after(0);
199  if (quote_char(0) == s(s.length()-1))
200  s = s.before((int)(s.length()-1));
201 }
202 
203 // uncompression via temporary file
205 uncompress_file_to_temporary(const EST_String &filename, const EST_String &prog_name)
206 {
207 
208  EST_String new_filename = make_tmp_filename();
209  EST_String sysstr = prog_name + " " + filename + " > " + new_filename;
210 
211  //cerr << "Uncompressing file : " << sysstr << endl;
212  int stat = system(sysstr);
213 
214  if(stat != 0)
215  {
216  (void)delete_file(new_filename);
217  new_filename = "";
218  }
219 
220  return new_filename;
221 }
222 
223 int compress_file_in_place(const EST_String &filename,
224  const EST_String &prog_name)
225 {
226  return system(prog_name + " " + filename);
227 }
228 
229 int compress_file(const EST_String &filename,
230  const EST_String &new_filename,
231  const EST_String &prog_name)
232 {
233 
234  EST_String sysstr;
235  if(new_filename == "-")
236  sysstr = prog_name + " " + filename;
237  else
238  sysstr = prog_name + " " + filename + " > " + new_filename;
239  return system(sysstr);
240 }
241 
242 /*
243 
244 EST_read_status load_TList_of_StrVector(EST_TList<EST_StrVector> &w,
245  const EST_String &filename,
246  const int vec_len)
247 {
248 
249  EST_TokenStream ts;
250  EST_String s;
251  EST_StrVector v;
252  int c;
253 
254  if(ts.open(filename) != 0){
255  cerr << "Can't open EST_TList<EST_StrVector> file " << filename << endl;
256  return misc_read_error;
257  }
258 
259  v.resize(vec_len);
260 // ts.set_SingleCharSymbols("");
261 // ts.set_PunctuationSymbols("");
262 
263  c=0;
264  while (!ts.eof())
265  {
266 
267  s = ts.get().string();
268  if(s != "")
269  {
270  if(c == vec_len)
271  {
272  cerr << "Too many points in line - expected " << vec_len << endl;
273  return wrong_format;
274  }
275  else
276  v[c++] = s;
277  }
278 
279  if(ts.eoln())
280  {
281  if(c != vec_len)
282  {
283  cerr << "Too few points in line - got "
284  << c << ", expected " << vec_len << endl;
285  return wrong_format;
286  }
287  else
288  {
289  w.append(v);
290  c=0;
291  }
292  }
293  }
294 
295  ts.close();
296  return format_ok;
297 
298 }
299 
300 int ilist_member(const EST_IList &l,int i)
301 {
302  EST_Litem *p;
303  for (p = l.head(); p != 0; p = p->next())
304  if (l.item(p) == i)
305  return TRUE;
306 
307  return FALSE;
308 }
309 
310 int ilist_index(const EST_IList &l,int i)
311 {
312  EST_Litem *p;
313  int j=0;
314  for (p = l.head(); p != 0; p = p->next())
315  {
316  if (l.item(p) == i)
317  return j;
318  j++;
319  }
320 
321  return -1;
322 }
323 
324 int strlist_member(const EST_StrList &l,const EST_String &s)
325 {
326  EST_Litem *p;
327  for (p = l.head(); p != 0; p = p->next())
328  if (l.item(p) == s)
329  return TRUE;
330 
331  return FALSE;
332 }
333 
334 int strlist_index(const EST_StrList &l,const EST_String &s)
335 {
336  EST_Litem *p;
337  int j=0;
338  for (p = l.head(); p != 0; p = p->next())
339  {
340  if (l.item(p) == s)
341  return j;
342  j++;
343  }
344 
345  return -1;
346 }
347 
348 */
int readable_file(char *filename)
return true if this file is readable
Definition: util_io.cc:65
int writable_file(char *filename)
return true if this file is writeable
Definition: util_io.cc:77
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
Definition: EST_String.h:365
Utility IO Function header file.
char * cmake_tmp_filename()
Definition: EST_cutils.c:66
EST_String itoString(int n)
Make a EST_String object from an integer.
Definition: util_io.cc:141
size_t index(const char *s, ssize_t pos=0) const
Position of substring (starting at pos)
Definition: EST_String.h:352
#define streq(X, Y)
Definition: EST_cutils.h:57
EST_String make_tmp_filename()
Make a unique temporary filename.
Definition: util_io.cc:56
EST_String ftoString(float n, int pres, int width, int right_justify)
Make a EST_String object from an float, with variable precision.
Definition: util_io.cc:149
int Stringtoi(EST_String s, int &success)
Definition: util_io.cc:120
int compress_file_in_place(const EST_String &filename, const EST_String &prog_name)
Uncompress file and over-write existing file with uncompressed version.
Definition: util_io.cc:223
EST_String uncompress_file_to_temporary(const EST_String &filename, const EST_String &prog_name)
Uncompress file by calling program prog, and write it to new tempoary file. Return name of temporary ...
Definition: util_io.cc:205
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
EST_String basename(EST_String full, EST_String ext)
This acts like the bourne shell basename command. By default, it strips any leading path from a strin...
Definition: util_io.cc:167
EST_String stdin_to_file()
Copy stdin to a file and return the name of that tmpfile.
Definition: util_io.cc:96
int delete_file(const EST_String &filename)
OS independent way of removing a file.
Definition: EST_io_aux.h:81
int compress_file(const EST_String &filename, const EST_String &new_filename, const EST_String &prog_name)
compress file by calling program prog, writing result to new_filename
Definition: util_io.cc:229
size_t length(void) const
Length of string ({not} length of underlying chunk)
Definition: EST_String.h:231
void strip_quotes(EST_String &s, const EST_String quote_char)
remove quotes from a string
Definition: util_io.cc:191
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:308
EST_String before(int pos, int len=0) const
Part before position.
Definition: EST_String.h:276
EST_String
void wfree(void *p)
Definition: walloc.c:131
#define TRUE
Definition: EST_bool.h:118
Utility EST_String Functions header file.