/* Try to open a file for reading. If the filename ends in one of the defined compressor extensions, pipe the file through the decompressor */ struct timidity_file *try_to_open(char *name, int decompress) { struct timidity_file *tf; URL url; int len; if((url = url_arc_open(name)) == NULL) if((url = url_open(name)) == NULL) return NULL; tf = (struct timidity_file *)safe_malloc(sizeof(struct timidity_file)); tf->url = url; tf->tmpname = NULL; len = strlen(name); if(decompress && len >= 3 && strcasecmp(name + len - 3, ".gz") == 0) { int method; if(!IS_URL_SEEK_SAFE(tf->url)) { if((tf->url = url_cache_open(tf->url, 1)) == NULL) { close_file(tf); return NULL; } } method = skip_gzip_header(tf->url); if(method == ARCHIVEC_DEFLATED) { url_cache_disable(tf->url); if((tf->url = url_inflate_open(tf->url, -1, 1)) == NULL) { close_file(tf); return NULL; } /* success */ return tf; } /* fail */ url_rewind(tf->url); url_cache_disable(tf->url); } #ifdef __W32__ /* Sorry, DECOMPRESSOR_LIST and PATCH_CONVERTERS are not worked yet. */ return tf; #endif /* __W32__ */ #if defined(DECOMPRESSOR_LIST) if(decompress) { static char *decompressor_list[] = DECOMPRESSOR_LIST, **dec; char tmp[1024]; /* Check if it's a compressed file */ for(dec = decompressor_list; *dec; dec += 2) { if(!check_file_extension(name, *dec, 0)) continue; tf->tmpname = url_dumpfile(tf->url, *dec); if (tf->tmpname == NULL) { close_file(tf); return NULL; } url_close(tf->url); snprintf(tmp, sizeof(tmp), *(dec+1), tf->tmpname); if((tf->url = url_pipe_open(tmp)) == NULL) { close_file(tf); return NULL; } break; } } #endif /* DECOMPRESSOR_LIST */ #if defined(PATCH_CONVERTERS) if(decompress == 2) { static char *decompressor_list[] = PATCH_CONVERTERS, **dec; char tmp[1024]; /* Check if it's a compressed file */ for(dec = decompressor_list; *dec; dec += 2) { if(!check_file_extension(name, *dec, 0)) continue; tf->tmpname = url_dumpfile(tf->url, *dec); if (tf->tmpname == NULL) { close_file(tf); return NULL; } url_close(tf->url); sprintf(tmp, *(dec+1), tf->tmpname); if((tf->url = url_pipe_open(tmp)) == NULL) { close_file(tf); return NULL; } break; } } #endif /* PATCH_CONVERTERS */ return tf; }
/* Small main program to retrive from a url using fgets and fread saving the * output to two test files (note the fgets method will corrupt binary files if * they contain 0 chars */ int main(int argc, char *argv[]) { URL_FILE *handle; FILE *outf; int nread; char buffer[256]; const char *url; if(argc < 2) { url="http://192.168.7.3/testfile";/* default to testurl */ } else { url=argv[1];/* use passed url */ } /* copy from url line by line with fgets */ outf=fopen("fgets.test","w+"); if(!outf) { perror("couldn't open fgets output file\n"); return 1; } handle = url_fopen(url, "r"); if(!handle) { printf("couldn't url_fopen()\n"); fclose(outf); return 2; } while(!url_feof(handle)) { url_fgets(buffer,sizeof(buffer),handle); fwrite(buffer,1,strlen(buffer),outf); } url_fclose(handle); fclose(outf); /* Copy from url with fread */ outf=fopen("fread.test","w+"); if(!outf) { perror("couldn't open fread output file\n"); return 1; } handle = url_fopen("testfile", "r"); if(!handle) { printf("couldn't url_fopen()\n"); fclose(outf); return 2; } do { nread = url_fread(buffer, 1,sizeof(buffer), handle); fwrite(buffer,1,nread,outf); } while(nread); url_fclose(handle); fclose(outf); /* Test rewind */ outf=fopen("rewind.test","w+"); if(!outf) { perror("couldn't open fread output file\n"); return 1; } handle = url_fopen("testfile", "r"); if(!handle) { printf("couldn't url_fopen()\n"); fclose(outf); return 2; } nread = url_fread(buffer, 1,sizeof(buffer), handle); fwrite(buffer,1,nread,outf); url_rewind(handle); buffer[0]='\n'; fwrite(buffer,1,1,outf); nread = url_fread(buffer, 1,sizeof(buffer), handle); fwrite(buffer,1,nread,outf); url_fclose(handle); fclose(outf); return 0;/* all done */ }
/* return 0 (successful or not convert) -1 (error and lost tf->url) */ int smfconv_w32(struct timidity_file *tf, char *fn) { URL url; int ret; struct midi_file_info *infop; /* rcpcv.dll convertion stage */ rcpcv_dll_stage: if(exist_rcpcv_dll()) { ret = is_midifile_filename(fn); if(ret == IS_RCP_FILE || ret == IS_R36_FILE || ret == IS_G18_FILE || ret == IS_G36_FILE) { if(!IS_URL_SEEK_SAFE(tf->url)) tf->url = url_cache_open(tf->url, 1); ret = is_midifile_url(tf->url); url_rewind(tf->url); if(ret == IS_RCP_FILE || ret == IS_R36_FILE || ret == IS_G18_FILE || ret == IS_G36_FILE) { ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Try to Convert RCP,R36,G18,G36 to SMF by RCPCV.DLL (c)1997 Fumy."); url = rcpcv_convert(tf->url,ret); if(url == NULL) { /* url_arc or url_cash is buggy ? */ /* url_rewind(tf->url); url_cache_disable(tf->url); ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Convert Failed."); goto end_of_rcpcv_dll_stage; */ char *buffer; int len; URL new_url; url_rewind(tf->url); if((buffer = (char *)url2mem(tf->url,&len))==NULL) { ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Convert Failed."); goto end_of_rcpcv_dll_stage; } if((new_url=url_mem_open(buffer,len,1))==NULL) { ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Convert Failed and Memory Allocate Error."); url_cache_disable(tf->url); return -1; } url_close(tf->url); tf->url = new_url; ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Convert Failed."); goto end_of_rcpcv_dll_stage; } url_cache_disable(tf->url); tf->url = url; ctl->cmsg(CMSG_INFO,VERB_NORMAL,"Convert Completed."); /* Store the midi file type information */ infop = get_midi_file_info(fn, 1); infop->file_type = ret; } else url_cache_disable(tf->url); } } /* end of rcpcv.dll convertion stage */ end_of_rcpcv_dll_stage: /* smfk32.dll convertion stage */ smfk32_dll_stage: /* not exist */ /* end of smfk32.dll convertion stage */ end_of_smfk32_dll_stage: last_stage: return 0; }