GNU Octave  4.4.1
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-2018 Benjamin Lindner
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "ls-ascii-helper.h"
28 
29 #include <iostream>
30 #include <sstream>
31 
32 // Helper functions when reading from ascii files.
33 
34 // These function take care of CR/LF issues when files are opened in
35 // text-mode for reading.
36 
37 // Skip characters from stream IS until a newline is reached.
38 // Depending on KEEP_NEWLINE, either eat newline from stream or
39 // keep it unread.
40 
41 void
42 skip_until_newline (std::istream& is, bool keep_newline)
43 {
44  if (! is)
45  return;
46 
47  while (is)
48  {
49  char c = is.peek ();
50 
51  if (c == '\n' || c == '\r')
52  {
53  // Reached newline.
54  if (! keep_newline)
55  {
56  // Eat the CR or LF character.
57  char d;
58  is.get (d);
59 
60  // Make sure that for binary-mode opened ascii files
61  // containing CRLF line endings we skip the LF after CR.
62  if (c == '\r' && is.peek () == '\n')
63  {
64  // Yes, LF following CR, eat it.
65  is.get (d);
66  }
67  }
68 
69  // Newline was found, and read from stream if
70  // keep_newline == true, so exit loop.
71  break;
72  }
73  else
74  {
75  // No newline charater peeked, so read it and proceed to next
76  // character.
77  char d;
78  is.get (d);
79  }
80  }
81 }
82 
83 // If stream IS currently points to a newline (a leftover from a previous read)
84 // then eat newline(s) until a non-newline character is found.
85 
86 void
87 skip_preceeding_newline (std::istream& is)
88 {
89  if (! is)
90  return;
91 
92  // Check whether IS currently points to newline character.
93  char c = is.peek ();
94 
95  if (c == '\n' || c == '\r')
96  {
97  // Yes, at newline.
98  do
99  {
100  // Eat the CR or LF character.
101  char d;
102  is.get (d);
103 
104  // Make sure that for binary-mode opened ascii files
105  // containing CRLF line endings we skip the LF after CR.
106  if (c == '\r' && is.peek () == '\n')
107  {
108  // Yes, LF following CR, eat it.
109  is.get (d);
110  }
111 
112  // Peek into next character.
113  c = is.peek ();
114 
115  // Loop while still a newline ahead.
116  }
117  while (c == '\n' || c == '\r');
118  }
119 }
120 
121 // Read charaters from stream IS until a newline is reached.
122 // Depending on KEEP_NEWLINE, either eat newline from stream or keep
123 // it unread. Characters read are stored and returned as
124 // std::string.
125 
127 read_until_newline (std::istream& is, bool keep_newline)
128 {
129  if (! is)
130  return "";
131 
132  std::ostringstream buf;
133 
134  while (is)
135  {
136  char c = is.peek ();
137 
138  if (c == '\n' || c == '\r')
139  {
140  // Reached newline.
141  if (! keep_newline)
142  {
143  // Eat the CR or LF character.
144  char d;
145  is.get (d);
146 
147  // Make sure that for binary-mode opened ascii files
148  // containing CRLF line endings we skip the LF after
149  // CR.
150 
151  if (c == '\r' && is.peek () == '\n')
152  {
153  // Yes, LF following CR, eat it.
154  is.get (d);
155  }
156  }
157 
158  // Newline was found, and read from stream if
159  // keep_newline == true, so exit loop.
160  break;
161  }
162  else
163  {
164  // No newline charater peeked, so read it, store it, and
165  // proceed to next.
166  char d;
167  is.get (d);
168  buf << d;
169  }
170  }
171 
172  return buf.str ();
173 }
std::string read_until_newline(std::istream &is, bool keep_newline)
void skip_until_newline(std::istream &is, bool keep_newline)
void skip_preceeding_newline(std::istream &is)
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
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
write the output to stdout if nargout is
Definition: load-save.cc:1612
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