GNU Octave  9.1.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-2024 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 
39 
40 class file_info
41 {
42 public:
43 
45  : m_file_buf (), m_offsets (), m_timestamp (static_cast<OCTAVE_TIME_T> (0))
46  { }
47 
48  file_info (const std::string& text, const sys::time& timestamp)
49  : m_file_buf (text), m_offsets (get_line_offsets (m_file_buf)),
50  m_timestamp (timestamp)
51  { }
52 
53  file_info (const std::string& fname)
54  : m_file_buf (snarf_file (fname)),
55  m_offsets (get_line_offsets (m_file_buf)),
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 () = default;
64 
65  OCTAVE_API std::string get_line (std::size_t line) const;
66 
67  OCTAVE_API std::deque<std::string>
68  get_lines (std::size_t line, std::size_t num_lines) const;
69 
70  std::size_t num_lines () const { return m_offsets.size (); }
71 
72  std::string text () const { return m_file_buf; }
73 
74  std::vector<std::size_t> line_offsets () const { return m_offsets; }
75 
76  sys::time timestamp () const { return m_timestamp; }
77 
78  std::size_t size () const { return m_file_buf.length (); }
79 
80 private:
81 
82  // File contents as a string.
83  std::string m_file_buf;
84 
85  // Offsets to line beginnings.
86  std::vector<std::size_t> m_offsets;
87 
88  sys::time m_timestamp;
89 
90  // Read entire file called fname and return the contents as a string
91  static OCTAVE_API std::string snarf_file (const std::string& fname);
92 
93  static OCTAVE_API std::vector<std::size_t>
94  get_line_offsets (const std::string& buf);
95 };
96 
97 OCTAVE_END_NAMESPACE(octave)
98 
99 #endif
std::string text() const
Definition: file-info.h:72
file_info(const file_info &)=default
~file_info()=default
std::vector< std::size_t > line_offsets() const
Definition: file-info.h:74
std::size_t num_lines() const
Definition: file-info.h:70
file_info()
Definition: file-info.h:44
std::size_t size() const
Definition: file-info.h:78
file_info(const std::string &text, const sys::time &timestamp)
Definition: file-info.h:48
sys::time timestamp() const
Definition: file-info.h:76
file_info(const std::string &fname)
Definition: file-info.h:53
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.cc:55