-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMFileHandlingVars.cpp
More file actions
executable file
·152 lines (128 loc) · 3.5 KB
/
MFileHandlingVars.cpp
File metadata and controls
executable file
·152 lines (128 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "MFileHandlingVars.h"
#include <stdio.h>
MFileHandlingVars::MFileHandlingVars()
{
f_inputfile = NULL;
f_logfile = NULL;
f_case = NULL;
f_geo = NULL;
f_sig = NULL;
f_dis = NULL;
f_vel = NULL;
f_acc = NULL;
f_eps = NULL;
f_rho = NULL;
f_egy = NULL;
f_snd = NULL;
f_mas = NULL;
f_bnd = NULL;
f_nbr = NULL;
f_tmp = NULL;
f_time = NULL;
}
MFileHandlingVars::~MFileHandlingVars()
{
// Close all open files
for (int i=sph_openfiles.size()-1; i>0; i--)
{
if (*sph_openfiles[i]!=NULL) fclose(*sph_openfiles[i]);
*sph_openfiles[i] = NULL;
sph_openfiles.pop_back();
}
}
bool MFileHandlingVars::FileExists(const std::string &filename)
{
FILE *file;
if ( (file = fopen(filename.c_str(), "r")) )
{
fclose(file);
return true;
}
return false;
}
int MFileHandlingVars::OpenInputFile()
{
// sph_openfiles[0] == input file
std::string filename = sph_filein + ".mcm";
// check that input file exists, if it does not then error termination
if ( !FileExists(filename) )
{
printf("\nError - input file %s does not exist.\n", filename.c_str());
return ERROR;
}
// Open input file
if ( (f_inputfile=fopen(filename.c_str(), "rt"))==NULL )
{
printf("\tError: Couldn't open input file.\n");
return ERROR;
}
sph_openfiles.push_back(&f_inputfile);
return OK;
}
int MFileHandlingVars::OpenLogFile()
{
// sph_openfiles[1] == log file
std::string filename = sph_filein + ".log";
if ( (f_logfile=fopen(filename.c_str(), "wt"))==NULL )
{
printf("\tError: Couldn't open log file.\n");
return ERROR;
}
sph_openfiles.push_back(&f_logfile);
return OK;
}
// Open all Ensight CASE files
int MFileHandlingVars::OpenCaseFiles(int status)
{
if (OpenOutputFile("_sig.ens",&f_sig, status)!=OK) return ERROR;
if (OpenOutputFile("_dis.ens",&f_dis, status)!=OK) return ERROR;
if (OpenOutputFile("_vel.ens",&f_vel, status)!=OK) return ERROR;
if (OpenOutputFile("_acc.ens",&f_acc, status)!=OK) return ERROR;
if (OpenOutputFile("_eps.ens",&f_eps, status)!=OK) return ERROR;
if (OpenOutputFile("_rho.ens",&f_rho, status)!=OK) return ERROR;
if (OpenOutputFile("_egy.ens",&f_egy, status)!=OK) return ERROR;
if (OpenOutputFile("_snd.ens",&f_snd, status)!=OK) return ERROR;
if (OpenOutputFile("_mas.ens",&f_mas, status)!=OK) return ERROR;
if (OpenOutputFile("_bnd.ens",&f_bnd, status)!=OK) return ERROR;
if (OpenOutputFile("_nbr.ens",&f_nbr, status)!=OK) return ERROR;
if (OpenOutputFile("_tmp.ens",&f_tmp, status)!=OK) return ERROR;
return OK;
}
// Status should be true for first time step, false otherwise
int MFileHandlingVars::OpenOutputFile(const std::string &ext, FILE **file, int status)
{
std::string filename;
std::string symbol;
filename = sph_filein + ext;
// To open new or to append to a file, new file at zero time step
if (status==0)
symbol="wt";
else
symbol="at";
if ( (*file=fopen(filename.c_str(), symbol.c_str()))==NULL )
{
printf("\tERROR: Couldn't open Ensight CASE file for writting.\n");
return ERROR;
}
sph_openfiles.push_back(file);
return OK;
}
int MFileHandlingVars::CloseLastOpenedFile()
{
int ilast=sph_openfiles.size()-1;
if (*sph_openfiles[ilast]!=NULL) fclose(*sph_openfiles[ilast]);
*sph_openfiles[ilast] = NULL;
sph_openfiles.pop_back();
return OK;
}
// Routine all files except input and log files 0 and 1
int MFileHandlingVars::CloseAllButInputAndLog()
{
for (int i=sph_openfiles.size()-1; i>1; i--)
{
if (*sph_openfiles[i]!=NULL) fclose(*sph_openfiles[i]);
*sph_openfiles[i] = NULL;
sph_openfiles.pop_back();
}
return OK;
}