Skip to content
Snippets Groups Projects
Commit d5ab1fc5 authored by Martin Nilsson's avatar Martin Nilsson
Browse files

Throw a recognizable object from ADT.struct in case of parse errors.

parent d2e2853b
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,12 @@ constant List = __builtin.List; ...@@ -13,6 +13,12 @@ constant List = __builtin.List;
protected int item_counter; protected int item_counter;
int get_item_id() { return item_counter++; } int get_item_id() { return item_counter++; }
protected class structError
{
inherit Error.Generic;
constant ADT_struct = 1;
}
//! String buffer with the possibility to read and write data //! String buffer with the possibility to read and write data
//! as they would be formatted in structs. //! as they would be formatted in structs.
class struct { class struct {
...@@ -61,7 +67,7 @@ class struct { ...@@ -61,7 +67,7 @@ class struct {
this_program put_uint(int i, int(0..) len) this_program put_uint(int i, int(0..) len)
{ {
if (i<0) if (i<0)
error("Negative argument.\n"); throw(structError("Negative argument.\n"));
add_data([string(0..255)]sprintf("%*c", len, i)); add_data([string(0..255)]sprintf("%*c", len, i));
return this; return this;
} }
...@@ -81,7 +87,7 @@ class struct { ...@@ -81,7 +87,7 @@ class struct {
this_program put_bignum(Gmp.mpz i, int(0..)|void len_width) this_program put_bignum(Gmp.mpz i, int(0..)|void len_width)
{ {
if (i<0) if (i<0)
error("Negative argument.\n"); throw(structError("Negative argument.\n"));
put_var_string(i->digits(256), len_width || 2); put_var_string(i->digits(256), len_width || 2);
return this; return this;
} }
...@@ -117,7 +123,7 @@ class struct { ...@@ -117,7 +123,7 @@ class struct {
{ {
int(0..) i; int(0..) i;
if ( (sizeof(buffer) - index) < len) if ( (sizeof(buffer) - index) < len)
error("No data.\n"); throw(structError("No data.\n"));
sscanf(buffer, "%*" + (string) index +"s%" + (string) len + "c", i); sscanf(buffer, "%*" + (string) index +"s%" + (string) len + "c", i);
index += len; index += len;
return i; return i;
...@@ -127,7 +133,7 @@ class struct { ...@@ -127,7 +133,7 @@ class struct {
string(0..255) get_fix_string(int len) string(0..255) get_fix_string(int len)
{ {
if ((sizeof(buffer) - index) < len) if ((sizeof(buffer) - index) < len)
error("No data\n"); throw(structError("No data\n"));
string(0..255) res = buffer[index .. index + len - 1]; string(0..255) res = buffer[index .. index + len - 1];
index += len; index += len;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment