Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileselect.cpp
Go to the documentation of this file.
1 #include "fileselect.h"
2 
3 datafile * Fileselect::detectFiletype(ifstream &infile, ofstream &outfile, bool loud, string infile_name, int dim,
4  std::vector< std::vector<int> > rFieldMappings, std::vector<index_order> rIndexOrder,
5  std::vector<adj_map> conv_factor, std::vector<adj_map> norm_val){
6 
7  datafile *ret = NULL;
8 
9  //Test if file is a tecplot file
10  if( testTecplot(infile, string("BLOCK")) ){
11  ret = new tecplot_data(infile, outfile, loud, infile_name, dim, rFieldMappings, rIndexOrder, conv_factor, norm_val);
12  }
13  //Filetype not supported, output error message
14  else{
15  outfile << "ERROR | File \"" << infile_name << "\": ";
16  outfile << "Unsupported file format" << "\n";
17 
18  cout << "ERROR | File \"" << infile_name << "\": ";
19  cout << "Unsupported file format" << "\n";
20 
21  ret = NULL;
22  }
23 
24  return ret;
25 
26 }
27 
28 bool Fileselect::testTecplot(ifstream &infile, string datapacking){
29 
30  //Seek to beginning
31  infile.clear();
32  infile.seekg(0, std::ios::beg);
33 
34  char line[BUFFER_SIZE];
35  string fdstr("");
36 
37  bool zone_found = false;
38  bool zonetype_found = false;
39  bool datapacking_found = false;
40 
41  bool ordered = false;
42 
43  bool ijk[3] = {false, false, false};
44  bool nodes_elements[2] = {false, false};
45 
46  //Loop through file, check lines for required variable declarations
47  while(true){
48 
49  infile.getline(line, BUFFER_SIZE-1);
50  fdstr = line;
51 
52 
53 
54  //
55  //Strip beginning whitespace
56  //
57 
58  int offset = 0;
59 
60  while(fdstr.c_str()[offset] == ' ' && offset < fdstr.length())
61  offset++;
62 
63  if(offset > 0){
64 
65  char tmp[fdstr.length()-offset];
66  memset(tmp, '\0', fdstr.length()-offset);
67 
68  for(int i=0; i<fdstr.length(); i++)
69  tmp[i] = fdstr.c_str()[i+offset];
70 
71  fdstr = "";
72  fdstr = tmp;
73 
74  }
75 
76 
77 
78  //
79  //Check header
80  //
81 
82  if(fdstr.find("ZONE") != string::npos){
83  zone_found = true;
84  }
85 
86  //Try to read file's I value
87  if(fdstr.find("I=") != string::npos){
88  std::vector<string> i_val = readCommaExpression(fdstr, string("I"));
89 
90  if(i_val.size() == 1){
91  if( !check_non_numeric_str(string(i_val[0])) ){
92  ijk[0] = true;
93  }
94  }
95  }
96 
97  //Try to read file's J value
98  if(fdstr.find("J=") != string::npos){
99  std::vector<string> j_val = readCommaExpression(fdstr, string("J"));
100 
101  if(j_val.size() == 1){
102  if( !check_non_numeric_str(string(j_val[0])) ){
103  ijk[1] = true;
104  }
105  }
106  }
107 
108  //Try to read file's K value
109  if(fdstr.find("K=") != string::npos){
110  std::vector<string> k_val = readCommaExpression(fdstr, string("K"));
111 
112  if(k_val.size() == 1){
113  if( !check_non_numeric_str(string(k_val[0])) ){
114  ijk[2] = true;
115  }
116  }
117  }
118 
119  //Look for a Nodes expression
120  if(fdstr.find("Nodes=") != string::npos){
121  std::vector<string> node_val = readCommaExpression(fdstr, string("Nodes"));
122 
123  if(node_val.size() == 1){
124  if(atoi(node_val[0].c_str()) >= 0){
125  nodes_elements[0] = true;
126  }
127  }
128  }
129 
130  //Look for an Elements expression
131  if(fdstr.find("Elements=") != string::npos){
132  std::vector<string> elem_val = readCommaExpression(fdstr, string("Elements"));
133 
134  if(elem_val.size() == 1){
135  if(atoi(elem_val[0].c_str()) >= 0){
136  nodes_elements[1] = true;
137  }
138  }
139  }
140 
141 
142  //TODO
143  //Integrate this with zone class code
144  if(fdstr.find("ZONETYPE=Ordered") != string::npos){
145  zonetype_found = true;
146  ordered = true;
147  }
148  else if(fdstr.find("ZONETYPE=FEQuadrilateral") != string::npos){
149  zonetype_found = true;
150  ordered = false;
151  }
152  //--------
153  //
154 
155  if(fdstr.find("DATAPACKING=") != string::npos){
156 
157  char type[256];
158  char space[256];
159  sscanf(fdstr.c_str(), "DATAPACKING=%255s", type);
160 
161  if(string(type).compare(datapacking) == 0)
162  datapacking_found = true;
163  }
164 
165 
166 
167  //
168  //Loop exit conditions
169  //
170 
171  //Check detected data for valid file
172  if(zone_found && zonetype_found && datapacking_found){
173  if(ordered && check_bool_arr(ijk, 3))
174  return true;
175  else if(!ordered && check_bool_arr(nodes_elements, 2))
176  return true;
177  }
178 
179  //If at end of file, and not all required values have been found
180  //return false
181  if(infile.eof()){
182  return false;
183  }
184 
185  }
186 
187  return false;
188 }
189 
190 /*
191  * Print list of valid filetypes to stdout
192  */
194  cout << "\t" << "Tecplot ASCII Block Datafile - Ordered Data" << endl;
195  cout << "\t" << "Tecplot ASCII Block Datafile - FEQuadrilateral" << endl;
196 }
static datafile * detectFiletype(ifstream &infile, ofstream &outfile, bool loud, string infile_name, int dim, std::vector< std::vector< int > > rFieldMappings, std::vector< index_order > rIndexOrder, std::vector< adj_map > conv_factor, std::vector< adj_map > norm_val)
Use test functions to determine the filetype and to create a new file object for the input file...
Definition: fileselect.cpp:3
static const int BUFFER_SIZE
Definition: fileselect.h:60
Base class for file parsing.
Definition: file.h:32
real *8 function offset(vNorm, x2, y2, z2)
Definition: PlaneNorm.f90:211
bool check_bool_arr(bool test[], int size)
std::vector< string > readCommaExpression(string rStack, string rNeedle)
static bool testTecplot(ifstream &infile, string datapacking)
Test if a file is a tecplot file.
Definition: fileselect.cpp:28
blockLoc i
Definition: read.cpp:79
static void printFiletypes()
Print list valid filetypes to stdout.
Definition: fileselect.cpp:193
bool check_non_numeric_str(string test_str)
Base class for tecplot files Implements useful functions for tecplot file parsing.
Definition: file.h:206