Salome HOME
Update SketchPlugin_Projection feature (issue #1459)
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
index 16df0a1fe6b5946f4940f14cf3cad44b350882dd..e0579464007a69089e5e9578d58dfcb40bb747bb 100644 (file)
@@ -1,8 +1,8 @@
 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 
-// File:        SketchPlugin_Validators.cpp
-// Created:     01 Aug 2014
-// Author:      Vitaly SMETANNIKOV
+// File:    ExchangePlugin_Validators.cpp
+// Created: Aug 01, 2014
+// Author:  Sergey BELASH
 
 #include <ExchangePlugin_Validators.h>
 
@@ -38,19 +38,26 @@ bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>&
 }
 
 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
-                                             const std::list<std::string>& theArguments) const
+                                             const std::list<std::string>& theArguments,
+                                             std::string& theError) const
 {
-  if (!theAttribute->isInitialized())
+  if (!theAttribute->isInitialized()) {
+    theError = "Is not initialized.";
     return false;
+  }
 
   const AttributeStringPtr aStrAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
-  if (!aStrAttr)
+  if (!aStrAttr) {
+    theError = "Is not a string attribute.";
     return false;
+  }
 
   std::string aFileName = aStrAttr->value();
-  if (aFileName.empty())
+  if (aFileName.empty()) {
+    theError = "File name is empty.";
     return false;
+  }
 
   std::list<std::string> aFormats;
   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
@@ -59,10 +66,13 @@ bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
   // Is file name ends with the format
   for (; itFormats != aFormats.end(); ++itFormats) {
-    size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
-    if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
-      return true;
+    if (aFileNameLen > (*itFormats).length()) {
+      size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
+      if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
+        return true;
+      }
     }
   }
+  theError = "File name does not end with any available format.";
   return false;
 }