WvStreams
strtouuid.cc
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * XPLC - Cross-Platform Lightweight Components
00004  * Copyright (C) 2002-2003, Pierre Phaneuf
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00019  * USA
00020  *
00021  * As a special exception, you may use this file as part of a free
00022  * software library without restriction.  Specifically, if other files
00023  * instantiate templates or use macros or inline functions from this
00024  * file, or you compile this file and link it with other files to
00025  * produce an executable, this file does not by itself cause the
00026  * resulting executable to be covered by the GNU Lesser General Public
00027  * License.  This exception does not however invalidate any other
00028  * reasons why the executable file might be covered by the GNU Lesser
00029  * General Public License.
00030  */
00031 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <xplc/uuid.h>
00035 
00036 const UUID UuidFromString(const char* str) {
00037   UUID rv;
00038   char tmp[3];
00039   char* end;
00040   bool format1 = false;
00041   bool ok = false;
00042 
00043   do {
00044     if(*str == '{') {
00045       format1 = true;
00046       ++str;
00047     }
00048 
00049     rv.Data1 = strtoul(str, &end, 16);
00050     if(end != str + 8)
00051       break;
00052     str = end;
00053 
00054     if(*str != '-')
00055       break;
00056     ++str;
00057 
00058     rv.Data2 = static_cast<unsigned short>(strtoul(str, &end, 16));
00059     if(end != str + 4)
00060       break;
00061     str = end;
00062 
00063     if(*str != '-')
00064       break;
00065     ++str;
00066 
00067     rv.Data3 = static_cast<unsigned short>(strtoul(str, &end, 16));
00068     if(end != str + 4)
00069       break;
00070     str = end;
00071 
00072     if(*str != '-')
00073       break;
00074     ++str;
00075 
00076     tmp[2] = 0;
00077 
00078     strncpy(tmp, str, 2);
00079     rv.Data4[0] = static_cast<unsigned char>(strtoul(tmp, &end, 16));
00080     if(end != tmp + 2)
00081       break;
00082     str += 2;
00083 
00084     strncpy(tmp, str, 2);
00085     rv.Data4[1] = static_cast<unsigned char>(strtoul(tmp, &end, 16));
00086     if(end != tmp + 2)
00087       break;
00088     str += 2;
00089 
00090     if(*str != '-')
00091       break;
00092     ++str;
00093 
00094     for(int i = 2; i < 8; ++i) {
00095       strncpy(tmp, str, 2);
00096       rv.Data4[i] = static_cast<unsigned char>(strtoul(tmp, &end, 16));
00097       if(end != tmp + 2)
00098         break;
00099       str += 2;
00100     }
00101 
00102     if(format1) {
00103       if(*str != '}')
00104         break;
00105       ++str;
00106     }
00107 
00108     if(*str != 0)
00109       break;
00110 
00111     ok = true;
00112   } while(0);
00113 
00114   if(!ok)
00115     rv = UUID_null;
00116 
00117   return rv;
00118 }
00119