* Refactoring.

This commit is contained in:
Eelco Dolstra 2004-06-20 13:37:51 +00:00
parent 23bb902d1f
commit 85ae781765
3 changed files with 52 additions and 41 deletions

View file

@ -337,32 +337,9 @@ bool Database::queryStrings(const Transaction & txn, TableId table,
const string & key, Strings & data) const string & key, Strings & data)
{ {
string d; string d;
if (!queryString(txn, table, key, d)) if (!queryString(txn, table, key, d))
return false; return false;
data = unpackStrings(d);
string::iterator it = d.begin();
while (it != d.end()) {
if (it + 4 > d.end())
throw Error(format("short db entry: `%1%'") % d);
unsigned int len;
len = (unsigned char) *it++;
len |= ((unsigned char) *it++) << 8;
len |= ((unsigned char) *it++) << 16;
len |= ((unsigned char) *it++) << 24;
if (it + len > d.end())
throw Error(format("short db entry: `%1%'") % d);
string s;
while (len--) s += *it++;
data.push_back(s);
}
return true; return true;
} }
@ -383,23 +360,7 @@ void Database::setString(const Transaction & txn, TableId table,
void Database::setStrings(const Transaction & txn, TableId table, void Database::setStrings(const Transaction & txn, TableId table,
const string & key, const Strings & data) const string & key, const Strings & data)
{ {
string d; setString(txn, table, key, packStrings(data));
for (Strings::const_iterator it = data.begin();
it != data.end(); it++)
{
string s = *it;
unsigned int len = s.size();
d += len & 0xff;
d += (len >> 8) & 0xff;
d += (len >> 16) & 0xff;
d += (len >> 24) & 0xff;
d += s;
}
setString(txn, table, key, d);
} }

View file

@ -446,3 +446,48 @@ void _interrupted()
throw Error("interrupted by the user"); 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;
}

View file

@ -216,4 +216,9 @@ void inline checkInterrupt()
} }
/* String packing / unpacking. */
string packStrings(const Strings & strings);
Strings unpackStrings(const string & s);
#endif /* !__UTIL_H */ #endif /* !__UTIL_H */