writerc.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. // Copyright (c) 2006-2010 Kirill Simonov
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. // of the Software, and to permit persons to whom the Software is furnished to do
  10. // so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. package yaml
  23. // Set the writer error and return false.
  24. func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool {
  25. emitter.error = yaml_WRITER_ERROR
  26. emitter.problem = problem
  27. return false
  28. }
  29. // Flush the output buffer.
  30. func yaml_emitter_flush(emitter *yaml_emitter_t) bool {
  31. if emitter.write_handler == nil {
  32. panic("write handler not set")
  33. }
  34. // Check if the buffer is empty.
  35. if emitter.buffer_pos == 0 {
  36. return true
  37. }
  38. if err := emitter.write_handler(emitter, emitter.buffer[:emitter.buffer_pos]); err != nil {
  39. return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error())
  40. }
  41. emitter.buffer_pos = 0
  42. return true
  43. }