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     knetFile *knet_open(const char *fn, const char *mode);
00052 
00053     /* 
00054        This only works with local files.
00055      */
00056     knetFile *knet_dopen(int fd, const char *mode);
00057 
00058     /*
00059       If ->is_ready==0, this routine updates ->fd; otherwise, it simply
00060       reads from ->fd.
00061      */
00062     off_t knet_read(knetFile *fp, void *buf, off_t len);
00063 
00064     /*
00065       This routine only sets ->offset and ->is_ready=0. It does not
00066       communicate with the FTP server.
00067      */
00068     off_t knet_seek(knetFile *fp, int64_t off, int whence);
00069     int knet_close(knetFile *fp);
00070 
00071 #ifdef __cplusplus
00072 }
00073 #endif
00074 
00075 #endif
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3