GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ls-ascii-helper.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-2021 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 "ls-ascii-helper.h"
31 
32 #include <istream>
33 #include <sstream>
34 
35 // Helper functions when reading from ascii files.
36 
37 // These functions take care of different line endings (LF, CR, CRLF)
38 // when files were opened in text mode for writing and are now opened in
39 // binary mode for reading.
40 // Even though we no longer store files in text mode, keep this logic
41 // to allow loading older files that might have CRLF or CR line endings.
42 
43 // Skip characters from stream IS until a newline is reached.
44 // Depending on KEEP_NEWLINE, either eat newline from stream or
45 // keep it unread.
46 
47 void
48 skip_until_newline (std::istream& is, bool keep_newline)
49 {
50  if (! is)
51  return;
52 
53  while (is)
54  {
55  char c = is.peek ();
56 
57  if (c == '\n' || c == '\r')
58  {
59  // Reached newline.
60  if (! keep_newline)
61  {
62  // Eat the CR or LF character.
63  char d;
64  is.get (d);
65 
66  // Make sure that for binary-mode opened ascii files
67  // containing CRLF line endings we skip the LF after CR.
68  if (c == '\r' && is.peek () == '\n')
69  {
70  // Yes, LF following CR, eat it.
71  is.get (d);
72  }
73  }
74 
75  // Newline was found, and read from stream if
76  // keep_newline == true, so exit loop.
77  break;
78  }
79  else
80  {
81  // No newline character peeked, so read it and proceed to next
82  // character.
83  char d;
84  is.get (d);
85  }
86  }
87 }
88 
89 // If stream IS currently points to a newline (a leftover from a previous read)
90 // then eat newline(s) until a non-newline character is found.
91 
92 void
93 skip_preceeding_newline (std::istream& is)
94 {
95  if (! is)
96  return;
97 
98  // Check whether IS currently points to newline character.
99  char c = is.peek ();
100 
101  if (c == '\n' || c == '\r')
102  {
103  // Yes, at newline.
104  do
105  {
106  // Eat the CR or LF character.
107  char d;
108  is.get (d);
109 
110  // Make sure that for binary-mode opened ascii files
111  // containing CRLF line endings we skip the LF after CR.
112  if (c == '\r' && is.peek () == '\n')
113  {
114  // Yes, LF following CR, eat it.
115  is.get (d);
116  }
117 
118  // Peek into next character.
119  c = is.peek ();
120 
121  // Loop while still a newline ahead.
122  }
123  while (c == '\n' || c == '\r');
124  }
125 }
126 
127 // Read characters from stream IS until a newline is reached.
128 // Depending on KEEP_NEWLINE, either eat newline from stream or keep
129 // it unread. Characters read are stored and returned as
130 // std::string.
131 
132 std::string
133 read_until_newline (std::istream& is, bool keep_newline)
134 {
135  if (! is)
136  return "";
137 
138  std::ostringstream buf;
139 
140  while (is)
141  {
142  char c = is.peek ();
143 
144  if (c == '\n' || c == '\r')
145  {
146  // Reached newline.
147  if (! keep_newline)
148  {
149  // Eat the CR or LF character.
150  char d;
151  is.get (d);
152 
153  // Make sure that for binary-mode opened ascii files
154  // containing CRLF line endings we skip the LF after
155  // CR.
156 
157  if (c == '\r' && is.peek () == '\n')
158  {
159  // Yes, LF following CR, eat it.
160  is.get (d);
161  }
162  }
163 
164  // Newline was found, and read from stream if
165  // keep_newline == true, so exit loop.
166  break;
167  }
168  else
169  {
170  // No newline character peeked, so read it, store it, and
171  // proceed to next.
172  char d;
173  is.get (d);
174  buf << d;
175  }
176  }
177 
178  return buf.str ();
179 }
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void skip_until_newline(std::istream &is, bool keep_newline)
std::string read_until_newline(std::istream &is, bool keep_newline)
void skip_preceeding_newline(std::istream &is)