| 1 |
#ifndef __PLINK2_TEXT_H__ |
| 2 |
#define __PLINK2_TEXT_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This library is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU Lesser General Public License as published by the |
| 9 |
// Free Software Foundation, either version 3 of the License, or (at your |
| 10 |
// option) any later version. |
| 11 |
// |
| 12 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 15 |
// for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Scanning one line at a time from a text file is one of the most common |
| 22 |
// workflows in all of computing. |
| 23 |
// |
| 24 |
// Usually, text files are small; the obvious reason to choose text over binary |
| 25 |
// is human-readability, after all, and humans can't read multi-gigabyte files |
| 26 |
// in a reasonable amount of time. As a consequence, the most commonly used C |
| 27 |
// and C++ text-processing library functions sacrifice a substantial amount of |
| 28 |
// performance in favor of ease-of-use. |
| 29 |
// |
| 30 |
// However, plink2 is frequently asked to load a multi-gigabyte text file and |
| 31 |
// then do something very simple with it. Often, the file is in the operating |
| 32 |
// system's page cache, since the user or script is doing multiple things with |
| 33 |
// the file and they're split across multiple invocations of plink2 and other |
| 34 |
// programs. In this setting, the usual "I/O cost > processing cost, it isn't |
| 35 |
// worth worrying much about the latter" assumption is very, very wrong, and it |
| 36 |
// is worth going to great lengths to keep baseline text-processing cost to a |
| 37 |
// minimum. |
| 38 |
// |
| 39 |
// In addition, multi-gigabyte text files are practically guaranteed to |
| 40 |
// compress well, and gzipped and bgzipped text files are widely used in |
| 41 |
// bioinformatics practice. Ordinarily, when sequentially processing a text |
| 42 |
// file, there's little to gain from spawning a separate thread to issue |
| 43 |
// file-read requests, since a modern operating system will recognize the |
| 44 |
// access pattern and read-ahead from the disk on its own. However, the |
| 45 |
// operating system can't *decompress-ahead* for you; and when decompression |
| 46 |
// has comparable latency to processing, decompress-ahead reduces runtime by up |
| 47 |
// to 50%. |
| 48 |
// |
| 49 |
// Thus, this library provides a text reader that |
| 50 |
// 1. allows the caller to treat gzipped and Zstd-compressed text files as if |
| 51 |
// they were uncompressed. This is functionally identical to Zstd's |
| 52 |
// zlibWrapper, which was used for most of plink2's alpha testing period, |
| 53 |
// but I've decided to phase out zlibWrapper thanks to compilation headaches |
| 54 |
// and its static-linking requirement. |
| 55 |
// 2. decompresses-ahead, potentially with multiple threads. |
| 56 |
// a. For now, multithreaded decompression can only kick in for bgzipped |
| 57 |
// files. However, if a clear use case exists, it should be possible to |
| 58 |
// build a multithreaded Zstd decoder that isn't restricted to a Zstd |
| 59 |
// sub-format; see |
| 60 |
// https://github.com/facebook/zstd/issues/1702#issuecomment-515124700 |
| 61 |
// If you have such a use case, I recommend responding to Yann Collet in |
| 62 |
// that GitHub issue. |
| 63 |
// b. Tabix-based seek support was considered and rejected, since the tabix |
| 64 |
// index only stores CHROM/POS, while plink2 also needs record numbers in |
| 65 |
// its most critical use case (.pvar loading). A suitable index format |
| 66 |
// may be adopted or developed later, and if/when that's supported, there |
| 67 |
// will probably also be a way for callers which don't need record |
| 68 |
// numbers to exploit tabix indexes. (In that event, plink2's Zstd |
| 69 |
// compressor will be modified to support seekable-Zstd output and index |
| 70 |
// generation.) |
| 71 |
// 3. has line-reader functions that don't force the user to provide their own |
| 72 |
// buffer to put the line in. Instead, they just return a (possibly const) |
| 73 |
// pointer to the beginning of the line and expose a pointer to the end of |
| 74 |
// the line. This simultaneously saves memory and reduces overhead. |
| 75 |
// a. Since this reuses a single buffer, the string-view is invalidated when |
| 76 |
// the next line is read. |
| 77 |
// b. When the last line in the file is not terminated by '\n', this text |
| 78 |
// reader automatically appends '\n'. Thus, while C library functions |
| 79 |
// that assume null-termination can't be used here (unless you're using a |
| 80 |
// wrapper function that replaces the terminating '\n' with '\0' after |
| 81 |
// the line-read function call, which is a totally valid thing to do), |
| 82 |
// plink2_string.h functions which either iterate to any end-of-line |
| 83 |
// character (ASCII code < 32 and unequal to 9=tab) or explicitly assume |
| 84 |
// '\n'-termination can be used, and these use essentially the same |
| 85 |
// optimizations as modern memchr implementations. |
| 86 |
// (A C++17 interface that returns std::string_view objects was considered, |
| 87 |
// but then rejected since std::string_view's design makes it much better |
| 88 |
// suited to be a function input-parameter type than a return type. It is |
| 89 |
// easy enough to efficiently construct a string_view using the current |
| 90 |
// interface when it's time to call a function that accepts one. The rest |
| 91 |
// of the time, there's no meaningful advantage over plain C pointers.) |
| 92 |
// 4. can be used with either a single fixed-size memory buffer (this plays |
| 93 |
// well with plink2's memory allocation strategy), or dynamic resizing with |
| 94 |
// malloc()/realloc() calls. |
| 95 |
// |
| 96 |
// Two other readers are provided: |
| 97 |
// - A decompress-ahead token reader. This also shards the tokens, for the |
| 98 |
// common use case where the tokens don't need to be parsed in order (e.g. |
| 99 |
// --extract/--exclude). |
| 100 |
// - A simpler single-threaded (no decompress-ahead) reader. |
| 101 |
|
| 102 |
#ifdef STATIC_ZLIB |
| 103 |
# include "../../zlib-1.3.1/zlib.h" |
| 104 |
#else |
| 105 |
# include <zlib.h> |
| 106 |
# if !defined(ZLIB_VERNUM) || (ZLIB_VERNUM < 0x1240) |
| 107 |
# error "plink2_text requires zlib 1.2.4 or later." |
| 108 |
# endif |
| 109 |
#endif |
| 110 |
|
| 111 |
#include "plink2_bgzf.h" |
| 112 |
#include "plink2_zstfile.h" |
| 113 |
|
| 114 |
#ifdef __cplusplus |
| 115 |
namespace plink2 { |
| 116 |
#endif |
| 117 |
|
| 118 |
PglErr GetFileType(const char* fname, FileCompressionType* ftype_ptr); |
| 119 |
|
| 120 |
typedef struct TextFileBaseStruct { |
| 121 |
// Not noncopyable, since this is copied wholesale by the TextStream |
| 122 |
// move-constructor. |
| 123 |
|
| 124 |
// Positioned first so the compiler doesn't need to add an offset to access |
| 125 |
// it. |
| 126 |
char* consume_iter; |
| 127 |
|
| 128 |
char* consume_stop; // should always point after the last loaded \n |
| 129 |
|
| 130 |
const char* errmsg; |
| 131 |
PglErr reterr; |
| 132 |
|
| 133 |
FileCompressionType file_type; |
| 134 |
FILE* ff; // could use e.g. htslib for some network support later |
| 135 |
uint32_t dst_owned_by_consumer; |
| 136 |
uint32_t enforced_max_line_blen; |
| 137 |
// Note that when dst_owned_by_consumer isn't true, the reader thread can |
| 138 |
// alter the values below. However, this can only happen when the consumer |
| 139 |
// thread is blocked waiting for the next line, or before it's attempted to |
| 140 |
// read the first line. So there's no advantage to placing these in a |
| 141 |
// different cacheline from consume_iter (which is constantly altered by the |
| 142 |
// consumer). |
| 143 |
char* dst; |
| 144 |
uint32_t dst_len; |
| 145 |
uint32_t dst_capacity; |
| 146 |
} TextFileBase; |
| 147 |
|
| 148 |
typedef struct GzRawDecompressStreamStruct { |
| 149 |
// Copied by TextStream move-constructor. |
| 150 |
unsigned char* in; |
| 151 |
z_stream ds; |
| 152 |
uint32_t ds_initialized; |
| 153 |
uint32_t eof; |
| 154 |
} GzRawDecompressStream; |
| 155 |
|
| 156 |
// BgzfRawDecompressStream declared in plink2_bgzf.h. |
| 157 |
|
| 158 |
typedef struct ZstRawDecompressStreamStruct { |
| 159 |
// Copied by TextStream move-constructor. |
| 160 |
ZSTD_DStream* ds; |
| 161 |
ZSTD_inBuffer ib; |
| 162 |
} ZstRawDecompressStream; |
| 163 |
|
| 164 |
typedef union { |
| 165 |
GzRawDecompressStream gz; |
| 166 |
// Even in the single-threaded case, it's worth distinguishing bgzf from |
| 167 |
// generic .gz, since with bgzf we can use libdeflate. |
| 168 |
BgzfRawDecompressStream bgzf; |
| 169 |
ZstRawDecompressStream zst; |
| 170 |
} RawDecompressStream; |
| 171 |
|
| 172 |
typedef struct textFILEMainStruct { |
| 173 |
TextFileBase base; |
| 174 |
RawDecompressStream rds; |
| 175 |
} textFILEMain; |
| 176 |
|
| 177 |
typedef struct textFILEStruct { |
| 178 |
#ifdef __cplusplus |
| 179 |
textFILEMain& GET_PRIVATE_m() { return m; } |
| 180 |
textFILEMain const& GET_PRIVATE_m() const { return m; } |
| 181 |
private: |
| 182 |
#endif |
| 183 |
textFILEMain m; |
| 184 |
} textFILE; |
| 185 |
|
| 186 |
void PreinitTextFile(textFILE* txf_ptr); |
| 187 |
|
| 188 |
// kDecompressChunkSize = 1 MiB currently declared in plink2_bgzf.h, may move |
| 189 |
// somewhere closer to the base later. |
| 190 |
CONSTI32(kTextStreamBlenFast, 11 * kDecompressChunkSize); |
| 191 |
CONSTI32(kTokenStreamBlen, 12 * kDecompressChunkSize); |
| 192 |
CONSTI32(kMaxTokenBlen, 8 * kDecompressChunkSize); |
| 193 |
static_assert(kMaxTokenBlen >= kDecompressChunkSize, "kMaxTokenBlen too small."); |
| 194 |
static_assert(kMaxTokenBlen + kDecompressChunkSize <= kTokenStreamBlen, "kMaxTokenBlen + kDecompressChunkSize can't be larger than kTokenStreamBlen."); |
| 195 |
|
| 196 |
// max_line_blen and enforced_max_line_blen lower bound. |
| 197 |
CONSTI32(kDecompressMinBlen, kDecompressChunkSize); |
| 198 |
|
| 199 |
CONSTI32(kDecompressMinCapacity, kDecompressMinBlen + kDecompressChunkSize); |
| 200 |
|
| 201 |
// * Can return nomem, open-fail, or read-fail. |
| 202 |
// * If dst == nullptr, this mallocs a buffer of size 2 * kDecompressChunkSize, |
| 203 |
// and it'll be realloced as necessary and freed by CleanupTextFile(). |
| 204 |
// The original value of dst_capacity doesn't matter in this case. |
| 205 |
// Otherwise, the buffer is owned by the caller, assumed to have size >= |
| 206 |
// dst_capacity, and never grown. |
| 207 |
// * enforced_max_line_blen must be >= dst_capacity - kDecompressChunkSize. |
| 208 |
// It's the point at which long-line errors instead of out-of-memory errors |
| 209 |
// are reported. It isn't permitted to be less than 1 MiB. |
| 210 |
PglErr TextFileOpenEx(const char* fname, uint32_t enforced_max_line_blen, uint32_t dst_capacity, char* dst, textFILE* txf_ptr); |
| 211 |
|
| 212 |
HEADER_INLINE PglErr TextFileOpen(const char* fname, textFILE* txf_ptr) { |
| 213 |
return TextFileOpenEx(fname, kMaxLongLine, 0, nullptr, txf_ptr); |
| 214 |
} |
| 215 |
|
| 216 |
extern const char kShortErrLongLine[]; |
| 217 |
extern const char kShortErrInteriorEmptyLine[]; |
| 218 |
|
| 219 |
PglErr TextFileAdvance(textFILE* txf_ptr); |
| 220 |
|
| 221 |
HEADER_INLINE PglErr TextFileNextLine(textFILE* txf_ptr, char** line_startp) { |
| 222 |
TextFileBase* basep = &GET_PRIVATE(*txf_ptr, m).base; |
| 223 |
if (basep->consume_iter == basep->consume_stop) { |
| 224 |
PglErr reterr = TextFileAdvance(txf_ptr); |
| 225 |
// not unlikely() due to eof |
| 226 |
if (reterr) { |
| 227 |
return reterr; |
| 228 |
} |
| 229 |
} |
| 230 |
*line_startp = basep->consume_iter; |
| 231 |
basep->consume_iter = AdvPastDelim(*line_startp, '\n'); |
| 232 |
return kPglRetSuccess; |
| 233 |
} |
| 234 |
|
| 235 |
HEADER_INLINE char* TextFileLineEnd(textFILE* txf_ptr) { |
| 236 |
return GET_PRIVATE(*txf_ptr, m).base.consume_iter; |
| 237 |
} |
| 238 |
|
| 239 |
HEADER_INLINE PglErr TextFileNextLineLstrip(textFILE* txf_ptr, char** line_startp) { |
| 240 |
TextFileBase* basep = &GET_PRIVATE(*txf_ptr, m).base; |
| 241 |
if (basep->consume_iter == basep->consume_stop) { |
| 242 |
PglErr reterr = TextFileAdvance(txf_ptr); |
| 243 |
if (reterr) { |
| 244 |
return reterr; |
| 245 |
} |
| 246 |
} |
| 247 |
*line_startp = FirstNonTspace(basep->consume_iter); |
| 248 |
basep->consume_iter = AdvPastDelim(*line_startp, '\n'); |
| 249 |
return kPglRetSuccess; |
| 250 |
} |
| 251 |
|
| 252 |
PglErr TextFileOnlyEmptyLinesLeft(textFILE* txf_ptr); |
| 253 |
|
| 254 |
// API fix (30-31 Oct 2019): While we want to tolerate *trailing* empty lines |
| 255 |
// (as well as lack of a final newline), since those arise from manual |
| 256 |
// text-editing every once in a while, there's usually no good reason to |
| 257 |
// tolerate *interior* empty lines like we were previously doing. So the |
| 258 |
// generic empty-line-skipping interface has been replaced with one that |
| 259 |
// reports an "Unexpected empty line" error with a kPglRetMalformedInput error |
| 260 |
// code unless the empty line(s) are at EOF. |
| 261 |
HEADER_INLINE char* TextFileGet(textFILE* txf_ptr) { |
| 262 |
TextFileBase* basep = &GET_PRIVATE(*txf_ptr, m).base; |
| 263 |
if (basep->consume_iter == basep->consume_stop) { |
| 264 |
// not unlikely() due to eof |
| 265 |
if (TextFileAdvance(txf_ptr)) { |
| 266 |
return nullptr; |
| 267 |
} |
| 268 |
} |
| 269 |
char* line_start = FirstNonTspace(basep->consume_iter); |
| 270 |
basep->consume_iter = AdvPastDelim(line_start, '\n'); |
| 271 |
if (!IsEolnKns(*line_start)) { |
| 272 |
return line_start; |
| 273 |
} |
| 274 |
TextFileOnlyEmptyLinesLeft(txf_ptr); |
| 275 |
return nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
void TextFileRewind(textFILE* txf_ptr); |
| 279 |
|
| 280 |
HEADER_INLINE int32_t TextFileIsOpen(const textFILE* txf_ptr) { |
| 281 |
return (GET_PRIVATE(*txf_ptr, m).base.ff != nullptr); |
| 282 |
} |
| 283 |
|
| 284 |
HEADER_INLINE int32_t TextFileEof(const textFILE* txf_ptr) { |
| 285 |
return (GET_PRIVATE(*txf_ptr, m).base.reterr == kPglRetEof); |
| 286 |
} |
| 287 |
|
| 288 |
HEADER_INLINE const char* TextFileError(const textFILE* txf_ptr) { |
| 289 |
return GET_PRIVATE(*txf_ptr, m).base.errmsg; |
| 290 |
} |
| 291 |
|
| 292 |
HEADER_INLINE PglErr TextFileErrcode(const textFILE* txf_ptr) { |
| 293 |
const PglErr reterr = GET_PRIVATE(*txf_ptr, m).base.reterr; |
| 294 |
if (reterr == kPglRetEof) { |
| 295 |
return kPglRetSuccess; |
| 296 |
} |
| 297 |
return reterr; |
| 298 |
} |
| 299 |
|
| 300 |
// Note that this does not assign to *reterrp at all in the usual eof case. |
| 301 |
HEADER_INLINE BoolErr TextFileErrcode2(const textFILE* txf_ptr, PglErr* reterrp) { |
| 302 |
const PglErr reterr = GET_PRIVATE(*txf_ptr, m).base.reterr; |
| 303 |
if (reterr == kPglRetEof) { |
| 304 |
return 0; |
| 305 |
} |
| 306 |
*reterrp = reterr; |
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
// Does not convert kPglRetEof -> kPglRetSuccess. |
| 311 |
HEADER_INLINE PglErr TextFileRawErrcode(const textFILE* txf_ptr) { |
| 312 |
return GET_PRIVATE(*txf_ptr, m).base.reterr; |
| 313 |
} |
| 314 |
|
| 315 |
// Relevant when about to move-construct a TextStream; see plink2_glm. |
| 316 |
HEADER_INLINE uint32_t TextFileLinebufLen(const textFILE* txf_ptr) { |
| 317 |
return GET_PRIVATE(*txf_ptr, m).base.dst_len; |
| 318 |
} |
| 319 |
|
| 320 |
// Ok to pass reterrp == nullptr. |
| 321 |
// Returns nonzero iff file-close fails, and either reterrp == nullptr or |
| 322 |
// *reterrp == kPglRetSuccess; this is intended to be followed by logging of |
| 323 |
// strerror(errno). In the latter case, *reterrp is set to kPglRetReadFail. |
| 324 |
BoolErr CleanupTextFile(textFILE* txf_ptr, PglErr* reterrp); |
| 325 |
|
| 326 |
|
| 327 |
// consumer -> reader message |
| 328 |
// could add a "close current file and open another one" case |
| 329 |
ENUM_U31_DEF_START() |
| 330 |
kTxsInterruptNone, |
| 331 |
kTxsInterruptRetarget, |
| 332 |
kTxsInterruptShutdown |
| 333 |
ENUM_U31_DEF_END(TxsInterrupt); |
| 334 |
|
| 335 |
typedef struct TextStreamSyncStruct { |
| 336 |
// Mutex shared state, and everything guarded by the mutex. Allocated to |
| 337 |
// different cacheline(s) than consume_stop. |
| 338 |
#ifdef _WIN32 |
| 339 |
CRITICAL_SECTION critical_section; |
| 340 |
HANDLE reader_progress_event; |
| 341 |
HANDLE consumer_progress_event; |
| 342 |
#else |
| 343 |
pthread_mutex_t sync_mutex; |
| 344 |
pthread_cond_t reader_progress_condvar; |
| 345 |
pthread_cond_t consumer_progress_condvar; |
| 346 |
// bugfix (7 Mar 2018): need to avoid waiting on consumer_progress_condvar if |
| 347 |
// this is set. (could also check an appropriate predicate) |
| 348 |
uint32_t consumer_progress_state; |
| 349 |
|
| 350 |
uint32_t sync_init_state; |
| 351 |
#endif |
| 352 |
|
| 353 |
pthread_t read_thread; |
| 354 |
|
| 355 |
char* consume_tail; |
| 356 |
char* cur_circular_end; |
| 357 |
char* available_end; |
| 358 |
|
| 359 |
// Separate from the TextFileBase instances of these values, since we don't |
| 360 |
// want to force the user to worry about these values changing at any moment. |
| 361 |
// Instead, the TextFileBase instances are only updated during TextAdvance() |
| 362 |
// calls and the like. |
| 363 |
const char* errmsg; |
| 364 |
PglErr reterr; // note that this is set to kPglRetEof once we reach eof |
| 365 |
|
| 366 |
uint32_t dst_reallocated; |
| 367 |
TxsInterrupt interrupt; |
| 368 |
const char* new_fname; |
| 369 |
} TextStreamSync; |
| 370 |
|
| 371 |
typedef union { |
| 372 |
GzRawDecompressStream gz; |
| 373 |
BgzfRawMtDecompressStream bgzf; |
| 374 |
ZstRawDecompressStream zst; |
| 375 |
} RawMtDecompressStream; |
| 376 |
|
| 377 |
typedef struct TextStreamMainStruct { |
| 378 |
TextFileBase base; |
| 379 |
RawMtDecompressStream rds; |
| 380 |
uint32_t decompress_thread_ct; |
| 381 |
TextStreamSync* syncp; |
| 382 |
} TextStreamMain; |
| 383 |
|
| 384 |
typedef struct TextStreamStruct { |
| 385 |
#ifdef __cplusplus |
| 386 |
TextStreamMain& GET_PRIVATE_m() { return m; } |
| 387 |
TextStreamMain const& GET_PRIVATE_m() const { return m; } |
| 388 |
private: |
| 389 |
#endif |
| 390 |
TextStreamMain m; |
| 391 |
} TextStream; |
| 392 |
|
| 393 |
void PreinitTextStream(TextStream* txs_ptr); |
| 394 |
|
| 395 |
// * Can return nomem, open-fail, read-fail, or thread-create-fail. |
| 396 |
// * Exactly one of fname and txf_ptr must be nullptr. If txf_ptr is null, |
| 397 |
// fname is opened. Otherwise, the returned stream is "move-constructed" |
| 398 |
// from txf_ptr. |
| 399 |
// When not move-constructing, enforced_max_line_blen, dst_capacity, and dst |
| 400 |
// are interpreted the same way as TextFileOpenEx(). |
| 401 |
// When move-constructing, enforced_max_line_blen and dst_capacity may be |
| 402 |
// smaller than what the textFILE was opened with. |
| 403 |
PglErr TextStreamOpenEx(const char* fname, uint32_t enforced_max_line_blen, uint32_t dst_capacity, uint32_t decompress_thread_ct, textFILE* txf_ptr, char* dst, TextStream* txs_ptr); |
| 404 |
|
| 405 |
HEADER_INLINE PglErr TextStreamOpen(const char* fname, TextStream* txs_ptr) { |
| 406 |
return TextStreamOpenEx(fname, kMaxLongLine, 0, NumCpu(nullptr), nullptr, nullptr, txs_ptr); |
| 407 |
} |
| 408 |
|
| 409 |
// We drop 'Stream' from the function names outside of open/close, to emphasize |
| 410 |
// that this is the default choice. |
| 411 |
// (Originally this was named 'TextRstream', but then I realized that the write |
| 412 |
// case doesn't really care about whether the input is text or binary.) |
| 413 |
HEADER_INLINE char* TextLineEnd(TextStream* txs_ptr) { |
| 414 |
return GET_PRIVATE(*txs_ptr, m).base.consume_iter; |
| 415 |
} |
| 416 |
|
| 417 |
HEADER_INLINE int32_t TextIsOpen(const TextStream* txs_ptr) { |
| 418 |
return (GET_PRIVATE(*txs_ptr, m).base.ff != nullptr); |
| 419 |
} |
| 420 |
|
| 421 |
HEADER_INLINE int32_t TextEof(const TextStream* txs_ptr) { |
| 422 |
return (GET_PRIVATE(*txs_ptr, m).base.reterr == kPglRetEof); |
| 423 |
} |
| 424 |
|
| 425 |
uint32_t TextDecompressThreadCt(const TextStream* txs_ptr); |
| 426 |
|
| 427 |
PglErr TextAdvance(TextStream* txs_ptr); |
| 428 |
|
| 429 |
HEADER_INLINE PglErr TextNextLine(TextStream* txs_ptr, char** line_startp) { |
| 430 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 431 |
if (basep->consume_iter == basep->consume_stop) { |
| 432 |
PglErr reterr = TextAdvance(txs_ptr); |
| 433 |
// not unlikely() due to eof |
| 434 |
if (reterr) { |
| 435 |
return reterr; |
| 436 |
} |
| 437 |
} |
| 438 |
*line_startp = basep->consume_iter; |
| 439 |
basep->consume_iter = AdvPastDelim(basep->consume_iter, '\n'); |
| 440 |
return kPglRetSuccess; |
| 441 |
} |
| 442 |
|
| 443 |
HEADER_INLINE PglErr TextNextLineK(TextStream* txs_ptr, const char** line_startp) { |
| 444 |
return TextNextLine(txs_ptr, K_CAST(char**, line_startp)); |
| 445 |
} |
| 446 |
|
| 447 |
// plink2 functions strip leading whitespace by default (unless there's a clear |
| 448 |
// reason not to, e.g. it isn't allowed in VCF files), since plink 1.x inserts |
| 449 |
// it so often. |
| 450 |
HEADER_INLINE PglErr TextNextLineLstrip(TextStream* txs_ptr, char** line_startp) { |
| 451 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 452 |
if (basep->consume_iter == basep->consume_stop) { |
| 453 |
PglErr reterr = TextAdvance(txs_ptr); |
| 454 |
// not unlikely() due to eof |
| 455 |
if (reterr) { |
| 456 |
return reterr; |
| 457 |
} |
| 458 |
} |
| 459 |
*line_startp = FirstNonTspace(basep->consume_iter); |
| 460 |
basep->consume_iter = AdvPastDelim(*line_startp, '\n'); |
| 461 |
return kPglRetSuccess; |
| 462 |
} |
| 463 |
|
| 464 |
HEADER_INLINE PglErr TextNextLineLstripK(TextStream* txs_ptr, const char** line_startp) { |
| 465 |
return TextNextLineLstrip(txs_ptr, K_CAST(char**, line_startp)); |
| 466 |
} |
| 467 |
|
| 468 |
// returns kPglRetEof if true, otherwise kPglRetMalformedInput and sets |
| 469 |
// errmsg. |
| 470 |
PglErr TextOnlyEmptyLinesLeft(TextStream* txs_ptr); |
| 471 |
|
| 472 |
// This was previously named TextNextLineLstripNoempty, and returned a PglErr, |
| 473 |
// but some compilers were (i) not inlining it and (ii) spamming spurious |
| 474 |
// uninitialized-variable warnings as a side effect. And it's plink2's most |
| 475 |
// common use case. |
| 476 |
// So we'll use a different interface here (and a shorter name). User is |
| 477 |
// expected to call TextStream[Raw]Errcode when nullptr is returned. |
| 478 |
HEADER_INLINE char* TextGet(TextStream* txs_ptr) { |
| 479 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 480 |
if (basep->consume_iter == basep->consume_stop) { |
| 481 |
// not unlikely() due to eof |
| 482 |
if (TextAdvance(txs_ptr)) { |
| 483 |
return nullptr; |
| 484 |
} |
| 485 |
} |
| 486 |
char* line_start = FirstNonTspace(basep->consume_iter); |
| 487 |
basep->consume_iter = AdvPastDelim(line_start, '\n'); |
| 488 |
if (!IsEolnKns(*line_start)) { |
| 489 |
return line_start; |
| 490 |
} |
| 491 |
TextOnlyEmptyLinesLeft(txs_ptr); |
| 492 |
return nullptr; |
| 493 |
} |
| 494 |
|
| 495 |
PglErr TextSkipNz(uintptr_t skip_ct, TextStream* txs_ptr); |
| 496 |
|
| 497 |
HEADER_INLINE PglErr TextSkip(uintptr_t skip_ct, TextStream* txs_ptr) { |
| 498 |
if (skip_ct == 0) { |
| 499 |
return kPglRetSuccess; |
| 500 |
} |
| 501 |
return TextSkipNz(skip_ct, txs_ptr); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
// 'Unsafe' functions require line_iter to already point to the start of the |
| 506 |
// line, and don't update txsp->base.consume_iter; they primarily wrap the |
| 507 |
// TextAdvance() call. |
| 508 |
HEADER_INLINE PglErr TextNextLineUnsafe(TextStream* txs_ptr, char** line_iterp) { |
| 509 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 510 |
if (*line_iterp != basep->consume_stop) { |
| 511 |
return kPglRetSuccess; |
| 512 |
} |
| 513 |
basep->consume_iter = *line_iterp; |
| 514 |
PglErr reterr = TextAdvance(txs_ptr); |
| 515 |
// not unlikely() due to eof |
| 516 |
if (reterr) { |
| 517 |
return reterr; |
| 518 |
} |
| 519 |
*line_iterp = basep->consume_iter; |
| 520 |
return kPglRetSuccess; |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
HEADER_INLINE PglErr TextNextLineLstripUnsafe(TextStream* txs_ptr, char** line_iterp) { |
| 525 |
char* line_iter = *line_iterp; |
| 526 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 527 |
if (line_iter == basep->consume_stop) { |
| 528 |
basep->consume_iter = line_iter; |
| 529 |
PglErr reterr = TextAdvance(txs_ptr); |
| 530 |
// not unlikely() due to eof |
| 531 |
if (reterr) { |
| 532 |
return reterr; |
| 533 |
} |
| 534 |
line_iter = basep->consume_iter; |
| 535 |
} |
| 536 |
*line_iterp = FirstNonTspace(line_iter); |
| 537 |
return kPglRetSuccess; |
| 538 |
} |
| 539 |
|
| 540 |
HEADER_INLINE PglErr TextNextLineLstripUnsafeK(TextStream* txs_ptr, const char** line_iterp) { |
| 541 |
return TextNextLineLstripUnsafe(txs_ptr, K_CAST(char**, line_iterp)); |
| 542 |
} |
| 543 |
|
| 544 |
// Caller is responsible for advancing line_iter past \n when it wants the next |
| 545 |
// line. |
| 546 |
HEADER_INLINE PglErr TextGetUnsafe(TextStream* txs_ptr, char** line_iterp) { |
| 547 |
char* line_iter = *line_iterp; |
| 548 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 549 |
if (line_iter == basep->consume_stop) { |
| 550 |
basep->consume_iter = line_iter; |
| 551 |
PglErr reterr = TextAdvance(txs_ptr); |
| 552 |
// not unlikely() due to eof |
| 553 |
if (reterr) { |
| 554 |
return reterr; |
| 555 |
} |
| 556 |
line_iter = basep->consume_iter; |
| 557 |
} |
| 558 |
line_iter = FirstNonTspace(line_iter); |
| 559 |
if (!IsEolnKns(*line_iter)) { |
| 560 |
*line_iterp = line_iter; |
| 561 |
return kPglRetSuccess; |
| 562 |
} |
| 563 |
return TextOnlyEmptyLinesLeft(txs_ptr); |
| 564 |
} |
| 565 |
|
| 566 |
/* |
| 567 |
HEADER_INLINE PglErr TextGetUnsafeK(TextStream* txs_ptr, const char** line_iterp) { |
| 568 |
return TextGetUnsafe(txs_ptr, K_CAST(char**, line_iterp)); |
| 569 |
} |
| 570 |
*/ |
| 571 |
|
| 572 |
// Returns *zero* when it's time to stop iterating. Designed to be the middle |
| 573 |
// argument in a for (; ; ) loop. |
| 574 |
// Caller is responsible for advancing line_iter past \n when it wants the next |
| 575 |
// line. |
| 576 |
HEADER_INLINE uint32_t TextGetUnsafe2(TextStream* txs_ptr, char** line_iterp) { |
| 577 |
char* line_iter = *line_iterp; |
| 578 |
TextFileBase* basep = &GET_PRIVATE(*txs_ptr, m).base; |
| 579 |
if (line_iter == basep->consume_stop) { |
| 580 |
basep->consume_iter = line_iter; |
| 581 |
// not unlikely() due to eof |
| 582 |
if (TextAdvance(txs_ptr)) { |
| 583 |
return 0; |
| 584 |
} |
| 585 |
line_iter = basep->consume_iter; |
| 586 |
} |
| 587 |
line_iter = FirstNonTspace(line_iter); |
| 588 |
if (!IsEolnKns(*line_iter)) { |
| 589 |
*line_iterp = line_iter; |
| 590 |
return 1; |
| 591 |
} |
| 592 |
TextOnlyEmptyLinesLeft(txs_ptr); |
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
HEADER_INLINE uint32_t TextGetUnsafe2K(TextStream* txs_ptr, const char** line_iterp) { |
| 597 |
return TextGetUnsafe2(txs_ptr, K_CAST(char**, line_iterp)); |
| 598 |
} |
| 599 |
|
| 600 |
HEADER_INLINE void TextSetPos(char* new_consume_iter, TextStream* txs_ptr) { |
| 601 |
GET_PRIVATE(*txs_ptr, m).base.consume_iter = new_consume_iter; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
HEADER_INLINE uint32_t TextIsMt(const TextStream* txs_ptr) { |
| 606 |
// Only bgzf decoder is multithreaded for now. |
| 607 |
return (GET_PRIVATE(*txs_ptr, m).base.file_type == kFileBgzf); |
| 608 |
} |
| 609 |
|
| 610 |
PglErr TextRetarget(const char* new_fname, TextStream* txs_ptr); |
| 611 |
|
| 612 |
HEADER_INLINE PglErr TextRewind(TextStream* txs_ptr) { |
| 613 |
return TextRetarget(nullptr, txs_ptr); |
| 614 |
} |
| 615 |
|
| 616 |
HEADER_INLINE const char* TextStreamError(const TextStream* txs_ptr) { |
| 617 |
return GET_PRIVATE(*txs_ptr, m).base.errmsg; |
| 618 |
} |
| 619 |
|
| 620 |
HEADER_INLINE PglErr TextStreamErrcode(const TextStream* txs_ptr) { |
| 621 |
const PglErr reterr = GET_PRIVATE(*txs_ptr, m).base.reterr; |
| 622 |
if (reterr == kPglRetEof) { |
| 623 |
return kPglRetSuccess; |
| 624 |
} |
| 625 |
return reterr; |
| 626 |
} |
| 627 |
|
| 628 |
// Note that this does not assign to *reterrp at all in the usual eof case. |
| 629 |
HEADER_INLINE BoolErr TextStreamErrcode2(const TextStream* txs_ptr, PglErr* reterrp) { |
| 630 |
const PglErr reterr = GET_PRIVATE(*txs_ptr, m).base.reterr; |
| 631 |
if (reterr == kPglRetEof) { |
| 632 |
return 0; |
| 633 |
} |
| 634 |
*reterrp = reterr; |
| 635 |
return 1; |
| 636 |
} |
| 637 |
|
| 638 |
// Does not convert kPglRetEof -> kPglRetSuccess. |
| 639 |
HEADER_INLINE PglErr TextStreamRawErrcode(const TextStream* txs_ptr) { |
| 640 |
return GET_PRIVATE(*txs_ptr, m).base.reterr; |
| 641 |
} |
| 642 |
|
| 643 |
// Ok to pass reterrp == nullptr. |
| 644 |
// Returns nonzero iff file-close fails, and either reterrp == nullptr or |
| 645 |
// *reterrp == kPglRetSuccess. In the latter case, *reterrp is set to |
| 646 |
// kPglRetReadFail. (Note that this does *not* retrieve the existing |
| 647 |
// txsp->reterr value; caller is responsible for checking TextStreamErrcode() |
| 648 |
// first when they care.) |
| 649 |
BoolErr CleanupTextStream(TextStream* txs_ptr, PglErr* reterrp); |
| 650 |
|
| 651 |
|
| 652 |
// Low-level token-batch-reading interface, using the extra TextStream mode |
| 653 |
// which cares about token rather than line endings. |
| 654 |
typedef struct TokenStreamStruct { |
| 655 |
NONCOPYABLE(TokenStreamStruct); |
| 656 |
TextStream txs; |
| 657 |
} TokenStream; |
| 658 |
|
| 659 |
HEADER_INLINE void PreinitTokenStream(TokenStream* tksp) { |
| 660 |
PreinitTextStream(&tksp->txs); |
| 661 |
} |
| 662 |
|
| 663 |
// Note that shard_boundaries must have length (piece_ct + 1). |
| 664 |
PglErr TksNext(TokenStream* tksp, uint32_t shard_ct, char** shard_boundaries); |
| 665 |
|
| 666 |
HEADER_INLINE PglErr TokenStreamRetarget(const char* new_fname, TokenStream* tksp) { |
| 667 |
return TextRetarget(new_fname, &(tksp->txs)); |
| 668 |
} |
| 669 |
|
| 670 |
HEADER_INLINE PglErr TokenRewind(TokenStream* tksp) { |
| 671 |
return TextRetarget(nullptr, &(tksp->txs)); |
| 672 |
} |
| 673 |
|
| 674 |
HEADER_INLINE const char* TokenStreamError(const TokenStream* tksp) { |
| 675 |
return GET_PRIVATE(tksp->txs, m).base.errmsg; |
| 676 |
} |
| 677 |
|
| 678 |
HEADER_INLINE PglErr TokenStreamErrcode(const TokenStream* tksp) { |
| 679 |
const PglErr reterr = GET_PRIVATE(tksp->txs, m).base.reterr; |
| 680 |
if (reterr == kPglRetEof) { |
| 681 |
return kPglRetSuccess; |
| 682 |
} |
| 683 |
return reterr; |
| 684 |
} |
| 685 |
|
| 686 |
HEADER_INLINE BoolErr CleanupTokenStream(TokenStream* tksp, PglErr* reterrp) { |
| 687 |
return CleanupTextStream(&(tksp->txs), reterrp); |
| 688 |
} |
| 689 |
|
| 690 |
// Could create a slightly simpler interface for the one-token-at-a-time case, |
| 691 |
// but I won't bother for now since it's kind of good for the relative cost of |
| 692 |
// parallelizing token processing to be low. |
| 693 |
|
| 694 |
|
| 695 |
#ifdef __cplusplus |
| 696 |
} // namespace plink2 |
| 697 |
#endif |
| 698 |
|
| 699 |
#endif // __PLINK2_TEXT_H__ |