GNU Octave  4.4.1
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) 2017-2018 John W. Eaton
4 Copyright (C) 2001-2018 Ben Sapp
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <deque>
29 #include <fstream>
30 #include <iostream>
31 
32 #include "file-info.h"
33 #include "file-stat.h"
34 #include "lo-error.h"
35 
36 namespace octave
37 {
39  {
41 
42  if (line == 0)
43  return retval;
44 
45  if (line < m_offsets.size ())
46  {
47  size_t bol = m_offsets[line-1];
48  size_t eol = m_offsets[line];
49 
50  while (eol > 0 && eol > bol
51  && (m_file_buf[eol-1] == '\n' || m_file_buf[eol-1] == '\r'))
52  eol--;
53 
54  retval = m_file_buf.substr (bol, eol - bol);
55  }
56 
57  return retval;
58  }
59 
60  std::deque<std::string>
61  file_info::get_lines (size_t line, size_t num_lines) const
62  {
63  std::deque<std::string> retval;
64 
65  for (size_t i = line; i < line+num_lines; i++)
66  retval.push_back (get_line (i));
67 
68  return retval;
69  }
70 
71  // Read entire file called fname and return the contents as a string
72 
74  {
76 
78 
79  if (! fs)
80  (*current_liboctave_error_handler) ("no such file, '%s'", fname.c_str ());
81 
82  size_t sz = fs.size ();
83 
84  std::ifstream file (fname.c_str (), std::ios::in | std::ios::binary);
85 
86  if (file)
87  {
88  std::string buf (sz+1, 0);
89 
90  file.read (&buf[0], sz+1);
91 
92  if (! file.eof ())
94  ("error reading file %s", fname.c_str ());
95 
96  // Expected to read the entire file.
97  retval = buf;
98  }
99 
100  return retval;
101  }
102 
103  std::vector<size_t> file_info::get_line_offsets (const std::string& buf)
104  {
105  std::deque<size_t> tmp_offsets;
106 
107  tmp_offsets.push_back (0);
108 
109  size_t len = buf.length ();
110 
111  for (size_t i = 0; i < len; i++)
112  {
113  char c = buf[i];
114 
115  if (c == '\r' && ++i < len)
116  {
117  c = buf[i];
118 
119  if (c == '\n')
120  tmp_offsets.push_back (i+1);
121  else
122  tmp_offsets.push_back (i);
123  }
124  else if (c == '\n')
125  tmp_offsets.push_back (i+1);
126  }
127 
128  tmp_offsets.push_back (len-1);
129 
130  size_t n = tmp_offsets.size ();
131 
132  std::vector<size_t> retval (n);
133  size_t i = 0;
134  for (auto& elt : tmp_offsets)
135  retval[i++] = elt;
136 
137  return retval;
138  }
139 }
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
std::vector< size_t > m_offsets
Definition: file-info.h:83
fname
Definition: load-save.cc:767
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
off_t size(void) const
Definition: file-stat.h:125
static std::string snarf_file(const std::string &fname)
Definition: file-info.cc:73
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
static std::vector< size_t > get_line_offsets(const std::string &buf)
Definition: file-info.cc:103
std::string get_line(size_t line) const
Definition: file-info.cc:38
octave_value retval
Definition: data.cc:6246
std::deque< std::string > get_lines(size_t line, size_t num_lines) const
Definition: file-info.cc:61
sz
Definition: data.cc:5264
size_t num_lines(void) const
Definition: file-info.h:67
std::string m_file_buf
Definition: file-info.h:80
octave::sys::file_stat fs(filename)
for i
Definition: data.cc:5264
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888