9021b937ac211c1459ef1afec3c20b49ce6007a1
[samtools.git] / knetfile.h
1 #ifndef KNETFILE_H
2 #define KNETFILE_H
3
4 #include <stdint.h>
5 #include <fcntl.h>
6
7 #ifndef _WIN32
8 #define netread(fd, ptr, len) read(fd, ptr, len)
9 #define netwrite(fd, ptr, len) write(fd, ptr, len)
10 #define netclose(fd) close(fd)
11 #else
12 #include <winsock.h>
13 #define netread(fd, ptr, len) recv(fd, ptr, len, 0)
14 #define netwrite(fd, ptr, len) send(fd, ptr, len, 0)
15 #define netclose(fd) closesocket(fd)
16 #endif
17
18 // FIXME: currently I/O is unbuffered
19
20 #define KNF_TYPE_LOCAL 1
21 #define KNF_TYPE_FTP   2
22 #define KNF_TYPE_HTTP  3
23
24 typedef struct knetFile_s {
25         int type, fd;
26         int64_t offset;
27         char *host, *port;
28
29         // the following are for FTP only
30         int ctrl_fd, pasv_ip[4], pasv_port, max_response, no_reconnect, is_ready;
31         char *response, *retr;
32         int64_t seek_offset; // for lazy seek
33
34         // the following are for HTTP only
35         char *path, *http_host;
36 } knetFile;
37
38 #define knet_tell(fp) ((fp)->offset)
39 #define knet_fileno(fp) ((fp)->fd)
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #ifdef _WIN32
46         int knet_win32_init();
47         void knet_win32_destroy();
48 #endif
49
50         knetFile *knet_open(const char *fn, const char *mode);
51
52         /* 
53            This only works with local files.
54          */
55         knetFile *knet_dopen(int fd, const char *mode);
56
57         /*
58           If ->is_ready==0, this routine updates ->fd; otherwise, it simply
59           reads from ->fd.
60          */
61         off_t knet_read(knetFile *fp, void *buf, off_t len);
62
63         /*
64           This routine only sets ->offset and ->is_ready=0. It does not
65           communicate with the FTP server.
66          */
67         int knet_seek(knetFile *fp, off_t off, int whence);
68         int knet_close(knetFile *fp);
69
70 #ifdef __cplusplus
71 }
72 #endif
73
74 #endif