NumCpp  1.0
A C++ implementation of the Python Numpy library
fromFile.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/Core/Error.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <fstream>
37 #include <memory>
38 #include <string>
39 #include <sstream>
40 
41 namespace nc
42 {
43  //============================================================================
44  // Method Description:
56  template<typename dtype>
57  NdArray<dtype> fromfile(const std::string& inFilename, const std::string& inSep = "")
58  {
59  if (!filesystem::File(inFilename).exists())
60  {
61  THROW_INVALID_ARGUMENT_ERROR("input filename does not exist.\n\t" + inFilename);
62  }
63 
64  if (inSep.compare("") == 0)
65  {
66  // read in as binary file
67  std::ifstream file(inFilename.c_str(), std::ios::in | std::ios::binary);
68  if (!file.is_open())
69  {
70  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
71  }
72 
73  file.seekg(0, file.end);
74  const uint32 fileSize = static_cast<uint32>(file.tellg());
75  file.seekg(0, file.beg);
76 
77  const auto fileBuffer = std::unique_ptr<char>(new char[fileSize]);
78  file.read(fileBuffer.get(), fileSize);
79 
80  if (file.bad() || file.fail())
81  {
82  THROW_INVALID_ARGUMENT_ERROR("error occured while reading the file");
83  }
84 
85  file.close();
86 
87  NdArray<dtype> returnArray(reinterpret_cast<dtype*>(fileBuffer.get()), fileSize / sizeof(dtype));
88 
89  return returnArray;
90  }
91  else
92  {
93  // read in as txt file
94  if (!(inSep.compare(" ") == 0 || inSep.compare("\t") == 0 || inSep.compare("\n") == 0))
95  {
96  THROW_INVALID_ARGUMENT_ERROR("only [' ', '\\t', '\\n'] seperators are supported");
97  }
98 
99  std::vector<dtype> values;
100 
101  std::ifstream file(inFilename.c_str());
102  if (file.is_open())
103  {
104  uint32 lineNumber = 0;
105  while (!file.eof())
106  {
107  std::string line;
108  std::getline(file, line);
109 
110  std::istringstream iss(line);
111  try
112  {
113  dtype value;
114  while (iss >> value)
115  {
116  values.push_back(value);
117  }
118  }
119  catch (const std::invalid_argument& ia)
120  {
121  std::cout << "Warning: fromfile: line " << lineNumber << "\n" << ia.what() << std::endl;
122  }
123  catch (...)
124  {
125  std::cout << "Warning: fromfile: line " << lineNumber << std::endl;
126  }
127 
128  ++lineNumber;
129  }
130  file.close();
131  }
132  else
133  {
134  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
135  }
136 
137  return NdArray<dtype>(values);
138  }
139  }
140 }
NdArray< dtype > fromfile(const std::string &inFilename, const std::string &inSep="")
Definition: fromFile.hpp:57
Definition: Coordinate.hpp:45
uint32_t uint32
Definition: Types.hpp:41
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:38
Provides simple filesystem functions.
Definition: Filesystem.hpp:40
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:66