Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
util_io.cc
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 EST_String make_tmp_filename()
55 {
56  // returns tmp filename
57  char *tname = cmake_tmp_filename();
58  EST_String cname = tname;
59  wfree(tname);
60  return cname;
61 }
62 
63 int readable_file(char *filename)
64 {
65  // Returns TRUE if this is file is readable, FALSE otherwise
66 
67  if (streq(filename,"-"))
68  return TRUE;
69  else if (access(filename,R_OK) == 0)
70  return TRUE;
71  else
72  return FALSE;
73 }
74 
75 int writable_file(char *filename)
76 {
77  // Returns TRUE if this is afile is writable or creatable, FALSE
78  // otherwise
79  // Note this is *not* guaranteed to work, if the file doesn't
80  // exist the directory is checked if its writable but it can
81  // lie, esp. with ro file systems and NFS.
82 
83  if (streq(filename,"-"))
84  return TRUE;
85  else if (access(filename,W_OK) == 0)
86  return TRUE;
87  else if ((access(filename,F_OK) == -1) && // doesn't exists
88  (access(EST_Pathname(filename).directory(),W_OK) == 0))
89  return TRUE; // probably;
90  else
91  return FALSE;
92 }
93 
94 EST_String stdin_to_file()
95 {
96  /* Copy stding to a file and return the name of that tmpfile */
97  EST_String tmpname = make_tmp_filename();
98  char buff[1024];
99  FILE *fd;
100  unsigned int n;
101 
102  if ((fd = fopen(tmpname,"wb")) == NULL)
103  {
104  cerr << "Write access failed for temporary file\n";
105  return tmpname;
106  }
107  while ((n=fread(buff,1,1024,stdin)) > 0)
108  if (fwrite(buff,1,n,fd) != n)
109  {
110  cerr << "Write error on temporary file";
111  return tmpname;
112  }
113  fclose(fd);
114  return tmpname;
115 }
116 
117 int Stringtoi(EST_String s, int success)
118 {
119  char *a;
120  int d;
121 
122  d = strtol(s, &a, 0);
123  success = (*a == '\0') ? 0: 1;
124 
125  return d;
126 }
127 
128 int Stringtoi(EST_String s)
129 {
130  char *a;
131  int d;
132 
133  d = strtol(s, &a, 0);
134 
135  return d;
136 }
137 
138 EST_String itoString(int n)
139 {
140  char tmp[1000];
141 
142  sprintf(tmp, "%d", n);
143  return EST_String(tmp);
144 }
145 
146 EST_String ftoString(float n, int pres, int width, int right_justify)
147 {
148  (void)right_justify;
149  EST_String val;
150  char tmp[1000];
151  char spec[10];
152  strcpy(spec, "%");
153  if (width != 0)
154  strcat(spec, itoString(width));
155  strcat(spec, ".");
156  strcat(spec, itoString(pres));
157  strcat(spec, "f");
158 
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.
167 EST_String basename(EST_String full, EST_String ext)
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 */