#include "dictionary.h"
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int listprint(List* node)
{
if (!node) {
return -1;
}
List* ptr = node;
do {
printf("%2d %-10s", ptr->value, ptr->key);
ptr = ptr->next;
} while (ptr != NULL);
return 0;
}
List* list_createnode(char* key, int value)
{
List* p;
p = calloc(sizeof(*p), 1);
if (p != NULL) {
p->key = key;
p->value = value;
p->next = NULL;
}
return p;
}
int printDictionary(dictionary* d)
{
if (!d) {
return -1;
}
for (int i = 0; i < d->count; i++) {
listprint(d->lines[i]);
printf("\n");
}
return 0;
}
List* list_lookup(List* list, char* key)
{
for (; list != NULL; list = list->next) {
if (strcmp(list->key, key) == 0) {
return list;
}
}
return NULL; /* Не нашли */
}
List* list_search(List* node, int value)
{
for (; node != NULL; node = node->next) {
if (node->value == value) {
return node;
}
}
return NULL;
}
List* list_addend(List* node, char* key, int value)
{
List* newnode = list_createnode(key, value);
if (node == NULL) {
return newnode;
}
List* temp = node;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newnode;
return node;
}
int fill_dictionary(Dictionary* d)
{
if (d == NULL) {
return -1;
}
FILE* file;
char name[] = "./source/d.txt";
if ((file = fopen(name, "r")) == NULL) {
printf("Не удалось открыть файл\n");
return -1;
}
fseek(file, 0, SEEK_END);
long pos = ftell(file);
if (pos > 0) {
rewind(file);
} else {
return -1;
}
char* str = calloc(sizeof(char), 100);
if (str == NULL) {
return -1;
}
d->count = 0;
for (int i = 0; fgets(str, 100, file); i++) {
d->count++;
char* pch = strtok(str, " \n");
for (int j = 1; j < 4 && pch != NULL; j++) {
char* word = strdup(pch);
d->lines[i] = list_addend(d->lines[i], word, j);
pch = strtok(NULL, " \n");
printf("%ld %p %s\n",strlen(pch),&word,word);
//free(word);
}
while (pch != NULL) {
char* word = strdup(pch);
d->lines[i] = list_addend(d->lines[i], word, 0);
pch = strtok(NULL, " \n");
//free(word);
}
//free(pch);
}
fclose(file);
free(str);
return d->count;
}