Refactor database handling: remove db_handler module, implement file_handler for CSV operations, and update TUI to integrate new export functionality.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#include "file_handler.h"
|
||||
|
||||
int loadListFromCSV(char *_filename, list_t *_list)
|
||||
{
|
||||
FILE *f_in = fopen(_filename, "r");
|
||||
if (f_in == NULL)
|
||||
{
|
||||
// doprasit
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
char *buffer = NULL;
|
||||
size_t buffer_size = 0;
|
||||
|
||||
int load_sum = 0;
|
||||
|
||||
char _name[128], _surname[128];
|
||||
uint32_t _department;
|
||||
uint16_t _uuid;
|
||||
time_t _last_event, _total;
|
||||
BYTE _avail;
|
||||
while (-1 != getline(&buffer, &buffer_size, f_in))
|
||||
{
|
||||
if (sscanf(buffer, "%hu,%127[^,],%127[^,],%u,%ld,%ld,%hhu", &_uuid, _name, _surname, &_department, &_last_event, &_total, &_avail) == 7)
|
||||
{
|
||||
loadPersonToList(_list, _uuid, _name, _surname, _department, _last_event, _total, _avail);
|
||||
load_sum++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// vstupni databaze je vadna
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
fclose(f_in);
|
||||
return load_sum;
|
||||
}
|
||||
|
||||
int saveListToCSV(char *_filename, list_t *_list)
|
||||
{
|
||||
FILE *f_out = fopen(_filename, "w");
|
||||
if (f_out == NULL)
|
||||
{
|
||||
// doprasit
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
node_t *cursor = _list->head;
|
||||
while (cursor != NULL)
|
||||
{
|
||||
if (cursor->user.uuid != 0)
|
||||
{
|
||||
fprintf(f_out, "%hu,%s,%s,%u,%ld,%ld,%hhu\n", cursor->user.uuid, cursor->user.name, cursor->user.surname, cursor->user.department, cursor->user.last_time_event, cursor->user.total, cursor->user.available);
|
||||
}
|
||||
cursor = cursor->next;
|
||||
}
|
||||
|
||||
int save_sum = 0;
|
||||
|
||||
fclose(f_out);
|
||||
return save_sum;
|
||||
}
|
||||
|
||||
int exportPersonInfo(node_t *_person)
|
||||
{
|
||||
char filename[64];
|
||||
sprintf(filename, "person_uuid%d.txt", _person->user.uuid);
|
||||
|
||||
FILE *f_out = fopen(filename, "w");
|
||||
if (f_out == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char last_time_event_str[64];
|
||||
printDateAndTimeInString(_person->user.last_time_event, last_time_event_str);
|
||||
|
||||
char total_time_str[64];
|
||||
printTimeInString(_person->user.total, total_time_str);
|
||||
|
||||
fprintf(f_out, "NAME: %s\nSURNAME: %s\nDEPARTMENT: %d\nLAST TIME EVENT: %s\nTOTAL TIME: %s\n", _person->user.name, _person->user.surname, _person->user.department, last_time_event_str, total_time_str);
|
||||
|
||||
fclose(f_out);
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user