|
| 1 | +#define _XOPEN_SOURCE 700 |
| 2 | +#define PCRE2_CODE_UNIT_WIDTH 8 |
| 3 | + |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <ctype.h> |
| 7 | +#include <string.h> |
| 8 | +#include <pcre2.h> |
| 9 | + |
| 10 | +#include "regex.h" |
| 11 | +#include "matrix.h" |
| 12 | +#include "commons.h" |
| 13 | + |
| 14 | +// ~ Static function propotypes ================== |
| 15 | +static int isCyclicError(const Worksheet *worksheet, const char *visitedCells, CellReference *cellReference); |
| 16 | + |
| 17 | +/** |
| 18 | + * Initializes the worksheet cells of size rows * columns. |
| 19 | + * |
| 20 | + * Returns 0 if an error is encountered. |
| 21 | + */ |
| 22 | +int initWorksheet(Worksheet *worksheet, int rows, int columns) { |
| 23 | + worksheet->rows = rows; |
| 24 | + worksheet->cols = columns; |
| 25 | + |
| 26 | + char ***matrix = (char ***) malloc(sizeof(char **) * rows); |
| 27 | + for(int i = 0; i < rows; i++) { |
| 28 | + matrix[i] = (char **) malloc(sizeof(char *) * columns); |
| 29 | + |
| 30 | + for(int j = 0; j < columns; j++) { |
| 31 | + matrix[i][j] = (char *) calloc(sizeof(char) * CELL_CONTENT_LIMIT, sizeof(char)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + worksheet->cells = matrix; |
| 36 | + return 1; |
| 37 | +} |
| 38 | + |
| 39 | +int closeWorksheet(Worksheet *w) |
| 40 | +{ |
| 41 | + for(int i = 0; i < w->rows; i++) |
| 42 | + { |
| 43 | + for(int j = 0; j < w->cols; j++) |
| 44 | + { |
| 45 | + free(w->cells[i][j]); |
| 46 | + } |
| 47 | + free(w->cells[i]); |
| 48 | + } |
| 49 | + free(w->cells); |
| 50 | + free(w); |
| 51 | + return 1; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Sets s to the worksheet cell at row, column. |
| 56 | + * |
| 57 | + * Returns 0 if strlen(s) > CELL_CONTENT_LIMIT |
| 58 | + */ |
| 59 | +int setValue(Worksheet *worksheet, int row, int column, char* s) { |
| 60 | + if (strlen(s) <= CELL_CONTENT_LIMIT) { |
| 61 | + strncpy(worksheet->cells[row][column], s, strlen(s)); |
| 62 | + return 1; |
| 63 | + } else { |
| 64 | + return 0; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Returns the value at row, col |
| 70 | + */ |
| 71 | +char* getValue(const Worksheet *worksheet, int row, int column) { |
| 72 | + return worksheet->cells[row][column]; |
| 73 | +} |
| 74 | + |
| 75 | +int getValue2(const Worksheet *w, char **buffer, int row, int column) |
| 76 | +{ |
| 77 | + if (w == NULL) return 0; |
| 78 | + if (w->cells[row][column] == NULL) return 0; |
| 79 | + |
| 80 | + *buffer = malloc(sizeof(char) * strlen(w->cells[row][column]) + 1); |
| 81 | + strcpy(*buffer, w->cells[row][column]); |
| 82 | + |
| 83 | + return 1; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Converts the given location (row, column) to a spreadsheet cell reference i.e. A1 |
| 88 | + */ |
| 89 | +CellReference *convertToCellReference(const MatrixLocation *location) |
| 90 | +{ |
| 91 | + CellReference *ref = malloc(sizeof(CellReference)); |
| 92 | + |
| 93 | + char *colRef = malloc(sizeof(char) * getNumberOfDigits(location->col) + 1); |
| 94 | + sprintf(colRef, "%d", location->col + 1); |
| 95 | + |
| 96 | + // row ref + col ref + null terminator |
| 97 | + ref->cellReference = malloc(sizeof(char) * (1 + strlen(colRef) + 1) + 1); |
| 98 | + sprintf(ref->cellReference, "%c%s", (char)(location->row + ROW_TO_ASCII_OFFSET), colRef); |
| 99 | + free(colRef); |
| 100 | + |
| 101 | + return ref; |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Returns a pointer to a MatrixLocation structure |
| 106 | + */ |
| 107 | +MatrixLocation *convertToMatrixLocation(const CellReference *ref) |
| 108 | +{ |
| 109 | + MatrixLocation *loc = malloc(sizeof(MatrixLocation)); |
| 110 | + loc->row = ((int) toupper(ref->cellReference[0])) - ROW_TO_ASCII_OFFSET; |
| 111 | + |
| 112 | + char *col = calloc(sizeof(char) + strlen(ref->cellReference), sizeof(char)); |
| 113 | + col = strncpy(col, ref->cellReference + 1, strlen(ref->cellReference) - 1); |
| 114 | + loc->col = atoi(col) - 1; |
| 115 | + free(col); |
| 116 | + return loc; |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Checks if a given formula at row, col refers to some cells which themselves |
| 121 | + * refer back to row,col |
| 122 | + * |
| 123 | + * Returns 0 if an error is encountered. |
| 124 | + */ |
| 125 | +int isCyclicRefError(const Worksheet *worksheet, int row, int col) |
| 126 | +{ |
| 127 | + MatrixLocation m = { row, col }; |
| 128 | + |
| 129 | + CellReference *cellRef = convertToCellReference(&m); |
| 130 | + int result = isCyclicError(worksheet, "", cellRef); |
| 131 | + |
| 132 | + free(cellRef->cellReference); |
| 133 | + free(cellRef); |
| 134 | + return result; |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * An internal function for checking cyclic reference dependency error. |
| 139 | + * |
| 140 | + * ========== |
| 141 | + * Returns 1 if cyclic dependency is found, 0 if otherwise |
| 142 | + */ |
| 143 | +static int isCyclicError(const Worksheet *worksheet, const char *visitedCells, CellReference *cellRef) |
| 144 | +{ |
| 145 | + pcre2_match_data *match_data = NULL; |
| 146 | + char *saveptr = NULL; |
| 147 | + int rc = 0; |
| 148 | + |
| 149 | + MatrixLocation *m = convertToMatrixLocation(cellRef); |
| 150 | + |
| 151 | + char *cellValue = NULL; |
| 152 | + getValue2(worksheet, &cellValue, m->row, m->col); |
| 153 | + free(m); |
| 154 | + |
| 155 | + if (cellValue == NULL) |
| 156 | + { |
| 157 | + return 0; |
| 158 | + } |
| 159 | + |
| 160 | + // do work on working copy |
| 161 | + char *token = NULL; |
| 162 | + token = strtok_r(cellValue, " ", &saveptr); |
| 163 | + pcre2_code *re = getCellReferencePattern(); |
| 164 | + while(token != NULL) |
| 165 | + { |
| 166 | + match_data = pcre2_match_data_create(20, NULL); |
| 167 | + int subjectLength = strlen(token); |
| 168 | + rc = pcre2_match(re, (PCRE2_SPTR) token, subjectLength, 0, 0, match_data, NULL); |
| 169 | + |
| 170 | + if (rc > 0) |
| 171 | + { |
| 172 | + // search if current cellref is in the visited cells |
| 173 | + pcre2_code *searchVal = compilePattern(token); |
| 174 | + int isCyclicDependency = pcre2_match(searchVal, (PCRE2_SPTR) visitedCells, strlen(visitedCells), 0, 0, match_data, NULL); |
| 175 | + if (isCyclicDependency > 0) |
| 176 | + { |
| 177 | + free(cellValue); |
| 178 | + free(match_data); |
| 179 | + free(searchVal); |
| 180 | + free(re); |
| 181 | + return 1; |
| 182 | + } |
| 183 | + |
| 184 | + free(searchVal); |
| 185 | + |
| 186 | + //length of existing visitedCells + space character + length of cellRef to be appended + null terminator |
| 187 | + char *newVisitedCells = malloc(sizeof(char) * (strlen(visitedCells) + 1 + strlen(cellRef->cellReference)) + 1); |
| 188 | + strcpy(newVisitedCells, visitedCells); |
| 189 | + strcat(newVisitedCells, " "); |
| 190 | + strcat(newVisitedCells, cellRef->cellReference); |
| 191 | + |
| 192 | + CellReference *tokenCellRef = malloc(sizeof(CellReference)); |
| 193 | + tokenCellRef->cellReference = token; |
| 194 | + |
| 195 | + if(isCyclicError(worksheet, (const char *) newVisitedCells, tokenCellRef)) |
| 196 | + { |
| 197 | + free(cellValue); |
| 198 | + free(newVisitedCells); |
| 199 | + free(match_data); |
| 200 | + free(re); |
| 201 | + return 1; |
| 202 | + } |
| 203 | + |
| 204 | + free(newVisitedCells); |
| 205 | + free(tokenCellRef); |
| 206 | + } |
| 207 | + token = strtok_r(NULL, " ", &saveptr); |
| 208 | + free(match_data); |
| 209 | + } |
| 210 | + |
| 211 | + free(cellValue); |
| 212 | + free(re); |
| 213 | + return 0; |
| 214 | +} |
| 215 | + |
0 commit comments