Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. struct dictionary_t* load_dictionary_b(const char *filename, int *err_code)
  2. {
  3. if(!filename)
  4. {
  5. if(err_code)
  6. *err_code=1;
  7. return NULL;
  8. }
  9. FILE *f=fopen(filename, "rb");
  10. if(!f)
  11. {
  12. if(err_code)
  13. *err_code=2;
  14. return NULL;
  15. }
  16. int size=0;
  17. if(!fread(&size, sizeof(int), 1, f))
  18. {
  19. fclose(f);
  20. if(err_code)
  21. *err_code=3;
  22. return NULL;
  23. }
  24. if(size<=0)
  25. {
  26. if(err_code)
  27. *err_code=3;
  28. fclose(f);
  29. return NULL;
  30. }
  31. int error=0;
  32. struct dictionary_t *p=create_dictionary(size, &error);
  33. if(!p)
  34. {
  35. fclose(f);
  36. if(err_code)
  37. *err_code=4;
  38. return NULL;
  39. }
  40. for(int i=0; i<size; i++)
  41. {
  42. int len=0;
  43. if(!fread(&len, sizeof(int), 1, f))
  44. {
  45. fclose(f);
  46. destroy_dictionary(&p);
  47. if(err_code)
  48. *err_code=3;
  49. return NULL;
  50. }
  51. if(len<=0)
  52. {
  53. destroy_dictionary(&p);
  54. fclose(f);
  55. if(err_code)
  56. *err_code=3;
  57. return NULL;
  58. }
  59. char* txt=malloc(len+1);
  60. if(!txt)
  61. {
  62. fclose(f);
  63. destroy_dictionary(&p);
  64. if(err_code)
  65. *err_code=4;
  66. return NULL;
  67. }
  68. for(int j=0; j<len; j++)
  69. {
  70. if(!fread(txt+j, sizeof(char), 1, f))
  71. {
  72. destroy_dictionary(&p);
  73. fclose(f);
  74. free(txt);
  75. if(err_code)
  76. *err_code=3;
  77. return NULL;
  78. }
  79. }
  80. *(txt+len)='\0';
  81. if(dictionary_find_word(p, txt))
  82. {
  83. destroy_dictionary(&p);
  84. fclose(f);
  85. free(txt);
  86. if(err_code)
  87. *err_code=3;
  88. return NULL;
  89. }
  90. if(dictionary_add_word(p, txt)==2)
  91. {
  92. destroy_dictionary(&p);
  93. fclose(f);
  94. free(txt);
  95. if(err_code)
  96. *err_code=4;
  97. return NULL;
  98. }
  99. free(txt);
  100. int counter=0;
  101. if(!fread(&counter, sizeof(int), 1, f))
  102. {
  103. fclose(f);
  104. destroy_dictionary(&p);
  105. if(err_code)
  106. *err_code=3;
  107. return NULL;
  108. }
  109. if(counter<=0)
  110. {
  111. destroy_dictionary(&p);
  112. fclose(f);
  113. free(txt);
  114. if(err_code)
  115. *err_code=3;
  116. return NULL;
  117. }
  118. (p->wc+i)->counter=counter;
  119. }
  120. fclose(f);
  121. if(err_code)
  122. *err_code=0;
  123. return p;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement