Guest User

Untitled

a guest
Nov 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <seq.h>
  5. #include <table.h>
  6. #include <atom.h>
  7.  
  8. enum { MAX_NAME_SIZE = 200 };
  9. enum { MAX_FP_SIZE = 512 };
  10. enum { TEMP_SIZE = 1 };
  11. enum { MAX_FP_NUM = 500000 };
  12.  
  13. void table_add (Table_T* hash, char* fp, char** all_name, int name_it);
  14. void table_print (Table_T* hash, const char** all_fp, int all_fp_it);
  15.  
  16. int main ()
  17. {
  18. char temp [TEMP_SIZE];
  19. char first;
  20. char fp_temp [MAX_FP_SIZE]; //all of the fingerprint except first char
  21. char fp [MAX_FP_SIZE]; //the entire fingerprint
  22. char name [MAX_NAME_SIZE];
  23. const char* all_fp [MAX_FP_NUM];
  24. char* all_name [MAX_FP_NUM];
  25.  
  26. int all_fp_it = 0; //iterator
  27. int all_name_it = 0; //name iterator
  28. Table_T hash_table = Table_new(0, NULL, NULL);
  29. do {
  30. temp [0] = getc(stdin);
  31. first = temp[0];
  32. if (first != EOF)
  33. {
  34. scanf("%s", fp_temp);
  35. strcpy (fp, temp);
  36. strcat (fp, fp_temp);
  37. fgets(name, MAX_NAME_SIZE, stdin);
  38. all_fp [all_fp_it] = Atom_string(fp);
  39. all_fp_it++;
  40. all_name [all_name_it] = name;
  41. all_name_it++;
  42. table_add (&hash_table, fp, all_name, all_name_it);
  43. }
  44. } while (first != EOF);
  45. table_print (&hash_table, all_fp, all_fp_it);
  46. Table_free(&hash_table);
  47. return 0;
  48. }
  49. //CHANGE CHAR** name to CHAR* and get rid of int name_it
  50. void table_add (Table_T* hash, char* fp_full, char** name, int name_it)
  51. {
  52. //atomize the fingerprint
  53. const char* fp = Atom_string(fp_full);
  54. //if sequence exists, add to it, if not, create new sequence
  55. if (Table_get(*hash, fp) != NULL)
  56. {
  57. //THIS IS WHERE WE RAN INTO THE MALLOC ISSUE!!!
  58. //THIS HAPPENED WITH NO ALL_NAME IT
  59. Seq_T* list = Table_get(*hash, fp);
  60. Seq_addlo (*list, name[name_it]);
  61. }
  62. else
  63. {
  64. //create a new sequence (alloc space, then link), add name
  65. Seq_T* list = malloc(sizeof(Seq_seq(name, NULL)));
  66. *list = Seq_seq(name, NULL);
  67. Table_put (*hash, fp, list);
  68. }
  69. }
  70.  
  71. void table_print (Table_T* hash, const char** all_fp, int all_fp_it)
  72. {
  73. char name [MAX_NAME_SIZE]; //used as holder for printing
  74. printf ("\n");
  75. for (int i = 0;i < all_fp_it; i++)
  76. {
  77. if (Table_get(*hash, all_fp[i]) != NULL)
  78. {
  79. Seq_T* list = Table_get(*hash, all_fp[i]);
  80. if (Seq_length(*list) > 1)
  81. {
  82. while (Seq_length(*list) > 0)
  83. {
  84. strcpy(name, Seq_remlo(*list));
  85. printf("%s\n", name);
  86. }
  87. free (list);
  88. Table_remove (*hash, all_fp[i]);
  89. }
  90. else
  91. {
  92. free (list);
  93. Table_remove (*hash, all_fp[i]);
  94. }
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment