Salome HOME
Copyright update: 2016
[modules/smesh.git] / src / SMESHUtils / SMESH_File.hxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File      : SMESH_File.hxx
21 // Created   : Wed Mar 10 10:33:04 2010
22 // Author    : Edward AGAPOV (eap)
23 //
24 #ifndef __SMESH_File_HXX__
25 #define __SMESH_File_HXX__
26
27 #include "SMESH_Utils.hxx"
28
29 #include <string>
30 #include <vector>
31
32 #ifdef WIN32
33 #include <windows.h>
34 #else
35 #include <dlfcn.h>
36 #endif
37
38 /*!
39  * \brief High level util for effective file reading and other file operations
40  */
41 class SMESHUtils_EXPORT SMESH_File
42 {
43 public:
44
45   SMESH_File(const std::string& name, bool openForReading=true);
46
47   ~SMESH_File();
48
49   std::string getName() const { return _name; }
50
51   const std::string& error() const { return _error; }
52
53   void close();
54
55   bool remove();
56
57   long size();
58
59   bool exists();
60
61   bool isDirectory();
62
63   // ------------------------
64   // Access to file contents
65   // ------------------------
66
67   bool open(); // for reading
68
69   operator const char*() const { return _pos; }
70
71   bool operator++() { return ++_pos < _end; }
72
73   void operator +=(int posDelta) { _pos+=posDelta; }
74
75   bool eof() const { return _pos >= _end; }
76
77   const char* end() const { return _end; }
78
79   const char* getPos() const { return _pos; }
80
81   void setPos(const char* pos);
82
83   std::string getLine();
84
85   void rewind();
86
87   bool getInts(std::vector<int>& ids);
88
89   // ------------------------
90   // Writting a binary file
91   // ------------------------
92
93   bool openForWriting(); // binary writing only
94
95   template <typename T>
96     bool write( const T* values, size_t nbTValues )
97   {
98     return writeRaw((const void*) values, nbTValues * sizeof(T));
99   }
100
101   template <typename T>
102     bool write( const T& value )
103   {
104     return writeRaw((const void*) & value, sizeof(T));
105   }
106
107   bool writeRaw(const void* data, size_t size);
108
109 private:
110
111   std::string _name; //!< file name
112   int         _size; //!< file size
113   std::string _error;
114 #ifdef WIN32
115   HANDLE      _file, _mapObj;
116 #else
117   int         _file;
118 #endif
119   void*       _map;
120   const char* _pos; //!< current position
121   const char* _end; //!< position after file end
122 };
123
124 #endif