Message.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Message.h
  3. * ---------
  4. * Purpose: Various functions for processing song messages (allocating, reading from file...)
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include <string>
  12. #include "../common/FileReaderFwd.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. class SongMessage : public std::string
  15. {
  16. public:
  17. // Line ending types (for reading song messages from module files)
  18. enum LineEnding
  19. {
  20. leCR, // Carriage Return (0x0D, \r)
  21. leLF, // Line Feed (0x0A \n)
  22. leCRLF, // Carriage Return, Line Feed (0x0D0A, \r\n)
  23. leMixed, // It is not defined whether Carriage Return or Line Feed is the actual line ending. Both are accepted.
  24. leAutodetect, // Detect suitable line ending
  25. };
  26. enum
  27. {
  28. InternalLineEnding = '\r', // The character that represents line endings internally
  29. };
  30. // Read song message from a mapped file.
  31. // [in] data: pointer to the data in memory that is going to be read
  32. // [in] length: number of characters that should be read, not including a possible trailing null terminator (it is automatically appended).
  33. // [in] lineEnding: line ending formatting of the text in memory.
  34. // [out] returns true on success.
  35. bool Read(const std::byte *data, const size_t length, LineEnding lineEnding);
  36. bool Read(FileReader &file, const size_t length, LineEnding lineEnding);
  37. // Read comments with fixed line length from a mapped file.
  38. // [in] data: pointer to the data in memory that is going to be read
  39. // [in] length: number of characters that should be read, not including a possible trailing null terminator (it is automatically appended).
  40. // [in] lineLength: The fixed length of a line.
  41. // [in] lineEndingLength: The padding space between two fixed lines. (there could for example be a null char after every line)
  42. // [out] returns true on success.
  43. bool ReadFixedLineLength(const std::byte *data, const size_t length, const size_t lineLength, const size_t lineEndingLength);
  44. bool ReadFixedLineLength(FileReader &file, const size_t length, const size_t lineLength, const size_t lineEndingLength);
  45. // Retrieve song message.
  46. // [in] lineEnding: line ending formatting of the text in memory.
  47. // [out] returns formatted song message.
  48. std::string GetFormatted(const LineEnding lineEnding) const;
  49. // Set song message.
  50. // [in] lineEnding: line ending formatting of the text in memory. Must be leCR or leLF or leCRLF,
  51. // [out] returns true if the message has been changed.
  52. bool SetFormatted(std::string message, LineEnding lineEnding);
  53. // Sets the song message. Expects the provided string to already use the internal line ending character.
  54. void SetRaw(std::string message) noexcept { assign(std::move(message)); }
  55. };
  56. OPENMPT_NAMESPACE_END