LCOV - code coverage report
Current view: top level - alma/ASDM - EntityId.cc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 20 32 62.5 %
Date: 2024-11-06 17:42:47 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /*
       2             :  * ALMA - Atacama Large Millimeter Array
       3             :  * (c) European Southern Observatory, 2002
       4             :  * (c) Associated Universities Inc., 2002
       5             :  * Copyright by ESO (in the framework of the ALMA collaboration),
       6             :  * Copyright by AUI (in the framework of the ALMA collaboration),
       7             :  * All rights reserved.
       8             :  * 
       9             :  * This library is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  * 
      14             :  * This library is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY, without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  * 
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with this library; if not, write to the Free Software
      21             :  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      22             :  * MA 02111-1307  USA
      23             :  *
      24             :  * File EntityId.cpp
      25             :  */
      26             : //#define _POSIX_C_SOURCE
      27             : 
      28             : #ifndef WITHOUT_BOOST
      29             : #include <boost/regex.hpp>
      30             : #else
      31             : #include <regex>
      32             : #endif
      33             : 
      34             : #include <alma/ASDM/EntityId.h>
      35             : #include <alma/ASDM/OutOfBoundsException.h>
      36             : #include <alma/ASDM/InvalidArgumentException.h>
      37             : using namespace std;
      38             : 
      39             : namespace asdm {
      40             : 
      41             :   static bool EntityIdVerbose = getenv("ASDM_DEBUG") != NULL;
      42             : 
      43           0 :   EntityId EntityId::getEntityId(StringTokenizer &t) {
      44             :     try {
      45           0 :       string value = t.nextToken();
      46           0 :       return EntityId (value);
      47           0 :     } catch (const OutOfBoundsException &err) {
      48           0 :       throw InvalidArgumentException("Unexpected end-of-string!");
      49           0 :     }
      50             :   }
      51             :   
      52             :   /**
      53             :    * Returns a null string if the string x contains a valid
      54             :    * entity-id.  Otherwise, the string contains the error message.
      55             :    */
      56       35079 :   string EntityId::validate(string x) {
      57             : 
      58       35079 :     if (EntityIdVerbose) 
      59           0 :       cout << "EntityId::validate. Validating '" << x << "'." << endl;   
      60             : 
      61             :     // Here we use the boost machinery for the regular expressions, assuming its thread safety.
      62             :     // std::regex (optionally used here) is as thread-safe in this context
      63             : 
      64             :     // We will test two possible syntaxes for the EntityId, since EVLA did not want to follow
      65             :     // the rules established by ALMA.
      66             :     //
      67             : #ifndef WITHOUT_BOOST
      68             :     boost::regex ALMAbasicUIDRegex("^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$");
      69             :     boost::regex EVLAbasicUIDRegex("^[uU][iI][dD]:///?evla/[0-9a-zA-Z]+/.+$");
      70             : 
      71             :     const char * theUID_p = x.c_str();
      72             :     boost::cmatch whatALMA, whatEVLA;
      73             :     string result = "";
      74             : 
      75             :     if (!(boost::regex_match(theUID_p, whatALMA, ALMAbasicUIDRegex) ||
      76             :           boost::regex_match(theUID_p, whatEVLA, EVLAbasicUIDRegex))
      77             :         )
      78             :       result  = "Invalid format for EntityId: " + x;
      79             : #else
      80       35079 :     std::regex ALMAbasicUIDRegex("^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$");
      81       35079 :     std::regex EVLAbasicUIDRegex("^[uU][iI][dD]:///?evla/[0-9a-zA-Z]+/.+$");
      82             : 
      83       35079 :     const char * theUID_p = x.c_str();
      84       35079 :     std::cmatch whatALMA, whatEVLA;
      85       35079 :     string result = "";
      86             : 
      87       35480 :     if (!(std::regex_match(theUID_p, whatALMA, ALMAbasicUIDRegex) ||
      88         401 :           std::regex_match(theUID_p, whatEVLA, EVLAbasicUIDRegex))
      89             :         )
      90           0 :       result  = "Invalid format for EntityId: " + x;
      91             : #endif
      92             :    
      93       35079 :     if (EntityIdVerbose) 
      94           0 :       cout << "EntityId::validate. Validation message is '" << result << "'. (An empty message is a good sign.)" << endl;
      95             :     
      96       70158 :     return result;
      97             : 
      98             :     /*
      99             :      * This is the previous version of the code which was using
     100             :      * the regex machinery contained in libc. I was not sure
     101             :      * of its thread safetiness...
     102             : 
     103             :      // Check the entityId for the correct format.
     104             :      // the old one      char * rexp = "^uid://X[a-fA-F0-9]\\+/X[a-fA-F0-9]\\+\\(/X[a-fA-F0-9]\\+\\)\\?$";
     105             :      char * rexp = "^[uU][iI][dD]://[0-9a-zA-Z]+(/[xX][0-9a-fA-F]+){2}(#\\w{1,}){0,}$";
     106             :           
     107             :      regex_t preg;
     108             :      regcomp(&preg, rexp, REG_NOSUB);
     109             :      if ( regexec(&preg, x.c_str(), 0, 0, 0) )
     110             :      result =  msg;
     111             :           
     112             :      regfree(&preg);
     113             :      return string("");
     114             :     */          
     115       35079 :   }
     116             : 
     117       23234 :   EntityId::EntityId(const string &id) {
     118       23234 :     string msg = validate(id);
     119       23234 :     if (msg.length() != 0)
     120           0 :       throw InvalidArgumentException(msg);
     121       23234 :     this->id = id;           
     122       23234 :   }
     123             : 
     124             : #ifndef WITHOUT_ACS
     125             :   EntityId::EntityId(asdmIDLTypes::IDLEntityId &x) {
     126             :     string tmp(x.value);
     127             :     string msg = validate(tmp);
     128             :     if (msg.length() != 0)
     129             :       throw InvalidArgumentException(msg);
     130             :     this->id = tmp;          
     131             :   }
     132             : #endif
     133             :         
     134          44 :   void EntityId::toBin(EndianOSStream& eoss) const {
     135          44 :     eoss.writeString(id);
     136          44 :   }
     137             :         
     138           0 :   EntityId EntityId::fromBin(EndianIStream& eis) {
     139           0 :     return EntityId(eis.readString());          
     140             :   }
     141             : } // End namespace asdm

Generated by: LCOV version 1.16