
Шустрый

Профиль
Группа: Участник
Сообщений: 74
Регистрация: 14.4.2008
Где: Новосибирск
Репутация: нет Всего: нет
|
здравствуйте мне надо поправить код... я уже писал мне в общих чертах сказали что сделать)) начал править куча ошибок и разобраться не могу)) видимо руки совсем кривые) помогите поправить код пожалуйста) | Код | #include <stdio.h> #include <stdlib.h> #include <string.h>
typedef unsigned short int bool;
struct h_tree { unsigned char c; int count; struct h_tree *left,*right; }; int getfilesize(FILE *f) { int i=0; fpos_t pos; fseek(f,0,SEEK_END); fgetpos(f,&pos); i=pos; fseek(f,0,0); return i; } int get_lenght(int a[256]) { int i,r=0; for(i=0;i<256;i++) if (a[i]) r+=a[i]; return r; } int f_table_gen(FILE *f,int *a) { int i,k=0,N=0; unsigned char c; for(i=0;i<256;i++) a[i]=0; while (fscanf(f,"%c",&c)==1) { a[c]++; k++; } for (i = 0; i < 256; i++) if (a[i]) N++; printf("total lenght of text %d total number of different symbols %d\n",k,N); printf("Generating freq table\n"); return N; } void h_tree_print(struct h_tree **mass_t,int N) { int i; for(i=0;i<N;i++) printf("Symbol %c - Frequerency - %d\n",mass_t[i]->c,mass_t[i]->count); } void h_tree_codegen(char *code,struct h_tree *root,int **s,int ln[256]) {
char *cdl,*cdr; int i,l=strlen(code); if (root->left==NULL) { s[root->c]=(int *)malloc(strlen(code)*sizeof(int)); for(i=0;i<l;i++) s[root->c][i]=(code[i]=='0')? 0 : 1; ln[root->c]=strlen(code); free(code); return; } else { cdl=(char *)malloc((strlen(code)+2)*sizeof(char)); cdr=(char *)malloc((strlen(code)+2)*sizeof(char)); strcpy(cdl,code); strncpy(cdl+strlen(code),"0\0",2); strcpy(cdr,code); strncpy(cdr+strlen(code),"1\0",2); h_tree_codegen(cdl,root->left,s,ln); h_tree_codegen(cdr,root->right,s,ln); } } void h_tree_insert_sort(struct h_tree **mass_t,int N) { int i,j; struct h_tree *x; for (i=0;i<N;i++) { x=mass_t[i]; for(j=i-1; j>=0; j--) { if ( x->count>mass_t[j]->count) break; mass_t[j+1]=mass_t[j]; } mass_t[j+1]=x; } }
void h_tree_unpack(FILE *f,int l,int N,bool *code,struct h_tree *root) { static int i=0; int j=0; static int k=0; static struct h_tree *t; if(t==NULL) t=root; for(j=0;j<8;j++) { if(t==NULL) t=root; if ((code==NULL)||(i==N)||(k>l)) { t=NULL; i=0; k=0; return; } if (t->left==NULL) { if(i>=N) { i=0; t=root; return; } fprintf(f,"%c",t->c); t=root; i++; } if (code[j]==0) t=t->left; if (code[j]==1) t=t->right; k++; } } void h_tree_generate(int N,int a[256],struct h_tree **mass_t) { int i=0,j=0; struct h_tree *leaf; //mass_t=(struct h_tree **)malloc(N*sizeof(struct h_tree)); for(i=0;i<N;i++) mass_t[i]=(struct h_tree *)malloc(sizeof(struct h_tree)); for(i=0;i<256;i++) if(a[i]){ mass_t[j]->c=(unsigned char)i; mass_t[j]->count=a[i]; mass_t[j]->left=NULL; mass_t[j]->right=NULL; j++; } //h_tree_print(mass_t,N); while(N>1) { leaf=(struct h_tree *)malloc(sizeof(struct h_tree)); leaf->count=mass_t[0]->count+mass_t[1]->count; leaf->left=mass_t[0]; leaf->right=mass_t[1]; mass_t[0]=NULL; mass_t[1]=leaf; for(i=1;i<N;i++) mass_t[i-1]=mass_t[i]; N--; h_tree_insert_sort(mass_t,N); } } int buffered_input (FILE *f,FILE *f2,bool *code,int l,int N,struct h_tree* root) { int i=0; int k=0; unsigned char c,d; while((((fscanf(f,"%c",&c)==1))&&(i<l))) { for(k=0; k < 8; k++) { d=1<<7; code[k] = ((c&d)==d); c <<= 1; i++; } h_tree_unpack(f2,l,N,code,root); } h_tree_unpack(f2,l,N,NULL,root); return(i); } void buffered_output (FILE *f, int code[],int ln) { static unsigned char buffer = 0; static int buf_len = 0; int i = 0; if (NULL == code) { fputc(buffer, f); buffer = 0; buf_len = 0; return ; }
while (i < ln) { unsigned int mask; if (8 == buf_len) { fputc(buffer, f); buffer = 0; buf_len = 0; }
mask = 1 << (7 - buf_len); if (1 == code[i]) buffer |= mask; else buffer &= ~mask;
buf_len++; i++; } }
void file_arch(FILE *f1,FILE *f2,int **s,int *ln,int *a,int N,char *f_name) { unsigned char c; int i,l=0; fprintf(f2,"%s ",f_name); fprintf(f2,"%d ",N); for(i=0;i<256;i++) l+=ln[i]*a[i]; fprintf(f2,"%d ",l); for(i=0;i<256;i++) if(a[i]>0) fprintf(f2," %d %d ",i,a[i]); while((fscanf(f1,"%c",&c)==1)) { buffered_output(f2,s[c],ln[c]); //for(i=0;i<ln[c];i++) printf("%d",s[c][i]); } buffered_output(f2,NULL,0); } void file_cpy(FILE *f1,FILE *f2,int size) { int i=0; unsigned char c; while((fscanf(f1,"%c",&c)==1)&&(i<size)) { fprintf(f2,"%c",c); i++; } fprintf(f2," "); } int main (int argc, char **argv) { FILE *f,*f2; struct h_tree **mass_t,**root; int **s; int a[256],ln[256],u[256]; char f_name[256]; bool g[8]; int i,N,j=0,k,l; int y=0; fpos_t pos; if (!argv[1]) { printf("Error: no mode given\n"); return -1; } if (argv[1][2]!='\0') { printf("Error: incorrect mode\n"); return -1; } switch (argv[1][1]) { case 'a': { if(argv[2]) { if(f2=fopen(argv[2],"r+")) { if(argv[1][0]=='+') { fscanf(f2,"%d",&y); fseek(f2,0,0); for(i=3;i<argc;i++) if(f=fopen(argv[i],"rb")) fclose(f); else { printf("Error file not found\n"); return -2; } fprintf(f2,"%d ",argc-3+y); fclose(f2); f2=fopen(argv[2],"ab+"); } else f2=fopen(argv[2],"wb+"); } else if(f2=fopen(argv[2],"wb")); else { printf("Error file not found\n"); return -1; } } else { printf("Error no filename given\n"); return -2; } if (argv[1][0]!='+' || y==0) fprintf(f2,"%d ",argc-3); for(k=3;k<argc;k++) { if(argv[k]) { if(f=fopen(argv[k],"rb")) ; else { printf("Error file not found\n"); return -1; } } else { printf("Error no filename given\n"); return -2; } N=f_table_gen(f,a); fseek(f,0,0); s=(int **)malloc(256*sizeof(int *)); mass_t=(struct h_tree **)malloc(N*sizeof(struct h_tree)); h_tree_generate(N,a,mass_t); printf("Generating code\n"); h_tree_codegen("",mass_t[0],s,ln); printf("Generating completed.\n"); //FILE *f1=fopen(argv[3], "rb"); file_arch(f,f2,s,ln,a,N,argv[k]); fprintf(f2," "); printf("Archive created.\n"); fclose(f); // fclose(f2); } break; } case 'u': { if(argv[2]) { if(f=fopen(argv[2],"rb")) ; else { printf("Error file not found\n"); return -1; } } else { printf("Error no filename given\n"); return -2; } fscanf(f,"%d",&y); for(k=0;k<y;k++) { if ((fscanf(f,"%s",f_name))==1); else break; fscanf(f,"%d",&N); fscanf(f,"%d",&l); //g=(bool *)realloc(g,l*sizeof(bool)); //printf("%d\n",l); //printf("%d\n",sizeof(g)); for(i=0;i<256;i++) u[i]=0; for(i=0;i<N;i++){ fscanf(f," %d ",&j); fscanf(f,"%d ",&u[j]); // printf("%d %d\n",k,u[k]); } //for(i=0;i<256;i++) printf ("a[%d] - %d,u[%d] - %d\n",i,a[i],i,u[i]); //fclose(f); int t,e=0; if (argv[3]) e=1; for(t=3;t<argc;t++) if (strcmp(f_name,argv[t])==0) e=0; if(((f2=fopen(f_name,"r"))&&(argv[1][0]=='+'))||(e)) fseek(f,l/8+1,SEEK_CUR); else { root=(struct h_tree **)malloc(N*sizeof(struct h_tree)); h_tree_generate(N,u,root); f2=fopen(f_name,"wb"); i= buffered_input(f,f2,g,l,get_lenght(u),root[0]); fclose(f2); printf("Reading completed."); printf("\n"); //h_tree_unpack(f2,i,get_lenght(u),g,root[0]); printf("Extracting completed."); printf("\n"); } } fclose(f); break; } case 'i': { if(argv[2]) { if(f=fopen(argv[2],"rb")) ; else { printf("Error file not found\n"); return -1; } } else { printf("Error no filename given\n"); return -2; } } if(fscanf(f,"%d",&y)==1); else return -1; printf("This arhive contains %d files\n",y); for(k=0;k<y;k++) {
if(3==fscanf(f,"%s %d %d",f_name,&N,&l)) { for(i=0;i<256;i++) u[i]=0; for(i=0;i<N;i++){ fscanf(f," %d ",&j); fscanf(f,"%d ",&u[j]); } fseek(f,l/8+1,SEEK_CUR); int m=get_lenght(u),n=1; float r=(float)(m-l/8)/m; if (m<1024) printf("-- %s , filesize %d bytes,compressed %d bytes ,compession %.1f %% \n",f_name,m,l/8,r*100); else if (m<1048576) printf("-- %s , filesize %.1f kbytes,compressed %.1f kbytes compession %.1f %% \n",f_name,(float)m/1024,(float)l/(8*1024),r*100); else printf("-- %s , filesize %.2f mbytes,compressed %.2f mbytes , compession %.1f %% \n",f_name,(float)m/(1024*1024),(float)l/(8*1024*1024),r*100); } } i=getfilesize(f); if (i<1024) printf("Total arhive size: %d bytes ",i); else if(i<1048576) printf("Total arhive size: %.1f kbytes ",(float)i/1024); else printf("Total arhive size: %.2f mbytes ",(float)i/(1024*1024)); fclose(f); break; case 'd': { if(argv[2]) { if(f=fopen(argv[2],"rb")) ; else { printf("Error file not found\n"); return -1; } } else { printf("Error no filename given\n"); return -2; } if(!argv[3]) remove(argv[2]); fscanf(f,"%d",&y); for(k=0;k<y;k++) { if ((fscanf(f,"%s",f_name))==1); else break; fscanf(f,"%d",&N); fscanf(f,"%d",&l); //g=(bool *)realloc(g,l*sizeof(bool)); //printf("%d\n",l); //printf("%d\n",sizeof(g)); for(i=0;i<256;i++) u[i]=0; for(i=0;i<N;i++){ fscanf(f," %d ",&j); fscanf(f,"%d ",&u[j]); // printf("%d %d\n",k,u[k]); } //for(i=0;i<256;i++) printf ("a[%d] - %d,u[%d] - %d\n",i,a[i],i,u[i]); //fclose(f); int t,e=0; if (argv[3]) e=1; for(t=3;t<argc;t++) if (strcmp(f_name,argv[t])==0) e=0; } break; } case 'h': { printf("Info:\n"); printf("+a [arhive_name] [file1] ... [fileN] - add specified files to arhive, or create new if [archive_name] doesnt exist \n ( NOTE :If you are adding files to existing [archive_name] make sure it is a valid arhive) \n"); printf("-a [arhive_name] [file1] ... [fileN] - create new arhive with specified files \n( NOTE : If [archive_name] already exists, it will be overwritten )\n"); printf("+u [arhive_name] [file1] ... [fileN] - extract specified files from arhive ( if none given entire arhive will be extracted) , if file with the same name already exists, file wont be extracted.\n"); printf("-u [arhive_name] [file1] ... [fileN] - extract specified files from arhive ( if none given entire arhive will be extracted), if file with the same name exists , it will be overwritten.\n"); printf("+i [achive_name] - get information about files in arhive\n"); //system("h_arh.exe +h"); break; } default: { printf("Error : incorrect mode. Type +h for help"); break; } } return 0; }
|
в общем мне советовали сделать вот что: 1) Приведи в соответствие количество malloc и free, дабы избежать утечек памяти (это тебе не пхп с его автоматическим разгребанием мусора). 2) Старайся освобождать память в той же зоне видимости, где и выделил её. заранее большое спасибо за вашу помощь)
|