klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
klib / 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 <winsock2.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, *size_cmd; |
| 32 | int64_t seek_offset; // for lazy seek |
| 33 | int64_t file_size; |
| 34 | |
| 35 | // the following are for HTTP only |
| 36 | char *path, *http_host; |
| 37 | } knetFile; |
| 38 | |
| 39 | #define knet_tell(fp) ((fp)->offset) |
| 40 | #define knet_fileno(fp) ((fp)->fd) |
| 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
| 46 | #ifdef _WIN32 |
| 47 | int knet_win32_init(); |
| 48 | void knet_win32_destroy(); |
| 49 | #endif |
| 50 | |
| 51 | knetFile *knet_open(const char *fn, const char *mode); |
| 52 | |
| 53 | /* |
| 54 | This only works with local files. |
| 55 | */ |
| 56 | knetFile *knet_dopen(int fd, const char *mode); |
| 57 | |
| 58 | /* |
| 59 | If ->is_ready==0, this routine updates ->fd; otherwise, it simply |
| 60 | reads from ->fd. |
| 61 | */ |
| 62 | off_t knet_read(knetFile *fp, void *buf, off_t len); |
| 63 | |
| 64 | /* |
| 65 | This routine only sets ->offset and ->is_ready=0. It does not |
| 66 | communicate with the FTP server. |
| 67 | */ |
| 68 | off_t knet_seek(knetFile *fp, int64_t off, int whence); |
| 69 | int knet_close(knetFile *fp); |
| 70 | |
| 71 | #ifdef __cplusplus |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #endif |