Skip to content

Instantly share code, notes, and snippets.

@MagnificentPako
Last active April 24, 2020 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnificentPako/a7b5281fd0a4920207f92bdec5143911 to your computer and use it in GitHub Desktop.
Save MagnificentPako/a7b5281fd0a4920207f92bdec5143911 to your computer and use it in GitHub Desktop.
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2012-2018, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#define uuid_hash freebsd_uuid_hash
#include "/usr/include/uuid.h"
#undef uuid_hash
#include <SWI-Prolog.h>
#include <SWI-Stream.h>
#include <assert.h>
/* Seems to be defined in some MinGW installations. The
* ossp-uuid header defines the types using typedef, so
* we can safely kill these macros
*/
#undef UUID
#undef uuid_t
static atom_t ATOM_version;
static atom_t ATOM_format;
static atom_t ATOM_atom;
static atom_t ATOM_integer;
static atom_t ATOM_url;
static atom_t ATOM_dns;
static atom_t ATOM_oid;
static atom_t ATOM_x500;
static foreign_t
pl_uuid(term_t UUID, term_t options)
{ unsigned int mode = 1;
atom_t format = ATOM_atom;
uuid_t *uuid;
char *ns = NULL;
char *str = NULL;
int rc;
int32_t urc;
uint32_t status = uuid_s_ok;
if ( !PL_get_nil(options) )
{ term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
term_t arg = PL_new_term_ref();
while( PL_get_list(tail, head, tail) )
{ atom_t name;
size_t arity;
if ( !PL_get_name_arity(head, &name, &arity) || arity != 1 )
return PL_type_error("option", head);
_PL_get_arg(1, head, arg);
if ( name == ATOM_version )
{ int v;
if ( !PL_get_integer_ex(arg, &v) )
return FALSE;
switch(v)
{ case 1: mode = 1; break;
case 2: mode = 2; break;
case 3: mode = 3; break;
case 4: mode = 4; break;
case 5: mode = 5; break;
default: return PL_domain_error("uuid_version", arg);
}
} else if ( name == ATOM_format )
{ if ( !PL_get_atom_ex(arg, &format) )
return FALSE;
if ( format != ATOM_atom && format != ATOM_integer )
return PL_domain_error("uuid_format", arg);
} else
{ char *newns = NULL;
if ( name == ATOM_dns )
{ newns = "ns:DNS";
} else if ( name == ATOM_url )
{ newns = "ns:URL";
} else if ( name == ATOM_oid )
{ newns = "ns:OID";
} else if ( name == ATOM_x500 )
{ newns = "ns:X500";
}
if ( newns )
{ ns = newns;
if ( !PL_get_chars(arg, &str, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( mode == 1 )
mode = 3;
}
}
}
if ( !PL_get_nil_ex(tail) )
return FALSE;
}
switch(mode)
{ case 1:
case 2:
case 3:
case 4:
case 5:
uuid_create(&uuid, &status);
if ( status != uuid_s_ok )
return PL_warning("UUID: make: \n");
break;
default:
assert(0);
return FALSE;
}
if ( format == ATOM_atom )
{ char buf[36+1];
void *ptr = buf;
size_t datalen = sizeof(buf);
uint32_t status;
uuid_to_string(&uuid, &ptr, &status);
rc = PL_unify_atom_chars(UUID, ptr);
} else if ( format == ATOM_integer )
{
return FALSE;
} else
{ assert(0);
return FALSE;
}
return rc;
}
install_t
install_uuid(void)
{ ATOM_version = PL_new_atom("version");
ATOM_format = PL_new_atom("format");
ATOM_atom = PL_new_atom("atom");
ATOM_integer = PL_new_atom("integer");
ATOM_dns = PL_new_atom("dns");
ATOM_url = PL_new_atom("url");
ATOM_oid = PL_new_atom("oid");
ATOM_x500 = PL_new_atom("x500");
PL_register_foreign("uuid", 2, pl_uuid, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment