made some fixes
This commit is contained in:
98
main.c
98
main.c
@@ -4,6 +4,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_LENGTH 128
|
||||
#define LINE_LENGTH 1024
|
||||
#define HASH_BUCKETS 4000037
|
||||
#define MIN_COUNT 10000
|
||||
|
||||
@@ -18,7 +19,7 @@ void string_ncopy(char *dest, const char *src, size_t max_len) {
|
||||
|
||||
typedef struct person {
|
||||
struct person *next;
|
||||
char *name;
|
||||
char name[BUFFER_LENGTH];
|
||||
int count;
|
||||
} person;
|
||||
|
||||
@@ -28,24 +29,6 @@ void newPerson(person *p, const char *name) {
|
||||
p->next = NULL;
|
||||
}
|
||||
|
||||
void insert(person **head, person *node) {
|
||||
if (*head == NULL) {
|
||||
*head = node;
|
||||
} else {
|
||||
person *p = *head;
|
||||
int p_exists = strcmp(p->name, node->name);
|
||||
while (p->next != NULL && p_exists != 0) {
|
||||
p = p->next;
|
||||
p_exists = strcmp(p->name, node->name);
|
||||
}
|
||||
if (p_exists == 0) {
|
||||
p->count++;
|
||||
} else {
|
||||
p->next = node;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void sorted_name_insert(person **head, char *name) {
|
||||
person *node = (person *) malloc(sizeof(person));
|
||||
newPerson(node, name);
|
||||
@@ -54,26 +37,24 @@ void sorted_name_insert(person **head, char *name) {
|
||||
} else {
|
||||
person *p = *head;
|
||||
person *p_prev = NULL;
|
||||
int p_exists = strcmp(p->name, name);
|
||||
if (p_exists > 0) {
|
||||
node->next = *head;
|
||||
*head = node;
|
||||
return;
|
||||
}
|
||||
while (p->next != NULL && p_exists != 0) {
|
||||
p_exists = strcmp(p->next->name, name);
|
||||
if (p_exists > 0) {
|
||||
node->next = p->next;
|
||||
p->next = node;
|
||||
return;
|
||||
}
|
||||
int cmp = strcmp(p->name, name);
|
||||
while (p->next != NULL && cmp < 0) {
|
||||
p_prev = p;
|
||||
p = p->next;
|
||||
cmp = strcmp(p->name, name);
|
||||
}
|
||||
if (p_exists == 0) {
|
||||
if (cmp == 0){
|
||||
p->count++;
|
||||
free(node);
|
||||
}else if (p_prev == NULL) {
|
||||
node->next = *head;
|
||||
*head = node;
|
||||
} else if (p->next != NULL && cmp < 0) {
|
||||
node->next = p;
|
||||
p_prev->next = node;
|
||||
} else {
|
||||
p->next = node;
|
||||
node->next = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,6 +92,7 @@ void display(person *head) {
|
||||
}
|
||||
}
|
||||
|
||||
//djb2 hash http://www.cse.yorku.ca/~oz/hash.html
|
||||
u_long hash(const unsigned char *str) {
|
||||
u_long hash = 5381;
|
||||
int c;
|
||||
@@ -139,35 +121,37 @@ void parse_line(char *line, char *buffer) {
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
if (strcmp(buffer, "author") == 0 || strcmp(buffer, "editor") == 0) {
|
||||
memset(buffer, 0, BUFFER_LENGTH);
|
||||
while (*line_it != '>') {
|
||||
line_it++;
|
||||
}
|
||||
line_it++;
|
||||
char *surname_end = line_it, *surname_start;
|
||||
while (*line_it != '<') {
|
||||
char *surname_end = line_it, *surname_start = line_it;
|
||||
while (*line_it && *line_it != '<') {
|
||||
if (*line_it == ' ') {
|
||||
surname_start = surname_end;
|
||||
surname_end = line_it;
|
||||
}
|
||||
line_it++;
|
||||
printf("%s\n", line_it);
|
||||
}
|
||||
printf("line_it: %s\n", line_it);
|
||||
printf("sn start: %s\n", surname_start);
|
||||
printf("sn end: %s\n", surname_end);
|
||||
if (!isdigit(surname_end + 1)) {
|
||||
surname_start = surname_end + 1;
|
||||
surname_end = line_it - 1;
|
||||
} else {
|
||||
surname_start++;
|
||||
surname_end--;
|
||||
bool only_numbers = true;
|
||||
char *c = surname_end;
|
||||
while (only_numbers && c != line_it) {
|
||||
if (!isdigit(*c)) {
|
||||
only_numbers = false;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
|
||||
if (!only_numbers) {
|
||||
surname_start = surname_end;
|
||||
surname_end = line_it;
|
||||
}
|
||||
i = 0;
|
||||
size_t name_length = surname_end - surname_start;
|
||||
while (i < name_length) {
|
||||
buffer[i] = surname_start[i];
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
memcpy(buffer, surname_start, name_length);
|
||||
buffer[name_length] = '\0';
|
||||
} else {
|
||||
memset(buffer, 0, BUFFER_LENGTH);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,26 +202,30 @@ int main(void) {
|
||||
hashmap[i] = NULL;
|
||||
}
|
||||
FILE *fp = fopen("dblp.xml", "r");
|
||||
// char *line = malloc(sizeof(char) * LINE_LENGTH);
|
||||
// size_t line_len = LINE_LENGTH;
|
||||
char *line = NULL;
|
||||
size_t line_len = 0;
|
||||
char *buffer = (char *) malloc(sizeof(char) * BUFFER_LENGTH);
|
||||
if (fp) {
|
||||
while (!(getline(&line, &line_len, fp) < 0)) {
|
||||
while (getline(&line, &line_len, fp) >= 0) {
|
||||
memset(buffer, 0, BUFFER_LENGTH);
|
||||
parse_line(line, buffer);
|
||||
if (buffer != NULL) {
|
||||
if (*buffer != '\0') {
|
||||
hm_insert(hashmap, buffer);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
} else {
|
||||
while (!(getline(&line, &line_len, stdin) < 0)) {
|
||||
printf("%s", line);
|
||||
while (getline(&line, &line_len, stdin) >= 0) {
|
||||
memset(buffer, 0, BUFFER_LENGTH);
|
||||
parse_line(line, buffer);
|
||||
if (buffer != NULL) {
|
||||
if (*buffer != '\0') {
|
||||
hm_insert(hashmap, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(line);
|
||||
free(buffer);
|
||||
printf("Done parsing!\n");
|
||||
person *list = NULL;
|
||||
|
Reference in New Issue
Block a user