SSAGES  0.1
A MetaDynamics Package
FileContents.h
Go to the documentation of this file.
1 
26 #pragma once
27 
28 #include <string>
29 #include <cstdio>
30 #include <cerrno>
31 
37 namespace SSAGES
38 {
40 
47  inline std::string GetFileContents(const char *filename)
48  {
49  std::FILE *fp = std::fopen(filename, "rb");
50  if (fp)
51  {
52  std::string contents;
53  std::fseek(fp, 0, SEEK_END);
54  contents.resize(std::ftell(fp));
55  std::rewind(fp);
56 
57  // Stupid GCC bug. We do this to hide warnings.
58  if(!std::fread(&contents[0], 1, contents.size(), fp))
59  std::fclose(fp);
60  else
61  std::fclose(fp);
62 
63  return(contents);
64  }
65  throw(errno);
66  }
67 
69 
73  inline std::string GetFilePath(const std::string& str)
74  {
75  size_t found;
76  found = str.find_last_of("/\\");
77  if(found == str.npos)
78  return "./";
79  return str.substr(0, found);
80  }
81 }
std::string GetFilePath(const std::string &str)
Gets file path from filename.
Definition: FileContents.h:73
std::string GetFileContents(const char *filename)
Read contents from a file.
Definition: FileContents.h:47