Salome HOME
Bug #1596: Export does not work
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
index 5a62fea8f048dd260110e69fc81d83bc46547948..a0117d84ef3131ef50cdd11088287c60c8fb31cc 100644 (file)
@@ -1,10 +1,15 @@
 // 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>
+
+#include <ExchangePlugin_Tools.h>
+
+#include <Events_InfoMessage.h>
+
 #include <ModelAPI_Feature.h>
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Session.h>
@@ -12,7 +17,6 @@
 
 #include <list>
 #include <string>
-#include <sstream>
 #include <algorithm>
 
 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
@@ -22,35 +26,40 @@ bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>&
   bool result = true;
   for (; it != theArguments.end(); ++it) {
     std::string anArg = *it;
-    int aSepPos = anArg.find(":");
+    size_t aSepPos = anArg.find(":");
     if (aSepPos == std::string::npos) {
       result = false;
       continue;
     }
-    std::string aFormatList = anArg.substr(0, aSepPos);
-    std::transform(aFormatList.begin(), aFormatList.end(), aFormatList.begin(), toupper);
-    std::istringstream aStream(aFormatList);
-    std::string aFormat;
-    while (std::getline(aStream, aFormat, '|'))
-      outFormats.push_back(aFormat);
+    std::string aFormats = anArg.substr(0, aSepPos);
+    std::transform(aFormats.begin(), aFormats.end(), aFormats.begin(), toupper);
+    std::list<std::string> aFormatList = ExchangePlugin_Tools::split(aFormats, '|');
+    outFormats.insert(outFormats.end(), aFormatList.begin(), aFormatList.end());
   }
   return result;
 }
 
 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
-                                             const std::list<std::string>& theArguments) const
+                                             const std::list<std::string>& theArguments,
+                                             Events_InfoMessage& 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 +68,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;
 }