sps.c
State readTable(Table *table, FILE *f, char *delimiters) {
    // set the table's main delimiter
    table->delim = delimiters[0];

    char *fileBuffer = fileToBuffer(f);
    if (fileBuffer == NULL)
        return ERR_MEMORY;

    State s = SUCCESS;
    // current row and column
    unsigned row=0, col=0;
    size_t i = 0;

    while (true) {
        char cellBuffer[MAX_CELL_LENGTH];
        size_t shift = parseString(cellBuffer, &fileBuffer[i], delimiters);
        // +1 to skip the delimiter
        i += shift + 1;

        // if there is nothing left
        if (fileBuffer[i-1] == '\0')
            break;

        // write to table
        s = assureTableSize(table, row+1, col+1);
        if (s != SUCCESS)
            return s;

        s = writeCell(&table->cells[row][col], cellBuffer);
        if (s != SUCCESS)
            return s;

        if (fileBuffer[i-1] == '\n') {
            row++;
            col = 0;
            continue;
        }

        if (strchr(delimiters, fileBuffer[i-1])) {
            col++;
            continue;
        }
        // if nothing matches, the scanned STR was bad
        return ERR_BAD_INPUT;
    }
    free(fileBuffer);
    return SUCCESS;
}
// Reads table from stdin and saves it into the table structure
// The function also reads delimiters from arguments
// Returns program state
// Expects an empty table
Reads table from stdin and saves it into the table structure
The function also reads delimiters from arguments
Returns program state
Expects an empty table
Reads table from stdin and saves it into the table structure The function also reads delimiters from arguments Returns program state Expects an empty table
State readTable(Table *table, FILE *f, char *delimiters) {
    table->delim = delimiters[0];
    char *fileBuffer = fileToBuffer(f);
    if (fileBuffer == NULL)
        return ERR_MEMORY;
    State s = SUCCESS;
    unsigned row=0, col=0;
    size_t i = 0;
    while (true) {
        char cellBuffer[MAX_CELL_LENGTH];
        size_t shift = parseString(cellBuffer, &fileBuffer[i], delimiters);
        i += shift + 1;
        if (fileBuffer[i-1] == '\0')
            break;
        s = assureTableSize(table, row+1, col+1);
        if (s != SUCCESS)
            return s;
        s = writeCell(&table->cells[row][col], cellBuffer);
        if (s != SUCCESS)
            return s;
        if (fileBuffer[i-1] == '\n') {
            row++;
            col = 0;
            continue;
        }
        if (strchr(delimiters, fileBuffer[i-1])) {
            col++;
            continue;
        }
        return ERR_BAD_INPUT;
    }
    free(fileBuffer);
    return SUCCESS;
}
State readTable ( Table * table , FILE * f , char * delimiters ) { table -> delim = delimiters [ 0 ] ; char * fileBuffer = fileToBuffer ( f ) ; if ( fileBuffer == NULL ) return ERR_MEMORY ; State s = SUCCESS ; unsigned row = 0 , col = 0 ; size_t i = 0 ; while ( true ) { char cellBuffer [ MAX_CELL_LENGTH ] ; size_t shift = parseString ( cellBuffer , & fileBuffer [ i ] , delimiters ) ; i += shift + 1 ; if ( fileBuffer [ i - 1 ] == ' \0 ' ) break ; s = assureTableSize ( table , row + 1 , col + 1 ) ; if ( s != SUCCESS ) return s ; s = writeCell ( & table -> cells [ row ] [ col ] , cellBuffer ) ; if ( s != SUCCESS ) return s ; if ( fileBuffer [ i - 1 ] == ' \n ' ) { row ++ ; col = 0 ; continue ; } if ( strchr ( delimiters , fileBuffer [ i - 1 ] ) ) { col ++ ; continue ; } return ERR_BAD_INPUT ; } free ( fileBuffer ) ; return SUCCESS ; }
Reads table from stdin and saves it into the table structure
The function also reads delimiters from arguments
Returns program state
Expects an empty table
Reads table from stdin and saves it into the table structure The function also reads delimiters from arguments Returns program state Expects an empty table
// set the table's main delimiter // current row and column // +1 to skip the delimiter // if there is nothing left // write to table // if nothing matches, the scanned STR was bad