Mercurial > hg > octave-avbm
comparison src/oct-stream.cc @ 9701:531280b07625
implement fskipl
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 07 Oct 2009 11:08:54 +0200 |
parents | 34d6f005db4b |
children | 1369f13ae6b2 |
comparison
equal
deleted
inserted
replaced
9700:95a30d00f779 | 9701:531280b07625 |
---|---|
1066 | 1066 |
1067 std::string | 1067 std::string |
1068 octave_base_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) | 1068 octave_base_stream::gets (octave_idx_type max_len, bool& err, const std::string& who) |
1069 { | 1069 { |
1070 return do_gets (max_len, err, false, who); | 1070 return do_gets (max_len, err, false, who); |
1071 } | |
1072 | |
1073 long | |
1074 octave_base_stream::skipl (long num, bool& err, const std::string& who) | |
1075 { | |
1076 long cnt = -1; | |
1077 | |
1078 if ((interactive || forced_interactive) && file_number () == 0) | |
1079 { | |
1080 ::error ("%s: unable to read from stdin while running interactively", | |
1081 who.c_str ()); | |
1082 | |
1083 return count; | |
1084 } | |
1085 | |
1086 err = false; | |
1087 | |
1088 std::istream *isp = input_stream (); | |
1089 | |
1090 if (isp) | |
1091 { | |
1092 std::istream& is = *isp; | |
1093 | |
1094 int c = 0, lastc = -1; | |
1095 cnt = 0; | |
1096 | |
1097 while (is && (c = is.get ()) != EOF) | |
1098 { | |
1099 // Handle CRLF, CR, or LF as line ending. | |
1100 | |
1101 if (c == '\r' || (c == '\n' && lastc != '\r')) | |
1102 { | |
1103 if (++cnt == num) | |
1104 break; | |
1105 } | |
1106 | |
1107 lastc = c; | |
1108 } | |
1109 | |
1110 // Maybe eat the following \n if \r was just met. | |
1111 if (c == '\r' && is.peek () == '\n') | |
1112 is.get (); | |
1113 | |
1114 if (is.bad ()) | |
1115 { | |
1116 err = true; | |
1117 error (who, "read error"); | |
1118 } | |
1119 | |
1120 if (err) | |
1121 cnt = -1; | |
1122 } | |
1123 else | |
1124 { | |
1125 err = true; | |
1126 invalid_operation (who, "reading"); | |
1127 } | |
1128 | |
1129 return cnt; | |
1071 } | 1130 } |
1072 | 1131 |
1073 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) | 1132 #define OCTAVE_SCAN(is, fmt, arg) octave_scan (is, fmt, arg) |
1074 | 1133 |
1075 template <class T> | 1134 template <class T> |
2916 retval = gets (max_len, err, who); | 2975 retval = gets (max_len, err, who); |
2917 | 2976 |
2918 return retval; | 2977 return retval; |
2919 } | 2978 } |
2920 | 2979 |
2980 long | |
2981 octave_stream::skipl (long count, bool& err, const std::string& who) | |
2982 { | |
2983 long retval = -1; | |
2984 | |
2985 if (stream_ok ()) | |
2986 retval = rep->skipl (count, err, who); | |
2987 | |
2988 return retval; | |
2989 } | |
2990 | |
2991 long | |
2992 octave_stream::skipl (const octave_value& tc_count, bool& err, const std::string& who) | |
2993 { | |
2994 long retval = -1; | |
2995 | |
2996 err = false; | |
2997 | |
2998 int conv_err = 0; | |
2999 | |
3000 int count = 1; | |
3001 | |
3002 if (tc_count.is_defined ()) | |
3003 { | |
3004 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ())) | |
3005 count = -1; | |
3006 else | |
3007 { | |
3008 count = convert_to_valid_int (tc_count, conv_err); | |
3009 | |
3010 if (conv_err || count < 0) | |
3011 { | |
3012 err = true; | |
3013 ::error ("%s: invalid number of lines specified", who.c_str ()); | |
3014 } | |
3015 } | |
3016 } | |
3017 | |
3018 if (! error_state) | |
3019 retval = skipl (count, err, who); | |
3020 | |
3021 return retval; | |
3022 } | |
3023 | |
2921 int | 3024 int |
2922 octave_stream::seek (long offset, int origin) | 3025 octave_stream::seek (long offset, int origin) |
2923 { | 3026 { |
2924 int status = -1; | 3027 int status = -1; |
2925 | 3028 |