| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation, either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include "plink2_decompress.h" |
| 18 |
|
| 19 |
#ifdef __cplusplus |
| 20 |
namespace plink2 { |
| 21 |
#endif |
| 22 |
|
| 23 |
PglErr InitTextStreamEx(const char* fname, uint32_t alloc_at_end, uint32_t enforced_max_line_blen, uint32_t max_line_blen, uint32_t decompress_thread_ct, TextStream* txsp) { |
| 24 |
const uint32_t dst_capacity = RoundUpPow2(max_line_blen + kDecompressChunkSize, kCacheline); |
| 25 |
if (unlikely(dst_capacity > bigstack_left())) { |
| 26 |
return kPglRetNomem; |
| 27 |
} |
| 28 |
char* dst; |
| 29 |
if (!alloc_at_end) { |
| 30 |
dst = S_CAST(char*, bigstack_alloc_raw(dst_capacity)); |
| 31 |
} else { |
| 32 |
dst = S_CAST(char*, bigstack_end_alloc_raw(dst_capacity)); |
| 33 |
} |
| 34 |
return TextStreamOpenEx(fname, enforced_max_line_blen, dst_capacity, decompress_thread_ct, nullptr, dst, txsp); |
| 35 |
} |
| 36 |
|
| 37 |
void TextErrPrint(const char* file_descrip, const char* errmsg, PglErr reterr) { |
| 38 |
assert(reterr != kPglRetSuccess); |
| 39 |
if (reterr == kPglRetOpenFail) { |
| 40 |
logerrprintfww(kErrprintfFopen, file_descrip, errmsg); |
| 41 |
} else if (reterr == kPglRetReadFail) { |
| 42 |
logerrprintfww(kErrprintfFread, file_descrip, errmsg); |
| 43 |
} else if (reterr == kPglRetDecompressFail) { |
| 44 |
logerrprintfww(kErrprintfDecompress, file_descrip, errmsg); |
| 45 |
} else if (reterr == kPglRetMalformedInput) { |
| 46 |
if (errmsg == kShortErrInteriorEmptyLine) { |
| 47 |
logerrprintfww("Error: Unexpected interior empty line in %s.\n", file_descrip); |
| 48 |
} else { |
| 49 |
assert(errmsg == kShortErrLongLine); |
| 50 |
logerrprintfww("Error: Pathologically long line in %s.\n", file_descrip); |
| 51 |
} |
| 52 |
} else if (reterr == kPglRetRewindFail) { |
| 53 |
// Not produced directly by TextStream, but it's inserted in between by |
| 54 |
// some consumers. |
| 55 |
logerrprintfww(kErrprintfRewind, file_descrip); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#ifdef __cplusplus |
| 60 |
} |
| 61 |
#endif |