]> rtime.felk.cvut.cz Git - frescor/forb.git/commitdiff
Finished conversion of object references to and from string
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 23 Sep 2008 16:37:29 +0000 (18:37 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 23 Sep 2008 16:37:29 +0000 (18:37 +0200)
forb.c
forb.h
tests/object_to_string/obj2str.c
uuid.c
uuid.h

diff --git a/forb.c b/forb.c
index 5501cf99f504feaaa0233d9ec81d8fe19f6b7787..21e85d69cc684bb59b4875629b1230fa6fce7c77 100644 (file)
--- a/forb.c
+++ b/forb.c
@@ -136,10 +136,10 @@ forb_server_id_init(forb_server_id *server_id)
        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);
 }
 
 /** 
@@ -170,3 +170,38 @@ forb_object_to_string(const forb_object obj)
 
        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;
+}
diff --git a/forb.h b/forb.h
index 568ba561f4f998a0c74e49ed612d2a55144d0b91..34cff71fc3c82a798cc2f9b05df29c1d8fbaf26a 100644 (file)
--- a/forb.h
+++ b/forb.h
@@ -120,7 +120,7 @@ void
 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);
index c441ba2ce7fdcd86941b4c0a519934cb8360785e..aae694c6ef73e46c56258b115e96d27701675e54 100644 (file)
@@ -1,23 +1,39 @@
 #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;
 }
diff --git a/uuid.c b/uuid.c
index 18a8cb8caf08f112e2c95549522655781fba6f79..d482104fc7dc9619742e2ae3e12a677943284975 100644 (file)
--- a/uuid.c
+++ b/uuid.c
@@ -69,3 +69,41 @@ char *forb_uuid_to_string(char *dest, const forb_uuid_t *uuid, size_t n)
        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;
+}
+
diff --git a/uuid.h b/uuid.h
index bb7b9027aa25e529d8ea6a313305baec9b102f4f..aace76a285fda604b7838b68cafd524fa51cf2cc 100644 (file)
--- a/uuid.h
+++ b/uuid.h
@@ -17,5 +17,6 @@ static inline int forb_uuid_cmp(forb_uuid_t u1, forb_uuid_t u2)
 
 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