Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:36

0001 // -*- C++ -*-
0002 //
0003 // This file is part of YODA -- Yet more Objects for Data Analysis
0004 // Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
0005 //
0006 #ifndef YODA_GETLINE_H
0007 #define YODA_GETLINE_H
0008 
0009 #include <iostream>
0010 #include <string>
0011 
0012 namespace YODA {
0013   namespace Utils {
0014 
0015 
0016     // Portable version of getline taken from
0017     // http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
0018     inline std::istream& getline(std::istream& is, std::string& t) {
0019       t.clear();
0020 
0021       // The characters in the stream are read one-by-one using a std::streambuf.
0022       // That is faster than reading them one-by-one using the std::istream.
0023       // Code that uses streambuf this way must be guarded by a sentry object.
0024       // The sentry object performs various tasks,
0025       // such as thread synchronization and updating the stream state.
0026       std::istream::sentry se(is, true);
0027       std::streambuf* sb = is.rdbuf();
0028 
0029       for (;;) {
0030         int c = sb->sbumpc();
0031         switch (c) {
0032         case '\n':
0033           return is;
0034         case '\r':
0035           if (sb->sgetc() == '\n')
0036             sb->sbumpc();
0037           return is;
0038         case EOF:
0039           // Also handle the case when the last line has no line ending
0040           if (t.empty())
0041             is.setstate(std::ios::eofbit);
0042           return is;
0043         default:
0044           t += (char)c;
0045         }
0046       }
0047     }
0048 
0049 
0050   }
0051 }
0052 
0053 #endif