Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <seq.h>
- #include <table.h>
- #include <atom.h>
- enum { MAX_NAME_SIZE = 200 };
- enum { MAX_FP_SIZE = 512 };
- enum { TEMP_SIZE = 1 };
- enum { MAX_FP_NUM = 500000 };
- void table_add (Table_T* hash, char* fp, char** all_name, int name_it);
- void table_print (Table_T* hash, const char** all_fp, int all_fp_it);
- int main ()
- {
- char temp [TEMP_SIZE];
- char first;
- char fp_temp [MAX_FP_SIZE]; //all of the fingerprint except first char
- char fp [MAX_FP_SIZE]; //the entire fingerprint
- char name [MAX_NAME_SIZE];
- const char* all_fp [MAX_FP_NUM];
- char* all_name [MAX_FP_NUM];
- int all_fp_it = 0; //iterator
- int all_name_it = 0; //name iterator
- Table_T hash_table = Table_new(0, NULL, NULL);
- do {
- temp [0] = getc(stdin);
- first = temp[0];
- if (first != EOF)
- {
- scanf("%s", fp_temp);
- strcpy (fp, temp);
- strcat (fp, fp_temp);
- fgets(name, MAX_NAME_SIZE, stdin);
- all_fp [all_fp_it] = Atom_string(fp);
- all_fp_it++;
- all_name [all_name_it] = name;
- all_name_it++;
- table_add (&hash_table, fp, all_name, all_name_it);
- }
- } while (first != EOF);
- table_print (&hash_table, all_fp, all_fp_it);
- Table_free(&hash_table);
- return 0;
- }
- //CHANGE CHAR** name to CHAR* and get rid of int name_it
- void table_add (Table_T* hash, char* fp_full, char** name, int name_it)
- {
- //atomize the fingerprint
- const char* fp = Atom_string(fp_full);
- //if sequence exists, add to it, if not, create new sequence
- if (Table_get(*hash, fp) != NULL)
- {
- //THIS IS WHERE WE RAN INTO THE MALLOC ISSUE!!!
- //THIS HAPPENED WITH NO ALL_NAME IT
- Seq_T* list = Table_get(*hash, fp);
- Seq_addlo (*list, name[name_it]);
- }
- else
- {
- //create a new sequence (alloc space, then link), add name
- Seq_T* list = malloc(sizeof(Seq_seq(name, NULL)));
- *list = Seq_seq(name, NULL);
- Table_put (*hash, fp, list);
- }
- }
- void table_print (Table_T* hash, const char** all_fp, int all_fp_it)
- {
- char name [MAX_NAME_SIZE]; //used as holder for printing
- printf ("\n");
- for (int i = 0;i < all_fp_it; i++)
- {
- if (Table_get(*hash, all_fp[i]) != NULL)
- {
- Seq_T* list = Table_get(*hash, all_fp[i]);
- if (Seq_length(*list) > 1)
- {
- while (Seq_length(*list) > 0)
- {
- strcpy(name, Seq_remlo(*list));
- printf("%s\n", name);
- }
- free (list);
- Table_remove (*hash, all_fp[i]);
- }
- else
- {
- free (list);
- Table_remove (*hash, all_fp[i]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment