]> SALOME platform Git repositories - modules/shaper.git/blob - src/ExchangePlugin/ExchangePlugin_Validators.cpp
Salome HOME
Issue #529 : 4.07. Import IGES, export to BREP, STEP, IGES - Export IGES
[modules/shaper.git] / src / ExchangePlugin / ExchangePlugin_Validators.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketchPlugin_Validators.cpp
4 // Created:     01 Aug 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include <ExchangePlugin_Validators.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Object.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_AttributeString.h>
12
13 #include <list>
14 #include <string>
15 #include <sstream>
16 #include <algorithm>
17
18 bool ExchangePlugin_FormatValidator::parseFormats(const std::list<std::string>& theArguments,
19                                                   std::list<std::string>& outFormats)
20 {
21   std::list<std::string>::const_iterator it = theArguments.begin();
22   bool result = true;
23   for (; it != theArguments.end(); ++it) {
24     std::string anArg = *it;
25     int aSepPos = anArg.find(":");
26     if (aSepPos == std::string::npos) {
27       result = false;
28       continue;
29     }
30     std::string aFormatList = anArg.substr(0, aSepPos);
31     std::transform(aFormatList.begin(), aFormatList.end(), aFormatList.begin(), toupper);
32     std::istringstream aStream(aFormatList);
33     std::string aFormat;
34     while (std::getline(aStream, aFormat, '|'))
35       outFormats.push_back(aFormat);
36   }
37   return result;
38 }
39
40 bool ExchangePlugin_FormatValidator::isValid(const AttributePtr& theAttribute,
41                                              const std::list<std::string>& theArguments) const
42 {
43   if (!theAttribute->isInitialized())
44     return false;
45
46   const AttributeStringPtr aStrAttr =
47       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
48   if (!aStrAttr)
49     return false;
50
51   std::string aFileName = aStrAttr->value();
52   if (aFileName.empty())
53     return false;
54
55   std::list<std::string> aFormats;
56   ExchangePlugin_FormatValidator::parseFormats(theArguments, aFormats);
57   std::list<std::string>::const_iterator itFormats = aFormats.begin();
58   size_t aFileNameLen = aFileName.length();
59   std::transform(aFileName.begin(), aFileName.end(), aFileName.begin(), toupper);
60   // Is file name ends with the format
61   for (; itFormats != aFormats.end(); ++itFormats) {
62     size_t aFormatBeginPos = aFileNameLen - (*itFormats).length();
63     if (aFileName.compare(aFormatBeginPos, std::string::npos, *itFormats) == 0) {
64       return true;
65     }
66   }
67   return false;
68 }