OSSP CVS Repository

ossp - ossp-pkg/uuid/uuid++.cc 1.1
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/uuid/uuid++.cc 1.1
/*
**  OSSP uuid - Universally Unique Identifier
**  Copyright (c) 2004-2005 Ralf S. Engelschall <rse@engelschall.com>
**  Copyright (c) 2004-2005 The OSSP Project <http://www.ossp.org/>
**
**  This file is part of OSSP uuid, a library for the generation
**  of UUIDs which can found at http://www.ossp.org/pkg/lib/uuid/
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  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.
**
**  uuid++.cc: library C++ API implementation
*/

#include <string.h>
#include <stdarg.h>

#include "uuid++.hh"

using namespace std;

/*
**  OBJECT CONSTRUCTORS AND DESTRUCTOR
*/

/*  standard constructor */
uuid::uuid()
{
    if (uuid_create(&ctx) != UUID_RC_OK)
        throw "unable to create UUID context";
}

/*  standard destructor */
uuid::~uuid()
{
    uuid_destroy(ctx);
}

/*  construction: import of other C++ API object (= special C++ copy constructor) */
uuid::uuid(const uuid &obj)
{
    /* Notice: the copy constructor is the same as the assignment
       operator (with the object as the RHS) below, except that (1) no
       check for self-assignment is required, (2) no existing internals
       have to be destroyed and (3) no return value is given back. */
    if (uuid_clone(obj.ctx, &ctx) != UUID_RC_OK)
        throw "unable to clone UUID context";
}

/*  construction: import of other C API object */
uuid::uuid(uuid_t *obj)
{
    if (obj != NULL)
        ctx = obj;
    else {
        if (uuid_create(&ctx) != UUID_RC_OK)
            throw "unable to create UUID context";
    }
}

/*  construction: import of binary representation */
uuid::uuid(const unsigned char *bin)
{
    if (uuid_create(&ctx) != UUID_RC_OK)
        throw "unable to create UUID context";
    if (bin != NULL) {
        if (uuid_import(ctx, UUID_FMT_BIN, bin, UUID_LEN_BIN) != UUID_RC_OK)
            throw "unable to import UUID from binary representation";
    }
}

/*  construction: import of string representation */
uuid::uuid(const char *str)
{
    if (uuid_create(&ctx) != UUID_RC_OK)
        throw "unable to create UUID context";
    if (str != NULL) {
        if (uuid_import(ctx, UUID_FMT_STR, str, strlen(str)) != UUID_RC_OK)
            throw "unable to import UUID from string representation";
    }
}

/*
**  IMPORT METHODS
*/

/*  assignment: import of other C++ API object */
uuid &uuid::operator=(const uuid &rhs)
{
    if (this == &rhs)
        return *this;
    if (uuid_destroy(ctx) != UUID_RC_OK)
        throw "failed to destroy old UUID context";
    if (uuid_clone(rhs.ctx, &ctx) != UUID_RC_OK)
        throw "unable to clone UUID context";
    return *this;
}

/*  assignment: import of other C API object */
uuid &uuid::operator=(const uuid_t *rhs)
{
    if (rhs == NULL)
        throw "invalid RHS C object";
    if (uuid_clone((uuid_t *)rhs, &ctx) != UUID_RC_OK)
        throw "unable to clone UUID context";
    return *this;
}

/*  assignment: import of binary representation */
uuid &uuid::operator=(const unsigned char *rhs)
{
    if (rhs == NULL)
        throw "invalid RHS string";
    if (uuid_import(ctx, UUID_FMT_BIN, (const void *)rhs, UUID_LEN_BIN) != UUID_RC_OK)
        throw "unable to import into source UUID context";
    return *this;
}

/*  assignment: import of string representation */
uuid &uuid::operator=(const char *rhs)
{
    if (rhs == NULL)
        throw "invalid RHS string";
    if (uuid_import(ctx, UUID_FMT_STR, (const void *)rhs, strlen(rhs)) != UUID_RC_OK)
        throw "unable to import into source UUID context";
    return *this;
}

/*
**  GENERATION METHODS
*/

/*  generation: loading existing UUID by name */
void uuid::load(const char *name)
{
    if (name == NULL)
        throw "invalid UUID name";
    if (uuid_load(ctx, name) != UUID_RC_OK)
        throw "unable to load UUID by name";
    return;
}

/*  generation: making new UUID one from scratch */
void uuid::make(unsigned int mode, ...)
{
    uuid_rc_t rv;
    va_list ap;

    va_start(ap, mode);
    if ((mode & UUID_MAKE_V3) || (mode & UUID_MAKE_V5)) {
        const char *ns   = (const char *)va_arg(ap, char *);
        const char *name = (const char *)va_arg(ap, char *);
        if (ns == NULL || name == NULL)
            throw "invalid arguments";
        rv = uuid_make(ctx, mode, ns, name);
    }
    else
        rv = uuid_make(ctx, mode);
    va_end(ap);
    if (rv != UUID_RC_OK)
        throw "unable to make new UUID";
    return;
}

/*
**  COMPARISON METHODS
*/

/*  comparison: is-Nil-UUID */
int uuid::isnil(void)
{
    int rv;

    if (uuid_isnil(ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID for being Nil-UUID";
    return rv;
}

/*  comparison: equality */
int uuid::operator==(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv == 0);
}

/*  comparison: inequality */
int uuid::operator!=(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv != 0);
}

/*  comparison: lower-than */
int uuid::operator<(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv < 0);
}

/*  comparison: lower-than-or-equal */
int uuid::operator<=(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv <= 0);
}

/*  comparison: greater-than */
int uuid::operator>(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv > 0);
}

/*  comparison: greater-than-or-equal */
int uuid::operator>=(const uuid &rhs)
{
    int rv;

    if (uuid_compare(ctx, rhs.ctx, &rv) != UUID_RC_OK)
        throw "unable to compare UUID contexts";
    return (rv >= 0);
}

/*
**  EXPORT METHODS
*/

/*  export: object representation */
uuid_t *uuid::object(void)
{
    return ctx;
}
        
/*  export: binary representation */
unsigned char *uuid::binary(void)
{
    unsigned char *bin;

    bin = NULL;
    if (uuid_export(ctx, UUID_FMT_BIN, (void **)&bin, NULL) != UUID_RC_OK)
        throw "unable to export UUID context";
    return bin;
}

/*  export: string representation */
char *uuid::string(void)
{
    char *str;

    str = NULL;
    if (uuid_export(ctx, UUID_FMT_STR, (void **)&str, NULL) != UUID_RC_OK)
        throw "unable to export UUID context";
    return str;
}


CVSTrac 2.0.1