2

I have some *.bz2 file, that contains *.csv text file.

I need to process it - to unpack it and to change encoding of it.

Now I have code, that unpacks this file and changes *.csv file encoding by reading original file to another one. But I "got situation" with this moment - *.csv file is huge (5 GB), but server has only 8 GB. IE, if I will change file encoding by copying it to another one, I will not have hdd space for it.

So, I need to change encoding of the file "on-fly".

Currently, I have two pieces of code:

Unpacking

bool NetIO::unBZ2(string PathToBZ2, string PathToCSV) {
       try {

             boost::locale::generator gen;

             ofstream outFile("c:/temp/sqlShare/qqq.csv", std::ofstream::out | std::ofstream::binary);

             ifstream file(PathToBZ2, ios_base::in | ios_base::binary);

             filtering_streambuf<input> in;
             in.push(bzip2_decompressor());
             in.push(file);
             boost::iostreams::copy(in, outFile);

             file.close();                    
             outFile.close();
       }
       catch (const bzip2_error& exception) {
             int error = exception.error();

             if (error == boost::iostreams::bzip2::data_error) {
                    // compressed data stream is corrupted
                    cout << "compressed data stream is corrupted";
             }
             else if (error == boost::iostreams::bzip2::data_error_magic)
             {
                    // compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'
                    cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
             }
             else if (error == boost::iostreams::bzip2::config_error) {
                    // libbzip2 has been improperly configured for the current platform
                    cout << "libbzip2 has been improperly configured for the current platform";
             }
             return false;
       }

       return true;
}

and code for changing encoding of file

bool NetIO::replaceLineBreaks(const string& inFilePath, const string& searchStr, const string& replacement) {

       try {     

             boost::locale::generator gen;

             ifstream iss(inFilePath.c_str(), std::ios::in | std::ios::binary);

             std::locale lru = gen("ru_RU.CP1251");
             std::locale lru2 = gen("ru_RU.UTF-8");
             iss.imbue(lru2);    

             //string tempFilePath2 = inFilePath + ".tmp3";
             string tempFilePath2 = "d:/temp/qqq.tmp3";
             ofstream os(tempFilePath2, std::ios::out | std::ios::binary);
             os.imbue(lru);

             const int buffSize= 500000;
             char qqq[buffSize];
             //wchar_t wqqq[buffSize];
             while (iss) {
                    iss.read(qqq, buffSize);
             //     MultiByteToWideChar(CP_UTF8, 0, qqq, buffSize, wqqq, buffSize);
                    os << qqq;
                    //cnt++;
                    /*if (cnt > 80) {
                           break;
                    }*/
             }

             os.close();



             /*boost::iostreams::mapped_file istm(inFilePath.c_str());
             if (!istm.is_open())
                    return false;

             string tempFilePath = inFilePath + ".tmp";
             ofstream ostm(tempFilePath.c_str(), std::ios::out | std::ios::binary);
             if (!ostm.is_open()) {
                    istm.close();
                    return false;
             }

             boost::regex regexp(searchStr);
             ostreambuf_iterator<char> it(ostm);
             boost::regex_replace(it, istm.begin(), istm.end(), regexp, replacement, boost::match_default | boost::format_all);

             istm.close();
             ostm.close();

             boost::filesystem::rename(tempFilePath, inFilePath);*/
       }
       catch (exception ex) {
             cout << "Problem in line endings replacer" << std::endl;
             cout << ex.what() << std::endl;
             cout << "-----------------" << std::endl;
             return false;
       }

       return true;
}

I am trying to understand, how can I combine these two pieces of code?

As I understand, possible solution could be to create a custom ofstream object, that will receive data and will convert encoding of it, and to pass data to it with boost::iostreams::copy .

How can I combine these two pieces of code?

Thank you very much!

Arthur
  • 3,253
  • 7
  • 43
  • 75
  • what is the relevance of all the commented code? Does the code do anything with e.g. `MultiByteToWideChar` commented out? – sehe Dec 01 '16 at 13:40
  • Commented code is just my experiments... It doesn't work well. – Arthur Dec 01 '16 at 14:51

1 Answers1

2

Your code in the second function is questionable, where you do

iss.read(qqq, buffSize);
os << qqq;

Not only does it assume the full size of qqq is always read, but also qqq decays from char(&)[500000] to char* and will be interpreted as NUL-terminated string. This at least breaks with embedded NUL chars. Prefer:

auto bytes_read = iss.read(qqq, buffSize);
os.write(qqq, bytes_read);

Note: boost::iostreams::copy already does the correct thing, using the default buffersize (if unspecified)

#ifndef BOOST_IOSTREAMS_DEFAULT_DEVICE_BUFFER_SIZE
# define BOOST_IOSTREAMS_DEFAULT_DEVICE_BUFFER_SIZE 4096
#endif

Next, the loops are easily combined to something like

// input
std::ifstream file(PathToBZ2, std::ios::binary);

bio::filtering_stream<bio::input> in;
in.push(bio::bzip2_decompressor());
in.push(file);

std::locale lru2 = gen("ru_RU.UTF-8");
in.imbue(lru2);

// output
std::ofstream outFile("c:/temp/sqlShare/qqq.csv", std::ios::binary);
std::locale lru = gen("ru_RU.CP1251");
outFile.imbue(lru);

// copy
const int buffSize = 500000;
/*auto totalWritten =*/ boost::iostreams::copy(in, outFile, buffSize);

Note this uses filtering_stream, not filtering_streambuf

UPDATE

Forcing the required conversions using code_converter<>:

Live On Coliru

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/code_converter.hpp>
#include <boost/locale.hpp>
#include <iostream>
#include <fstream>

namespace bio = boost::iostreams;

bool unBZ2(std::string PathToBZ2, std::string PathToCSV) {
    try {
        boost::locale::generator gen;

        // input
        std::ifstream file(PathToBZ2, std::ios::binary);

        bio::filtering_stream<bio::input> in;
        in.push(bio::bzip2_decompressor());
        in.push(file);

        std::locale lru2 = gen("ru_RU.UTF-8");
        in.imbue(lru2);

        // output
        std::ofstream outFile(PathToCSV, std::ios::binary);
        std::locale lru = gen("ru_RU.CP1251");
        outFile.imbue(lru);

        {
            bio::code_converter<bio::filtering_stream<bio::input> > win(in);
            bio::code_converter<decltype(outFile)> wout(outFile);

            win.imbue(lru2);
            wout.imbue(lru);
            const int buffSize = 500000;
            /*auto totalWritten =*/ boost::iostreams::copy(win, wout, buffSize);
        }
    } catch (const bio::bzip2_error &exception) {
        int error = exception.error();

        if (error == boost::iostreams::bzip2::data_error) {
            // compressed data stream is corrupted
            std::cout << "compressed data stream is corrupted";
        } else if (error == boost::iostreams::bzip2::data_error_magic) {
            // compressed data stream does not begin with the 'magic' sequence 'B' 'Z'
            // 'h'
            std::cout << "compressed data stream does not begin with the 'magic' sequence " "'B' 'Z' 'h'";
        } else if (error == boost::iostreams::bzip2::config_error) {
            // libbzip2 has been improperly configured for the current platform
            std::cout << "libbzip2 has been improperly configured for the current platform";
        }
        return false;
    }

    return true;
}

int main() {
    unBZ2("input.txt.bz2", "output.txt");
}

Translates "Lorem Ipsum".ru into

00000000: cbee f0e5 ec20 e8ef f1f3 ec20 e4ee ebee  ..... ..... ....
00000010: f020 f1e8 f220 e0ec e5f2 2c20 f1ee ebf3  . ... ...., ....
00000020: ec20 eff0 e8ec e8f1 20e0 ed20 f1e5 e42e  . ...... .. ....
00000030: 20cd e5f6 20e5 f220 eee4 e8ee 20e4 e8f1   ... .. .... ...
00000040: eff3 f2e0 ede4 ee2c 20ef f0e8 20ed e50a  ......., ... ...
00000050: f4ee f0e5 edf1 e8e1 f3f1 20e2 eeeb f3ef  .......... .....
00000060: f2e0 f2e8 e1f3 f12e 20c5 efe8 f6f3 f0e8  ........ .......
00000070: 20f6 eeec ecf3 ede5 20f1 f3f1 f6e8 efe8   ....... .......
00000080: f220 e5f5 20ef f0ee 2e20 cff0 eee1 ee20  . .. .... ..... 
00000090: f4e5 f3e3 e0e8 f20a f0e5 f6f2 e5ff f3e5  ................
000000a0: 20f3 f220 eff0 e82c 20ec e5e8 20f2 eeeb   .. ..., ... ...
000000b0: ebe8 f220 ecee ebe5 f1f2 e8e5 20e4 e8f1  ... ........ ...
000000c0: eff3 f2e0 ede4 ee20 f6f3 2e20 d1e8 edf2  ....... ... ....
000000d0: 20e1 f0f3 f2e5 20ec e0e7 e8ec 20e5 eef1   ..... ..... ...
000000e0: 20e5 f32c 0ae1 f0f3 f2e5 20e0 ebf2 e5f0   ..,...... .....
000000f0: e020 eced e5f1 e0f0 f7f3 ec20 f6f3 20ec  . ......... .. .
00000100: e5eb 2c20 e5e8 20f5 e0f1 20eb f3ef f2e0  .., .. ... .....
00000110: f2f3 ec20 f6ee edf1 e5f6 f2e5 f2f3 e5f0  ... ............
00000120: 2e0a 0acd e520 e0eb e8e8 20ff f3e0 f120  ..... .... .... 
00000130: e0f1 f1e5 edf2 e8ee f020 e2e8 f12c 20e5  ......... ..., .
00000140: e820 e2e8 ec20 ecf3 f6e8 f3f1 20ec e5e4  . ... ...... ...
00000150: e8ee f6f0 e8f2 e0f2 e5ec 2c20 eff0 ee20  .........., ... 
00000160: e5e0 20e0 e5ff f3e5 20eb e0ee f0e5 e5f2  .. ..... .......
00000170: 0aef f5e0 e5e4 f0f3 ec2e 20c5 eef1 20f1  .......... ... .
00000180: f3ec ee20 e0e4 f5f3 f620 f6ee edf1 e5ff  ... ..... ......
00000190: f3e0 f220 e5f5 2c20 e8e4 20f5 e0f1 20eb  ... .., .. ... .
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you very much for your answer. But I have already tried this way... ((( In this case, I got in c:/temp/sqlShare/qqq.csv file with UTF-8 encoding, not in CP1251... ((( – Arthur Dec 01 '16 at 14:31
  • Well. I haven't tested things. I don't have a sample input, no expected output AND would likely not have the required locale. So, if you can provide me with those (and a working piece of code that _does_ convert to CP1251) I'll happily test until I have a working version. – sehe Dec 01 '16 at 14:34
  • Here is my code: http://pastebin.com/mxFm79mY . And here is bz2 archive https://guvm.mvd.ru/upload/expired-passports/list_of_expired_passports.csv.bz2 . This archive is unpacking with my code in UTF-8, not in CP1251, as I expected... – Arthur Dec 01 '16 at 14:40
  • 2
    I'm not sure you should be sharing that document with me – sehe Dec 01 '16 at 14:41
  • I can't understand, why? ) – Arthur Dec 01 '16 at 14:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129548/discussion-between-sehe-and-arthur-khusnutdinov). – sehe Dec 01 '16 at 14:46
  • @ArthurKhusnutdinov I finally "fixed" it. I don't know whether it's optimal. [Fixed code](http://coliru.stacked-crooked.com/a/a867c198f3103d3a) (And Coliru clearly [doesn't have the required locales](http://coliru.stacked-crooked.com/a/f9d0a07667ebcf8f)). – sehe Dec 01 '16 at 17:47
  • Thank you very much! I will test it now. Can you explain me, how to create file with texts, for tests, on Coliru? Thank you! – Arthur Dec 02 '16 at 12:07
  • That's already in my Coliru link. However it won't work unless you pick locales that have been installed on Coliru – sehe Dec 02 '16 at 12:09
  • OK, but how to create bz2 or text file on Coliru? May be some FTP access? And what is http://coliru.stacked-crooked.com/ , owned by whom? – Arthur Dec 02 '16 at 12:10
  • Coliru is owned by @stacked-crooked – sehe Dec 02 '16 at 12:12
  • Ahh, really - cat<input.txt.bz2; ))) . And who is @stacked-crooked? ) – Arthur Dec 02 '16 at 12:14
  • Well, http://coliru.stacked-crooked.com/a/a867c198f3103d3a , is not working ((( Output file is in UTF-8 (((... As I expected... P.S. I tested your code on my PC... – Arthur Dec 02 '16 at 12:18