| 1 |
#ifndef _melder_readtext_h_ |
| 2 |
#define _melder_readtext_h_ |
| 3 |
/* melder_readtext.h |
| 4 |
* |
| 5 |
* Copyright (C) 1992-2019,2023,2025 Paul Boersma |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* See the GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
struct structMelderReadText { |
| 22 |
autostring32 string32; |
| 23 |
char32 *readPointer32; |
| 24 |
autostring8 string8; |
| 25 |
char *readPointer8; |
| 26 |
kMelder_textInputEncoding input8Encoding; |
| 27 |
int previousPointerStep = 0; |
| 28 |
autostring32 lineBuffer; |
| 29 |
integer lineBufferSize = 0; |
| 30 |
structMelderReadText () : readPointer32 (nullptr), readPointer8 (nullptr) { |
| 31 |
/* |
| 32 |
Check that C++ default initialization has worked. |
| 33 |
*/ |
| 34 |
Melder_assert (! our string32); |
| 35 |
Melder_assert (! our string8); |
| 36 |
} |
| 37 |
}; |
| 38 |
typedef struct structMelderReadText *MelderReadText; |
| 39 |
|
| 40 |
#if 1 |
| 41 |
using autoMelderReadText = std::unique_ptr<structMelderReadText>; |
| 42 |
#else |
| 43 |
struct autoMelderReadText { |
| 44 |
MelderReadText text; |
| 45 |
autoMelderReadText () { |
| 46 |
our text = new structMelderReadText; |
| 47 |
} |
| 48 |
~ autoMelderReadText () { |
| 49 |
delete (our text); |
| 50 |
} |
| 51 |
MelderReadText operator-> () const { // as r-value |
| 52 |
return our text; |
| 53 |
} |
| 54 |
MelderReadText get () const { |
| 55 |
return our text; |
| 56 |
} |
| 57 |
autoMelderReadText (const autoMelderReadText&) = delete; // disable copy constructor |
| 58 |
autoMelderReadText (autoMelderReadText&& other) noexcept { // enable move constructor |
| 59 |
our text = other.text; |
| 60 |
other.text = nullptr; |
| 61 |
} |
| 62 |
autoMelderReadText& operator= (const autoMelderReadText&) = delete; // disable copy assignment |
| 63 |
autoMelderReadText& operator= (autoMelderReadText&& other) noexcept { // enable move assignment |
| 64 |
if (& other != this) { |
| 65 |
delete (our text); |
| 66 |
our text = other.text; |
| 67 |
other.text = nullptr; |
| 68 |
} |
| 69 |
return *this; |
| 70 |
} |
| 71 |
autoMelderReadText&& move () noexcept { return static_cast <autoMelderReadText&&> (*this); } |
| 72 |
explicit operator bool () const noexcept { return !! our text; } |
| 73 |
}; |
| 74 |
#endif |
| 75 |
|
| 76 |
autoMelderReadText MelderReadText_createFromFile (MelderFile file); |
| 77 |
autoMelderReadText MelderReadText_createFromText (autostring32 text); |
| 78 |
char32 MelderReadText_getChar (MelderReadText me); |
| 79 |
void MelderReadText_ungetChar (MelderReadText me); // WARNING: undefined behaviour if you haven't called getChar() first |
| 80 |
mutablestring32 MelderReadText_readLine (MelderReadText me); |
| 81 |
int64 MelderReadText_getNumberOfLines (MelderReadText me); |
| 82 |
conststring32 MelderReadText_getLineNumber (MelderReadText me); |
| 83 |
|
| 84 |
/* End of file melder_readText.h */ |
| 85 |
#endif |