| 155 | } |
| 156 | |
| 157 | inline void CreateHeader(std::ifstream& ifStream, HeaderInfo& headerInfo, Header& header) |
| 158 | { |
| 159 | char stringBuffer[headerInfo.m_HeaderLen]; |
| 160 | ifStream.read(stringBuffer, static_cast<std::streamsize>(headerInfo.m_HeaderLen)); |
| 161 | |
| 162 | header.m_HeaderString = std::string(stringBuffer, headerInfo.m_HeaderLen); |
| 163 | // Remove new line character at the end of the string |
| 164 | if(header.m_HeaderString.back() == '\n') |
| 165 | { |
| 166 | header.m_HeaderString.pop_back(); |
| 167 | } |
| 168 | |
| 169 | // Remove whitespace from the string. |
| 170 | // std::remove shuffles the string by place all whitespace at the end and |
| 171 | // returning the start location of the shuffled whitespace. |
| 172 | // std::string.erase then deletes the whitespace by deleting characters |
| 173 | // between the iterator returned from std::remove and the end of the std::string |
| 174 | std::string::iterator whitespaceSubstringStart = std::remove(header.m_HeaderString.begin(), |
| 175 | header.m_HeaderString.end(), ' '); |
| 176 | header.m_HeaderString.erase(whitespaceSubstringStart, header.m_HeaderString.end()); |
| 177 | |
| 178 | // The order of the dictionary should be alphabetical, |
| 179 | // however this is not guarenteed so we have to search for the string. |
| 180 | // Because of this we do some weird parsing using std::string.find and some magic patterns |
| 181 | // |
| 182 | // For the description value, we include the end character from the first substring |
| 183 | // to help us find the value in the second substring. This should return with a "," at the end. |
| 184 | // Since we previously left the "," at the end of the substring, |
| 185 | // we can use it to find the end of the description value and then remove it after. |
| 186 | std::string descrString = getSubstring(header.m_HeaderString, "'descr", ",", 0, 1); |
| 187 | header.m_DescrString = getSubstring(descrString, ":", ",", 1); |
| 188 | |
| 189 | // Fortran order is a python boolean literal, we simply look for a comma to delimit this pair. |
| 190 | // Since this is a boolean, both true and false end in an "e" without appearing in between. |
| 191 | // It is not great, but it is the easiest way to find the end. |
| 192 | // We have to ensure we include this e in the substring. |
| 193 | // Since this is a boolean we can check if the string contains |
| 194 | // either true or false and set the variable as required |
| 195 | std::string fortranOrderString = getSubstring(header.m_HeaderString, "'fortran_order", ","); |
| 196 | fortranOrderString = getSubstring(fortranOrderString, ":", "e", 1, 1); |
| 197 | header.m_FortranOrder = fortranOrderString.find("True") != std::string::npos ? true : false; |
| 198 | |
| 199 | // The shape is a python tuple so we search for the closing bracket of the tuple. |
| 200 | // We include the end character to help us isolate the value substring. |
| 201 | // We can extract the inside of the tuple by searching for opening and closing brackets. |
| 202 | // We can then remove the brackets isolating the inside of the tuple. |
| 203 | // We then need to parse the string into a vector of unsigned integers |
| 204 | std::string shapeString = getSubstring(header.m_HeaderString, "'shape", ")", 0, 1); |
| 205 | shapeString = getSubstring(shapeString, "(", ")", 1, 0); |
| 206 | parseShape(header, shapeString); |
| 207 | } |
| 208 | |
| 209 | template<typename T> |
| 210 | inline void ReadData(std::ifstream& ifStream, T* tensor, const unsigned int& numElements) |
no test coverage detected