1 ////////////////////////////////////////////////////////////////////////////////////// 2 // This file is distributed under the University of Illinois/NCSA Open Source License. 3 // See LICENSE file in top directory for details. 4 // 5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers. 6 // 7 // File developed by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory 8 // 9 // File created by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory 10 ////////////////////////////////////////////////////////////////////////////////////// 11 12 13 // http://pathintegrals.info // 14 ///////////////////////////////////////////////////////////// 15 16 #include "IO.h" 17 #include <fstream> 18 19 namespace IO 20 { 21 std::filebuf vbuf; 22 std::ostream verr(&vbuf); 23 SetVerbose(bool verb)24 void SetVerbose (bool verb) { 25 if (verb) 26 verr.rdbuf(std::cerr.rdbuf()); 27 } 28 29 /// In the file name format name.extn, returns the extension. 30 /// Actually returns everything after the trailing. Extension(std::string fileName)31 std::string Extension (std::string fileName) 32 { 33 std::string extn; 34 std::stack<char> bwExtn; 35 int pos = fileName.length()-1; 36 while ((pos >= 0) && fileName[pos]!='.') { 37 bwExtn.push(fileName[pos]); 38 pos--; 39 } 40 41 if (fileName[pos] == '.') 42 while (!bwExtn.empty()) { 43 extn += bwExtn.top(); 44 bwExtn.pop(); 45 } 46 else 47 extn = ""; 48 return (extn); 49 } 50 51 /// This function takes a filename, determines it extension, creates a 52 /// new IOTreeASCIIClass or IOTreeHDF5Class based on the 53 /// extension, and calls OpenFile on the new object. 54 /// Extensions: 55 /// .h5: HDF5 56 /// .xml: XML 57 /// .anything_else ASCII ReadTree(std::string fileName,std::string myName,IOTreeClass * parent)58 IOTreeClass *ReadTree (std::string fileName, std::string myName, IOTreeClass *parent) 59 { 60 IOTreeClass *newTree; 61 std::string extn = Extension (fileName); 62 newTree = new IOTreeASCIIClass; 63 64 newTree->FileName = fileName; 65 bool success = newTree->OpenFile (fileName, myName, parent); 66 if (success) 67 return (newTree); 68 else{ 69 delete newTree; 70 return (NULL); 71 } 72 } 73 NewTree(std::string fileName,std::string myName,IOTreeClass * parent)74 IOTreeClass *NewTree (std::string fileName, std::string myName, IOTreeClass *parent) 75 { 76 IOTreeClass *newTree; 77 std::string extn = Extension (fileName); 78 newTree = new IOTreeASCIIClass; 79 80 bool success = newTree->NewFile (fileName, myName, parent); 81 if (success) 82 return (newTree); 83 else{ 84 delete newTree; 85 return (NULL); 86 } 87 } 88 89 bool OpenFile(std::string fileName)90 IOSectionClass::OpenFile (std::string fileName) 91 { 92 CurrentSection = ReadTree (fileName, "Root", NULL); 93 if (CurrentSection == NULL) 94 return (false); 95 else 96 return (true); 97 } 98 99 bool NewFile(std::string fileName)100 IOSectionClass::NewFile (std::string fileName) 101 { 102 CurrentSection = NewTree (fileName, "Root", NULL); 103 if (CurrentSection == NULL) 104 return (false); 105 else 106 return (true); 107 } 108 109 110 void CloseFile()111 IOSectionClass::CloseFile () 112 { 113 while (CurrentSection->Parent != NULL) 114 CloseSection(); 115 CurrentSection->FlushFile(); 116 CurrentSection->CloseFile(); 117 delete (CurrentSection); 118 } 119 120 121 void FlushFile()122 IOSectionClass::FlushFile() 123 { 124 IOTreeClass *tree = CurrentSection; 125 while (tree->Parent != NULL) 126 tree = tree->Parent; 127 tree->FlushFile(); 128 } 129 130 OpenSection(std::string name,int num)131 bool IOSectionClass::OpenSection (std::string name, int num) 132 { 133 IOTreeClass *newSection; 134 bool success; 135 success = CurrentSection->FindSection(name, newSection, num); 136 if (success) 137 CurrentSection=newSection; 138 return success; 139 } 140 141 142 bool OpenSection(int num)143 IOSectionClass::OpenSection (int num) 144 { 145 IOTreeClass *newSection; 146 std::list<IOTreeClass*>::iterator Iter=CurrentSection->SectionList.begin(); 147 int i = 0; 148 while ((i<num) && 149 (Iter != CurrentSection->SectionList.end())){ 150 i++; 151 Iter++; 152 } 153 if (i<num) 154 return false; 155 else { 156 CurrentSection = *Iter; 157 return true; 158 } 159 } 160 161 162 bool IncludeSection(std::string name,std::string fileName)163 IOSectionClass::IncludeSection (std::string name, std::string fileName) 164 { 165 IOTreeClass *newSection; 166 newSection = ReadTree (fileName, name, CurrentSection); 167 if (newSection == NULL) 168 return false; 169 else { 170 CurrentSection->IncludeSection(newSection); 171 return true; 172 } 173 } 174 175 ///Don't think this pushes to back of list like it should nor does newfile 176 bool NewSection(std::string name,std::string fileName)177 IOSectionClass::NewSection (std::string name, std::string fileName) 178 { 179 IOTreeClass *newSection; 180 newSection = NewTree (fileName, name, CurrentSection); 181 if (newSection == NULL) 182 return false; 183 else { 184 CurrentSection->IncludeSection(newSection); 185 CurrentSection = newSection; 186 return true; 187 } 188 } 189 190 void CloseSection()191 IOSectionClass::CloseSection () 192 { 193 //cerr << "Closing Section " << CurrentSection->Name << endl; 194 assert (CurrentSection->Parent != NULL); 195 CurrentSection = CurrentSection->Parent; 196 } 197 198 199 std::string GetFileName()200 IOSectionClass::GetFileName() 201 { 202 IOTreeClass *tree = CurrentSection; 203 while (tree->Parent != NULL) 204 tree = tree->Parent; 205 return tree->FileName; 206 } 207 208 209 210 211 212 } 213