Skip to content

Commit

Permalink
Refactor cods with struct
Browse files Browse the repository at this point in the history
  • Loading branch information
PironP committed Dec 17, 2017
1 parent d8fe0f8 commit 8adffde
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 36 deletions.
10 changes: 7 additions & 3 deletions sources/headers/read_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
#define read_table_h

#include <stdio.h>
#include "config.h"

void findAllRecords(char *db_name, char *table_name);
void findSpecificRecords(char *db_name, char *table_name, char *record_value);
void printResult(char **column_name, int *columnNameSize, char **result, int *dataSize);
void findAllRecords(char* db_name, char* table_name);
void findSpecificRecords(char *db_name, char *table_name, char* column_name, char *record_value);
char** readColumnName(FILE* f, int *columnNameSize);
int filesFound(char *db_name, char *table_name, char *table_file);
char** readData(FILE* f, int *dataSize);
void parseData(char **data, int numberOfData, Table *currentTable);
void printResult(char **column_name, int *columnNameSize, Table *currentTable);
int searchColumnPlace(Table *currentTable, char *columnName);
int searchSpecificData(Table *currentTable, int columnPlace, char *dataSearched);
#endif

125 changes: 92 additions & 33 deletions sources/sources/read_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,77 @@ void findAllRecords(char* db_name, char* table_name)
{
char *table_file = createFilePath(table_name);

if (!filesFound(db_name, table_name, table_file)) {
if(!filesFound(db_name, table_name, table_file)) {
return;
}

Table currentTable;

FILE* f = fopen(table_file, "r");


int columnNameSize = 0;
char **columnName = readColumnName(f, &columnNameSize);


currentTable.columns = malloc(sizeof(Column) * columnNameSize);
currentTable.nbColumns = columnNameSize;

for (int i = 0; i < columnNameSize; i++) {
currentTable.columns[i].name = columnName[i];
}

int dataSize = 0;
char **data = readData(f, &dataSize);
printResult(columnName, &columnNameSize, data, &dataSize);


parseData(data, dataSize, &currentTable);

for (int i = 0; i < columnNameSize; i++) {
free(columnName[i]);
}
free(columnName);
printResult(columnName, &columnNameSize, &currentTable);
// func free table to clen memory

for (int i = 0; i < dataSize; i++) {
free(data[i]);
}
free(data);

fclose(f);
return;
}

/**
// For a given data, insert into a Table 'currentTable'
// value in Row and Cell
**/
void parseData(char **data, int numberOfData, Table *currentTable)
{
// allocate space for rows.
currentTable->rows = malloc(numberOfData);
currentTable->nbRows = numberOfData;

for (int i = 0; i < numberOfData; i++) {

char *currentRow = malloc(sizeof(char) * STRING_SIZE);
strcpy(currentRow, data[i] + 3);

char *token;
token = strtok(currentRow, ",");

currentTable->rows[i].cells = malloc(sizeof(Cell) * currentTable->nbRows);

for (int j = 0; j < currentTable->nbColumns && token != NULL; j++) {

currentTable->rows[i].cells[j].data = malloc(sizeof(char) * STRING_SIZE);

strcpy(currentTable->rows[i].cells[j].data, token);
token = strtok(NULL, ",");
currentTable->rows[i].nbCells = j + 1;

//printf("nbCells : %d, current cell : %s\n", currentTable->rows[i].nbCells, currentTable->rows[i].cells[j].data);
}
}
return;
}

/**
// Read the data for a given database name 'db_name'
// and a given 'tbale_name',
// and print result where is a given 'record_value'
**/
void findSpecificRecords(char* db_name, char* table_name, char* record_value)
void findSpecificRecords(char* db_name, char* table_name, char* column_name, char* record_value)
{
char *table_file = createFilePath(table_name);

Expand All @@ -75,8 +110,6 @@ void findSpecificRecords(char* db_name, char* table_name, char* record_value)
int columnNameSize = 0;

char **columnName = readColumnName(f, &columnNameSize);
char **data[10][STRING_SIZE] = {"test 1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10"};
//printResult(columnName, &columnNameSize, data);

while ((fgets(temp, STRING_SIZE, f) != NULL) && (strstr(temp, "-") != NULL)) {
if ((strstr(temp, " ")) != NULL && (strstr(temp, " ")) == NULL && (strstr(temp, record_value)) != NULL) {
Expand Down Expand Up @@ -114,8 +147,7 @@ char** readColumnName(FILE* f, int *columnNameSize)
int i = 0;
while ((fgets(temp, STRING_SIZE, f) != NULL) && (strstr(temp, "data") == NULL)) {
if ((strstr(temp, " ")) != NULL && (strstr(temp, " ")) == NULL) {
temp[strlen(temp)-1] = '\0';
temp[strlen(temp)-1] = '\0';
temp[strlen(temp)-2] = '\0';
columnName[i] = malloc(STRING_SIZE * sizeof(char));
strcpy(columnName[i], temp + 8);
i++;
Expand All @@ -125,22 +157,22 @@ char** readColumnName(FILE* f, int *columnNameSize)

return columnName;
}

/**
// Read the data in a given FILE* 'f'
// and return an array of string with the records
**/
char** readData(FILE* f, int *dataSize)
{

// update
char **data = malloc(50 * sizeof(char *));

char temp[STRING_SIZE];

int i = 0;
while ((fgets(temp, STRING_SIZE, f) != NULL) && (strstr(temp, "-") != NULL)) {
if ((strstr(temp, " ")) != NULL && (strstr(temp, " ")) == NULL) {
temp[strlen(temp)-1] = '\0';
temp[strlen(temp)-1] = '\0';
temp[strlen(temp)-2] = '\0';
data[i] = malloc(STRING_SIZE * sizeof(char));
strcpy(data[i], temp + 8);
i++;
Expand All @@ -153,32 +185,59 @@ char** readData(FILE* f, int *dataSize)

/**
// Print the result for a given 'column_name and
// for a given 'data'
// for a given 'table'
**/
void printResult(char **column_name, int *columnNameSize, char **data, int *dataSize)
void printResult(char **column_name, int *columnNameSize, Table *currentTable)
{

for (int i = 0; i < *columnNameSize; i++) {
printf("%s | ", column_name[i]);
}
printf("\n");

for (int i = 0; i < *dataSize; i++) {
char temp[STRING_SIZE];
strcpy(temp, data[i] + 3);
char *token;
token = strtok(temp, ",");
while (token != NULL) {
printf("%s | ", token);
token = strtok(NULL, ",");
for (int i = 0; i < currentTable->nbRows; i++) {
for (int j = 0; j < currentTable->rows[i].nbCells; j++) {
printf("%s | ", currentTable->rows[i].cells[j].data);
}
printf("\n");
}
}

return;

/**
// Search in the currentTable the place of the column searched
// and return the place or -1 if not found
**/
int searchColumnPlace(Table *currentTable, char *columnName)
{
for (int i = 0; i < currentTable->nbColumns; i++) {
if (strcmp(currentTable->columns[i].name, columnName) == 0) {
return i;
}
}
return -1;
}



/**
// NON TESTER
//
// Search in the currentTable if a the data searched is present at the specific
// columnPlace
// return the row number if found, -1 if not
**/
int searchSpecificData(Table *currentTable, int columnPlace, char *dataSearched)
{
for (int i = 0; i < currentTable->nbRows; i++) {
if (strstr(currentTable->rows[i].cells[columnPlace].data, dataSearched) != NULL) {
return i;
}
}
return -1;
}



/**
// Check if database file exists
// if table is present in database
Expand Down

0 comments on commit 8adffde

Please sign in to comment.