Simpler allocation of regex in CRL::Environment.

* Change: In CRL::Environment, regex_t are now pointers instead of values,
    this way the "in initialization" flag can be removed (maybe still too
    complicated).
This commit is contained in:
Jean-Paul Chaput 2020-08-11 14:46:31 +02:00
parent 0c6db7e8b9
commit fa15331793
2 changed files with 53 additions and 44 deletions

View File

@ -64,92 +64,110 @@ namespace CRL {
, _OUT_LO ("vst") , _OUT_LO ("vst")
, _OUT_PH ("ap") , _OUT_PH ("ap")
, _CATALOG ("CATAL") , _CATALOG ("CATAL")
, _inConstructor (true) , _POWER ("vdd")
, _GROUND ("vss")
, _CLOCK ("^ck$")
, _BLOCKAGE ("^blockage$")
, _pad ("^.*_px$")
, _LIBRARIES ()
, _PowerRegex (new regex_t)
, _GroundRegex (new regex_t)
, _ClockRegex (new regex_t)
, _BlockageRegex (new regex_t)
, _padRegex (new regex_t)
{ {
setPOWER ( "vdd" ); setPOWER ( "vdd" );
setGROUND ( "vss" ); setGROUND ( "vss" );
setCLOCK ( "^ck$" ); setCLOCK ( "^ck$" );
setBLOCKAGE ( "^obs$" ); setBLOCKAGE( "^blockage$" );
setPad ( "^.*_px$" ); setPad ( "^.*_px$" );
_LIBRARIES.append( ".", "working" ); _LIBRARIES.append( ".", "working" );
_inConstructor = false;
} }
Environment::~Environment () Environment::~Environment ()
{ {
regfree ( &_PowerRegex ); regfree( _PowerRegex );
regfree ( &_GroundRegex ); regfree( _GroundRegex );
regfree ( &_ClockRegex ); regfree( _ClockRegex );
regfree ( &_BlockageRegex ); regfree( _BlockageRegex );
regfree ( &_padRegex ); regfree( _padRegex );
delete _PowerRegex;
delete _GroundRegex;
delete _ClockRegex;
delete _BlockageRegex;
delete _padRegex;
} }
bool Environment::isPOWER ( const char* name ) const bool Environment::isPOWER ( const char* name ) const
{ {
return regexec ( &_PowerRegex, name, 0, NULL, 0 ) == 0; if (not _PowerRegex) return false;
return regexec( _PowerRegex, name, 0, NULL, 0 ) == 0;
} }
bool Environment::isGROUND ( const char* name ) const bool Environment::isGROUND ( const char* name ) const
{ {
return regexec ( &_GroundRegex, name, 0, NULL, 0 ) == 0; if (not _GroundRegex) return false;
return regexec ( _GroundRegex, name, 0, NULL, 0 ) == 0;
} }
bool Environment::isCLOCK ( const char* name ) const bool Environment::isCLOCK ( const char* name ) const
{ {
return regexec ( &_ClockRegex, name, 0, NULL, 0 ) == 0; if (not _ClockRegex) return false;
return regexec( _ClockRegex, name, 0, NULL, 0 ) == 0;
} }
bool Environment::isBLOCKAGE ( const char* name ) const bool Environment::isBLOCKAGE ( const char* name ) const
{ {
return regexec ( &_BlockageRegex, name, 0, NULL, 0 ) == 0; if (not _BlockageRegex) return false;
return regexec ( _BlockageRegex, name, 0, NULL, 0 ) == 0;
} }
bool Environment::isPad ( const char* name ) const bool Environment::isPad ( const char* name ) const
{ {
return regexec ( &_padRegex, name, 0, NULL, 0 ) == 0; if (not _padRegex) return false;
return regexec ( _padRegex, name, 0, NULL, 0 ) == 0;
} }
void Environment::setPOWER ( const char* value ) void Environment::setPOWER ( const char* value )
{ {
_POWER = value; _POWER = value;
_setRegex ( &_PowerRegex , _POWER , "Power" ); _setRegex ( _PowerRegex , _POWER , "Power" );
} }
void Environment::setGROUND ( const char* value ) void Environment::setGROUND ( const char* value )
{ {
_GROUND = value; _GROUND = value;
_setRegex ( &_GroundRegex , _GROUND , "Ground" ); _setRegex ( _GroundRegex , _GROUND , "Ground" );
} }
void Environment::setCLOCK ( const char* value ) void Environment::setCLOCK ( const char* value )
{ {
_CLOCK = value; _CLOCK = value;
_setRegex ( &_ClockRegex , _CLOCK , "Clock" ); _setRegex ( _ClockRegex , _CLOCK , "Clock" );
} }
void Environment::setBLOCKAGE ( const char* value ) void Environment::setBLOCKAGE ( const char* value )
{ {
_BLOCKAGE = value; _BLOCKAGE = value;
_setRegex ( &_BlockageRegex , _BLOCKAGE , "Blockage" ); _setRegex ( _BlockageRegex , _BLOCKAGE , "Blockage" );
} }
void Environment::setPad ( const char* value ) void Environment::setPad ( const char* value )
{ {
_pad = value; _pad = value;
_setRegex ( &_padRegex , _pad , "Pad" ); _setRegex ( _padRegex , _pad , "Pad" );
} }
@ -192,13 +210,11 @@ namespace CRL {
} }
void Environment::_setRegex ( regex_t* regex, const string& pattern, const char* name ) void Environment::_setRegex ( regex_t*& regex, const string& pattern, const char* name )
{ {
char regexError[1024]; char regexError[1024];
int regexCode; int regexCode;
if ( !_inConstructor ) regfree ( regex );
if ( (regexCode = regcomp(regex,getString(pattern).c_str(),REG_EXTENDED|REG_NOSUB)) ) { if ( (regexCode = regcomp(regex,getString(pattern).c_str(),REG_EXTENDED|REG_NOSUB)) ) {
regerror( regexCode, regex, regexError, 1024 ); regerror( regexCode, regex, regexError, 1024 );
throw Error ( badRegex, name, regexError ); throw Error ( badRegex, name, regexError );

View File

@ -14,9 +14,7 @@
// +-----------------------------------------------------------------+ // +-----------------------------------------------------------------+
#ifndef CRL_ENVIRONMENT_H #pragma once
#define CRL_ENVIRONMENT_H
#include <regex.h> #include <regex.h>
#include <string> #include <string>
#include "hurricane/Commons.h" #include "hurricane/Commons.h"
@ -92,26 +90,24 @@ namespace CRL {
std::string _CORIOLIS_TOP; std::string _CORIOLIS_TOP;
std::string _displayStyle; std::string _displayStyle;
long _SCALE_X; long _SCALE_X;
std::string _DISPLAY;
std::string _IN_LO; std::string _IN_LO;
std::string _IN_PH; std::string _IN_PH;
std::string _OUT_LO; std::string _OUT_LO;
std::string _OUT_PH; std::string _OUT_PH;
std::string _CATALOG;
std::string _POWER; std::string _POWER;
std::string _GROUND; std::string _GROUND;
std::string _CLOCK; std::string _CLOCK;
std::string _BLOCKAGE; std::string _BLOCKAGE;
std::string _pad; std::string _pad;
std::string _CATALOG;
SearchPath _LIBRARIES; SearchPath _LIBRARIES;
regex_t _PowerRegex; regex_t* _PowerRegex;
regex_t _GroundRegex; regex_t* _GroundRegex;
regex_t _ClockRegex; regex_t* _ClockRegex;
regex_t _BlockageRegex; regex_t* _BlockageRegex;
regex_t _padRegex; regex_t* _padRegex;
bool _inConstructor;
private: private:
void _setRegex ( regex_t* regex, const std::string& pattern, const char* name ); void _setRegex ( regex_t*& regex, const std::string& pattern, const char* name );
}; };
@ -158,6 +154,3 @@ namespace CRL {
INSPECTOR_P_SUPPORT(CRL::Environment); INSPECTOR_P_SUPPORT(CRL::Environment);
#endif // CRL_ENVIRONMENT_H