| 687 | |
| 688 | |
| 689 | FileEncoding ASConsole::readFile(const string &fileName_, stringstream &in) const |
| 690 | { |
| 691 | const int blockSize = 65536; // 64 KB |
| 692 | ifstream fin(fileName_.c_str(), ios::binary); |
| 693 | if (!fin) |
| 694 | error("Cannot open input file", fileName_.c_str()); |
| 695 | char* data = new(nothrow) char[blockSize]; |
| 696 | if (!data) |
| 697 | error("Cannot allocate memory for input file", fileName_.c_str()); |
| 698 | fin.read(data, sizeof(data)); |
| 699 | if (fin.bad()) |
| 700 | error("Cannot read input file", fileName_.c_str()); |
| 701 | size_t dataSize = static_cast<size_t>(fin.gcount()); |
| 702 | FileEncoding encoding = detectEncoding(data, dataSize); |
| 703 | if (encoding == UTF_32BE || encoding == UTF_32LE) |
| 704 | error(_("Cannot process UTF-32 encoding"), fileName_.c_str()); |
| 705 | bool firstBlock = true; |
| 706 | while (dataSize) |
| 707 | { |
| 708 | if (encoding == UTF_16LE || encoding == UTF_16BE) |
| 709 | { |
| 710 | // convert utf-16 to utf-8 |
| 711 | size_t utf8Size = Utf8LengthFromUtf16(data, dataSize, encoding); |
| 712 | char* utf8Out = new(nothrow) char[utf8Size]; |
| 713 | if (!utf8Out) |
| 714 | error("Cannot allocate memory for utf-8 conversion", fileName_.c_str()); |
| 715 | size_t utf8Len = Utf16ToUtf8(data, dataSize, encoding, firstBlock, utf8Out); |
| 716 | assert(utf8Len == utf8Size); |
| 717 | in << string(utf8Out, utf8Len); |
| 718 | delete []utf8Out; |
| 719 | } |
| 720 | else |
| 721 | in << string(data, dataSize); |
| 722 | fin.read(data, sizeof(data)); |
| 723 | if (fin.bad()) |
| 724 | error("Cannot read input file", fileName_.c_str()); |
| 725 | dataSize = static_cast<size_t>(fin.gcount()); |
| 726 | firstBlock = false; |
| 727 | } |
| 728 | fin.close(); |
| 729 | delete [] data; |
| 730 | return encoding; |
| 731 | } |
| 732 | |
| 733 | void ASConsole::setIgnoreExcludeErrors(bool state) |
| 734 | { ignoreExcludeErrors = state; } |
nothing calls this directly
no outgoing calls
no test coverage detected