Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct dictionary_t* load_dictionary_b(const char *filename, int *err_code)
- {
- if(!filename)
- {
- if(err_code)
- *err_code=1;
- return NULL;
- }
- FILE *f=fopen(filename, "rb");
- if(!f)
- {
- if(err_code)
- *err_code=2;
- return NULL;
- }
- int size=0;
- if(!fread(&size, sizeof(int), 1, f))
- {
- fclose(f);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- if(size<=0)
- {
- if(err_code)
- *err_code=3;
- fclose(f);
- return NULL;
- }
- int error=0;
- struct dictionary_t *p=create_dictionary(size, &error);
- if(!p)
- {
- fclose(f);
- if(err_code)
- *err_code=4;
- return NULL;
- }
- for(int i=0; i<size; i++)
- {
- int len=0;
- if(!fread(&len, sizeof(int), 1, f))
- {
- fclose(f);
- destroy_dictionary(&p);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- if(len<=0)
- {
- destroy_dictionary(&p);
- fclose(f);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- char* txt=malloc(len+1);
- if(!txt)
- {
- fclose(f);
- destroy_dictionary(&p);
- if(err_code)
- *err_code=4;
- return NULL;
- }
- for(int j=0; j<len; j++)
- {
- if(!fread(txt+j, sizeof(char), 1, f))
- {
- destroy_dictionary(&p);
- fclose(f);
- free(txt);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- }
- *(txt+len)='\0';
- if(dictionary_find_word(p, txt))
- {
- destroy_dictionary(&p);
- fclose(f);
- free(txt);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- if(dictionary_add_word(p, txt)==2)
- {
- destroy_dictionary(&p);
- fclose(f);
- free(txt);
- if(err_code)
- *err_code=4;
- return NULL;
- }
- free(txt);
- int counter=0;
- if(!fread(&counter, sizeof(int), 1, f))
- {
- fclose(f);
- destroy_dictionary(&p);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- if(counter<=0)
- {
- destroy_dictionary(&p);
- fclose(f);
- free(txt);
- if(err_code)
- *err_code=3;
- return NULL;
- }
- (p->wc+i)->counter=counter;
- }
- fclose(f);
- if(err_code)
- *err_code=0;
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement