[Debtags-commits] [svn] r1977 - central-database/trunk/c-tdb
Erich Schubert
erich at costa.debian.org
Mon Oct 2 17:03:55 UTC 2006
Author: erich
Date: Mon Oct 2 17:03:55 2006
New Revision: 1977
Modified:
central-database/trunk/c-tdb/tagcloud-json.cc
Log:
add string escaping for json output
Modified: central-database/trunk/c-tdb/tagcloud-json.cc
==============================================================================
--- central-database/trunk/c-tdb/tagcloud-json.cc (original)
+++ central-database/trunk/c-tdb/tagcloud-json.cc Mon Oct 2 17:03:55 2006
@@ -9,6 +9,7 @@
#include <vector>
#include <cmath>
#include <algorithm>
+#include <string>
using namespace std;
@@ -16,6 +17,16 @@
#define MIN_RESULTS 25
+string escape(const string & s) {
+ string result(s);
+ string::size_type i = 0;
+ while ((i = result.find_first_of("\\\"", i)) < result.length()) {
+ result.insert(i, "\\");
+ i += 2;
+ }
+ return result;
+}
+
int main(int argc, char* argv[]) {
/* open databases */
PkgDB dbpkg(O_RDONLY);
@@ -98,8 +109,8 @@
} else {
cout << ",";
}
- //cout << "[\"" << ti.name() << "\"," << i->second << ",\"" << ti.desc() << "\"]";
- cout << "[\"" << ti.name() << "\"," << i->second << "]";
+ //cout << "[\"" << escape(ti.name()) << "\"," << i->second << ",\"" << escape(ti.desc()) << "\"]";
+ cout << "[\"" << escape(ti.name()) << "\"," << i->second << "]";
} catch (etdb<dbid,VocInfo>::KeyNotFoundError er) {
cerr << "Unknown voc id: " << i->first.id << endl;
}
@@ -116,7 +127,7 @@
} else {
cout << ",";
}
- cout << "\"" << ti.name() << "\"";
+ cout << "\"" << escape(ti.name()) << "\"";
} catch (etdb<dbid,VocInfo>::KeyNotFoundError er) {
cerr << "Unknown voc id: " << i->id << endl;
}
@@ -133,7 +144,7 @@
} else {
cout << ",";
}
- cout << "\"" << pi.name() << "\"";
+ cout << "[\"" << escape(pi.name()) << "\",\"" << escape(pi.desc()) << "\"]";
} catch (etdb<dbid,PkgInfo>::KeyNotFoundError er) {
cerr << "Unknown pkg id: " << i->id << endl;
}
More information about the Debtags-commits
mailing list