GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ls-ascii-helper.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef 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 
84 // If stream IS currently points to a newline (a leftover from a
85 // previous read) then eat newline(s) until a non-newline character is
86 // found.
87 
88 void
89 skip_preceeding_newline (std::istream& is)
90 {
91  if (! is)
92  return;
93 
94  // Check whether IS currently points to newline character.
95  char c = is.peek ();
96 
97  if (c == '\n' || c == '\r')
98  {
99  // Yes, at newline.
100  do
101  {
102  // Eat the CR or LF character.
103  char d;
104  is.get (d);
105 
106  // Make sure that for binary-mode opened ascii files
107  // containing CRLF line endings we skip the LF after CR.
108  if (c == '\r' && is.peek () == '\n')
109  {
110  // Yes, LF following CR, eat it.
111  is.get (d);
112  }
113 
114  // Peek into next character.
115  c = is.peek ();
116 
117  // Loop while still a newline ahead.
118  }
119  while (c == '\n' || c == '\r');
120  }
121 }
122 
123 // Read charaters from stream IS until a newline is reached.
124 // Depending on KEEP_NEWLINE, either eat newline from stream or keep
125 // it unread. Characters read are stored and returned as
126 // std::string.
127 
128 std::string
129 read_until_newline (std::istream& is, bool keep_newline)
130 {
131  if (! is)
132  return std::string ();
133 
134  std::ostringstream buf;
135 
136  while (is)
137  {
138  char c = is.peek ();
139 
140  if (c == '\n' || c == '\r')
141  {
142  // Reached newline.
143  if (! keep_newline)
144  {
145  // Eat the CR or LF character.
146  char d;
147  is.get (d);
148 
149  // Make sure that for binary-mode opened ascii files
150  // containing CRLF line endings we skip the LF after
151  // CR.
152 
153  if (c == '\r' && is.peek () == '\n')
154  {
155  // Yes, LF following CR, eat it.
156  is.get (d);
157  }
158  }
159 
160  // Newline was found, and read from stream if
161  // keep_newline == true, so exit loop.
162  break;
163  }
164  else
165  {
166  // No newline charater peeked, so read it, store it, and
167  // proceed to next.
168  char d;
169  is.get (d);
170  buf << d;
171  }
172  }
173 
174  return buf.str ();
175 }
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)
F77_RET_T const double const double double * d