GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
file-info.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <deque>
31 #include <fstream>
32 
33 #include "file-info.h"
34 #include "file-stat.h"
35 #include "lo-error.h"
36 #include "lo-sysdep.h"
37 
39 
40 std::string file_info::get_line (std::size_t line) const
41 {
42  std::string retval;
43 
44  if (line == 0)
45  return retval;
46 
47  if (line < m_offsets.size ())
48  {
49  std::size_t bol = m_offsets[line-1];
50  std::size_t eol = m_offsets[line];
51 
52  while (eol > 0 && eol > bol
53  && (m_file_buf[eol-1] == '\n' || m_file_buf[eol-1] == '\r'))
54  eol--;
55 
56  retval = m_file_buf.substr (bol, eol - bol);
57  }
58 
59  return retval;
60 }
61 
62 std::deque<std::string>
63 file_info::get_lines (std::size_t line, std::size_t num_lines) const
64 {
65  std::deque<std::string> retval;
66 
67  for (std::size_t i = line; i < line+num_lines; i++)
68  retval.push_back (get_line (i));
69 
70  return retval;
71 }
72 
73 // Read entire file called fname and return the contents as a string
74 
75 std::string
76 file_info::snarf_file (const std::string& fname)
77 {
78  std::string retval;
79 
80  sys::file_stat fs (fname);
81 
82  if (! fs)
83  (*current_liboctave_error_handler) ("no such file, '%s'", fname.c_str ());
84 
85  std::size_t sz = fs.size ();
86 
87  std::ifstream file = sys::ifstream (fname.c_str (),
88  std::ios::in | std::ios::binary);
89 
90  if (file)
91  {
92  std::string buf (sz+1, 0);
93 
94  file.read (&buf[0], sz+1);
95 
96  if (! file.eof ())
97  (*current_liboctave_error_handler)
98  ("error reading file %s", fname.c_str ());
99 
100  // Expected to read the entire file.
101  retval = buf;
102  }
103 
104  return retval;
105 }
106 
107 std::vector<std::size_t>
108 file_info::get_line_offsets (const std::string& buf)
109 {
110  std::deque<std::size_t> tmp_offsets;
111 
112  tmp_offsets.push_back (0);
113 
114  std::size_t len = buf.length ();
115 
116  for (std::size_t i = 0; i < len; i++)
117  {
118  char c = buf[i];
119 
120  if (c == '\r' && ++i < len)
121  {
122  c = buf[i];
123 
124  if (c == '\n')
125  tmp_offsets.push_back (i+1);
126  else
127  tmp_offsets.push_back (i);
128  }
129  else if (c == '\n')
130  tmp_offsets.push_back (i+1);
131  }
132 
133  tmp_offsets.push_back (len-1);
134 
135  std::size_t n = tmp_offsets.size ();
136 
137  std::vector<std::size_t> retval (n);
138  std::size_t i = 0;
139  for (auto& elt : tmp_offsets)
140  retval[i++] = elt;
141 
142  return retval;
143 }
144 
145 OCTAVE_END_NAMESPACE(octave)
std::size_t num_lines() const
Definition: file-info.h:70
std::string get_line(std::size_t line) const
Definition: file-info.cc:40
std::deque< std::string > get_lines(std::size_t line, std::size_t num_lines) const
Definition: file-info.cc:63
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
Definition: lo-sysdep.cc:621
octave_idx_type n
Definition: mx-inlines.cc:761
F77_RET_T len
Definition: xerbla.cc:61