src/server/util/util.c

Sat, 14 Jan 2012 13:53:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 14 Jan 2012 13:53:44 +0100
changeset 14
b8bf95b39952
parent 1
src/server/util.c@3c066d52342d
child 24
1a7853a4257e
permissions
-rw-r--r--

New source folder layout

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 *
 * THE BSD LICENSE
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. 
 * 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. 
 *
 * Neither the name of the  nor the names of its contributors may be
 * used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * 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.
 */

/*
 * util.c: A hodge podge of utility functions and standard functions which 
 *         are unavailable on certain systems
 * 
 * Rob McCool
 */

#ifdef XP_UNIX
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "prthread.h"
#endif /* XP_UNIX */


#include "nspr.h"
#include "../public/nsapi.h"

#include "util.h"

/*
NSAPI_PUBLIC int util_getboolean(const char *v, int def) {
    if(v[0] == 'T' || v[0] == 't') {
        return 1;
    }
    if(v[0] == 'F' || v[0] == 'f') {
        return 0;
    }
    return def;
}
*/

NSAPI_PUBLIC int INTutil_getboolean(const char *v, int def) {
    if(v[0] == 'T' || v[0] == 't') {
        return 1;
    }
    if(v[0] == 'F' || v[0] == 'f') {
        return 0;
    }
    return def;
}


/* ------------------------------ util_itoa ------------------------------- */
/*
NSAPI_PUBLIC int util_itoa(int i, char *a)
{
    int len = util_i64toa(i, a);

    PR_ASSERT(len < UTIL_ITOA_SIZE);

    return len;
}
*/
NSAPI_PUBLIC int INTutil_itoa(int i, char *a) {
    return INTutil_i64toa(i, a);
}


/* ----------------------------- util_i64toa ------------------------------ */

/*
 * Assumption: Reversing the digits will be faster in the general case
 * than doing a log10 or some nasty trick to find the # of digits.
 */

NSAPI_PUBLIC int INTutil_i64toa(PRInt64 i, char *a)
{
    register int x, y, p;
    register char c;
    int negative;

    negative = 0;
    if(i < 0) {
        *a++ = '-';
        negative = 1;
        i = -i;
    }
    p = 0;
    while(i > 9) {
        a[p++] = (i%10) + '0';
        i /= 10;
    }
    a[p++] = i + '0';

    if(p > 1) {
        for(x = 0, y = p - 1; x < y; ++x, --y) {
            c = a[x];
            a[x] = a[y];
            a[y] = c;
        }
    }
    a[p] = '\0';

    //PR_ASSERT(p + negative < UTIL_I64TOA_SIZE);

    return p + negative;
}

mercurial