MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / checkVersionAndFindIndex

Function checkVersionAndFindIndex

src/api/c/stream.cpp:284–330  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

282}
283
284int checkVersionAndFindIndex(const char *filename, const char *k) {
285 char version = 0;
286 std::string key(k);
287 std::string filenameStr(filename);
288 std::ifstream fs(filenameStr, std::ifstream::in | std::ifstream::binary);
289
290 // Throw exception if file is not open
291 if (!fs.is_open()) {
292 std::string errStr = "Failed to open: " + filenameStr;
293 AF_ERROR(errStr.c_str(), AF_ERR_ARG);
294 }
295
296 if (fs.peek() == std::ifstream::traits_type::eof()) {
297 std::string errStr = filenameStr + " is empty";
298 AF_ERROR(errStr.c_str(), AF_ERR_ARG);
299 } else {
300 fs.read(&version, sizeof(char));
301 }
302
303 int index = -1;
304 if (version == 1) {
305 int n_arrays = -1;
306 fs.read(reinterpret_cast<char *>(&n_arrays), sizeof(int));
307 for (int i = 0; i < n_arrays; i++) {
308 int klen = -1;
309 fs.read(reinterpret_cast<char *>(&klen), sizeof(int));
310 string readKey;
311 readKey.resize(klen);
312 fs.read(&readKey.front(), klen);
313
314 if (key == readKey) {
315 // Ket matches, break
316 index = i;
317 break;
318 }
319 // Key doesn't match. Skip the data
320 intl offset = -1;
321 fs.read(reinterpret_cast<char *>(&offset), sizeof(intl));
322 fs.seekg(offset, std::ios_base::cur);
323 }
324 } else {
325 AF_ERROR("Invalid version", AF_ERR_ARG);
326 }
327 fs.close();
328
329 return index;
330}
331
332af_err af_read_array_index(af_array *out, const char *filename,
333 const unsigned index) {

Callers 2

af_read_array_keyFunction · 0.85
af_read_array_key_checkFunction · 0.85

Calls 1

closeMethod · 0.80

Tested by

no test coverage detected