FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
files.cc
Go to the documentation of this file.
1 /*
2 Copyright (C) 2013-2014, Siemens AG
3 
4 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
5 
6 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
7 
8 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
9 */
10 
11 #include "files.hpp"
12 #include <fstream>
13 #include <sys/stat.h>
14 #include <sstream>
15 
25 namespace fo
26 {
27 
38  std::string getStringFromFile(const char* filename, const unsigned long int maximumBytes)
39  {
40 
41 
42  std::ifstream inStream(filename, std::ios::in | std::ios::binary);
43  if (inStream)
44  {
45  std::string contents;
46  inStream.seekg(0, std::ios::end);
47  if (!(inStream.rdstate() & std::ifstream::failbit))
48  {
49  const unsigned long int endPos = inStream.tellg();
50  contents.resize((maximumBytes > 0 && (endPos > maximumBytes)) ? maximumBytes : endPos);
51  inStream.seekg(0, std::ios::beg);
52  inStream.read(&contents[0], contents.size());
53  }
54  else
55  {
56  // TODO respect limit of maximumBytes
57  inStream.clear(std::ifstream::goodbit);
58 
59  std::stringstream ss;
60  ss << inStream.rdbuf();
61 
62  return ss.str();
63  }
64  inStream.close();
65  return (contents);
66  }
67  throw(errno);
68  }
69 
73  std::string getStringFromFile(std::string const& filename, const unsigned long int maximumBytes)
74  {
75  return getStringFromFile(filename.c_str(), maximumBytes);
76  };
77 
85  std::string File::getContent(const unsigned long int maximumBytes) const
86  {
87  return getStringFromFile(fileName, maximumBytes);
88  }
89 
94  const std::string& File::getFileName() const
95  {
96  return fileName;
97  }
98 
104  File::File(unsigned long _id, const char* _fileName) : id(_id), fileName(_fileName)
105  {
106  }
107 
111  File::File(unsigned long id, std::string const& fileName) : id(id), fileName(fileName)
112  {
113  }
114 
119  unsigned long File::getId() const
120  {
121  return id;
122  }
123 
128  bool File::isReadable() const
129  {
130  struct stat statStr;
131  return (stat(fileName.c_str(), &statStr) == 0);
132  }
133 }
unsigned long id
ID of the file.
Definition: files.hpp:40
std::string getStringFromFile(const char *filename, const unsigned long int maximumBytes)
Reads the content of a file and return it as a string.
Definition: files.cc:38
std::string getContent(const unsigned long int maximumBytes=1<< 20) const
Get the content of the file limited by maximumBytes.
Definition: files.cc:85
std::string fileName
Path of the file.
Definition: files.hpp:41
bool isReadable() const
Definition: files.cc:128
unsigned long getId() const
Definition: files.cc:119
fo namespace holds the FOSSology library functions.
const std::string & getFileName() const
Definition: files.cc:94
Utility functions for file handling.
File(unsigned long id, const char *fileName)
Definition: files.cc:104