File: io.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (371 lines) | stat: -rw-r--r-- 9,935 bytes parent folder | download | duplicates (3)
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
 /************************************************************************/
 /*                                                                      */
 /*                Centre for Speech Technology Research                 */
 /*                     University of Edinburgh, UK                      */
 /*                       Copyright (c) 1996,1997                        */
 /*                        All Rights Reserved.                          */
 /*                                                                      */
 /*  Permission is hereby granted, free of charge, to use and distribute */
 /*  this software and its documentation without restriction, including  */
 /*  without limitation the rights to use, copy, modify, merge, publish, */
 /*  distribute, sublicense, and/or sell copies of this work, and to     */
 /*  permit persons to whom this work is furnished to do so, 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.                        */
 /*   4. The authors' names are not used to endorse or promote products  */
 /*      derived from this software without specific prior written       */
 /*      permission.                                                     */
 /*                                                                      */
 /*  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: Richard Caley (rjc@cstr.ed.ac.uk)            */
 /*                   Date: Tue Jun 10 1997                              */
 /************************************************************************/
 /*                                                                      */
 /* Functions to open file descriptors for various kinds of data         */
 /* sources and sinks.                                                   */
 /*                                                                      */
 /************************************************************************/

#include <cstdlib>
#include <cstdio>
#include <fcntl.h>
#include "EST_unix.h"
#include "EST_socket.h"

#include <sys/types.h>

#include "EST_String.h"
#include "EST_bool.h"
#include "siod.h"
#include "siodp.h"
#include "io.h"

EST_Regex RxURL("\\([a-z]+\\)://?\\([^/:]+\\)\\(:\\([0-9]+\\)\\)?\\(.*\\)");
EST_Regex RxFILEURL("file:.*");
static EST_Regex ipnum("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");

const int default_http_port = 80;
//const int default_ftp_port = 21;

#define MAX_LINE_LENGTH (256)

static int port_to_int(const char *port)
{
  struct servent *serv;

  if (!port || *port == '\0')
    return -1;

  if ((serv=getservbyname(port,  "tcp")))
    return serv->s_port;

  return atoi(port);
}

int parse_url(const EST_String &url,
	       EST_String &protocol, 
	       EST_String &host, 
	       EST_String &port, 
	       EST_String &path)
{
  EST_String bitpath;
  int start_of_bracket[EST_Regex_max_subexpressions];
  int end_of_bracket[EST_Regex_max_subexpressions];

  if (url.matches(RxFILEURL,0,start_of_bracket, end_of_bracket))
  {
    protocol = "file";
    host = "";
    port = "";
    path = url.after("file:");
    return TRUE;
  }
  else  if (!url.matches(RxURL, 0, start_of_bracket, end_of_bracket))
    return FALSE;

  protocol = url.at(start_of_bracket[1], end_of_bracket[1]-start_of_bracket[1]);
  host = url.at(start_of_bracket[2], end_of_bracket[2]-start_of_bracket[2]);
  port = url.at(start_of_bracket[4], end_of_bracket[4]-start_of_bracket[4]);
  bitpath = url.at(start_of_bracket[5], end_of_bracket[5]-start_of_bracket[5]);

  if (protocol == "http")
      path = protocol + "://" + host + bitpath;
  else
      path = bitpath;

  return TRUE;
}

static int connect_to_server(const char *host, int port)
{
  struct sockaddr_in address; 
  struct hostent *hostentp;
  EST_String shost=host;
  int s;
  
  memset(&address, 0, sizeof(address));

  if (shost.matches(ipnum))
  {  
    address.sin_addr.s_addr = inet_addr(host);
    address.sin_family = AF_INET;
  }
  else if ((hostentp=gethostbyname(host))==NULL)
	err("can't find host", host);
  else
  {
    memset(&(address.sin_addr),0,sizeof(struct in_addr));
    address.sin_family=hostentp->h_addrtype;
    memmove(&address.sin_addr, 
	    (hostentp->h_addr_list)[0],
	    hostentp->h_length);
  }
  address.sin_port=htons(port);

  if ((s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    err("can't create socket", NIL);

  if (connect(s, (struct sockaddr *)&address, sizeof(address)) < 0)
	{
	  close(s);
	  err("can't connect to host", 
	      inet_ntoa(address.sin_addr));
	}

  return s;
}

static void server_send(int s, const char *text)
{
  size_t n=strlen(text);
  ssize_t sent;

  while (n>0)
    if ((sent = write(s, text, n))<0)
      err("error talking to server", NIL);
    else
      n -= sent;
}

static const char *server_get_line(int s)
{
  static char buffer[MAX_LINE_LENGTH+1];
  char *p=buffer;
  ssize_t n;

  *p='\0';

  while(1==1)
    if ((n=read(s, p, 1)) == 0)
      break;
    else if (n < 0)
      err("error while reading from server", NIL);
    else if (*(p++) == '\n')
      break;

  *p = '\0';
      
  return buffer;
}


/*
 * Open stdin or stdout. Should this do a dup?
 */

int fd_open_stdinout(const char *r_or_w)
{
  int fd = -1;

  if (r_or_w[0] == 'r')
    fd = fileno(stdin);
  else if (r_or_w[0] == 'w')
    fd = fileno(stdout);
  else
    err("mode not understood for -", r_or_w);
  return fd;
}

/*
 * Duplicates the fopen interpretation of the type
 * parameter plus "rw" being a synonym for "r+" to preserve
 * some scheme semantics.
 */
int fd_open_file(const char *name, const char *r_or_w)
{
  int fd;
  int mode=0;
  int go_to_end=0;

  if (strcmp(name, "-")==0)
    return fd_open_stdinout(r_or_w);

  if (r_or_w[0] == 'r')
    if (r_or_w[1] == '+' || r_or_w[1] == 'w')
      mode = O_RDWR|O_CREAT;
    else
      mode = O_RDONLY;
  else if (r_or_w[0] == 'w')
      if (r_or_w[1] == '+')
	mode = O_RDWR|O_CREAT|O_TRUNC;
      else
	mode = O_WRONLY|O_CREAT|O_TRUNC;
  else if (r_or_w[0] == 'a')
    if (r_or_w[1] == '+')
      go_to_end = mode = O_RDWR;
    else
      go_to_end = mode = O_WRONLY|O_CREAT;
  else
    err("mode not understood", r_or_w);

  /* Should deal with `b' here for binary files.
   */
  
  fd= open(name, mode, 0666);

  if (fd >=0 && go_to_end)
    lseek(fd, 0, SEEK_END);

  return fd;
}

int fd_open_http(const char *host,
		 int port,
		 const char *path,
		 const char *r_or_w)
{
  int s;

  if (port <0)
    port=default_http_port;

  if ((s=connect_to_server(host, port)) < 0)
    return s;

  if (*r_or_w == 'r')
    {
      const char *line;
      float http_version;
      int   code;
      char  location[1024] = "";

      server_send(s, "GET ");
      server_send(s, path);
      server_send(s, " HTTP/1.0\n\n");
      shutdown(s, 1);

      line= server_get_line(s);

      if (sscanf(line, "HTTP/%f %d", &http_version, &code) != 2)
	{
	  close(s);
	  err("HTTP error", line);
	}

      // Skip rest of header.
      while((line = server_get_line(s)))
	{
	  if (*line=='\r' || *line == '\n' || *line == '\0')
	    break;
	  else if (sscanf(line, "Location: %s", location) == 1)
	    {
	      cout << "redirect to '" << location << "'\n";
	    }
	}

      if (code == 301 || code == 302)
	{
	  close(s);
	  
	  if (*location == '\0')
	    err("Redirection to no loction", NIL);


	  EST_String sprotocol, shost, sport, spath;

	  if (!parse_url(location, sprotocol, shost, sport, spath))
	    err("redirection to bad URL", location);
	  
	  s = fd_open_url(sprotocol, shost, sport, spath, "rb");
	}
      
    }
  else if (*r_or_w == 'w')
    err("Write to HTTP url not yet implemented", NIL);

  return s;
}

int fd_open_ftp(const char *host,
		 int port,
		 const char *path,
		 const char *r_or_w)
{
  (void)host;
  (void)port;
  (void)path;
  (void)r_or_w;

  return -1;
}

int fd_open_tcp(const char *host,
		 int port,
		const char *text,
		 const char *r_or_w)
{
  int s;

  if (port <0)
    return -1;

  if ((s=connect_to_server(host, port)) < 0)
    return s;

  server_send(s, text);

  if (*r_or_w == 'r')
    shutdown(s, 1);
  else if (*r_or_w == 'w')
    shutdown(s, 0);

  return s;
}

/*
 * Open a stream to a URL.
 */

int fd_open_url(const char *protocol,
		const char *host,
		const char *port,
		const char *path,
		const char *r_or_w)
{
  // special case for local file URLs
  if (strcmp(protocol, "file") == 0 
      && (!host || *host == '\0')
      && (!port || *port == '\0'))
    return fd_open_file(path, r_or_w);
  else if (strcmp(protocol, "file") == 0 || strcmp(protocol, "ftp") == 0)
    return fd_open_ftp(host, port_to_int(port), path, r_or_w);
  else if (strcmp(protocol, "http") == 0)
    return fd_open_http(host, port_to_int(port), path, r_or_w);
  else if (strcmp(protocol, "tcp") == 0)
    return fd_open_tcp(host, port_to_int(port), path, r_or_w);
  else
    return -1;
}