libStatGen Software  1
knetfile.h
00001 #ifndef KNETFILE_H
00002 #define KNETFILE_H
00003 
00004 #include <stdint.h>
00005 #include <fcntl.h>
00006 
00007 #ifndef _WIN32
00008 #define netread(fd, ptr, len) read(fd, ptr, len)
00009 #define netwrite(fd, ptr, len) write(fd, ptr, len)
00010 #define netclose(fd) close(fd)
00011 #else
00012 #include <winsock2.h>
00013 #define netread(fd, ptr, len) recv(fd, ptr, len, 0)
00014 #define netwrite(fd, ptr, len) send(fd, ptr, len, 0)
00015 #define netclose(fd) closesocket(fd)
00016 #endif
00017 
00018 // FIXME: currently I/O is unbuffered
00019 
00020 #define KNF_TYPE_LOCAL 1
00021 #define KNF_TYPE_FTP   2
00022 #define KNF_TYPE_HTTP  3
00023 
00024 typedef struct knetFile_s {
00025     int type, fd;
00026     int64_t offset;
00027     char *host, *port;
00028 
00029     // the following are for FTP only
00030     int ctrl_fd, pasv_ip[4], pasv_port, max_response, no_reconnect, is_ready;
00031     char *response, *retr, *size_cmd;
00032     int64_t seek_offset; // for lazy seek
00033     int64_t file_size;
00034 
00035     // the following are for HTTP only
00036     char *path, *http_host;
00037 } knetFile;
00038 
00039 #define knet_tell(fp) ((fp)->offset)
00040 #define knet_fileno(fp) ((fp)->fd)
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 #ifdef _WIN32
00047     int knet_win32_init();
00048     void knet_win32_destroy();
00049 #endif
00050 
00051         // Pass in non-zero to make knetfile silent (no messages), pass in 
00052         // 0 to keep any messages (default is 0).
00053         void knet_silent(int silent);
00054 
00055     knetFile *knet_open(const char *fn, const char *mode);
00056 
00057     /* 
00058        This only works with local files.
00059      */
00060     knetFile *knet_dopen(int fd, const char *mode);
00061 
00062     /*
00063       If ->is_ready==0, this routine updates ->fd; otherwise, it simply
00064       reads from ->fd.
00065      */
00066     ssize_t knet_read(knetFile *fp, void *buf, size_t len);
00067 
00068     /*
00069       This routine only sets ->offset and ->is_ready=0. It does not
00070       communicate with the FTP server.
00071      */
00072     off_t knet_seek(knetFile *fp, off_t off, int whence);
00073     int knet_close(knetFile *fp);
00074 
00075 #ifdef __cplusplus
00076 }
00077 #endif
00078 
00079 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends