summaryrefslogtreecommitdiff
path: root/stream/obj_array.c
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2008-03-21 13:28:39 +0100
committerTor Andersson <tor@ghostscript.com>2008-03-21 13:28:39 +0100
commit4b9181cdb56a9d2015f8d79e90015038f4792334 (patch)
tree507876fcec4f91aa591f3cbf2196f4dcc4deeaad /stream/obj_array.c
parent65e3374a2415399ded4624326a01bb9dfa77fa9e (diff)
downloadmupdf-4b9181cdb56a9d2015f8d79e90015038f4792334.tar.xz
Backport of chained error messages from experimental branch.
Diffstat (limited to 'stream/obj_array.c')
-rw-r--r--stream/obj_array.c107
1 files changed, 76 insertions, 31 deletions
diff --git a/stream/obj_array.c b/stream/obj_array.c
index cfcd3fb1..3953470a 100644
--- a/stream/obj_array.c
+++ b/stream/obj_array.c
@@ -10,7 +10,8 @@ fz_newarray(fz_obj **op, int initialcap)
int i;
obj = *op = fz_malloc(sizeof (fz_obj));
- if (!obj) return fz_outofmem;
+ if (!obj)
+ return fz_throw("outofmem: array struct");
obj->refs = 1;
obj->kind = FZ_ARRAY;
@@ -19,12 +20,16 @@ fz_newarray(fz_obj **op, int initialcap)
obj->u.a.cap = initialcap > 0 ? initialcap : 6;
obj->u.a.items = fz_malloc(sizeof (fz_obj*) * obj->u.a.cap);
- if (!obj->u.a.items) { fz_free(obj); return fz_outofmem; }
+ if (!obj->u.a.items)
+ {
+ fz_free(obj);
+ return fz_throw("outofmem: array item buffer");
+ }
for (i = 0; i < obj->u.a.cap; i++)
obj->u.a.items[i] = nil;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -35,18 +40,25 @@ fz_copyarray(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isarray(obj))
- return fz_throw("typecheck in copyarray");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
error = fz_newarray(&new, fz_arraylen(obj));
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new array");
- for (i = 0; i < fz_arraylen(obj); i++) {
+ for (i = 0; i < fz_arraylen(obj); i++)
+ {
error = fz_arraypush(new, fz_arrayget(obj, i));
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add item to array");
+ }
}
- return nil;
+ *op = new;
+
+ return fz_okay;
}
fz_error *
@@ -58,39 +70,69 @@ fz_deepcopyarray(fz_obj **op, fz_obj *obj)
int i;
if (!fz_isarray(obj))
- return fz_throw("typecheck in deepcopyarray");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
error = fz_newarray(&new, fz_arraylen(obj));
- if (error) return error;
- *op = new;
+ if (error)
+ return fz_rethrow(error, "cannot create new array");
for (i = 0; i < fz_arraylen(obj); i++)
{
val = fz_arrayget(obj, i);
- if (fz_isarray(val)) {
+ if (fz_isarray(val))
+ {
error = fz_deepcopyarray(&val, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
+
error = fz_arraypush(new, val);
- if (error) { fz_dropobj(val); fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
+
fz_dropobj(val);
}
- else if (fz_isdict(val)) {
+ else if (fz_isdict(val))
+ {
error = fz_deepcopydict(&val, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot deep copy item");
+ }
+
error = fz_arraypush(new, val);
- if (error) { fz_dropobj(val); fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_dropobj(val);
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
fz_dropobj(val);
}
- else {
+ else
+ {
error = fz_arraypush(new, val);
- if (error) { fz_droparray(new); return error; }
+ if (error)
+ {
+ fz_droparray(new);
+ return fz_rethrow(error, "cannot add copied item to array");
+ }
}
}
- return nil;
+ *op = new;
+
+ return fz_okay;
}
int
@@ -117,17 +159,17 @@ fz_error *
fz_arrayput(fz_obj *obj, int i, fz_obj *item)
{
if (!fz_isarray(obj))
- return fz_throw("typecheck in arrayput");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
if (i < 0)
- return fz_throw("rangecheck in arrayput: %d < 0", i);
+ return fz_throw("assert: index %d < 0", i);
if (i >= obj->u.a.len)
- return fz_throw("rangecheck in arrayput: %d > %d", i, obj->u.a.len);
+ return fz_throw("assert: index %d > length %d", i, obj->u.a.len);
if (obj->u.a.items[i])
fz_dropobj(obj->u.a.items[i]);
obj->u.a.items[i] = fz_keepobj(item);
- return nil;
+ return fz_okay;
}
static fz_error *
@@ -139,14 +181,15 @@ growarray(fz_obj *obj)
newcap = obj->u.a.cap * 2;
newitems = fz_realloc(obj->u.a.items, sizeof (fz_obj*) * newcap);
- if (!newitems) return fz_outofmem;
+ if (!newitems)
+ return fz_throw("outofmem: resize item buffer");
obj->u.a.items = newitems;
for (i = obj->u.a.cap ; i < newcap; i++)
obj->u.a.items[i] = nil;
obj->u.a.cap = newcap;
- return nil;
+ return fz_okay;
}
fz_error *
@@ -155,17 +198,19 @@ fz_arraypush(fz_obj *obj, fz_obj *item)
fz_error *error;
if (!fz_isarray(obj))
- return fz_throw("typecheck in arraypush");
+ return fz_throw("assert: not an array (%s)", fz_objkindstr(obj));
- if (obj->u.a.len + 1 > obj->u.a.cap) {
+ if (obj->u.a.len + 1 > obj->u.a.cap)
+ {
error = growarray(obj);
- if (error) return error;
+ if (error)
+ return fz_rethrow(error, "cannot grow item buffer");
}
obj->u.a.items[obj->u.a.len] = fz_keepobj(item);
obj->u.a.len++;
- return nil;
+ return fz_okay;
}
void