char *download_from_remote(const char *url) { const int buf_size = 1 * 1024 * 1024; char *fn; FILE *fp; uint8_t *buf; knetFile *fp_remote; int l; l = strlen(url); for (fn = (char*)url + l - 1; fn >= url; --fn) if (*fn == '/') break; ++fn; // fn now points to the file name if(fexists(fn)) return fn; fprintf(stderr, "attempting to download the remote index file: %s\n",url); extern std::vector <char *> dumpedFiles; dumpedFiles.push_back(strdup(fn)); fp_remote = knet_open(url, "r"); if (fp_remote == 0) { fprintf(stderr, "Fail to open remote file:%s\n",url); exit(0); } if ((fp = fopen(fn, "wb")) == 0) { fprintf(stderr, "Fail to save remote file:%s in working dir\n",url); exit(0); knet_close(fp_remote); } buf = (uint8_t*)calloc(buf_size, 1); while ((l = knet_read(fp_remote, buf, buf_size)) != 0) fwrite(buf, 1, l, fp); free(buf); fclose(fp); knet_close(fp_remote); return fn; }
knetFile *knet_open(const char *fn, const char *mode) { knetFile *fp = 0; if (mode[0] != 'r') { fprintf(stderr, "[kftp_open] only mode \"r\" is supported.\n"); return 0; } if (strstr(fn, "ftp://") == fn) { fp = kftp_parse_url(fn, mode); if (fp == 0) return 0; if (kftp_connect(fp) == -1) { knet_close(fp); return 0; } kftp_connect_file(fp); } else if (strstr(fn, "http://") == fn) { fp = khttp_parse_url(fn, mode); if (fp == 0) return 0; khttp_connect_file(fp); } else { // local file #ifdef _WIN32 /* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may * be undefined on some systems, although it is defined on my * Mac and the Linux I have tested on. */ FILE *fpf = fopen(fn, "rb"); #else FILE *fpf = fopen(fn, "r"); #endif if (!fpf) { perror("open"); return 0; } fp = (knetFile*)calloc(1, sizeof(knetFile)); fp->type = KNF_TYPE_LOCAL; fp->fp = fpf; fp->fd = -1; fp->ctrl_fd = -1; if (g_block_size > 0) { fp->buffer = malloc(g_block_size << 10); setvbuf(fp->fp, fp->buffer, _IOFBF, g_block_size << 10); } } if (fp && fp->type != KNF_TYPE_LOCAL && fp->fd == -1) { knet_close(fp); return 0; } if (fp && fp->type == KNF_TYPE_LOCAL && !fp->fp) { knet_close(fp); return 0; } return fp; }
knetFile *knet_open(const char *fn, const char *mode) { #ifdef KNETFILE_HOOKS if (alt_open) return alt_open(fn, mode); #endif knetFile *fp = 0; if (mode[0] != 'r') { fprintf(stderr, "[kftp_open] only mode \"r\" is supported.\n"); return 0; } if (strstr(fn, "ftp://") == fn) { fp = kftp_parse_url(fn, mode); if (fp == 0) return 0; if (kftp_connect(fp) == -1) { knet_close(fp); return 0; } kftp_connect_file(fp); } else if (strstr(fn, "http://") == fn) { fp = khttp_parse_url(fn, mode); if (fp == 0) return 0; khttp_connect_file(fp); } else { // local file #ifdef _WIN32 /* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may * be undefined on some systems, although it is defined on my * Mac and the Linux I have tested on. */ int fd = open(fn, O_RDONLY | O_BINARY); #else int fd = open(fn, O_RDONLY); #endif if (fd == -1) { perror("open"); return 0; } fp = (knetFile*)calloc(1, sizeof(knetFile)); fp->type = KNF_TYPE_LOCAL; fp->fd = fd; fp->ctrl_fd = -1; } if (fp && fp->fd == -1) { knet_close(fp); return 0; } return fp; }
FILE *download_and_open(const char *fn) { const int buf_size = 1 * 1024 * 1024; uint8_t *buf; FILE *fp; knetFile *fp_remote; const char *url = fn; const char *p; int l = strlen(fn); for (p = fn + l - 1; p >= fn; --p) if (*p == '/') break; fn = p + 1; // First try to open a local copy fp = fopen(fn, "r"); if (fp) return fp; // If failed, download from remote and open fp_remote = knet_open(url, "rb"); if (fp_remote == 0) { fprintf(stderr, "[download_from_remote] fail to open remote file %s\n",url); return NULL; } if ((fp = fopen(fn, "wb")) == 0) { fprintf(stderr, "[download_from_remote] fail to create file in the working directory %s\n",fn); knet_close(fp_remote); return NULL; } buf = (uint8_t*)calloc(buf_size, 1); while ((l = knet_read(fp_remote, buf, buf_size)) != 0) fwrite(buf, 1, l, fp); free(buf); fclose(fp); knet_close(fp_remote); return fopen(fn, "r"); }
bool PhoneHome::connect() { if(ourNumber == -1) { srand (time(NULL)); ourNumber = rand(); String numString; numString = ourNumber; String thinningString; thinningString = allThinning; add("uniqNum", numString); add("thinning", thinningString); } if((ourNumber % 100) >= allThinning) { // Skip phoneHome. return(true); } // std::cerr << "url = " << ourURL << std::endl; ourReturnString.Clear(); // return(true); #ifndef _NO_PHONEHOME knet_silent(1); knetFile *file = knet_open(ourURL.c_str(), "r"); if (file == 0) return(false); const int BUF_SIZE = 100; char buf[BUF_SIZE]; ssize_t readLen = BUF_SIZE-1; ssize_t numRead = readLen; while(numRead == readLen) { numRead = knet_read(file, buf, readLen); buf[numRead] = '\0'; ourReturnString += buf; } // std::cerr << ourReturnString.c_str() << std::endl; knet_close(file); knet_silent(0); ourReturnString = buf; #endif return(true); }
//{{{void destroy_vid_file(struct vid_file *v) void destroy_vid_file(struct vid_file *v) { if (v->type == VID_LOCAL) { if (fclose(v->file.local)) err(EX_IOERR, "Error closeing VID file '%s'", v->file_name); } else { if (knet_close(v->file.remote) != 0) err(EX_IOERR, "Error closeing remote VID file '%s'", v->file_name); } free(v->file_name); if (v->vids != NULL) free(v->vids); free(v); v = NULL; }
int main( int argc , char * argv[] ) { int rc,ver; /* Set the libkorebot debug level - Highly recommended for development. */ kb_set_debug_level(2); if((rc = kb_init( argc , argv )) < 0 ) return 1; signal( SIGINT , ctrlc_handler ); printf("K-Team KoreIO Test Program\r\n"); koreio = knet_open( "KoreIOLE:Board", KNET_BUS_ANY, 0 , NULL ); if(!koreio) { printf("Cannot open KoreIO device trying alternate address\r\n"); koreio = knet_open( "KoreIOLE:AltBoard", KNET_BUS_ANY, 0 , NULL ); if(!koreio) { printf("Cannot open KoreIO device\r\n"); return 1; } } /* Get and display the koreio firmware version */ kio_GetFWVersion(koreio,&ver); printf("KoreIO firmware %d.%d\r\n", (ver&0x000000F0)>>4, (ver&0x0000000F)); /* parse commands */ while (!quitReq) { printf("\n> "); if ( fgets( buf , sizeof(buf) , stdin ) != NULL ) { buf[strlen(buf)-1] = '\0'; kb_parse_command( buf , cmds , NULL); } } knet_close( koreio ); }
int main(void) { char *buf; knetFile *fp; int type = 4, l; #ifdef _WIN32 knet_win32_init(); #endif buf = calloc(0x100000, 1); if (type == 0) { fp = knet_open("knetfile.c", "r"); knet_seek(fp, 1000, SEEK_SET); } else if (type == 1) { // NCBI FTP, large file fp = knet_open("ftp://ftp.ncbi.nih.gov/1000genomes/ftp/data/NA12878/alignment/NA12878.chrom6.SLX.SRP000032.2009_06.bam", "r"); knet_seek(fp, 2500000000ll, SEEK_SET); l = knet_read(fp, buf, 255); } else if (type == 2) { fp = knet_open("ftp://ftp.sanger.ac.uk/pub4/treefam/tmp/index.shtml", "r"); knet_seek(fp, 1000, SEEK_SET); } else if (type == 3) { fp = knet_open("http://www.sanger.ac.uk/Users/lh3/index.shtml", "r"); knet_seek(fp, 1000, SEEK_SET); } else if (type == 4) { fp = knet_open("http://www.sanger.ac.uk/Users/lh3/ex1.bam", "r"); knet_read(fp, buf, 10000); knet_seek(fp, 20000, SEEK_SET); knet_seek(fp, 10000, SEEK_SET); l = knet_read(fp, buf+10000, 10000000) + 10000; } if (type != 4 && type != 1) { knet_read(fp, buf, 255); buf[255] = 0; printf("%s\n", buf); } else write(fileno(stdout), buf, l); knet_close(fp); free(buf); return 0; }
int main( int argc , char * argv[] ) { int rc,ver,io,ad,pw = 0, freq; char newpage = 12, state; uint16_t val; uint32_t time; /* Set the libkorebot debug level - Highly recommended for development. */ kb_set_debug_level(2); if((rc = kb_init( argc , argv )) < 0 ) return 1; signal( SIGINT , ctrlc_handler ); printf("K-Team KoreIO Test Program\r\n"); koreio = knet_open( "KoreIOLE:Board", KNET_BUS_ANY, 0 , NULL ); if(!koreio) { printf("Cannot open KoreIOLE device trying alternate address\r\n"); koreio = knet_open( "KoreIOLE:AltBoard", KNET_BUS_ANY, 0 , NULL ); if(!koreio) { printf("Cannot open KoreIOLE device\r\n"); return 1; } } /* Get and display the koreio firmware version */ kio_GetFWVersion(koreio,&ver); printf("KoreIOLE firmware %d.%d\r\n", (ver&0x000000F0)>>4, (ver&0x0000000F)); /* config all the IO as a output */ io = 0; while(io < 16) while(io < 16) { kio_ConfigIO(koreio,io,1); io++; usleep(1000); } /* set one of two Output */ io = 0; while(io < 16) { kio_SetIO(koreio,io); io+= 2; } /* clear one of two Output */ io = 1; while(io < 16) { kio_ClearIO(koreio,io); io+= 2; } /* parse commands */ while (!quitReq) { printf("\n> "); /* read all the analog input value */ ad = 0; while(ad < 12) { kio_ReadAnalog(koreio,ad,&val,&time); printf("read ad %d: %u at %lu mS\r\n",ad,val,time); ad++; } /* blink the output */ io = 0; while(io < 16) { kio_ChangeIO(koreio,io); io++; } /* blink the power output */ kio_ClearPW(koreio,pw); if(++pw > 5) pw = 0; kio_SetPW(koreio,pw); /*if ( fgets( buf , sizeof(buf) , stdin ) != NULL ) { buf[strlen(buf)-1] = '\0'; kb_parse_command( buf , cmds , NULL); }*/ /* clear the current page */ usleep(200000); printf("%c", newpage); } knet_close( koreio ); }
static int net_close(hFILE *fpv) { hFILE_net *fp = (hFILE_net *) fpv; return knet_close(fp->netfp); }