about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2004-06-20T13·37+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2004-06-20T13·37+0000
commit85ae78176520fbba0420e5dc772d76144b564605 (patch)
tree17d0b75cf1d6cd00e5c9ce1e61c4ecbdf55b885a /src/libutil
parent23bb902d1f65831d74a33051fdb8c0230b7a3e37 (diff)
* Refactoring.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc45
-rw-r--r--src/libutil/util.hh5
2 files changed, 50 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 676404ecfca6..2cfada35450a 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -446,3 +446,48 @@ void _interrupted()
         throw Error("interrupted by the user");
     }
 }
+
+
+string packStrings(const Strings & strings)
+{
+    string d;
+    for (Strings::const_iterator i = strings.begin();
+         i != strings.end(); ++i)
+    {
+        unsigned int len = i->size();
+        d += len & 0xff;
+        d += (len >> 8) & 0xff;
+        d += (len >> 16) & 0xff;
+        d += (len >> 24) & 0xff;
+        d += *i;
+    }
+    return d;
+}
+
+    
+Strings unpackStrings(const string & s)
+{
+    Strings strings;
+    
+    string::const_iterator i = s.begin();
+    
+    while (i != s.end()) {
+
+        if (i + 4 > s.end())
+            throw Error(format("short db entry: `%1%'") % s);
+        
+        unsigned int len;
+        len = (unsigned char) *i++;
+        len |= ((unsigned char) *i++) << 8;
+        len |= ((unsigned char) *i++) << 16;
+        len |= ((unsigned char) *i++) << 24;
+        
+        if (i + len > s.end())
+            throw Error(format("short db entry: `%1%'") % s);
+
+        strings.push_back(string(i, i + len));
+        i += len;
+    }
+    
+    return strings;
+}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index dcd0bf766cc7..e808f4e1b50b 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -216,4 +216,9 @@ void inline checkInterrupt()
 }
 
 
+/* String packing / unpacking. */
+string packStrings(const Strings & strings);
+Strings unpackStrings(const string & s);
+
+
 #endif /* !__UTIL_H */