GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
file-info.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2001-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_file_info_h)
27 #define octave_file_info_h 1
28 
29 #include "octave-config.h"
30 
31 #include <deque>
32 #include <map>
33 #include <string>
34 #include <vector>
35 
36 #include "oct-time.h"
37 
38 namespace octave
39 {
40  class file_info
41  {
42  public:
43 
44  file_info (void)
45  : m_file_buf (), m_offsets (), m_timestamp (static_cast<time_t> (0))
46  { }
47 
48  file_info (const std::string& text, const sys::time& timestamp)
51  { }
52 
53  file_info (const std::string& fname)
54  : m_file_buf (snarf_file (fname)),
56  m_timestamp ()
57  { }
58 
59  file_info (const file_info&) = default;
60 
61  file_info& operator = (const file_info&) = default;
62 
63  ~file_info (void) = default;
64 
65  std::string get_line (size_t line) const;
66 
67  std::deque<std::string> get_lines (size_t line, size_t num_lines) const;
68 
69  size_t num_lines (void) const { return m_offsets.size (); }
70 
71  std::string text (void) const { return m_file_buf; }
72 
73  std::vector<size_t> line_offsets (void) const { return m_offsets; }
74 
75  sys::time timestamp (void) const { return m_timestamp; }
76 
77  size_t size (void) const { return m_file_buf.length (); }
78 
79  private:
80 
81  // File contents as a string.
82  std::string m_file_buf;
83 
84  // Offsets to line beginnings.
85  std::vector<size_t> m_offsets;
86 
88 
89  // Read entire file called fname and return the contents as a string
90  static std::string snarf_file (const std::string& fname);
91 
92  static std::vector<size_t> get_line_offsets (const std::string& buf);
93  };
94 }
95 
96 #endif
size_t num_lines(void) const
Definition: file-info.h:69
sys::time timestamp(void) const
Definition: file-info.h:75
std::string m_file_buf
Definition: file-info.h:82
std::string get_line(size_t line) const
Definition: file-info.cc:40
std::deque< std::string > get_lines(size_t line, size_t num_lines) const
Definition: file-info.cc:63
static std::string snarf_file(const std::string &fname)
Definition: file-info.cc:75
file_info(const std::string &text, const sys::time &timestamp)
Definition: file-info.h:48
~file_info(void)=default
std::string text(void) const
Definition: file-info.h:71
std::vector< size_t > m_offsets
Definition: file-info.h:85
file_info(const file_info &)=default
std::vector< size_t > line_offsets(void) const
Definition: file-info.h:73
sys::time m_timestamp
Definition: file-info.h:87
static std::vector< size_t > get_line_offsets(const std::string &buf)
Definition: file-info.cc:106
file_info & operator=(const file_info &)=default
file_info(const std::string &fname)
Definition: file-info.h:53
size_t size(void) const
Definition: file-info.h:77