summaryrefslogtreecommitdiff
path: root/fitz/obj_dict.c
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2009-11-20 02:47:10 +0100
committerTor Andersson <tor@ghostscript.com>2009-11-20 02:47:10 +0100
commit549dbe8e5563f9a228c8de35f07d0108e3adf74c (patch)
tree535331b85b257ceaf44b9d2bfae67742ea4968d8 /fitz/obj_dict.c
parent856c0c8335d9a0a861558f92cd35c0cf3b5007c3 (diff)
downloadmupdf-549dbe8e5563f9a228c8de35f07d0108e3adf74c.tar.xz
As a new code policy in MuPDF, malloc cannot return NULL. As a result we can get rid of large chunks of untested error handling code. This patch cleans up the handling of fz_obj.
Diffstat (limited to 'fitz/obj_dict.c')
-rw-r--r--fitz/obj_dict.c220
1 files changed, 54 insertions, 166 deletions
diff --git a/fitz/obj_dict.c b/fitz/obj_dict.c
index 2af2eca5..1742df92 100644
--- a/fitz/obj_dict.c
+++ b/fitz/obj_dict.c
@@ -19,16 +19,13 @@ static inline int keystrcmp(fz_obj *key, char *s)
return -1;
}
-fz_error
-fz_newdict(fz_obj **op, int initialcap)
+fz_obj *
+fz_newdict(int initialcap)
{
fz_obj *obj;
int i;
- obj = *op = fz_malloc(sizeof (fz_obj));
- if (!obj)
- return fz_rethrow(-1, "out of memory: dict struct");
-
+ obj = fz_malloc(sizeof (fz_obj));
obj->refs = 1;
obj->kind = FZ_DICT;
@@ -37,141 +34,29 @@ fz_newdict(fz_obj **op, int initialcap)
obj->u.d.cap = initialcap > 0 ? initialcap : 10;
obj->u.d.items = fz_malloc(sizeof(fz_keyval) * obj->u.d.cap);
- if (!obj->u.d.items)
- {
- fz_free(obj);
- return fz_rethrow(-1, "out of memory: dict item buffer");
- }
-
for (i = 0; i < obj->u.d.cap; i++)
{
obj->u.d.items[i].k = nil;
obj->u.d.items[i].v = nil;
}
- return fz_okay;
+ return obj;
}
-fz_error
-fz_copydict(fz_obj **op, fz_obj *obj)
-{
- fz_error error;
- fz_obj *new;
- int i;
-
- if (!fz_isdict(obj))
- return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
-
- error = fz_newdict(&new, obj->u.d.cap);
- if (error)
- return fz_rethrow(error, "cannot create new dict");
-
- for (i = 0; i < fz_dictlen(obj); i++)
- {
- error = fz_dictput(new, fz_dictgetkey(obj, i), fz_dictgetval(obj, i));
- if (error)
- {
- fz_dropobj(new);
- return fz_rethrow(error, "cannot copy dict entry");
- }
- }
-
- *op = new;
- return fz_okay;
-}
-
-fz_error
-fz_deepcopydict(fz_obj **op, fz_obj *obj)
+fz_obj *
+fz_copydict(fz_obj *obj)
{
- fz_error error;
fz_obj *new;
- fz_obj *val;
int i;
if (!fz_isdict(obj))
- return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
-
- error = fz_newdict(&new, obj->u.d.cap);
- if (error)
- return fz_rethrow(error, "cannot create new dict");
+ fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
+ new = fz_newdict(obj->u.d.cap);
for (i = 0; i < fz_dictlen(obj); i++)
- {
- val = fz_dictgetval(obj, i);
-
- if (fz_isarray(val))
- {
- error = fz_deepcopyarray(&val, val);
- if (error)
- {
- fz_dropobj(new);
- return fz_rethrow(error, "cannot deep copy item");
- }
- error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error)
- {
- fz_dropobj(val);
- fz_dropobj(new);
- return fz_rethrow(error, "cannot add copied dict entry");
- }
- fz_dropobj(val);
- }
-
- else if (fz_isdict(val))
- {
- error = fz_deepcopydict(&val, val);
- if (error)
- {
- fz_dropobj(new);
- return fz_rethrow(error, "cannot deep copy item");
- }
- error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error)
- {
- fz_dropobj(val);
- fz_dropobj(new);
- return fz_rethrow(error, "cannot add copied dict entry");
- }
- fz_dropobj(val);
- }
-
- else
- {
- error = fz_dictput(new, fz_dictgetkey(obj, i), val);
- if (error)
- {
- fz_dropobj(new);
- return fz_rethrow(error, "cannot copy dict entry");
- }
- }
- }
-
- *op = new;
- return fz_okay;
-}
-
-static fz_error
-growdict(fz_obj *obj)
-{
- fz_keyval *newitems;
- int newcap;
- int i;
-
- newcap = obj->u.d.cap * 2;
-
- newitems = fz_realloc(obj->u.d.items, sizeof(fz_keyval) * newcap);
- if (!newitems)
- return fz_rethrow(-1, "out of memory: resize item buffer");
-
- obj->u.d.items = newitems;
- for (i = obj->u.d.cap; i < newcap; i++)
- {
- obj->u.d.items[i].k = nil;
- obj->u.d.items[i].v = nil;
- }
- obj->u.d.cap = newcap;
+ fz_dictput(new, fz_dictgetkey(obj, i), fz_dictgetval(obj, i));
- return fz_okay;
+ return new;
}
int
@@ -211,7 +96,8 @@ fz_dictgetval(fz_obj *obj, int i)
return obj->u.d.items[i].v;
}
-static inline int dictfinds(fz_obj *obj, char *key)
+static inline int
+fz_dictfinds(fz_obj *obj, char *key)
{
if (obj->u.d.sorted)
{
@@ -251,7 +137,7 @@ fz_dictgets(fz_obj *obj, char *key)
if (!fz_isdict(obj))
return nil;
- i = dictfinds(obj, key);
+ i = fz_dictfinds(obj, key);
if (i >= 0)
return obj->u.d.items[i].v;
@@ -276,39 +162,52 @@ fz_dictgetsa(fz_obj *obj, char *key, char *abbrev)
return fz_dictgets(obj, abbrev);
}
-fz_error
+void
fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val)
{
- fz_error error;
char *s;
int i;
obj = fz_resolveindirect(obj);
if (!fz_isdict(obj))
- return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
+ {
+ fz_warn("assert: not a dict (%s)", fz_objkindstr(obj));
+ return;
+ }
if (fz_isname(key))
s = fz_toname(key);
else
- return fz_throw("assert: key is not a name (%s)", fz_objkindstr(obj));
+ {
+ fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj));
+ return;
+ }
if (!val)
- return fz_throw("assert: val does not exist for key (%s)", s);
+ {
+ fz_warn("assert: val does not exist for key (%s)", s);
+ return;
+ }
- i = dictfinds(obj, s);
+ i = fz_dictfinds(obj, s);
if (i >= 0)
{
fz_dropobj(obj->u.d.items[i].v);
obj->u.d.items[i].v = fz_keepobj(val);
- return fz_okay;
+ return;
}
if (obj->u.d.len + 1 > obj->u.d.cap)
{
- error = growdict(obj);
- if (error)
- return fz_rethrow(error, "cannot grow dict item buffer");
+ int newcap = obj->u.d.cap * 2;
+ obj->u.d.items = fz_realloc(obj->u.d.items, sizeof(fz_keyval) * newcap);
+ for (i = obj->u.d.cap; i < newcap; i++)
+ {
+ obj->u.d.items[i].k = nil;
+ obj->u.d.items[i].v = nil;
+ }
+ obj->u.d.cap = newcap;
}
/* borked! */
@@ -319,55 +218,44 @@ fz_dictput(fz_obj *obj, fz_obj *key, fz_obj *val)
obj->u.d.items[obj->u.d.len].k = fz_keepobj(key);
obj->u.d.items[obj->u.d.len].v = fz_keepobj(val);
obj->u.d.len ++;
-
- return fz_okay;
}
-fz_error
+void
fz_dictputs(fz_obj *obj, char *key, fz_obj *val)
{
- fz_error error;
- fz_obj *keyobj;
- error = fz_newname(&keyobj, key);
- if (error)
- return fz_rethrow(error, "cannot create dict key");
- error = fz_dictput(obj, keyobj, val);
+ fz_obj *keyobj = fz_newname(key);
+ fz_dictput(obj, keyobj, val);
fz_dropobj(keyobj);
- if (error)
- return fz_rethrow(error, "cannot insert dict entry");
- return fz_okay;
}
-fz_error
+void
fz_dictdels(fz_obj *obj, char *key)
{
- int i;
-
obj = fz_resolveindirect(obj);
if (!fz_isdict(obj))
- return fz_throw("assert: not a dict (%s)", fz_objkindstr(obj));
-
- i = dictfinds(obj, key);
- if (i >= 0)
+ fz_warn("assert: not a dict (%s)", fz_objkindstr(obj));
+ else
{
- fz_dropobj(obj->u.d.items[i].k);
- fz_dropobj(obj->u.d.items[i].v);
- obj->u.d.sorted = 0;
- obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1];
- obj->u.d.len --;
+ int i = fz_dictfinds(obj, key);
+ if (i >= 0)
+ {
+ fz_dropobj(obj->u.d.items[i].k);
+ fz_dropobj(obj->u.d.items[i].v);
+ obj->u.d.sorted = 0;
+ obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1];
+ obj->u.d.len --;
+ }
}
-
- return fz_okay;
}
-fz_error
+void
fz_dictdel(fz_obj *obj, fz_obj *key)
{
if (fz_isname(key))
- return fz_dictdels(obj, fz_toname(key));
+ fz_dictdels(obj, fz_toname(key));
else
- return fz_throw("assert: key is not a name (%s)", fz_objkindstr(obj));
+ fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj));
}
void