forb_uuid_generate((forb_uuid_t*)server_id->uuid);
}
-forb_object
-forb_string_to_object(const char *str)
+forb_server_id *
+forb_server_id_from_string(forb_server_id *server_id, const char *string)
{
- return NULL;
+ return (forb_server_id *)forb_uuid_from_string((forb_uuid_t*)&server_id->uuid, string);
}
/**
return str;
}
+
+/**
+ * Creates object reference from string.
+ *
+ * The string should be generated by forb_object_to_string() in the
+ * same or another FORB. When the returned reference is not needed, it
+ * should be freed by forb_object_destrou().
+ *
+ * @param orb FORB which should handle the object.
+ * @param string The string returned by forb_object_to_string().
+ *
+ * @return FORB object reference or NULL in case of error.
+ */
+forb_object
+forb_string_to_object(const forb_orb orb, const char *string)
+{
+ forb_object obj;
+ forb_object_key key;
+ forb_server_id server_id;
+ int ret;
+
+ if (forb_server_id_from_string(&server_id, string) == NULL)
+ return NULL;
+
+ if (string[2*sizeof(server_id)] != '-')
+ return NULL;
+
+ ret = sscanf(&string[2*sizeof(server_id)+1], "%llx", &key);
+ if (ret == 0 || ret == EOF)
+ return NULL;
+
+ obj = forb_object_new(orb, &server_id, key);
+
+ return obj;
+}
forb_register_interface(const struct forb_interface *interface);
forb_object
-forb_string_to_object(const char *str);
+forb_string_to_object(const forb_orb orb, const char *string);
char *
forb_object_to_string(const forb_object obj);
#include <forb/forb.h>
#include <stdio.h>
#include "interface.h"
+#include <error.h>
struct forb_i_impl i_impl = { NULL };
int main()
{
- forb_object orb, obj;
- char *str;
+ forb_object orb, obj, obj2;
+ char *str, *str2;
orb = forb_init();
obj = forb_i_new(NULL, &i_impl, orb);
str = forb_object_to_string(obj);
if (str) {
- printf("Object reference: %s\n", str);
- forb_free(str);
+ printf("Object reference1: %s\n", str);
} else {
return 1;
}
+
+ obj2 = forb_string_to_object(orb, str);
+
+ str2 = forb_object_to_string(obj2);
+ if (str) {
+ printf("Object reference2: %s\n", str2);
+ } else {
+ return 1;
+ }
+
+ if (strcmp(str, str2) != 0)
+ error(1, 0, "Object references should be the same");
+
+ forb_free(str);
+ forb_free(str2);
+
return 0;
}
dest[2*i] = '\0';
return dest;
}
+
+static inline int hexval(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c-'0';
+ if (c >= 'a' && c <= 'f')
+ return 10+c-'a';
+ if (c >= 'A' && c <= 'F')
+ return 10+c-'A';
+ return -1;
+}
+
+/**
+ * Creates UUID from string form.
+ *
+ * @param dest Where to store the UUID.
+ * @param string UUID in the string form.
+ *
+ * @return @a dest in case of success, NULL in case of error.
+ */
+forb_uuid_t *forb_uuid_from_string(forb_uuid_t *dest, const char *string)
+{
+ int i;
+
+ for (i = 0; i<sizeof(dest->byte); i++) {
+ int a, b;
+ a = hexval(string[2*i]);
+ if (a < 0)
+ return NULL;
+ b = hexval(string[2*i+1]);
+ if (b < 0)
+ return NULL;
+
+ dest->byte[i] = (a << 4) + b;
+ }
+ return dest;
+}
+
forb_uuid_t *forb_uuid_generate(forb_uuid_t *dest);
char *forb_uuid_to_string(char *dest, const forb_uuid_t *uuid, size_t n);
+forb_uuid_t *forb_uuid_from_string(forb_uuid_t *dest, const char *string);
#endif