Version: 9.16.0
SMESH_File.hxx
Go to the documentation of this file.
1// Copyright (C) 2007-2026 CEA, EDF, 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
42{
43public:
44
45 SMESH_File(const std::string& name, bool openForReading=true);
46
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 // Writing 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
109private:
110
111 std::string _name;
112 long _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;
121 const char* _end;
122};
123
124#endif
#define SMESHUtils_EXPORT
Definition: SMESH_Utils.hxx:37
High level util for effective file reading and other file operations.
Definition: SMESH_File.hxx:42
std::string _error
Definition: SMESH_File.hxx:113
const char * _end
position after file end
Definition: SMESH_File.hxx:121
const char * _pos
current position
Definition: SMESH_File.hxx:120
const char * end() const
Definition: SMESH_File.hxx:77
std::string getName() const
Definition: SMESH_File.hxx:49
bool operator++()
Definition: SMESH_File.hxx:71
bool write(const T &value)
Definition: SMESH_File.hxx:102
bool eof() const
Definition: SMESH_File.hxx:75
const char * getPos() const
Definition: SMESH_File.hxx:79
const std::string & error() const
Definition: SMESH_File.hxx:51
long _size
file size
Definition: SMESH_File.hxx:112
int _file
Definition: SMESH_File.hxx:117
bool write(const T *values, size_t nbTValues)
Definition: SMESH_File.hxx:96
void * _map
Definition: SMESH_File.hxx:119
std::string _name
file name
Definition: SMESH_File.hxx:111
bool exists(const std::string &fileName)
Definition: MED_Factory.cxx:73