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