Mercurial > octave
annotate liboctave/util/file-info.cc @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
1 /* |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
2 |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 Copyright (C) 2001-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27102
diff
changeset
|
4 |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27102
diff
changeset
|
5 See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27102
diff
changeset
|
6 or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27102
diff
changeset
|
7 |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
8 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
9 This file is part of Octave. |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
10 |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23728
diff
changeset
|
11 Octave is free software: you can redistribute it and/or modify it |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
12 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23728
diff
changeset
|
13 the Free Software Foundation, either version 3 of the License, or |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
14 (at your option) any later version. |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
15 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
16 Octave is distributed in the hope that it will be useful, but |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
17 WITHOUT ANY WARRANTY; without even the implied warranty of |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
19 GNU General Public License for more details. |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
20 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
21 You should have received a copy of the GNU General Public License |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
22 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23728
diff
changeset
|
23 <https://www.gnu.org/licenses/>. |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
24 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
25 */ |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
26 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
27 #if defined (HAVE_CONFIG_H) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
28 # include "config.h" |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
29 #endif |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
30 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
31 #include <deque> |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
32 #include <fstream> |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
33 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
34 #include "file-info.h" |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
35 #include "file-stat.h" |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
36 #include "lo-error.h" |
25548
d6050ba12c0c
Call get_ASCII_filename for std::ifstream (bug #49118).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25438
diff
changeset
|
37 #include "lo-sysdep.h" |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
38 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
39 namespace octave |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
40 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
41 std::string file_info::get_line (size_t line) const |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
42 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
43 std::string retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
44 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
45 if (line == 0) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
46 return retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
47 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
48 if (line < m_offsets.size ()) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
49 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
50 size_t bol = m_offsets[line-1]; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
51 size_t eol = m_offsets[line]; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
52 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
53 while (eol > 0 && eol > bol |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
54 && (m_file_buf[eol-1] == '\n' || m_file_buf[eol-1] == '\r')) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
55 eol--; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
56 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
57 retval = m_file_buf.substr (bol, eol - bol); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
58 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
59 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
60 return retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
61 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
62 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
63 std::deque<std::string> |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
64 file_info::get_lines (size_t line, size_t num_lines) const |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
65 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
66 std::deque<std::string> retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
67 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
68 for (size_t i = line; i < line+num_lines; i++) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
69 retval.push_back (get_line (i)); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
70 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
71 return retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
72 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
73 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
74 // Read entire file called fname and return the contents as a string |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
75 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
76 std::string file_info::snarf_file (const std::string& fname) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
77 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
78 std::string retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
79 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
80 sys::file_stat fs (fname); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
81 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
82 if (! fs) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
83 (*current_liboctave_error_handler) ("no such file, '%s'", fname.c_str ()); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
84 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
85 size_t sz = fs.size (); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
86 |
27102
84ff9953faa1
where possible, eliminate octave:: namespace qualifier inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
87 std::string ascii_fname = sys::get_ASCII_filename (fname); |
25604
ca413f326224
Fix lifetime issues with temporary char arrays returned by get_ASCII_filename (bug #54299).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25548
diff
changeset
|
88 |
ca413f326224
Fix lifetime issues with temporary char arrays returned by get_ASCII_filename (bug #54299).
Markus Mützel <markus.muetzel@gmx.de>
parents:
25548
diff
changeset
|
89 std::ifstream file (ascii_fname.c_str (), std::ios::in | std::ios::binary); |
23728
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
90 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
91 if (file) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
92 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
93 std::string buf (sz+1, 0); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
94 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
95 file.read (&buf[0], sz+1); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
96 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
97 if (! file.eof ()) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
98 (*current_liboctave_error_handler) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
99 ("error reading file %s", fname.c_str ()); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
100 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
101 // Expected to read the entire file. |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
102 retval = buf; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
103 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
104 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
105 return retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
106 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
107 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
108 std::vector<size_t> file_info::get_line_offsets (const std::string& buf) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
109 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
110 std::deque<size_t> tmp_offsets; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
111 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
112 tmp_offsets.push_back (0); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
113 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
114 size_t len = buf.length (); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
115 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
116 for (size_t i = 0; i < len; i++) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
117 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
118 char c = buf[i]; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
119 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
120 if (c == '\r' && ++i < len) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
121 { |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
122 c = buf[i]; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
123 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
124 if (c == '\n') |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
125 tmp_offsets.push_back (i+1); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
126 else |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
127 tmp_offsets.push_back (i); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
128 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
129 else if (c == '\n') |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
130 tmp_offsets.push_back (i+1); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
131 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
132 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
133 tmp_offsets.push_back (len-1); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
134 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
135 size_t n = tmp_offsets.size (); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
136 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
137 std::vector<size_t> retval (n); |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
138 size_t i = 0; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
139 for (auto& elt : tmp_offsets) |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
140 retval[i++] = elt; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
141 |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
142 return retval; |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
143 } |
b40b7243a782
new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
144 } |