192 lines
3.8 KiB
C
192 lines
3.8 KiB
C
// Laboratorio 4 - Esercizio 2 - item.c
|
|
// Matteo Schiff - s295565
|
|
|
|
#ifndef ITEM_C_DEFINED
|
|
#define ITEM_C_DEFINED
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include "item.h"
|
|
|
|
int DateGreater(Date a, Date b) {
|
|
if (a.year > b.year)
|
|
return true;
|
|
else if (a.year < b.year)
|
|
return false;
|
|
|
|
if (a.month > b.month)
|
|
return true;
|
|
else if (a.month < b.month)
|
|
return false;
|
|
|
|
if (a.day >= b.day)
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
Link newNode(Item val, Link next) {
|
|
Link t = (Link) malloc(sizeof(Node));
|
|
if (t == NULL) {
|
|
printf("Impossibile allocare memoria");
|
|
exit(2);
|
|
}
|
|
t->val = val;
|
|
t->next = next;
|
|
|
|
return t;
|
|
}
|
|
|
|
void freeNode(Link node) {
|
|
free(node->val);
|
|
free(node);
|
|
}
|
|
|
|
Item ItemFromString(char *str) {
|
|
Item item;
|
|
|
|
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
|
printf("Impossibile allocare memoria");
|
|
exit(2);
|
|
}
|
|
|
|
if (sscanf(str, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
|
item->code,
|
|
item->name,
|
|
item->surname,
|
|
&item->date.day,
|
|
&item->date.month,
|
|
&item->date.year,
|
|
item->address,
|
|
item->city,
|
|
&item->cap
|
|
) != 9) {
|
|
free(item);
|
|
return NULL;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
|
|
Item ItemRead(FILE * fp) {
|
|
Item item;
|
|
|
|
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
|
printf("Impossibile allocare memoria");
|
|
exit(2);
|
|
}
|
|
|
|
if (fscanf(fp, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
|
item->code,
|
|
item->name,
|
|
item->surname,
|
|
&item->date.day,
|
|
&item->date.month,
|
|
&item->date.year,
|
|
item->address,
|
|
item->city,
|
|
&item->cap
|
|
) != 9) {
|
|
free(item);
|
|
return NULL;
|
|
}
|
|
|
|
return item;
|
|
}
|
|
|
|
Key ItemKey(Item val) {
|
|
return val->code;
|
|
}
|
|
|
|
int ItemEq(Key x, Key y) {
|
|
return strcmp(x, y) == 0;
|
|
}
|
|
|
|
void ItemPrint(FILE *fp, Item x) {
|
|
fprintf(fp, "%s %s %s %02d/%02d/%04d %s %s %05d\n",
|
|
x->code,
|
|
x->name,
|
|
x->surname,
|
|
x->date.day,
|
|
x->date.month,
|
|
x->date.year,
|
|
x->address,
|
|
x->city,
|
|
x->cap
|
|
);
|
|
}
|
|
|
|
Link insertOrderedByDate(Item val, Link head) {
|
|
Link x, p;
|
|
|
|
if (head == NULL || DateGreater(val->date, head->val->date))
|
|
return newNode(val, head);
|
|
|
|
for (p = head, x = head->next; x != NULL && DateGreater(x->val->date, val->date); p = x, x = x->next);
|
|
p->next = newNode(val, x);
|
|
|
|
return head;
|
|
}
|
|
|
|
Item getItemByCode(Link head, Key key) {
|
|
for (Link t = head; t != NULL; t = t->next) {
|
|
if (ItemEq(ItemKey(t->val), key))
|
|
return t->val;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void printList(FILE *fp, Link head) {
|
|
for (Link t = head; t != NULL; t = t->next) {
|
|
ItemPrint(fp, t->val);
|
|
}
|
|
}
|
|
|
|
Item extractCode(Link *headp, Key key) {
|
|
Link *t, r;
|
|
Item element = NULL;
|
|
|
|
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
|
if (ItemEq(ItemKey((*t)->val), key)) {
|
|
element = (*t)->val;
|
|
// Salvo `r` per poi chiamare free
|
|
r = *t;
|
|
// rimuovo il nodo dalla lista
|
|
*t = (*t)->next;
|
|
// Faccio free solo del nodo (non dell'item)
|
|
free(r);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return element;
|
|
}
|
|
|
|
Item extractDates(Link *headp, Date first, Date last) {
|
|
Link *t, r;
|
|
Item element = NULL;
|
|
|
|
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
|
if (DateGreater((*t)->val->date, first) && DateGreater(last, (*t)->val->date)) {
|
|
element = (*t)->val;
|
|
r = *t;
|
|
*t = (*t)->next;
|
|
free(r);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return element;
|
|
}
|
|
|
|
void freeList(Link head) {
|
|
Link p, x;
|
|
for (p = head, x = head->next; x != NULL; p = x, x = x->next){
|
|
freeNode(p);
|
|
}
|
|
}
|
|
|
|
#endif |