1
Fork 0
This repository has been archived on 2022-04-02. You can view files and clone it, but cannot push or open issues or pull requests.
UOC_PP20212_PR/UOC20212/UOCVaccine/src/api.c
2022-04-01 15:58:20 +02:00

185 lines
4.7 KiB
C

#include <stdio.h>
#include <assert.h>
#include "csv.h"
#include "api.h"
#include <string.h>
#include "person.h"
#include "vaccine.h"
#define FILE_READ_BUFFER_SIZE 2048
#define VACCINE_LOT_TYPE_NAME "VACCINE_LOT"
#define VACCINE_LOT_NUM_FIELDS 7
// Get the API version information
const char* api_version() {
return "UOC PP 20212";
}
// Load data from a CSV file. If reset is true, remove previous data
tApiError api_loadData(tApiData* data, const char* filename, bool reset) {
tApiError error;
FILE *fin;
char buffer[FILE_READ_BUFFER_SIZE];
tCSVEntry entry;
// Check input data
assert( data != NULL );
assert(filename != NULL);
// Reset current data
if (reset) {
// Remove previous information
error = api_freeData(data);
if (error != E_SUCCESS) {
return error;
}
// Initialize the data
error = api_initData(data);
if (error != E_SUCCESS) {
return error;
}
}
// Open the input file
fin = fopen(filename, "r");
if (fin == NULL) {
return E_FILE_NOT_FOUND;
}
// Read file line by line
while (fgets(buffer, FILE_READ_BUFFER_SIZE, fin)) {
// Remove new line character
buffer[strcspn(buffer, "\n\r")] = '\0';
csv_initEntry(&entry);
csv_parseEntry(&entry, buffer, NULL);
// Add this new entry to the api Data
error = api_addDataEntry(data, entry);
if (error != E_SUCCESS) {
return error;
}
csv_freeEntry(&entry);
}
fclose(fin);
return E_SUCCESS;
}
// Initialize the data structure
tApiError api_initData(tApiData* data) {
//////////////////////////////////
// Ex PR1 2b
/////////////////////////////////
population_init(&(data->population));
vaccineList_init(&(data->vaccines));
vaccineLotData_init(&(data->vaccineLots));
return E_SUCCESS;
}
// Add a new vaccines lot
tApiError api_addVaccineLot(tApiData* data, tCSVEntry entry) {
//////////////////////////////////
// Ex PR1 2c
/////////////////////////////////
tApiError ret = E_NOT_IMPLEMENTED;
if (csv_numFields(entry) == VACCINE_LOT_NUM_FIELDS) {
if (csv_getType(&entry) == VACCINE_LOT_TYPE_NAME) {
// OK
ret = E_SUCCESS;
} else {
// numFields other than valid one
ret = E_INVALID_ENTRY_TYPE;
}
} else {
// csv record type other than valid one
ret = E_INVALID_ENTRY_FORMAT;
}
return ret;
}
// Get the number of persons registered on the application
int api_populationCount(tApiData data) {
//////////////////////////////////
// Ex PR1 2d
/////////////////////////////////
return -1;
}
// Get the number of vaccines registered on the application
int api_vaccineCount(tApiData data) {
//////////////////////////////////
// Ex PR1 2d
/////////////////////////////////
return -1;
}
// Get the number of vaccine lots registered on the application
int api_vaccineLotsCount(tApiData data) {
//////////////////////////////////
// Ex PR1 2d
/////////////////////////////////
return -1;
}
// Free all used memory
tApiError api_freeData(tApiData* data) {
//////////////////////////////////
// Ex PR1 2e
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}
// Add a new entry
tApiError api_addDataEntry(tApiData* data, tCSVEntry entry) {
//////////////////////////////////
// Ex PR1 2f
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}
// Get vaccine data
tApiError api_getVaccine(tApiData data, const char *name, tCSVEntry *entry) {
//////////////////////////////////
// Ex PR1 3a
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}
// Get vaccine lot data
tApiError api_getVaccineLot(tApiData data, const char* cp, const char* vaccine, tDateTime timestamp, tCSVEntry *entry) {
//////////////////////////////////
// Ex PR1 3b
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}
// Get registered vaccines
tApiError api_getVaccines(tApiData data, tCSVData *vaccines) {
//////////////////////////////////
// Ex PR1 3c
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}
// Get vaccine lots
tApiError api_getVaccineLots(tApiData data, tCSVData *lots) {
//////////////////////////////////
// Ex PR1 3d
/////////////////////////////////
return E_NOT_IMPLEMENTED;
}