1 // This library is part of PLINK 2.00, copyright (C) 2005-2020 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 const char kErrprintfDecompress[] = "Error: %s decompression failure: %s.\n";
24 
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)25 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) {
26   const uint32_t dst_capacity = RoundUpPow2(max_line_blen + kDecompressChunkSize, kCacheline);
27   if (unlikely(dst_capacity > bigstack_left())) {
28     return kPglRetNomem;
29   }
30   char* dst;
31   if (!alloc_at_end) {
32     dst = S_CAST(char*, bigstack_alloc_raw(dst_capacity));
33   } else {
34     dst = S_CAST(char*, bigstack_end_alloc_raw(dst_capacity));
35   }
36   return TextStreamOpenEx(fname, enforced_max_line_blen, dst_capacity, decompress_thread_ct, nullptr, dst, txsp);
37 }
38 
TextErrPrint(const char * file_descrip,const char * errmsg,PglErr reterr)39 void TextErrPrint(const char* file_descrip, const char* errmsg, PglErr reterr) {
40   assert(reterr != kPglRetSuccess);
41   if (reterr == kPglRetOpenFail) {
42     logerrprintfww(kErrprintfFopen, file_descrip, errmsg);
43   } else if (reterr == kPglRetReadFail) {
44     logerrprintfww(kErrprintfFread, file_descrip, errmsg);
45   } else if (reterr == kPglRetDecompressFail) {
46     logerrprintfww(kErrprintfDecompress, file_descrip, errmsg);
47   } else if (reterr == kPglRetMalformedInput) {
48     if (errmsg == kShortErrInteriorEmptyLine) {
49       logerrprintfww("Error: Unexpected interior empty line in %s.\n", file_descrip);
50     } else {
51       assert(errmsg == kShortErrLongLine);
52       logerrprintfww("Error: Pathologically long line in %s.\n", file_descrip);
53     }
54   } else if (reterr == kPglRetRewindFail) {
55     // Not produced directly by TextStream, but it's inserted in between by
56     // some consumers.
57     logerrprintfww(kErrprintfRewind, file_descrip);
58   }
59 }
60 
61 #ifdef __cplusplus
62 }
63 #endif
64