Back to index

lightning-sunbird  0.9+nobinonly
nsID.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Daniel Bratell <bratell@lysator.liu.se>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #include "nsID.h"
00039 #include "prprf.h"
00040 #include "prmem.h"
00041 
00042 static const char gIDFormat[] = 
00043   "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
00044 
00045 static const char gIDFormat2[] = 
00046   "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
00047 
00048 
00055 #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
00056     the_int_var = (the_int_var << 4) + the_char; \
00057     if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
00058     else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
00059     else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
00060     else return PR_FALSE
00061 
00062 
00070 #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
00071   do { PRInt32 _i=number_of_chars; \
00072   dest_variable = 0; \
00073   while(_i) { \
00074     ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
00075     char_pointer++; \
00076     _i--; \
00077   } } while(0)
00078 
00079 
00086  #define PARSE_HYPHEN(char_pointer)   if(*(char_pointer++) != '-') return PR_FALSE
00087     
00088 /* 
00089  * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
00090  * an nsID. It can also handle the old format without the { and }.
00091  */
00092 
00093 PRBool nsID::Parse(const char *aIDStr)
00094 {
00095   /* Optimized for speed */
00096   if(!aIDStr) {
00097     return PR_FALSE;
00098   }
00099 
00100   PRBool expectFormat1 = (aIDStr[0] == '{');
00101   if(expectFormat1) aIDStr++;
00102 
00103   PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
00104   PARSE_HYPHEN(aIDStr);
00105   PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
00106   PARSE_HYPHEN(aIDStr);
00107   PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
00108   PARSE_HYPHEN(aIDStr);
00109   int i;
00110   for(i=0; i<2; i++)
00111     PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
00112   PARSE_HYPHEN(aIDStr);
00113   while(i < 8) {
00114     PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
00115     i++;
00116   }
00117   
00118   return expectFormat1 ? *aIDStr == '}' : PR_TRUE;
00119 }
00120 
00121 /*
00122  * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
00123  * format. The string is allocated with PR_Malloc and should be freed by
00124  * the caller.
00125  */
00126 
00127 char *nsID::ToString() const 
00128 {
00129   char *res = (char*)PR_Malloc(39);    // use PR_Malloc if this is to be freed with nsCRT::free
00130 
00131   if (res != NULL) {
00132     PR_snprintf(res, 39, gIDFormat,
00133                 m0, (PRUint32) m1, (PRUint32) m2,
00134                 (PRUint32) m3[0], (PRUint32) m3[1], (PRUint32) m3[2],
00135                 (PRUint32) m3[3], (PRUint32) m3[4], (PRUint32) m3[5],
00136                 (PRUint32) m3[6], (PRUint32) m3[7]);
00137   }
00138   return res;
00139 }
00140