From 9f1775334a8b9970841aa825c4c3d9383e25df52 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ale=C5=A1=20K=C5=99enek?= Date: Fri, 15 Jul 2005 11:12:12 +0000 Subject: [PATCH] better glite_jp_attr_union() --- org.glite.jp.common/src/attr.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/org.glite.jp.common/src/attr.c b/org.glite.jp.common/src/attr.c index 7599a63..3dfa1cd 100644 --- a/org.glite.jp.common/src/attr.c +++ b/org.glite.jp.common/src/attr.c @@ -43,19 +43,27 @@ void glite_jp_attr_union(const glite_jp_attr_t *a, const glite_jp_attr_t *b, int ac,bc,c,i,j; glite_jp_attr_t *res; - assert(a); assert(b); assert(out); - for (ac=0; a[ac].type; ac++); - for (bc=0; b[bc].type; bc++); + assert(out); + if (a) for (ac=0; a[ac].type; ac++); else ac=0; + if (b) for (bc=0; b[bc].type; bc++); else bc=0; + + if ((c = ac+bc) == 0) { + *out = NULL; + return; + } + res = malloc((ac+bc+1) * sizeof *res); memcpy(res,a,ac * sizeof *a); memcpy(res+ac,b,bc * sizeof *b); memset(res+ac+bc,0,sizeof *res); - c = ac+bc; qsort(res,c,sizeof *res,attr_cmp); for (i=0; i i+1) memmove(res+i+1,res+j,c-j); + if (j > i+1) { + memmove(res+i+1,res+j,c-j); + c -= j - (i+1); + } } for (i=0; res[i].type; i++) switch (res[i].type) { -- 1.8.2.3