Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
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 
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 
129 {
130  int success;
131  char *a;
132  int d;
133 
134  d = strtol(s, &a, 0);
135  success = (*a == '\0') ? 0: 1;
136 
137  return d;
138 }
139 
141 {
142  char tmp[1000];
143 
144  sprintf(tmp, "%d", n);
145  return EST_String(tmp);
146 }
147 
148 EST_String ftoString(float n, int pres, int width, int right_justify)
149 {
150  (void)right_justify;
151  EST_String val;
152  char tmp[1000];
153  char spec[10];
154  strcpy(spec, "%");
155  if (width != 0)
156  strcat(spec, itoString(width));
157  strcat(spec, ".");
158  strcat(spec, itoString(pres));
159  strcat(spec, "f");
160 
161  sprintf(tmp, spec, n);
162  val = tmp;
163  return val;
164 }
165 
166 // Carry out equivalent of Bourne shell basename command, i.e. strip of
167 // leading path and optionally remove extension. It assumes directories
168 // are separated by a forward "/". This wont work on deviant OSs.
170 {
171  if (full.contains("/"))
172  {
173  full= full.after(full.index("/", -1));
174 // full= full.after("/"); //- don't know why this was here
175  }
176 
177  if (ext == "*")
178  {
179  if (full.contains("."))
180  full = full.before(".", -1); // everything apart from last extension
181  }
182  else if (ext == "?")
183  {
184  if (full.contains("."))
185  full = full.before("."); // everything up to first extension
186  }
187  else if (ext != "")
188  full = full.before(ext);
189 
190  return full;
191 }
192 
193 void strip_quotes(EST_String &s, const EST_String quote_char)
194 {
195  // if s is has quote_char as first and/or last char, strip them
196  if (s == "")
197  return;
198 
199  if (quote_char(0) == s(0))
200  s = s.after(0);
201  if (quote_char(0) == s(s.length()-1))
202  s = s.before((int)(s.length()-1));
203 }
204 
205 // uncompression via temporary file
207 uncompress_file_to_temporary(const EST_String &filename, const EST_String &prog_name)
208 {
209 
210  EST_String new_filename = make_tmp_filename();
211  EST_String sysstr = prog_name + " " + filename + " > " + new_filename;
212 
213  //cerr << "Uncompressing file : " << sysstr << endl;
214  int stat = system(sysstr);
215 
216  if(stat != 0)
217  {
218  (void)delete_file(new_filename);
219  new_filename = "";
220  }
221 
222  return new_filename;
223 }
224 
225 int compress_file_in_place(const EST_String &filename,
226  const EST_String &prog_name)
227 {
228  return system(prog_name + " " + filename);
229 }
230 
231 int compress_file(const EST_String &filename,
232  const EST_String &new_filename,
233  const EST_String &prog_name)
234 {
235 
236  EST_String sysstr;
237  if(new_filename == "-")
238  sysstr = prog_name + " " + filename;
239  else
240  sysstr = prog_name + " " + filename + " > " + new_filename;
241  return system(sysstr);
242 }
243 
244 /*
245 
246 EST_read_status load_TList_of_StrVector(EST_TList<EST_StrVector> &w,
247  const EST_String &filename,
248  const int vec_len)
249 {
250 
251  EST_TokenStream ts;
252  EST_String s;
253  EST_StrVector v;
254  int c;
255 
256  if(ts.open(filename) != 0){
257  cerr << "Can't open EST_TList<EST_StrVector> file " << filename << endl;
258  return misc_read_error;
259  }
260 
261  v.resize(vec_len);
262 // ts.set_SingleCharSymbols("");
263 // ts.set_PunctuationSymbols("");
264 
265  c=0;
266  while (!ts.eof())
267  {
268 
269  s = ts.get().string();
270  if(s != "")
271  {
272  if(c == vec_len)
273  {
274  cerr << "Too many points in line - expected " << vec_len << endl;
275  return wrong_format;
276  }
277  else
278  v[c++] = s;
279  }
280 
281  if(ts.eoln())
282  {
283  if(c != vec_len)
284  {
285  cerr << "Too few points in line - got "
286  << c << ", expected " << vec_len << endl;
287  return wrong_format;
288  }
289  else
290  {
291  w.append(v);
292  c=0;
293  }
294  }
295  }
296 
297  ts.close();
298  return format_ok;
299 
300 }
301 
302 int ilist_member(const EST_IList &l,int i)
303 {
304  EST_Litem *p;
305  for (p = l.head(); p != 0; p = p->next())
306  if (l.item(p) == i)
307  return TRUE;
308 
309  return FALSE;
310 }
311 
312 int ilist_index(const EST_IList &l,int i)
313 {
314  EST_Litem *p;
315  int j=0;
316  for (p = l.head(); p != 0; p = p->next())
317  {
318  if (l.item(p) == i)
319  return j;
320  j++;
321  }
322 
323  return -1;
324 }
325 
326 int strlist_member(const EST_StrList &l,const EST_String &s)
327 {
328  EST_Litem *p;
329  for (p = l.head(); p != 0; p = p->next())
330  if (l.item(p) == s)
331  return TRUE;
332 
333  return FALSE;
334 }
335 
336 int strlist_index(const EST_StrList &l,const EST_String &s)
337 {
338  EST_Litem *p;
339  int j=0;
340  for (p = l.head(); p != 0; p = p->next())
341  {
342  if (l.item(p) == s)
343  return j;
344  j++;
345  }
346 
347  return -1;
348 }
349 
350 */
int readable_file(char *filename)
return true if this file is readable
Definition: util_io.cc:63
int writable_file(char *filename)
return true if this file is writeable
Definition: util_io.cc:75
Utility IO Function header file.
int index(const char *s, int pos=0) const
Position of substring (starting at pos)
Definition: EST_String.h:361
EST_String itoString(int n)
Make a EST_String object from an integer.
Definition: util_io.cc:140
EST_String make_tmp_filename()
Make a unique temporary filename.
Definition: util_io.cc:54
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:148
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:225
int Stringtoi(EST_String s)
Make an int from a EST_String. EST_String equivalent of atoi()
Definition: util_io.cc:128
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:207
int length(void) const
Length of string ({not} length of underlying chunk)
Definition: EST_String.h:240
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:169
EST_String stdin_to_file()
Copy stdin to a file and return the name of that tmpfile.
Definition: util_io.cc:94
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:231
int contains(const char *s, int pos=-1) const
Does it contain this substring?
Definition: EST_String.h:374
void strip_quotes(EST_String &s, const EST_String quote_char)
remove quotes from a string
Definition: util_io.cc:193
EST_String after(int pos, int len=1) const
Part after pos+len.
Definition: EST_String.h:317
EST_String before(int pos, int len=0) const
Part before position.
Definition: EST_String.h:285
Utility EST_String Functions header file.