home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / src / nsID.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  1.8 KB  |  67 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include "nsID.h"
  21.  
  22. const static char gIDFormat[] = 
  23.   "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
  24.  
  25. /* 
  26.  * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
  27.  * an nsID
  28.  */
  29.  
  30. PRBool nsID::Parse(char *aIDStr) {
  31.   int count, n1, n2, n3[8];
  32.   long int n0;
  33.  
  34.   count = sscanf(aIDStr, gIDFormat,
  35.                  &n0, &n1, &n2, 
  36.                  &n3[0],&n3[1],&n3[2],&n3[3],
  37.                  &n3[4],&n3[5],&n3[6],&n3[7]);
  38.  
  39.   m0 = (PRInt32) n0;
  40.   m1 = (PRInt16) n1;
  41.   m2 = (PRInt16) n2;
  42.   for (int i = 0; i < 8; i++) {
  43.     m3[i] = (PRInt8) n3[i];
  44.   }
  45.  
  46.   return (PRBool) (count == 11);
  47. }
  48.  
  49. /*
  50.  * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
  51.  * format. Caller should delete [] the string.
  52.  */
  53.  
  54. char *nsID::ToString() const 
  55. {
  56.   char *res = new char[39];
  57.  
  58.   if (res != NULL) {
  59.     sprintf(res, gIDFormat,
  60.             (long int) m0, (int) m1, (int) m2,
  61.             (int) m3[0], (int) m3[1], (int) m3[2], (int) m3[3],
  62.             (int) m3[4], (int) m3[5], (int) m3[6], (int) m3[7]);
  63.   }
  64.   return res;
  65. }
  66.  
  67.