VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nsID.cpp@ 102345

Last change on this file since 102345 was 102345, checked in by vboxsync, 11 months ago

libs/xpcom: Replace remaining APIs from prprf.h with IPRT equivalents, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Daniel Bratell <bratell@lysator.liu.se>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38#include "nsID.h"
39#include "prmem.h"
40
41#include <iprt/string.h>
42
43static const char gIDFormat[] =
44 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
45
46static const char gIDFormat2[] =
47 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
48
49
50/**
51 * Multiplies the_int_var with 16 (0x10) and adds the value of the
52 * hexadecimal digit the_char. If it fails it returns PR_FALSE from
53 * the function it's used in.
54 */
55
56#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
57 the_int_var = (the_int_var << 4) + the_char; \
58 if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
59 else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
60 else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
61 else return PR_FALSE
62
63
64/**
65 * Parses number_of_chars characters from the char_pointer pointer and
66 * puts the number in the dest_variable. The pointer is moved to point
67 * at the first character after the parsed ones. If it fails it returns
68 * PR_FALSE from the function the macro is used in.
69 */
70
71#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
72 do { PRInt32 _i=number_of_chars; \
73 dest_variable = 0; \
74 while(_i) { \
75 ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
76 char_pointer++; \
77 _i--; \
78 } } while(0)
79
80
81/**
82 * Parses a hyphen from the char_pointer string. If there is no hyphen there
83 * the function returns PR_FALSE from the function it's used in. The
84 * char_pointer is advanced one step.
85 */
86
87 #define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return PR_FALSE
88
89/*
90 * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
91 * an nsID. It can also handle the old format without the { and }.
92 */
93
94NS_COM PRBool nsID::Parse(const char *aIDStr)
95{
96 /* Optimized for speed */
97 if(!aIDStr) {
98 return PR_FALSE;
99 }
100
101 PRBool expectFormat1 = (aIDStr[0] == '{');
102 if(expectFormat1) aIDStr++;
103
104 PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
105 PARSE_HYPHEN(aIDStr);
106 PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
107 PARSE_HYPHEN(aIDStr);
108 PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
109 PARSE_HYPHEN(aIDStr);
110 int i;
111 for(i=0; i<2; i++)
112 PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
113 PARSE_HYPHEN(aIDStr);
114 while(i < 8) {
115 PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
116 i++;
117 }
118
119 return expectFormat1 ? *aIDStr == '}' : PR_TRUE;
120}
121
122/*
123 * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
124 * format. The string is allocated with PR_Malloc and should be freed by
125 * the caller.
126 */
127
128NS_COM char *nsID::ToString() const
129{
130 char *res = (char*)PR_Malloc(39); // use PR_Malloc if this is to be freed with nsCRT::free
131
132 if (res != NULL) {
133 RTStrPrintf2(res, 39, gIDFormat,
134 m0, (PRUint32) m1, (PRUint32) m2,
135 (PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
136 (PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
137 (PRUint32) m3[6], (PRUint32) m3[7]);
138 }
139 return res;
140}
141
142#ifdef VBOX
143void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const
144{
145 RTStrPrintf2(dest, NSID_LENGTH, gIDFormat,
146 m0, (PRUint32) m1, (PRUint32) m2,
147 (PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
148 (PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
149 (PRUint32) m3[6], (PRUint32) m3[7]);
150}
151#endif
152
153
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette