// modify data void modify(){ int p,c = 0; node *z; do { printf("\t\tWhat position want to modify (1st = 0 postion) : ");scanf("%d",&p);mfflush(); if(p < 0 || p > countList(root)-1) printf("\t\tInvaild location !!\nInput value again !! \n"); } while(p < 0 || p > countList(root)-1); if(p == 0) z = root; else { z = root->next; while(z != NULL){ c++; if(c == p) break; z = z->next; } } printf("\t\tData of position %d :",p); printf("%s %d gb %.2f'' %d VND\n",z->element.model,z->element.size,z->element.screen,z->element.price); printf("\t\tWhat you want to modify :"); if((c = getmenu("Model;Size;Size of screen;Price",4,0)) == 1){ printf("\t\t %s --> ",z->element.model);scanf("%[^\n]",z->element.model);mfflush(); } else if(c == 2 ){ printf("\t\t %d --> ",z->element.size);scanf("%d",&z->element.size);mfflush(); } else if (c == 3){ printf("\t\t %f --> ",z->element.screen);scanf("%f",&z->element.screen);mfflush(); } else{ printf("\t\t %d --> ",z->element.price);scanf("%d",&z->element.price);mfflush(); } printf("\t\tData of position %d after modifed :",p); printf("%s %d gb %.2f'' %d VND\n",z->element.model,z->element.size,z->element.screen,z->element.price); }
/* * A wrapper around vsprintf() to write to an mFILE. This also uses vflen() to * estimate how many additional bytes of storage will be required for the * vsprintf to work. */ int mfprintf(mFILE *mf, char *fmt, ...) { int ret; size_t est_length; va_list args; va_start(args, fmt); est_length = vflen(fmt, args); va_end(args); while (est_length + mf->offset > mf->alloced) { mf->alloced = mf->alloced ? mf->alloced * 2 : 1024; mf->data = (void *)realloc(mf->data, mf->alloced); } va_start(args, fmt); ret = vsprintf(&mf->data[mf->offset], fmt, args); va_end(args); if (ret > 0) { mf->offset += ret; if (mf->size < mf->offset) mf->size = mf->offset; } if (mf->fp == stderr) { /* Auto-flush for stderr */ mfflush(mf); } return ret; }
void exp_print_file(FILE *fp, Exp_info *e) { mFILE *mf; if (NULL == (mf = mfreopen(NULL, "wbx", fp))) return; exp_print_mfile(mf, e); mfflush(mf); mf->fp = NULL; /* Don't want this closed here */ mfclose(mf); }
int main () { ketquathidau vdv[MAX]; int z; for(;;){switch(GetMenu("Dang Ky thi dau;Thi dau;Xep Hang;Sieu xa thu;",4,1)){ case 1 : z = reg(vdv);print(vdv,z,0,1);break; case 2:play(vdv,z);mfflush();print(vdv,z,1,0);break; case 3:rank(vdv,z);break; case 4:superplayer(vdv,z);break; case 0:return 0; } } }
/* * Closes an mFILE. If the filename is known (implying write access) then this * also writes the data to disk. * * Stdout is handled by calling mfflush which writes to stdout if appropriate. */ int mfclose(mFILE *mf) { if (!mf) return -1; mfflush(mf); if (mf->fp) fclose(mf->fp); mfdestroy(mf); return 0; }
/* * Closes the file pointer contained within the mFILE without destroying * the in-memory data. */ int mfdetach(mFILE *mf) { if (!mf) return -1; mfflush(mf); if (mf->fp) { fclose(mf->fp); mf->fp = NULL; } return 0; }
int fwrite_scf(Scf *s, FILE *fp) { mFILE *mf; int r; if (NULL == (mf = mfreopen(NULL, "wbx", fp))) return -1; r = mfwrite_scf(s, mf); mfflush(mf); mf->fp = NULL; /* Don't want this closed here */ mfclose(mf); return r; }
int fwrite_ztr(FILE *fp, ztr_t *z) { mFILE *mf; int r; if (NULL == (mf = mfreopen(NULL, "wbx", fp))) return -1; r = mfwrite_ztr(mf, z); mfflush(mf); mf->fp = NULL; /* Don't want this closed here */ mfclose(mf); return r; }
/*! * Write a sequence to a FILE *fp of format "format". If "format" is 0, * we choose our favourite - SCF. * * Returns: * 0 for success * -1 for failure */ int fwrite_reading(FILE *fp, Read *read, int format) { int ret; mFILE *mf = mfreopen(NULL, "wbx", fp); if (mf) { ret = mfwrite_reading(mf, read, format); mfflush(mf); mf->fp = NULL; /* Don't want this closed here */ mfclose(mf); } else { return -1; } return ret; }
int reg(ketquathidau *vdv){ int num,i,j; do { printf("\tNhap So Van dong Vien :");scanf("%d",&num); if(num > 10 || num < 0) printf("\tso van dong vien khong hop le !!\n\tNhap lai !!\n");mfflush(); }while(num > 10 || num < 0); for(i = 0; i < num;i++) { printf("\tVan dong vien %d - Ho van ten : ",i+1); scanf("%[^\n]",vdv->hoten);mfflush(); standname(vdv->hoten); for(j = 0; j < 5;j++) vdv->diem[j] = 0; vdv++; } return num; }
/* * Closes an mFILE. If the filename is known (implying write access) then this * also writes the data to disk. * * Stdout is handled by calling mfflush which writes to stdout if appropriate. */ int mfclose(mFILE *mf) { if (!mf) return -1; if (mf->mode&MF_MEMORY) { mfdestroy(mf); return 0; } mfflush(mf); if (mf->fp) fclose(mf->fp); mfdestroy(mf); return 0; }
size_t mfwrite(mfile f, const void *buf, size_t len, int64_t deadline) { /* If it fits into the output buffer copy it there and be done. */ if(f->olen + len <= MILL_FILE_BUFLEN) { memcpy(&f->obuf[f->olen], buf, len); f->olen += len; errno = 0; return len; } /* If it doesn't fit, flush the output buffer first. */ mfflush(f, deadline); if(errno != 0) return 0; /* Try to fit it into the buffer once again. */ if(f->olen + len <= MILL_FILE_BUFLEN) { memcpy(&f->obuf[f->olen], buf, len); f->olen += len; errno = 0; return len; } /* The data chunk to send is longer than the output buffer. Let's do the writing in-place. */ char *pos = (char*)buf; size_t remaining = len; while(remaining) { ssize_t sz = write(f->fd, pos, remaining); if(sz == -1) { if(errno != EAGAIN && errno != EWOULDBLOCK) return 0; int rc = fdwait(f->fd, FDW_OUT, deadline); if(rc == 0) { errno = ETIMEDOUT; return len - remaining; } mill_assert(rc == FDW_OUT); continue; } pos += sz; remaining -= sz; } return len; }
/* * Closes an mFILE. If the filename is known (implying write access) then this * also writes the data to disk. * * Stdout is handled by calling mfflush which writes to stdout if appropriate. */ int mfclose(mFILE *mf) { if (!mf) return -1; mfflush(mf); #ifdef HAVE_MMAP if ((mf->mode & MF_MMAP) && mf->data) { /* Mmaped */ munmap(mf->data, mf->size); mf->data = NULL; } #endif if (mf->fp) fclose(mf->fp); mfdestroy(mf); return 0; }
/* * Write a sequence to a FILE *fp of format "format". If "format" is 0, * we choose our favourite - SCF. * * Returns: * 0 for success * -1 for failure */ int mfwrite_reading(mFILE *fp, Read *read, int format) { int r = -1; int no_compress = 0; #ifdef _WIN32 /* * jkb 09/06/00 comment below * * On windows "prog > file.scf" will work wrongly (compared to * "prog file.scf") because windows is rather stupid. It treats ascii * and binary streams differently, it considers stdout to be ascii unless * told otherwise, and it can only be told otherwise by using non-ansi * windows-specific function calls. */ if (format != TT_EXP && format != TT_PLN && fp->fp) _setmode(_fileno(fp->fp), _O_BINARY); #endif switch (format) { default: /* Defaults to ZTR type */ #ifdef IOLIB_ZTR case TT_ZTR: case TT_ZTR2: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 2); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); no_compress = 1; break; } case TT_ZTR1: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 1); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); break; } case TT_ZTR3: { ztr_t *ztr; ztr = read2ztr(read); compress_ztr(ztr, 3); r = mfwrite_ztr(fp, ztr); delete_ztr(ztr); no_compress = 1; break; } #endif #ifdef IOLIB_SCF case TT_SCF: { Scf *scf; scf = read2scf(read); r = mfwrite_scf(scf, fp); scf_deallocate(scf); break; } #endif #ifdef IOLIB_CTF case TT_CTF: r = mfwrite_ctf(fp, read); break; #endif #ifdef IOLIB_ABI case TT_ABI: /*return mfwrite_abi(fp, read); */ break; #endif #ifdef IOLIB_ALF case TT_ALF: /* return mfwrite_alf(fp, read); */ break; #endif #ifdef IOLIB_EXP case TT_EXP: { Exp_info *e = read2exp(read, read->ident ? read->ident : "unknown"); if (NULL == e) { fprintf(stderr, "Failed to create experiment file.\n"); r = -1; } else { exp_print_mfile(fp, e); exp_destroy_info(e); r = 0; } break; } #endif #ifdef IOLIB_PLN case TT_PLN: r = mfwrite_pln(fp, read); break; #endif } mftruncate(fp, -1); if (r == 0 && !no_compress) { fcompress_file(fp); } mfflush(fp); return r; }
int convert(char *file, int format, mFILE *ofp, char *name, int output_conf) { Read *r; Exp_info *e; char buf[50]; double aq; if (format == TT_BIO) { if (NULL == (r = read_reading(file, format))) { fprintf(stderr, "%s: failed to read\n", file); return 1; } } else { FILE *infp; if (NULL == (infp = open_trace_file(file, NULL))) { perror(file); return 1; } if (NULL == (r = fread_reading(infp, file, format))) { fprintf(stderr, "%s: failed to read\n", file); return 1; } fclose(infp); } e = read2exp(r, name); if (NULL == e) { fprintf(stderr, "Failed to create experiment file.\n"); read_deallocate(r); return 1; } sprintf(buf, "%f", aq = avg_qual(r)); exp_set_entry(e, EFLT_AQ, buf); exp_print_mfile(ofp, e); if (output_conf && aq != 0) { char *cstr; int1 *conf; int i; conf = xmalloc(r->NBases * sizeof(*conf)); cstr = xmalloc(5 * r->NBases+2); for (i = 0; i < r->NBases; i++) { switch (r->base[i]) { case 'a': case 'A': conf[i] = r->prob_A[i]; break; case 'c': case 'C': conf[i] = r->prob_C[i]; break; case 'g': case 'G': conf[i] = r->prob_G[i]; break; case 't': case 'T': conf[i] = r->prob_T[i]; break; default: conf[i] = (r->prob_A[i] + r->prob_C[i] + r->prob_G[i] + r->prob_T[i]) / 4; break; } } conf2str(conf, r->NBases, cstr); exp_set_entry(e, EFLT_AV, cstr); xfree(cstr); xfree(conf); } read_deallocate(r); exp_destroy_info(e); mfflush(ofp); return 0; }
int main(int argc, char *argv[]) { FILE *ft,*fd; while (1) { switch(getmenu("Import DB from NokiaDB.dat;Print data;Insert data;Delete data;Search Data & modify data;Devide list & Extract List;Reverse list;Save to file;Quit",9,0)) { mfflush(); case 1: if((fd = fopen(DAT,"r+b")) == NULL) printf("\t\tCan't read file %s",DAT); else { if(getmenu("Insert Whole;Insert Part",2,0) == 1) { importfdat(fd,1,1); } else { if(getmenu("From Begin;From ending",2,0) == 1) importfdat(fd,2,1); else importfdat(fd,2,2); } printf("\t\tImport DB complete !!\n"); fclose(fd); } break; case 2:if(root == NULL && root2 == NULL) { indent(2); printf("Import data first !!\n"); } else { if(root2 == NULL) printdat(root); else { int c ; if((c = getmenu("Print data of first list;Print data of second list",2,0)) == 1) printdat(root); else printdat(root2); } } break; case 3:if(root == NULL){ indent(2); printf("Import data first !!\n"); } else { int c; VARTYPE p; printf("\t\tModel of phone :");scanf("%[^\n]",p.model);mfflush(); printf("\t\tSize of phone :");scanf("%d",&p.size);mfflush(); printf("\t\tPhone's size of screen :");scanf("%f",&p.screen);mfflush(); printf("\t\tPhone's price :");scanf("%d",&p.price);mfflush(); if((c = getmenu("Insert at Begining;Insert at Ending;Insert at Position",3,0)) == 1) { insertB(p); } else if (c == 2) insertE(p); else { int z; node *g; do { printf("\t\t Where you want to insert (if you don't want to insert anymore input -1 !! 1st = 0 postion) :");scanf("%d",&z);mfflush(); g = insertAtPosition(p,z); } while (g == NULL && z != -1); } }break; case 4:if(root == NULL) { indent(2); printf("Import data first !!\n"); } else { int c; if((c = getmenu("Delete at Postion;Delete current;Delete first",3,0))==1) { int p; printf("\t\t Where you want to delelte (if you don't want to insert anymore input -1 !! 1st = 0 postion) :");scanf("%d",&p);mfflush(); deleteAtPosition(p); } else if(c == 2) delNode(cur); else if(c == 3) delNode(root); } break; case 5: if(root == NULL) { indent(2); printf("Import data first !!\n"); } else { int cc; if((cc = getmenu("Search data;Modify data",2,0)) == 1){ int c; if((c = getmenu("Search By name;Search By Price",2,0)) ==1) search(root,1); else search(root,2); } else { modify(); } } break; case 6: if(root == NULL) { indent(2); printf("Import data first !!\n"); } else { int c; if((c = getmenu("Devide List;Extract list",2,0))==1){ int p; printf("\t\tPostion you want to devide list : ");scanf("%d",&p);mfflush(); root2 = devideList(root,last,p); } else { int p,n; printf("\t\tPostion you want to start extract list : ");scanf("%d",&p);mfflush(); printf("\t\tLength of extract list : ");scanf("%d",&n);mfflush(); extractList(p,n); } }break; case 7:if(root == NULL) { indent(2); printf("Import data first !!\n"); } else { reversel(); }break; case 8:if(root == NULL) { indent(2); printf("Import data first !!\n"); } else { if((fd = fopen(DAT,"w+b")) == NULL) printf("Can't create file %s",DAT); else { exporttdat(fd); } }break; case 9: freelist(root); freelist(root2); indent(2);printf("Exit Program !!\n"); return 0; } } }
void search(node *r,int mode){ int num = countList(r); if(root == NULL) { printf("\t\tList hasn't create !!\n"); return; } if ( mode == 1){ // search model int i,c = 0; node *p = root; char s[100]; printf("\t\tSearch by model :");scanf("%[^\n]",s);mfflush(); for (i = 0; i < num; i++) { if(!strcmp(s,p->element.model)) c++; } if(c != 0){ p = root; c = 0; printf("\t\tSearch result : \n"); for (i = 0; i < num; i++) { if(!strcmp(s,p->element.model)) printf("%-3d %-40s | %-3d gb | %-3.1f '' |%-10d VND\n",c+1,p->element.model,p->element.size,p->element.screen,p->element.price); p = p ->next; } } else { printf("\t\tNot found !!\n"); } } else { int i,c = 0,z = 0,sum = 0; long d; printf("\t\tSearch product slow than price :");scanf("%ld",&d);mfflush(); node *p = root; for (i = 0; i < num; i++) { if(p->element.price < d && p->element.price != -1) { c++; } p = p ->next; } if(c != 0 ){ printf("\t\tSearch result : \n"); p = root; sum = c; i = 0; for(;;){ c = z*PAGE; int k = PAGE + z*PAGE; k = (k < sum)?k:sum; printf("\t\t Page %d\n",z+1); for (;c < k;) { if(p == NULL) break; if(p->element.price < d && p->element.price != -1) { printf("%-3d %-40s | %-3d gb | %-3.1f '' |%-10d VND\n",c+1,p->element.model,p->element.size,p->element.screen,p->element.price); c++; } p = p ->next; } if(PAGE > sum ) return ; if(z == 0) { if(navi(1,0)== 1) z++; else return ; } else if(z*PAGE + PAGE > sum) { if(navi(0,1)==1) { z--; int j; for(j = 0 ; j < PAGE ; j++) p = pevNode(root,p); for(j = 0 ; j < PAGE ; j++) p = pevNode(root,p); } else return; } else { if((c=navi(1,1)) ==1 ) { z--; int j; for(j = 0 ; j < PAGE ; j++) p = pevNode(root,p); for(j = 0 ; j < PAGE ; j++) p = pevNode(root,p); } else if(c ==2) z++; else return ; } } } else { printf("\t\tNot Found !! \n"); } } }
// import from dat file void importfdat(FILE *f,int wop,int boe){ freelist(root); phone *parr; // wop = part(2) or whole(1) // boe = end(2) or begin(1) int e,offset,i;num = sizefile(f)/sizeof(phone); if(wop == 1) {// whole parr = (phone *)malloc(sizeof(phone)*num); fread(parr,sizeof(phone),num,f); //printf("%d\n",z); rewind(f); importfA(parr); free(parr); } else { if(boe == 1){ //part begin do { printf("\t\tInput offset(<%d) : ",num);scanf("%d",&offset);mfflush(); if(offset < 0 || offset > num ) printf("\t\tWrong input value !!\n input again \n"); } while (offset < 0 || offset > num); do { printf("\t\tInput element want to read (<%d) : ",num-offset);scanf("%d",&e);mfflush(); if(e < 0 || e > num-offset ) printf("\t\tWrong input value !!\n input again \n"); } while (e < 0 || e > num-offset); parr = (phone *)malloc(sizeof(phone)*e); if(offset != 0) fseek(f,sizeof(phone)*(offset-1),SEEK_SET); fread(parr,sizeof(phone),e,f); rewind(f); importfA(parr); free(parr); } else {//part end do { printf("\t\tInput offset(<%d) : ",num);scanf("%d",&offset);mfflush(); if(offset < 1 || offset > num ) printf("\t\tWrong input value !!\n input again \n"); } while (offset < 1 || offset > num); do { printf("\t\tInput element want to read (<%d) : ",num-offset);scanf("%d",&e);mfflush(); if(e < 0 || e > num-offset ) printf("\t\tWrong input value !!\n input again \n"); } while (e < 0 || e > num-offset); parr = (phone *)malloc(sizeof(phone)*e); fseek(f,(long)0,SEEK_END); fseek(f,-sizeof(phone)*offset,SEEK_CUR); for (i = 0; i < e; i++) { fread(&parr[i],sizeof(phone),1,f); fseek(f,-sizeof(phone)*2,SEEK_CUR); //printf("%-3d %-40s | %-3d gb | %-3.1f '' |%-10d VND\n",i+1,parr[i].model,parr[i].size,parr[i].screen,parr[i].price); } rewind(f); importfA(parr); free(parr); } num = e; } }