1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/ls-ascii-helper.cc Wed Mar 18 15:23:14 2009 +0100
1.3 @@ -0,0 +1,160 @@
1.4 +/*
1.5 +
1.6 +Copyright (C) 2003, 2005, 2006, 2007 John W. Eaton
1.7 +
1.8 +This file is part of Octave.
1.9 +
1.10 +Octave is free software; you can redistribute it and/or modify it
1.11 +under the terms of the GNU General Public License as published by the
1.12 +Free Software Foundation; either version 3 of the License, or (at your
1.13 +option) any later version.
1.14 +
1.15 +Octave is distributed in the hope that it will be useful, but WITHOUT
1.16 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.17 +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.18 +for more details.
1.19 +
1.20 +You should have received a copy of the GNU General Public License
1.21 +along with Octave; see the file COPYING. If not, see
1.22 +<http://www.gnu.org/licenses/>.
1.23 +
1.24 +*/
1.25 +
1.26 +
1.27 +#include "ls-ascii-helper.h"
1.28 +
1.29 +#include <iostream>
1.30 +#include <sstream>
1.31 +
1.32 +// Helper functions when reading from ascii files.
1.33 +// These function take care of CR/LF issues when files are opened in text-mode for reading
1.34 +
1.35 +// Skip characters from stream IS until a newline is reached.
1.36 +// Depending on KEEP_NEWLINE, either eat newline from stream or
1.37 +// keep it unread
1.38 +
1.39 +void
1.40 +skip_until_newline( std::istream& is, bool keep_newline )
1.41 +{
1.42 + if (!is)
1.43 + return;
1.44 +
1.45 + char c,d;
1.46 +
1.47 + while (is)
1.48 + {
1.49 + c = is.peek();
1.50 + if (c == '\n' || c == '\r')
1.51 + {
1.52 + // reached newline
1.53 + if (keep_newline == false)
1.54 + {
1.55 + // eat the CR or LF character
1.56 + is.get(d);
1.57 +
1.58 + // make sure that for binary-mode opened ascii files containing CRLF line endings
1.59 + // we skip the LF after CR...
1.60 + if (c == '\r' && is.peek()=='\n')
1.61 + {
1.62 + // yes, LF following CR, eat it...
1.63 + is.get(d);
1.64 + }
1.65 + }
1.66 +
1.67 + // Newline was found, and read from stream if keep_newline==true, so exit loop
1.68 + break;
1.69 + }
1.70 + else
1.71 + // no newline charater peeked, so read it and proceed to next character
1.72 + is.get(d);
1.73 + }
1.74 +
1.75 + return;
1.76 +}
1.77 +
1.78 +
1.79 +// If stream IS currently points to a newline (a leftover from a previous read)
1.80 +// then eat newline(s) until a non-newline character is found
1.81 +
1.82 +void
1.83 +skip_preceeding_newline( std::istream& is )
1.84 +{
1.85 + if (!is)
1.86 + return;
1.87 +
1.88 + char c,d;
1.89 +
1.90 + // Check if IS currently points to newline character
1.91 + c = is.peek();
1.92 + if (c == '\n' || c == '\r')
1.93 + {
1.94 + // Yes, at newline
1.95 + do {
1.96 + // eat the CR or LF character
1.97 + is.get(d);
1.98 +
1.99 + // make sure that for binary-mode opened ascii files containing CRLF line endings
1.100 + // we skip the LF after CR...
1.101 + if (c == '\r' && is.peek() == '\n')
1.102 + {
1.103 + // yes, LF following CR, eat it...
1.104 + is.get(d);
1.105 + }
1.106 +
1.107 + // Peek into next character
1.108 + c = is.peek();
1.109 + // Loop while still a newline ahead
1.110 + } while( c == '\n' || c == '\r' );
1.111 + }
1.112 +
1.113 + return;
1.114 +}
1.115 +
1.116 +
1.117 +// Read charaters from stream IS until a newline is reached.
1.118 +// Depending on KEEP_NEWLINE, either eat newline from stream or
1.119 +// keep it unread
1.120 +// Characters read are stored and returned as std::string
1.121 +
1.122 +std::string
1.123 +read_until_newline( std::istream& is, bool keep_newline )
1.124 +{
1.125 + if (!is)
1.126 + return std::string();
1.127 +
1.128 + char c,d;
1.129 + std::ostringstream buf;
1.130 +
1.131 + while (is)
1.132 + {
1.133 + c = is.peek();
1.134 + if (c == '\n' || c == '\r')
1.135 + {
1.136 + // reached newline
1.137 + if (keep_newline == false)
1.138 + {
1.139 + // eat the CR or LF character
1.140 + is.get(d);
1.141 +
1.142 + // make sure that for binary-mode opened ascii files containing CRLF line endings
1.143 + // we skip the LF after CR...
1.144 + if (c == '\r' && is.peek() == '\n')
1.145 + {
1.146 + // yes, LF following CR, eat it...
1.147 + is.get(d);
1.148 + }
1.149 + }
1.150 +
1.151 + // Newline was found, and read from stream if keep_newline==true, so exit loop
1.152 + break;
1.153 + }
1.154 + else
1.155 + {
1.156 + // no newline charater peeked, so read it, store it, and proceed to next
1.157 + is.get(d);
1.158 + buf << d;
1.159 + }
1.160 + }
1.161 +
1.162 + return buf.str();
1.163 +}