e9f3c9b3b80c3552555a734268f3503da5ee8ffe
tdreszer
  Thu Mar 17 16:32:27 2011 -0700
Added support for turning slPairs list back into string
diff --git src/lib/common.c src/lib/common.c
index d3796a1..fede24b 100644
--- src/lib/common.c
+++ src/lib/common.c
@@ -982,30 +982,62 @@
     {
     char *val = strchr(name,'=');
     if (!val)
 	{
 	warn("missing equals sign in name=value pair: name=[%s] in string=[%s]\n", name, s);
 	return NULL;
 	}
     *val++ = 0;
     slPairAdd(&list, name, cloneString(val));
     }
 freez(&ss);
 slReverse(&list);
 return list;
 }
 
+char *slPairListToString(struct slPair *list)
+// Returns an allocated string of pairs in form of
+// name1=val1 name2=val2 ...
+// Will wrap vals in quotes if contain spaces: name3="val 3"
+{
+// Don't rely on dyString.  Do the accounting ourselves
+int count = 0;
+struct slPair *pair = list;
+for(;pair != NULL; pair = pair->next)
+    {
+    count += strlen(pair->name);
+    count += strlen((char *)(pair->val));
+    count += 2; // = and ' ' delimit
+    if (hasWhiteSpace((char *)(pair->val)))
+        count += 2; // " and "
+    }
+if (count == 0)
+    return NULL;
+char *str = needMem(count+5); // A bit of slop
+
+char *s = str;
+for(pair = list;pair != NULL; pair = pair->next)
+    {
+    if (hasWhiteSpace((char *)(pair->val)))
+        sprintf(s,"%s=\"%s\" ",pair->name,(char *)(pair->val));
+    else
+        sprintf(s,"%s=%s ",pair->name,(char *)(pair->val));
+    s += strlen(s);
+    }
+str[strlen(str) - 1] = '\0'; // For sweetness, remove the trailing space.
+return str;
+}
 
 int slPairCmpCase(const void *va, const void *vb)
 /* Compare two slPairs, ignore case. */
 {
 const struct slPair *a = *((struct slPair **)va);
 const struct slPair *b = *((struct slPair **)vb);
 return strcasecmp(a->name, b->name);
 }
 
 void slPairSortCase(struct slPair **pList)
 /* Sort slPair list, ignore case. */
 {
 slSort(pList, slPairCmpCase);
 }