melder_info.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* melder_info.cpp
  2. *
  3. * Copyright (C) 1992-2007,2011-2018 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. #include "melder.h"
  19. void MelderInfo::_defaultProc (conststring32 message) {
  20. MelderConsole::write (message, false);
  21. }
  22. MelderInfo::Proc MelderInfo::_p_currentProc = & MelderInfo::_defaultProc;
  23. void Melder_setInformationProc (MelderInfo::Proc proc) {
  24. MelderInfo::_p_currentProc = ( proc ? proc : MelderInfo::_defaultProc );
  25. }
  26. MelderString MelderInfo::_foregroundBuffer = { 0, 0, nullptr };
  27. MelderString *MelderInfo::_p_currentBuffer = & MelderInfo::_foregroundBuffer;
  28. void MelderInfo_open () {
  29. MelderString_empty (MelderInfo::_p_currentBuffer);
  30. }
  31. void MelderInfo_close () {
  32. if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) {
  33. /*
  34. When writing to the Info window or the console, we must add a newline symbol,
  35. because a subsequent MelderInfo_write call has to start on the next line.
  36. When writing to a diverted string, we must *not* add a newline symbol,
  37. because scripts expect returned strings without appended newlines!
  38. */
  39. if (MelderInfo::_p_currentBuffer -> length == 0 ||
  40. MelderInfo::_p_currentBuffer -> string [MelderInfo::_p_currentBuffer -> length - 1] != U'\n') // only if no newline there yet
  41. {
  42. MelderString_appendCharacter (MelderInfo::_p_currentBuffer, U'\n');
  43. if (MelderInfo::_p_currentProc == MelderInfo::_defaultProc)
  44. MelderConsole::write (U"\n", false);
  45. }
  46. if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc)
  47. MelderInfo::_p_currentProc (MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U"");
  48. }
  49. }
  50. void MelderInfo_drain () {
  51. if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) {
  52. if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc)
  53. MelderInfo::_p_currentProc (MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U"");
  54. }
  55. }
  56. void Melder_informationReal (double value, conststring32 units) {
  57. MelderInfo_open ();
  58. if (! units) {
  59. MelderInfo_write (value);
  60. } else {
  61. MelderInfo_write (value, U" ", units);
  62. }
  63. MelderInfo_close ();
  64. }
  65. void Melder_divertInfo (MelderString *p_buffer) {
  66. MelderInfo::_p_currentBuffer = ( p_buffer ? p_buffer : & MelderInfo::_foregroundBuffer );
  67. }
  68. void Melder_clearInfo () {
  69. if (MelderInfo::_p_currentBuffer == & MelderInfo::_foregroundBuffer) {
  70. MelderString_empty (MelderInfo::_p_currentBuffer);
  71. if (MelderInfo::_p_currentProc != & MelderInfo::_defaultProc)
  72. MelderInfo::_p_currentProc (U"");
  73. }
  74. }
  75. conststring32 Melder_getInfo () {
  76. return MelderInfo::_p_currentBuffer -> string ? MelderInfo::_p_currentBuffer -> string : U"";
  77. }
  78. /* End of file melder_info.cpp */