clang-tags
C/C++ source code indexing tool based on libclang
 All Classes Functions Variables Typedefs Groups Pages
sourceFile.hxx
1 #pragma once
2 
3 #include <fstream>
4 #include <sstream>
5 
6 class SourceFile : public std::ifstream {
7 public:
8  SourceFile (const std::string & fileName)
9  : std::ifstream (fileName.c_str())
10  { }
11 
12  std::string substring (const unsigned int offset1, const unsigned int offset2) {
13  unsigned int len = offset2 - offset1;
14  char * buffer = new char [len+1];
15 
16  seekg (offset1);
17  read (buffer, len);
18  buffer[len] = 0;
19 
20  std::string res (buffer);
21  delete[] buffer;
22 
23  return shorten_ (res, 42);
24  }
25 
26  std::string line (const unsigned int lineno) {
27  seekg (0);
28  std::string line;
29  for (unsigned int i=0 ; i<lineno ; ++i) {
30  std::getline (*this, line);
31  }
32 
33  return line;
34  }
35 
36 private:
37  static std::string shorten_ (const std::string & s, const unsigned int sizeMax) {
38  std::istringstream iss (s);
39 
40  std::string word;
41  iss >> word;
42  std::string res = word;
43  while (! iss.eof()) {
44  iss >> word;
45  res += " " + word;
46 
47  if (res.size() > sizeMax) {
48  res = res.substr(0, sizeMax-3);
49  res += "...";
50  break;
51  }
52  }
53 
54  return res;
55  }
56 
57 };