@@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
34
35
35
#include < cstring>
36
36
#include < sstream>
37
+ #include < string_view>
37
38
38
39
// ----------------------------------------------------------------------------
39
40
@@ -625,28 +626,28 @@ struct _col_data_spec_ {
625
626
626
627
template <typename I, typename H>
627
628
void DataFrame<I, H>::
628
- read_csv2_ (std::istream & stream,
629
+ read_csv2_ (std::FILE * stream,
629
630
bool columns_only,
630
631
size_type starting_row,
631
632
size_type num_rows) {
632
633
633
634
using SpecVec = StlVecType<_col_data_spec_>;
634
635
635
- std::string line;
636
+ char line[ 64 * 1024 ] ;
636
637
std::string value;
637
638
SpecVec spec_vec;
638
639
bool header_read { false };
639
640
size_type col_count { 0 };
640
641
size_type data_rows_read { 0 };
641
642
size_type row_cnt { 0 };
642
643
643
- line.reserve (1024 );
644
644
value.reserve (64 );
645
645
spec_vec.reserve (32 );
646
- while (! stream.eof ()) {
647
- std::getline (stream, line);
646
+ while (! std::feof (stream)) {
647
+ line[0 ] = ' \0 ' ;
648
+ std::fgets (line, sizeof (line) - 1 , stream);
648
649
649
- if (line. size () < 2 || line. empty () || line [0 ] == ' #' ) continue ;
650
+ if (line[ 0 ] == ' \0 ' || line[0 ] == ' #' ) [[unlikely]] continue ;
650
651
651
652
std::stringstream sstream { line };
652
653
@@ -1531,17 +1532,25 @@ read (const char *file_name,
1531
1532
size_type starting_row,
1532
1533
size_type num_rows) {
1533
1534
1534
- std::ifstream stream;
1535
- const IOStreamOpti io_opti (stream, file_name, iof == io_format::binary );
1535
+ if (iof == io_format::csv2) {
1536
+ IOFileOpti io_opti (file_name );
1536
1537
1537
- if (stream.fail ()) [[unlikely]] {
1538
- String1K err;
1539
-
1540
- err.printf (" read(): ERROR: Unable to open file '%s'" , file_name);
1541
- throw DataFrameError (err.c_str ());
1538
+ read_csv2_ (io_opti.file , columns_only, starting_row, num_rows);
1542
1539
}
1540
+ else {
1541
+ std::ifstream stream;
1542
+ const IOStreamOpti io_opti (stream,
1543
+ file_name, iof == io_format::binary);
1544
+
1545
+ if (stream.fail ()) [[unlikely]] {
1546
+ String1K err;
1543
1547
1544
- read <std::istream>(stream, iof, columns_only, starting_row, num_rows);
1548
+ err.printf (" read(): ERROR: Unable to open file '%s'" , file_name);
1549
+ throw DataFrameError (err.c_str ());
1550
+ }
1551
+
1552
+ read <std::istream>(stream, iof, columns_only, starting_row, num_rows);
1553
+ }
1545
1554
return (true );
1546
1555
}
1547
1556
@@ -1568,7 +1577,8 @@ read (S &in_s,
1568
1577
read_csv_ (in_s, columns_only);
1569
1578
}
1570
1579
else if (iof == io_format::csv2) {
1571
- read_csv2_ (in_s, columns_only, starting_row, num_rows);
1580
+ throw NotImplemented (" read(): You can read a file in io_format::csv2 "
1581
+ " format only by calling read() with file name" );
1572
1582
}
1573
1583
else if (iof == io_format::json) {
1574
1584
if (starting_row != 0 ||
0 commit comments