Skip to content
Browse files

mongo-utils: New function: mongo_util_oid_as_string().

  • Loading branch information...
1 parent 1b58547 commit 915ec904352c248fab0aec45b408e5d75d8b5c74 @algernon committed Jul 13, 2011
Showing with 60 additions and 0 deletions.
  1. +4 −0 NEWS
  2. +1 −0 src/libmongo-client.ver
  3. +17 −0 src/mongo-utils.c
  4. +11 −0 src/mongo-utils.h
  5. +1 −0 tests/Makefile.am
  6. +26 −0 tests/unit/mongo/utils/oid_as_string.c
View
4 NEWS
@@ -15,6 +15,10 @@ pre-allocated collections.
Another new helper function to aid in retrieving information about a
collection - whether it exists, and if so, with what parameters.
+** New function: mongo_util_oid_as_string()
+When one needs to print the value of an ObjectId in human-readable
+hexadecimal format, this new function is the one to turn to.
+
** New index options: background & sparse.
It is now possible to create sparse indexes, and create them in the
background, using two new option flags: MONGO_INDEX_BACKGROUND and
View
1 src/libmongo-client.ver
@@ -25,4 +25,5 @@ LMC_0.1.3 {
mongo_sync_gridfs_*;
mongo_sync_cmd_create;
mongo_sync_cmd_exists;
+ mongo_util_oid_as_string;
} LMC_0.1.2;
View
17 src/mongo-utils.c
@@ -19,6 +19,7 @@
*/
#include <glib.h>
+#include <glib/gprintf.h>
#include <sys/types.h>
#include <string.h>
@@ -87,6 +88,22 @@ mongo_util_oid_new (gint32 seq)
return mongo_util_oid_new_with_time (time (NULL), seq);
}
+gchar *
+mongo_util_oid_as_string (guint8 *oid)
+{
+ gchar *str;
+ gint j;
+
+ if (!oid)
+ return NULL;
+
+ str = g_new (gchar, 26);
+ for (j = 0; j < 12; j++)
+ g_sprintf (&str[j * 2], "%02x", oid[j]);
+ str[25] = 0;
+ return str;
+}
+
gboolean
mongo_util_parse_addr (const gchar *addr, gchar **host, gint *port)
{
View
11 src/mongo-utils.h
@@ -84,6 +84,17 @@ guint8 *mongo_util_oid_new (gint32 seq);
*/
guint8 *mongo_util_oid_new_with_time (gint32 time, gint32 seq);
+/** Convert an ObjectID to its string representation.
+ *
+ * Turns a binary ObjectID into a hexadecimal string.
+ *
+ * @param oid is the binary ObjectID.
+ *
+ * @returns A newly allocated string representation of the ObjectID,
+ * or NULL on error.
+ */
+gchar *mongo_util_oid_as_string (guint8 *oid);
+
/** Parse a HOST:IP pair.
*
* Given a HOST:IP pair, split it up into a host and a port. IPv6
View
1 tests/Makefile.am
@@ -66,6 +66,7 @@ mongo_utils_unit_tests = \
unit/mongo/utils/oid_init \
unit/mongo/utils/oid_new \
unit/mongo/utils/oid_new_with_time \
+ unit/mongo/utils/oid_as_string \
unit/mongo/utils/parse_addr
mongo_wire_unit_tests = \
View
26 tests/unit/mongo/utils/oid_as_string.c
@@ -0,0 +1,26 @@
+#include "test.h"
+#include "mongo.h"
+
+void
+test_mongo_utils_oid_as_string (void)
+{
+ guint8 *oid;
+ gchar *oid_str;
+
+ mongo_util_oid_init (0);
+
+ oid = mongo_util_oid_new (1);
+
+ ok (mongo_util_oid_as_string (NULL) == NULL,
+ "mongo_util_oid_as_string() should fail with a NULL oid");
+
+ oid_str = mongo_util_oid_as_string (oid);
+
+ ok (oid_str != NULL,
+ "mongo_util_oid_as_string() works");
+
+ g_free (oid_str);
+ g_free (oid);
+}
+
+RUN_TEST (2, mongo_utils_oid_as_string);

0 comments on commit 915ec90

Please sign in to comment.
Something went wrong with that request. Please try again.