Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintAngle.cpp
index 10d27ee1c24069b400354b80b2b4404678d6f05b..cdb3309ecf47c08e50ea813d3c2e0ec5ce2730a6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -28,6 +28,8 @@
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Validator.h>
 
+#include <Locale_Convert.h>
+
 #include <GeomDataAPI_Point2D.h>
 
 #include <GeomAPI_Angle2d.h>
 #include <cmath>
 #include <regex>
 #include <sstream>
+#include <vector>
 
 const double tolerance = 1.e-7;
 #define PI 3.1415926535897932
 
+// To support old types of GCC (less than 4.9), check the regular expressions are working
+#if (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)  && \
+    (__cplusplus >= 201402L || !defined(__GLIBCXX__)   || \
+        (defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
+         defined(_GLIBCXX_REGEX_STATE_LIMIT)           || \
+         (defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE > 4)))
+#define HAVE_WORKING_REGEX 1
+#else
+#define HAVE_WORKING_REGEX 0
+#endif
+
+
 /// \brief Calculate intersection point of two lines
 static std::shared_ptr<GeomAPI_Pnt2d> intersect(FeaturePtr theLine1, FeaturePtr theLine2);
 
@@ -131,11 +146,6 @@ void SketchPlugin_ConstraintAngle::execute()
       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
   if(!aFlyOutAttr->isInitialized())
     compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
-
-  static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
-  std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage(
-      new ModelAPI_EventReentrantMessage(anId, this));
-  Events_Loop::loop()->send(aMessage);
 }
 
 AISObjectPtr SketchPlugin_ConstraintAngle::getAISObject(AISObjectPtr thePrevious)
@@ -313,6 +323,46 @@ double SketchPlugin_ConstraintAngle::getAngleForType(double theAngle,
   return anAngle;
 }
 
+#if !HAVE_WORKING_REGEX
+static bool parseString(const std::wstring& theString, std::wostringstream* theResult)
+{
+  // skip leading spaces
+  size_t aLength = theString.size();
+  size_t aPos = theString.find_first_not_of(L' ');
+  if (aPos == std::wstring::npos)
+    return false;
+  // first should be a value
+  if (theString[aPos] == L'-' || theString[aPos] == L'+')
+    theResult[1] << theString[aPos++];
+  while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
+    theResult[1] << theString[aPos++];
+  if (theString[aPos] != L' ') {
+    if (theString[aPos] != L'.')
+      return false;
+    theResult[1] << theString[aPos++];
+    while (aPos < aLength && theString[aPos] >= L'0' && theString[aPos] <= L'9')
+      theResult[1] << theString[aPos++];
+  }
+
+  // next, find the sign
+  aPos = theString.find_first_not_of(L' ', aPos);
+  if (aPos == std::wstring::npos)
+    return false;
+  if (theString[aPos] == L'-' || theString[aPos] == L'+')
+    theResult[2] << theString[aPos++];
+
+  // a variable should be at the end
+  aPos = theString.find_first_not_of(L' ', aPos);
+  if (aPos == std::wstring::npos)
+    return false;
+  if (theString[aPos] != L'(' || theString.back() != L')')
+    return false;
+  theResult[3] << theString.substr(aPos + 1, aLength - aPos - 2);
+
+  return true;
+}
+#endif
+
 // Convert angle value or a text expression from one angle type to another
 static void convertAngle(AttributeDoublePtr theAngle,
                          const int thePrevType, const int theNewType)
@@ -328,17 +378,25 @@ static void convertAngle(AttributeDoublePtr theAngle,
     }
     else {
       // process the parametric value
-      std::string anAngleText = theAngle->text();
-      std::regex anAngleRegex("\\s*([-+]?[0-9]*\\.?[0-9]*)\\s*([-+])\\s*\\((.*)\\)$",
-                              std::regex_constants::ECMAScript);
+      std::wstring anAngleText = theAngle->text();
+#if HAVE_WORKING_REGEX
+      std::wregex anAngleRegex(L"\\s*([-+]?[0-9]*\\.?[0-9]*)\\s*([-+])\\s*\\((.*)\\)$",
+                               std::regex_constants::ECMAScript);
+#endif
 
       double anAnglePrefix = 0.0;
-      static const char aSignPrefix[2] = { '-', '+' };
+      static const wchar_t aSignPrefix[2] = { L'-', L'+' };
       int aSignInd = 1;
 
-      std::smatch aResult;
+#if HAVE_WORKING_REGEX
+      std::wsmatch aResult;
       if (std::regex_search(anAngleText, aResult, anAngleRegex)) {
-        anAnglePrefix = std::atof(aResult[1].str().c_str());
+#else
+      // workaround to support old versions of GCC (less than 4.9)
+      std::wostringstream aResult[4];
+      if (parseString(anAngleText, aResult)) {
+#endif
+        anAnglePrefix = std::atof(Locale::Convert::toString(aResult[1].str()).c_str());
         aSignInd = aResult[2].str()[0] == aSignPrefix[0] ? 0 : 1;
         anAngleText = aResult[3].str();
       }
@@ -351,15 +409,15 @@ static void convertAngle(AttributeDoublePtr theAngle,
         aSignInd = 1 - aSignInd;
       anAnglePrefix = angleForType(anAnglePrefix, theNewType);
 
-      std::ostringstream aText;
+      std::wostringstream aText;
       bool isPrintSign = true;
       if (fabs(anAnglePrefix) > tolerance)
         aText << anAnglePrefix;
       else
         isPrintSign = aSignInd == 0;
       if (isPrintSign)
-        aText << " " << aSignPrefix[aSignInd] << " (";
-      aText << anAngleText << (isPrintSign ? ")" : "");
+        aText << L" " << aSignPrefix[aSignInd] << L" (";
+      aText << anAngleText << (isPrintSign ? L")" : L"");
       theAngle->setText(aText.str());
     }
   }