182 lines
No EOL
5.6 KiB
C
Executable file
182 lines
No EOL
5.6 KiB
C
Executable file
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "contact.h"
|
|
|
|
// Parse a tDateTime from string information
|
|
/*void dateTime_parse(tDateTime* dateTime, const char* date, const char* time) {
|
|
// Check output data
|
|
assert(dateTime != NULL);
|
|
|
|
// Check input date
|
|
assert(date != NULL);
|
|
assert(strlen(date) == 10);
|
|
|
|
// Check input time
|
|
assert(time != NULL);
|
|
assert(strlen(time) == 5);
|
|
|
|
// Parse the input date
|
|
sscanf(date, "%d/%d/%d", &(dateTime->date.day), &(dateTime->date.month), &(dateTime->date.year));
|
|
|
|
// Parse the input time
|
|
sscanf(time, "%d:%d", &(dateTime->time.hour), &(dateTime->time.minutes));
|
|
}*/
|
|
|
|
// Compare two tDateTime structures and return true if they contain the same value or false otherwise.
|
|
/*bool dateTime_cmp(tDateTime dateTime1, tDateTime dateTime2) {
|
|
if (dateTime1.date.day != dateTime2.date.day || dateTime1.date.month != dateTime2.date.month || dateTime1.date.year != dateTime2.date.year) {
|
|
return false;
|
|
}
|
|
|
|
return dateTime1.time.hour == dateTime2.time.hour && dateTime1.time.minutes == dateTime2.time.minutes;
|
|
}*/
|
|
|
|
// EX2: Implement your methods here....
|
|
|
|
////////////////////////////////////////////
|
|
|
|
// Initialize the geopositon data
|
|
void geoData_init(tGeolocationData* data) {
|
|
// Set the initial number of elements to zero.
|
|
data->count = 0;
|
|
data->elems = NULL;
|
|
}
|
|
|
|
// Parse input from CSVEntry
|
|
void contact_parse(tTimePosition* data, tCSVEntry entry) {
|
|
char date[11];
|
|
char time[6];
|
|
|
|
// Check input data (Pre-conditions)
|
|
assert(data != NULL);
|
|
assert(csv_numFields(entry) == 5);
|
|
|
|
// Get the identity document
|
|
csv_getAsString(entry, 0, data->person, 15);
|
|
|
|
// Get the date and time
|
|
csv_getAsString(entry, 1, date, 11);
|
|
csv_getAsString(entry, 2, time, 6);
|
|
dateTime_parse(&(data->timestamp), date, time);
|
|
|
|
// Assign the coordinates
|
|
data->coordinate.latitude = csv_getAsReal(entry, 3);
|
|
data->coordinate.longitude = csv_getAsReal(entry, 4);
|
|
}
|
|
|
|
// Add a new position
|
|
void geoData_add(tGeolocationData* data, tTimePosition position) {
|
|
// Check input data (Pre-conditions)
|
|
assert(data != NULL);
|
|
|
|
// PAC2 No limit on max positions
|
|
//assert(data->count < MAX_POSITIONS);
|
|
|
|
// Check if an entry with this data already exists
|
|
if (geoData_find(data[0], position.person, position.timestamp) < 0) {
|
|
|
|
// Need to allocate new memory space for new item to be inserted
|
|
if (data->count == 0) {
|
|
// Allocate new position if structure is still empty
|
|
data->elems=(tTimePosition*)malloc(sizeof(tTimePosition));
|
|
} else {
|
|
|
|
// Resize vector to allocate new position
|
|
data->elems = (tTimePosition*)realloc(data->elems, (data->count+1) * sizeof(tTimePosition));
|
|
}
|
|
|
|
// Check that returned pointer to elems is not NULL
|
|
if(data->elems == NULL) {
|
|
printf("Error: Not enough space to allocate tTimePosition entry.");
|
|
return;
|
|
}
|
|
|
|
timePos_cpy(&(data->elems[data->count]), position);
|
|
data->count ++;
|
|
}
|
|
}
|
|
|
|
|
|
// Remove the data from a person in a certain time moment
|
|
void geoData_del(tGeolocationData* data, const char* document, tDateTime time) {
|
|
int pos;
|
|
int i;
|
|
|
|
// Find if it exists
|
|
pos = geoData_find(data[0], document, time);
|
|
|
|
if (pos >= 0) {
|
|
// Shift elements to remove selected
|
|
for(i = pos; i < data->count-1; i++) {
|
|
// Copy element on position i+1 to position i
|
|
timePos_cpy(&(data->elems[i]), data->elems[i+1]);
|
|
}
|
|
// Update the number of elements
|
|
data->count--;
|
|
|
|
if (data->count==0) {
|
|
free(data->elems);
|
|
data->elems == NULL;
|
|
} else {
|
|
data->elems=(tTimePosition*)realloc(data->elems, data->count * sizeof(tTimePosition));
|
|
|
|
if (data->elems == NULL) {
|
|
printf("Error: resize when deleting failed.\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Print the geoposition data
|
|
void geoData_print(tGeolocationData data) {
|
|
int i;
|
|
|
|
for(i = 0; i < data.count; i++) {
|
|
// Print position and document
|
|
printf("%d;%s;", i, data.elems[i].person);
|
|
// Print date and time
|
|
printf("%02d/%02d/%04d;%02d:%02d;", data.elems[i].timestamp.date.day, data.elems[i].timestamp.date.month, data.elems[i].timestamp.date.year, data.elems[i].timestamp.time.hour, data.elems[i].timestamp.time.minutes);
|
|
// Print position
|
|
printf("%.4f;%.4f\n", data.elems[i].coordinate.latitude, data.elems[i].coordinate.longitude);
|
|
}
|
|
}
|
|
|
|
|
|
// [AUX METHOD] Return the position of a geoposition entry with provided information. -1 if it does not exist
|
|
int geoData_find(tGeolocationData data, const char* document, tDateTime time) {
|
|
int i;
|
|
|
|
for(i = 0; i < data.count; i++) {
|
|
if(strcmp(data.elems[i].person, document) == 0 && dateTime_cmp(data.elems[i].timestamp, time)) {
|
|
return i;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
// [AUX METHOD] Copy the data from the source to destination
|
|
void timePos_cpy(tTimePosition* destination, tTimePosition source) {
|
|
strcpy(destination->person, source.person);
|
|
destination->coordinate = source.coordinate;
|
|
destination->timestamp = source.timestamp;
|
|
}
|
|
|
|
///////////////////////////////////////////
|
|
|
|
// PAC3
|
|
void geoData_free(tGeolocationData* data) {
|
|
|
|
// Check input data
|
|
assert(data != NULL);
|
|
|
|
// Release memory
|
|
if (data->count > 0) {
|
|
free(data->elems);
|
|
data->elems = NULL;
|
|
data->count = 0;
|
|
}
|
|
}
|
|
///////////////////////////////////////////
|