better error reporting

This commit is contained in:
Francesco Cozzuto
2022-01-14 19:21:26 +01:00
parent 242398584c
commit c391de463c
2 changed files with 68 additions and 21 deletions
+30 -15
View File
@@ -116,9 +116,13 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
do do
{ {
if(n == max_length) if(n == max_length)
// ERROR: Internal buffer is too small to hold {
// the representation of this item. // ERROR: Internal buffer is too small to hold
return 0; // the representation of this item.
*error = "Internal buffer is too small to hold "
"the representation of a numeric value";
return 0;
}
buffer[n++] = c; buffer[n++] = c;
@@ -136,27 +140,38 @@ static int scanValue(FILE *fp, char *buffer, int max_length, char first, char *f
if(dot) if(dot)
{ {
if(n == max_length) if(n == max_length)
// ERROR: Internal buffer is too small to hold {
// the representation of this item. // ERROR: Internal buffer is too small to hold
// (The dot doesn't fit.) // the representation of this item.
return 0; // (The dot doesn't fit.)
*error = "Internal buffer is too small to hold "
"the representation of a numeric value";
return 0;
}
buffer[n++] = '.'; buffer[n++] = '.';
c = getc(fp); c = getc(fp);
if(!isdigit(c)) if(!isdigit(c))
// ERROR: Got something other than a {
// digit after the dot. // ERROR: Got something other than a
return 0; // digit after the dot.
*error = "Got something other than a digit after the dot.";
return 0;
}
do do
{ {
if(n == max_length) if(n == max_length)
// ERROR: Internal buffer is too small {
// to hold the representation of // ERROR: Internal buffer is too small
// this item. // to hold the representation of
return 0; // this item.
*error = "Internal buffer is too small to hold "
"the representation of a numeric value";
return 0;
}
buffer[n++] = c; buffer[n++] = c;
+38 -6
View File
@@ -15,12 +15,44 @@ struct {
double *matrix; double *matrix;
int w, h; int w, h;
} load_tests[] = { } load_tests[] = {
{ .src = "", .matrix = NULL }, {
{ .src = " [] ", .matrix = NULL }, .src = "",
{ .src = " [1] ", .matrix = (double[]) {1}, .w = 1, .h = 1 }, .matrix = NULL
{ .src = "[1 2 3]", .matrix = (double[]) {1, 2, 3}, .w = 3, .h = 1 }, },
{ .src = "[1, 2, 3]", .matrix = (double[]) {1, 2, 3}, .w = 1, .h = 3 }, {
{ .src = "[1 2 3, 4 5 6, 7 8 9]", .matrix = (double[]) {1, 2, 3, 4, 5, 6, 7, 8, 9}, .w = 3, .h = 3 }, .src = " [] ",
.matrix = NULL
},
{
.src = " [1] ",
.matrix = (double[]) {1},
.w = 1,
.h = 1
},
{
.src = "[1 2 3]",
.matrix = (double[]) {1, 2, 3},
.w = 3,
.h = 1
},
{
.src = "[1, 2, 3]",
.matrix = (double[]) {1, 2, 3},
.w = 1,
.h = 3
},
{
.src = "[1 2 3, 4 5 6, 7 8 9]",
.matrix = (double[]) {1, 2, 3, 4, 5, 6, 7, 8, 9},
.w = 3,
.h = 3
},
{
.src = "[1.0 2.0 3.0, 4.7 5.0 6.0, 7.0 8.0 9.5]",
.matrix = (double[]) {1, 2, 3, 4.7, 5, 6, 7, 8, 9.5},
.w = 3,
.h = 3
},
}; };
int main() int main()