2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsID.h"
|
2009-03-25 06:33:03 -07:00
|
|
|
#include "prprf.h"
|
2009-03-25 06:19:08 -07:00
|
|
|
#include "nsMemory.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static const char gIDFormat[] =
|
|
|
|
"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
|
|
|
|
|
|
|
|
static const char gIDFormat2[] =
|
|
|
|
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiplies the_int_var with 16 (0x10) and adds the value of the
|
2011-10-17 07:59:28 -07:00
|
|
|
* hexadecimal digit the_char. If it fails it returns false from
|
2007-03-22 10:30:00 -07:00
|
|
|
* the function it's used in.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
|
|
|
|
the_int_var = (the_int_var << 4) + the_char; \
|
|
|
|
if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
|
|
|
|
else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
|
|
|
|
else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
|
2011-10-17 07:59:28 -07:00
|
|
|
else return false
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses number_of_chars characters from the char_pointer pointer and
|
|
|
|
* puts the number in the dest_variable. The pointer is moved to point
|
|
|
|
* at the first character after the parsed ones. If it fails it returns
|
2011-10-17 07:59:28 -07:00
|
|
|
* false from the function the macro is used in.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
|
|
|
|
do { PRInt32 _i=number_of_chars; \
|
|
|
|
dest_variable = 0; \
|
|
|
|
while(_i) { \
|
|
|
|
ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
|
|
|
|
char_pointer++; \
|
|
|
|
_i--; \
|
|
|
|
} } while(0)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a hyphen from the char_pointer string. If there is no hyphen there
|
2011-10-17 07:59:28 -07:00
|
|
|
* the function returns false from the function it's used in. The
|
2007-03-22 10:30:00 -07:00
|
|
|
* char_pointer is advanced one step.
|
|
|
|
*/
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
#define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return false
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
|
|
|
|
* an nsID. It can also handle the old format without the { and }.
|
|
|
|
*/
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsID::Parse(const char *aIDStr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* Optimized for speed */
|
|
|
|
if(!aIDStr) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool expectFormat1 = (aIDStr[0] == '{');
|
2007-03-22 10:30:00 -07:00
|
|
|
if(expectFormat1) aIDStr++;
|
|
|
|
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
int i;
|
|
|
|
for(i=0; i<2; i++)
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
while(i < 8) {
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return expectFormat1 ? *aIDStr == '}' : true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:49:18 -08:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
|
2009-03-25 06:19:08 -07:00
|
|
|
* format. The string is allocated with NS_Alloc and should be freed by
|
2007-03-22 10:30:00 -07:00
|
|
|
* the caller.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *nsID::ToString() const
|
|
|
|
{
|
2009-03-25 06:19:08 -07:00
|
|
|
char *res = (char*)NS_Alloc(NSID_LENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (res != NULL) {
|
2008-01-11 20:30:42 -08:00
|
|
|
PR_snprintf(res, NSID_LENGTH, gIDFormat,
|
2007-03-22 10:30:00 -07:00
|
|
|
m0, (PRUint32) m1, (PRUint32) m2,
|
|
|
|
(PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
|
|
|
|
(PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
|
|
|
|
(PRUint32) m3[6], (PRUint32) m3[7]);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:30:42 -08:00
|
|
|
void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const
|
|
|
|
{
|
|
|
|
PR_snprintf(dest, NSID_LENGTH, gIDFormat,
|
|
|
|
m0, (PRUint32) m1, (PRUint32) m2,
|
|
|
|
(PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
|
|
|
|
(PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
|
|
|
|
(PRUint32) m3[6], (PRUint32) m3[7]);
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:49:18 -08:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|