GNU Octave 7.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-2022 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
38namespace octave
39{
41 {
42 public:
43
44 file_info (void)
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)
51 { }
52
53 file_info (const std::string& fname)
54 : m_file_buf (snarf_file (fname)),
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 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 (void) const { return m_offsets.size (); }
71
72 std::string text (void) const { return m_file_buf; }
73
74 std::vector<std::size_t> line_offsets (void) const { return m_offsets; }
75
76 sys::time timestamp (void) const { return m_timestamp; }
77
78 std::size_t size (void) 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
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
98#endif
sys::time timestamp(void) const
Definition: file-info.h:76
std::size_t num_lines(void) const
Definition: file-info.h:70
OCTAVE_API std::string get_line(std::size_t line) const
Definition: file-info.cc:40
std::string m_file_buf
Definition: file-info.h:83
static OCTAVE_API 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:72
std::size_t size(void) const
Definition: file-info.h:78
file_info(const file_info &)=default
std::vector< std::size_t > m_offsets
Definition: file-info.h:86
file_info & operator=(const file_info &)=default
std::vector< std::size_t > line_offsets(void) const
Definition: file-info.h:74
sys::time m_timestamp
Definition: file-info.h:88
static OCTAVE_API std::vector< std::size_t > get_line_offsets(const std::string &buf)
Definition: file-info.cc:106
OCTAVE_API std::deque< std::string > get_lines(std::size_t line, std::size_t num_lines) const
Definition: file-info.cc:63
file_info(const std::string &fname)
Definition: file-info.h:53
#define OCTAVE_API
Definition: main.in.cc:55