annotate liboctave/util/file-info.h @ 23730:85f1d31956c0

make echo work for command-line functions * file-info.h (file_info::file_info): New constructor from program text and time when text was processed. * ov-usr-fcn.h, ov-usr-fcn.cc (octave_user_code::get_file_info): New function. (octave_user_code::get_code_line, octave_user_code::get_code_lines): Use get_file_info to update m_file_info if it is not already defined. (octave_user_code::cache_function_text): New function. * lex.h, lex.ll (base_lexer::lexical_feedback::buffer_function_text, base_lexer::lexical_feedback::function_text): New variables. (base_lexer::is_keyword_token, lexer::fill_flex_buffer): Handle buffering of function text command-line functions. * oct-parse.in.yy (base_parser::finish_function): Cache function text for command-line functions.
author John W. Eaton <jwe@octave.org>
date Wed, 05 Jul 2017 15:24:55 -0400
parents b40b7243a782
children 194eb4bd202b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 Copyright (C) 2017 John W. Eaton
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 Copyright (C) 2001-2017 Ben Sapp
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 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
7
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 the Free Software Foundation; either version 3 of the License, or
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 (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
12
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 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
14 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
15 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
16 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
17
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 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
19 along with Octave; see the file COPYING. If not, see
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>.
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 */
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 #if ! defined (octave_file_info_h)
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 #define octave_file_info_h 1
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 #include "octave-config.h"
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #include <deque>
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include <map>
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 #include <string>
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include <vector>
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 "oct-time.h"
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 namespace octave
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 {
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 class file_info
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 {
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 public:
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 file_info (void)
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 : m_file_buf (), m_offsets (), m_timestamp (static_cast<time_t> (0))
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
23730
85f1d31956c0 make echo work for command-line functions
John W. Eaton <jwe@octave.org>
parents: 23728
diff changeset
46 file_info (const std::string& text, const sys::time& timestamp)
85f1d31956c0 make echo work for command-line functions
John W. Eaton <jwe@octave.org>
parents: 23728
diff changeset
47 : m_file_buf (text), m_offsets (get_line_offsets (m_file_buf)),
85f1d31956c0 make echo work for command-line functions
John W. Eaton <jwe@octave.org>
parents: 23728
diff changeset
48 m_timestamp (timestamp)
85f1d31956c0 make echo work for command-line functions
John W. Eaton <jwe@octave.org>
parents: 23728
diff changeset
49 { }
85f1d31956c0 make echo work for command-line functions
John W. Eaton <jwe@octave.org>
parents: 23728
diff changeset
50
23728
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 file_info (const std::string& fname)
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 : m_file_buf (snarf_file (fname)),
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 m_offsets (get_line_offsets (m_file_buf)),
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 m_timestamp ()
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 { }
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 file_info (const file_info&) = default;
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 file_info& operator = (const file_info&) = default;
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 ~file_info (void) = default;
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::string 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
64
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 std::deque<std::string> 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
66
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 size_t num_lines (void) const { return m_offsets.size (); }
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 std::string text (void) const { return m_file_buf; }
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 std::vector<size_t> line_offsets (void) const { return m_offsets; }
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 sys::time timestamp (void) const { return m_timestamp; }
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 size_t size (void) const { return m_file_buf.length (); }
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 private:
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 // File contents as a string.
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 std::string m_file_buf;
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 // Offsets to line beginnings.
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 std::vector<size_t> m_offsets;
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 sys::time m_timestamp;
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 // 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
88 static std::string 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
89
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 static std::vector<size_t> 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
91 };
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
b40b7243a782 new class for caching file contents for debug and echo
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 #endif