//K路串行读取 void k_num_read(void) { char* filename; int i,cnt,*array; FILE* fin; FILE* tmpfile; //计算knum,每路应读取的整数个数 int n = NUMBER/KNUM; if (n * KNUM < NUMBER)n++; //建立存储分块读取的数据的数组 array = (int*)malloc(n * sizeof(int));assert(array); //打开输入文件 fin = fopen(in_file,"rt"); i = 0; //分块循环读取数据,并写入硬盘上的临时文件 while ( (cnt = read_data(fin,array,n))>0) { //对每次读取的数据,先进行快速排序,然后写入硬盘上的临时文件 QuickSort(array,0,cnt - 1); filename = temp_filename(i++); tmpfile = fopen(filename,"w"); free(filename); write_data(tmpfile,array,cnt); fclose(tmpfile); } assert(i == KNUM); //没有生成K路文件时进行诊断 //关闭输入文件句柄和临时存储数组 fclose(fin); free(array); }
void RF_destroy(RF_class *RF, WORD compact_threshold) { OD_link *link, *next; WORD lost_percent; ULONG lost_space, entry, nentries; RF_class *new_class, *old; BYTE *RF_filename, *temp_fn; if (RF->touched) { lseek(RF->file, 0L, SEEK_SET); r_write(RF->file, &RF->hdr, sizeof(RF_file_hdr)); } link = RF->root; while (link != NULL) { if (link->touched) { lseek(RF->file, link->origin, SEEK_SET); r_write(RF->file, &link->blk, sizeof(OD_block)); } next = link->next; mem_free(link); link = next; } close(RF->file); lost_space = RF->hdr.lost_space; lost_percent = (WORD) ((lost_space * 100L) / RF->hdr.file_size); RF_filename = str_alloc(RF->filename); mem_free(RF->filename); mem_free(RF); if (lost_space && (lost_percent >= compact_threshold)) { rename(RF_filename, temp_fn = temp_filename(NULL)); old = RF_construct(temp_fn, 0); new_class = RF_construct(RF_filename, 1); new_class->hdr.modify_time = old->hdr.modify_time; new_class->hdr.create_time = old->hdr.create_time; new_class->touched = 1; nentries = RF_entry_count(old); for (entry = 0; entry < nentries; entry++) { RF_new_entry(new_class, &old->file, RF_header(old, entry), RTYP_HOUSECLEAN); RF_set_flags(new_class, entry, RF_flags(old, entry)); } RF_destroy(new_class, 0); RF_destroy(old, 100); remove_tempfile(temp_fn); } mem_free(RF_filename); }
/** int save_https_xml(int cofd, SSL* ssl, tList** pl, tXML** xml, char** recvfn, const char* wdir, int timeout, int* state) 受信した XML通信データをファイルに保存 @param cofd 接続相手へのソケット @param ssl 接続相手への SSLソケット.SSL通信でない場合は NULL を指定. @param[out] *pl 受信したヘッダが格納されるリストへのポインタ. @param[out] *xml パースされたデータが格納されるXML構造体へのポインタ @param[out] *recvfn 保存するファイル名 @param wdir 作業用ディレクトリを指定する.NULLの場合は /tmp になる. @param timeout 最初の受信までのタイムアウト(s) @param[out] state サーバとの接続状態.接続中なら TRUE.切断した場合は FALSE.NULLを指定しても良い. @retval 1以上 受信したファイルのサイズ(Byte)(ヘッダを含む) @retval 0 正常切断 @retval -1 受信エラー @retval RECV_TIMEOUTED タイムアウトした. */ int save_https_xml(int cofd, SSL* ssl, tList** pl, tXML** xml, char** recvfn, const char* wdir, int timeout, int* state) { int header; if (pl==NULL || recvfn==NULL) return 0; *pl = NULL; if (xml!=NULL) *xml = NULL; *recvfn = temp_filename(wdir, WORK_FILENAME_LEN); int cc = recv_https_file(cofd, ssl, pl, *recvfn, wdir, timeout, &header, state); if (cc<=0 || *pl==NULL) { free(*recvfn); *recvfn = NULL; if (cc==0) return 0; return -1; } if (header) { free(*recvfn); *recvfn = NULL; } else { if (xml!=NULL && *pl!=NULL && *recvfn!=NULL && cc>0) { Buffer buf = search_protocol_header(*pl, (char*)"Content-Type", 1); //application/llsd+xml; charset=UTF-8 //if (buf.buf!=NULL && strstrcase((const char*)buf.buf, "xml")!=NULL) { *xml = xml_parse_file(*recvfn); //if (*xml!=NULL && (*xml)->state!=XML_PARSED) del_xml(xml); if (*xml!=NULL && (*xml)->state<0) del_xml(xml); //} free_Buffer(&buf); } } return cc; }
/* executes an extern command and redirects the stdout */ static char *tag_exec (int argc, char *argv[]) { if (argc >= 1) { char *s, *filename = temp_filename (); int outbak = dup (1); int outdsc = open (filename, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); char buf[4096] = ""; STREAM *in; int c; /* make the command to run */ for (c = 0; c < argc; c++) { s = process_text (argv[c]); sprintf (buf + strlen (buf), "%s ", s); free (s); } /* redirect the stdout */ fflush (stdout); dup2 (outdsc, 1); /* run the command */ system (buf); /* restore the stdout */ dup2 (outbak, 1); close (outdsc); close (outbak); /* read the redirected output from the file and put it to the active output stream */ in = stopen (filename, "r"); while (stgets (buf, sizeof (buf), in)) stputs (buf, _o_stream); stclose (in); /* close */ remove (filename); free (filename); } /* nothing to add */ return NULL; }
static pngquant_error write_image(png8_image *output_image, png24_image *output_image24, const char *outname, struct pngquant_options *options) { FILE *outfile; char *tempname = NULL; if (options->using_stdout) { set_binary_mode(stdout); outfile = stdout; if (output_image) { verbose_printf(options, " writing %d-color image to stdout", output_image->num_palette); } else { verbose_printf(options, " writing truecolor image to stdout"); } } else { tempname = temp_filename(outname); if (!tempname) return OUT_OF_MEMORY_ERROR; if ((outfile = fopen(tempname, "wb")) == NULL) { fprintf(stderr, " error: cannot open '%s' for writing\n", tempname); free(tempname); return CANT_WRITE_ERROR; } if (output_image) { verbose_printf(options, " writing %d-color image as %s", output_image->num_palette, filename_part(outname)); } else { verbose_printf(options, " writing truecolor image as %s", filename_part(outname)); } } pngquant_error retval; #pragma omp critical (libpng) { if (output_image) { retval = rwpng_write_image8(outfile, output_image); } else { retval = rwpng_write_image24(outfile, output_image24); } } if (!options->using_stdout) { fclose(outfile); if (SUCCESS == retval) { // Image has been written to a temporary file and then moved over destination. // This makes replacement atomic and avoids damaging destination file on write error. if (!replace_file(tempname, outname, options->force)) { retval = CANT_WRITE_ERROR; } } if (retval) { unlink(tempname); } } free(tempname); if (retval && retval != TOO_LARGE_FILE) { fprintf(stderr, " error: failed writing image to %s\n", outname); } return retval; }
//k路合并(败者树) void k_num_merge(void) { FILE *fout; FILE **farray; char *filename; int *data; char *hasNext; int i,j,m,min; #ifdef OUTPUT_OUT_FILE_DATAint id; #endif //打开输出文件 fout = fopen(out_file,"wt"); //打开各路临时分块文件 farray = (FILE**)malloc(KNUM*sizeof(FILE*)); assert(farray); for(i = 0; i< KNUM;i++) { filename = temp_filename(i); farray[i] = fopen(filename,"rt"); free(filename); } //建立KNUM个元素的data,hasNext数组,存储K路文件的临时数组和读取结束状态 data = (int*)malloc(KNUM*sizeof(int)); assert(data); hasNext = (char*)malloc(sizeof(char)*KNUM); assert(hasNext); memset(data, 0, sizeof(int) * KNUM); memset(hasNext, 1, sizeof(char) * KNUM); //读K路文件先读取第一组数据,并对读取结束的各路文件设置不可再读状态 for(i = 0; i < KNUM; i++) { if(fscanf(farray[i], "%d", &data[i]) == EOF) { hasNext[i] = 0; } } //读取各路文件,利用败者树从小到大输出到输出文件 #ifdef OUTPUT_OUT_FILE_DATAid = 0; #endif j = 0;F_LOOP: if (j < KNUM) //以下这段代码嵌套过深,日后应尽量避免此类问题。 { while(1==1) { min = data[j]; m = j; for(i = j+1; i < KNUM; i++) { if(hasNext[i] == 1 && min > data[i]) { min = data[i];m = i; } } if(fscanf(farray[m], "%d", &data[m]) == EOF) { hasNext[m] = 0; } fprintf(fout, "%d ", min); #ifdef OUTPUT_OUT_FILE_DATAprintf("fout :%d %d/n",++id,min); #endif if (m == j && hasNext[m] == 0) { for (i = j+1; i < KNUM; i++) { if (hasNext[m] != hasNext[i]) { m = i; //第i个文件未读完,从第i个继续往下读 break; } } if (m != j) { j = m; goto F_LOOP; } break; } } } //关闭分配的数据和数组 free(hasNext); free(data); for(i = 0; i < KNUM; ++i) { fclose(farray[i]); } free(farray); fclose(fout); }