タイトルでは「テキストファイルを読む」とありますが、ファイルに限定する必要はないので、まずは反復子を使って汎用的に実装したあと、ファイルを扱うための関数を多重定義していくことにします。
ところで、最近は改行文字にCRを使うケースに遭遇することはあまりなくなったものの、あえて除外する理由もないので対応していくことにしましょう。
まずは反復子を使った実装からです。
| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | template <typename InputIterator, typename OutputIterator> void convert_eol(InputIterator first, InputIterator last, std::string_view eol, OutputIterator dest, int (*p_count)[3]) {   // 出現した改行文字のカウント用配列と添字定数   int count[3]{};   constexpr int count_cr = 0;   constexpr int count_lf = 1;   constexpr int count_crlf = 2;   // 出現した改行文字のカウントと置換する改行文字の出力   auto put_eol = [eol, &dest, &count](int count_type)   {     ++count[count_type];     std::copy(eol.begin(), eol.end(), dest);   };   while (first != last)   {     switch (auto c = *first++)     {     case '\r':     next:       if (first != last)       {         switch (auto c2 = *first++)         {         case '\n':           put_eol(count_crlf);           break;         default:           put_eol(count_cr);           if (c2 == '\r')             goto next;           *dest++ = c2;           break;         }       }       else       {         put_eol(count_cr);       }       break;     case '\n':       put_eol(count_lf);       break;     default:       *dest++ = c;       break;     }   }   if (p_count)     std::memcpy(p_count, count, sizeof(count)); } | 
ちょっと長くなりましたが、元ネタも同じぐらいの長さなのでまあ良しとしましょう。この実装ではstring_viewを使っているのでC++17以降でのコンパイルが必要です。この連載はコードのサンプル的な意味合いもあるので、やや新しい言語仕様も使っていきたいと思います。
今度は、この関数に1枚被せることで、ストリームに対応させてみましょう。
| 0 1 2 3 4 5 | void convert_eol(std::istream& is, std::string_view eol, std::ostream& os, int (*p_count)[3]) {   convert_eol(std::istreambuf_iterator<char>(is.rdbuf()), std::istreambuf_iterator<char>(), eol, std::ostreambuf_iterator<char>(os), p_count); } | 
1文字ずつ読み込む必要があるので、istream_iteratorではなくistreambuf_iteratorを使っています。istream_iteratorを使うと空白類文字が読み飛ばされてしまいますので。
最後に、元ネタに近い形で入力するファイルのファイル名を渡す形の関数も実装してみることにしましょう。
| 0 1 2 3 4 5 6 7 8 9 | bool convert_eol(std::string const& filename, std::string_view eol, std::ostream& os, int (*p_count)[3]) {   std::ifstream ifs(filename, std::ios_base::in | std::ios_base::binary);   if (!ifs.is_open())     return false;   convert_eol(ifs, eol, os, p_count);   return true; } | 
こんな感じで何段階かの関数を用意しておけば汎用性が増しそうです。






