SaveLoad.cpp 4.53 KB
Newer Older
1
2
3
#include <fstream>
#include <complex>

4
5
6
#include "SaveLoad.hpp"
#include "ctools.hpp"
#include "Parallel.hpp"
7
8
9
10
11
12
13
14
15


#ifdef BUILD_IA

#include "IACInterval.hpp"

#endif

bool file_exists(const char *name) {
16
17
18
  std::ifstream file(name);
  if (!file) return false;
  return true;
19
20
21
}

void SaveLoad::open(const char *name, const char *mode, int i) {
22
23
24
25
26
27
28
29
  char namebuffer[128];
  pNumberName(name, namebuffer, i);
  if (mode[0] == 'w') {
    if (file_exists(namebuffer)) {
      char old[128];
      if (rename(namebuffer, pNumberOldName(name, old, i))) {
        Exit("Error: Cannot copy files");
      }
30
    }
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  }
  file = fopen(pNumberName(name, namebuffer, i), mode);
  if (PPM->Or(!file)) Exit("Error: Cannot open files")

  if (mode[0] == 'w') {
    xdrstdio_create(&xdrs, file, XDR_ENCODE);
    int procs = PPM->size();
    xdr_int(xdr(), &procs);
  }
  if (mode[0] == 'r') {
    xdrstdio_create(&xdrs, file, XDR_DECODE);
    int procs;
    xdr_int(xdr(), &procs);
    if (procs != int(PPM->size())) {
      WarningOnProc("Error: Number of procs does not fit")
46
    }
47
  }
48
49
50
}

void SaveLoad::openOnProc(int proc, const char *name, const char *mode, int i) {
51
52
53
54
55
56
57
58
59
60
  if (PPM->proc() != proc) return;

  char namebuffer[128];
  NumberName(name, namebuffer, i);
  if (mode[0] == 'w') {
    if (file_exists(namebuffer)) {
      char old[128];
      if (rename(namebuffer, NumberOldName(name, old, i))) {
        Exit("Error: Cannot copy files");
      }
61
    }
62
63
64
65
66
67
68
69
70
71
72
73
74
  }
  file = fopen(NumberName(name, namebuffer, i), mode);

  if (mode[0] == 'w') {
    xdrstdio_create(&xdrs, file, XDR_ENCODE);
    xdr_int(xdr(), &proc);
  }
  if (mode[0] == 'r') {
    xdrstdio_create(&xdrs, file, XDR_DECODE);
    int p;
    xdr_int(xdr(), &p);
    if (p != proc) {
      WarningOnProc("Error: Number of proc does not fit")
75
    }
76
  }
77
78
79
}

void SaveLoad::close() {
80
81
82
  if (!file) return;
  fclose(file);
  file = nullptr;
83
84
}

85
86
87
88
89
90
91
92
93
94
bool SaveLoad::FileExists(const char *name, int i) {
  char namebuffer[128];
  pNumberName(name, namebuffer, i);
  return PPM->And(file_exists(namebuffer));
}

bool SaveLoad::PFileExists(const char *name) {
  return FileExists(name, PPM->size());
}

95
Saver &Saver::operator<<(bool i) {
96
97
98
  bool_t tmp = i;
  xdr_bool(xdr(), &tmp);
  return *this;
99
100
101
}

Saver &Saver::operator<<(short int i) {
102
103
  xdr_short(xdr(), &i);
  return *this;
104
105
106
}

Saver &Saver::operator<<(int i) {
107
108
  xdr_int(xdr(), &i);
  return *this;
109
110
111
}

Saver &Saver::operator<<(size_t i) {
112
113
114
  int j = int(i); // CONVERSION!
  xdr_int(xdr(), &j);
  return *this;
115
116
117
}

Saver &Saver::operator<<(double a) {
118
119
  xdr_double(xdr(), &a);
  return *this;
120
121
122
}

Saver &Saver::operator<<(char a) {
123
124
  xdr_char(xdr(), &a);
  return *this;
125
126
127
}

Saver &Saver::operator<<(std::complex<int> z) {
128
129
130
131
132
  int re = std::real(z);
  int im = std::imag(z);
  xdr_int(xdr(), &re);
  xdr_int(xdr(), &im);
  return *this;
133
134
135
}

Saver &Saver::operator<<(std::complex<double> z) {
136
137
138
139
140
  double re = std::real(z);
  double im = std::imag(z);
  xdr_double(xdr(), &re);
  xdr_double(xdr(), &im);
  return *this;
141
142
143
144
145
}

#ifdef BUILD_IA

Saver &Saver::operator<<(IAInterval a) {
146
  return *this << inf(a) << sup(a);
147
148
149
}

Saver &Saver::operator<<(IACInterval a) {
150
  return *this << real(a) << imag(a);
151
152
153
154
155
156
157
}

#endif

PSaver::PSaver(const char *name) : Saver(name, PPM->size()) {}

MSaver::MSaver(const char *name) {
158
  SaveLoad::openOnProc(0, name, "w");
159
160
161
}

bool Loader::files_exist(const char *name) {
162
163
164
  char namebuffer[128];
  pNumberName(name, namebuffer, PPM->size());
  return PPM->And(file_exists(namebuffer));
165
166
167
}

Loader &Loader::operator>>(bool &i) {
168
169
170
171
  bool_t tmp;
  xdr_bool(xdr(), &tmp);
  i = tmp;
  return *this;
172
173
174
}

Loader &Loader::operator>>(short int &i) {
175
176
  xdr_short(xdr(), &i);
  return *this;
177
178
179
}

Loader &Loader::operator>>(int &i) {
180
181
  xdr_int(xdr(), &i);
  return *this;
182
183
184
}

Loader &Loader::operator>>(size_t &i) {
185
186
187
188
  int j;
  xdr_int(xdr(), &j);
  i = size_t(j);
  return *this;
189
190
191
}

Loader &Loader::operator>>(double &a) {
192
193
  xdr_double(xdr(), &a);
  return *this;
194
195
196
}

Loader &Loader::operator>>(char &a) {
197
198
  xdr_char(xdr(), &a);
  return *this;
199
200
201
}

Loader &Loader::operator>>(std::complex<double> &z) {
202
203
204
205
206
  double re, im;
  xdr_double(xdr(), &re);
  xdr_double(xdr(), &im);
  z = std::complex<double>(re, im);
  return *this;
207
208
209
210
211
}

#ifdef BUILD_IA

Loader &Loader::operator>>(IAInterval &a) {
212
213
214
215
  double inf, sup;
  *this >> inf >> sup;
  a = IAInterval(inf, sup);
  return *this;
216
217
218
}

Loader &Loader::operator>>(IACInterval &a) {
219
220
221
222
  IAInterval re, im;
  *this >> re >> im;
  a = IACInterval(re, im);
  return *this;
223
224
225
226
227
228
229
}

#endif

PLoader::PLoader(const char *name) : Loader(name, PPM->size()) {}

MLoader::MLoader(const char *name) {
230
  SaveLoad::openOnProc(0, name, "r");
231
}