00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef XAPIAN_INCLUDED_SAFEUUID_H
00023 #define XAPIAN_INCLUDED_SAFEUUID_H
00024
00025 #if defined __CYGWIN__ || defined __WIN32__
00026 # include "common/win32_uuid.h"
00027 #elif defined HAVE_UUID_UUID_H
00028 # include <uuid/uuid.h>
00029
00030
00031
00032
00033
00034
00035
00036
00037 # ifndef uuid_parse
00038 # define uuid_parse(IN, UU) (uuid_parse)(const_cast<char*>(IN), (UU))
00039 # endif
00040
00041 # ifndef HAVE_UUID_UNPARSE_LOWER
00042
00043
00044
00045 inline void uuid_unparse_lower(uuid_t uu, char *out) {
00046 uuid_unparse(uu, out);
00047
00048
00049
00050 for (size_t i = 0; i < 36; ++i) out[i] |= 0x20;
00051 }
00052 # endif
00053
00054 #else
00055
00056
00057 # include <uuid.h>
00058 # include <cstring>
00059 # include <exception>
00060
00061 typedef unsigned char uuid_t_[16];
00062
00063 inline void
00064 uuid_generate(uuid_t_ out) {
00065 uuid_t uuid;
00066 uint32_t status;
00067 uuid_create(&uuid, &status);
00068 if (status != uuid_s_ok) {
00069
00070 throw std::bad_alloc();
00071 }
00072 std::memcpy(&out, &uuid, sizeof(uuid_t_));
00073 }
00074
00075 inline int
00076 uuid_parse(const char * in, uuid_t_ uu)
00077 {
00078 uuid_t uuid;
00079 uint32_t status;
00080 uuid_from_string(in, &uuid, &status);
00081 if (status != uuid_s_ok)
00082 return -1;
00083 std::memcpy(uu, &uuid, sizeof(uuid_t_));
00084 return 0;
00085 }
00086
00087 inline void
00088 uuid_unparse_lower(const uuid_t_ uu, char * out)
00089 {
00090 uuid_t uuid;
00091 uint32_t status;
00092 std::memcpy(&uuid, uu, sizeof(uuid_t));
00093 char * result;
00094 uuid_to_string(&uuid, &result, &status);
00095 std::memcpy(out, result, 36);
00096 free(result);
00097 if (status != uuid_s_ok) {
00098
00099 throw std::bad_alloc();
00100 }
00101
00102
00103
00104 for (size_t i = 0; i < 36; ++i) out[i] |= 0x20;
00105 }
00106
00107 inline void
00108 uuid_clear(uuid_t_ uu)
00109 {
00110 std::memset(uu, 0, sizeof(uuid_t_));
00111 }
00112
00113 inline int
00114 uuid_is_null(const uuid_t_ uu)
00115 {
00116 size_t i = 0;
00117 while (i < sizeof(uuid_t_)) {
00118 if (uu[i++])
00119 return 0;
00120 }
00121 return 1;
00122 }
00123
00124
00125 # define uuid_t uuid_t_
00126
00127 #endif
00128
00129 #endif // XAPIAN_INCLUDED_SAFEUUID_H