| 1 | /* melder_info.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2007,2011-2018,2020 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "melder.h" |
| 20 | |
| 21 | void MelderInfo::_defaultProc (conststring32 message) { |
| 22 | MelderConsole::write (message, false); |
| 23 | } |
| 24 | |
| 25 | MelderInfo::Proc MelderInfo::_p_currentProc = & MelderInfo::_defaultProc; |
| 26 | |
| 27 | void Melder_setInformationProc (MelderInfo::Proc proc) { |
| 28 | MelderInfo::_p_currentProc = ( proc ? proc : MelderInfo::_defaultProc ); |
| 29 | } |
| 30 | |
| 31 | MelderString MelderInfo::_foregroundBuffer; |
| 32 | MelderString *MelderInfo::_p_currentBuffer = & MelderInfo::_foregroundBuffer; |
| 33 | |
| 34 | void MelderInfo_open () { |
| 35 | MelderString_empty (MelderInfo::_p_currentBuffer); |
| 36 | } |
| 37 | |
| 38 | void MelderInfo_close () { |
| 39 | if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) { |
| 40 | /* |
| 41 | When writing to the Info window or the console, we must add a newline symbol, |
| 42 | because a subsequent MelderInfo_write call has to start on the next line. |
| 43 | When writing to a diverted string, we must *not* add a newline symbol, |
| 44 | because scripts expect returned strings without appended newlines! |
| 45 | */ |
| 46 | if (MelderInfo::_p_currentBuffer -> length == 0 || |
| 47 | MelderInfo::_p_currentBuffer -> string [MelderInfo::_p_currentBuffer -> length - 1] != U'\n') // only if no newline there yet |
| 48 | { |
| 49 | MelderString_appendCharacter (MelderInfo::_p_currentBuffer, U'\n'); |
| 50 | if (MelderInfo::_p_currentProc == MelderInfo::_defaultProc) |
| 51 | MelderConsole::write (U"\n", false); |
| 52 | } |
| 53 | if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc) |
| 54 | MelderInfo::_p_currentProc (MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U""); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void MelderInfo_drain () { |
| 59 | if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) { |
| 60 | if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc) |
| 61 | MelderInfo::_p_currentProc (MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U""); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void Melder_informationReal (double value, conststring32 units) { |
| 66 | MelderInfo_open (); |
| 67 | if (! units) { |
| 68 | MelderInfo_write (value); |
| 69 | } else { |
| 70 | MelderInfo_write (value, U" ", units); |
| 71 | } |
| 72 | MelderInfo_close (); |
| 73 | } |
| 74 | |
| 75 | void Melder_divertInfo (MelderString *p_buffer) { |
| 76 | MelderInfo::_p_currentBuffer = ( p_buffer ? p_buffer : & MelderInfo::_foregroundBuffer ); |
| 77 | } |
| 78 | |
| 79 | void Melder_clearInfo () { |
| 80 | if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) { |
| 81 | MelderString_empty (MelderInfo::_p_currentBuffer); |
| 82 | if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc) |
| 83 | MelderInfo::_p_currentProc (U""); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | conststring32 Melder_getInfo () { |
| 88 | return MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U""; |
| 89 | } |
| 90 | |
| 91 | /* End of file melder_info.cpp */ |