1 module pxtone.text;
2 // '12/03/03
3 
4 import pxtone.descriptor;
5 
6 bool _read4_malloc(char[]* pp, int* p_buf_size, ref pxtnDescriptor p_doc) @system {
7 	if (!pp) {
8 		return false;
9 	}
10 	p_doc.r(p_buf_size[0 .. 1]);
11 	if (*p_buf_size < 0) {
12 		return false;
13 	}
14 
15 	bool b_ret = false;
16 
17 	*pp = new char[](*p_buf_size + 1);
18 	if (!(*pp)) {
19 		return false;
20 	}
21 
22 	(*pp)[0 .. *p_buf_size + 1] = 0;
23 
24 	if (*p_buf_size) {
25 		p_doc.r((*pp)[0 .. *p_buf_size]);
26 	}
27 
28 	b_ret = true;
29 term:
30 	if (!b_ret) {
31 		*pp = null;
32 	}
33 
34 	return b_ret;
35 }
36 
37 void _write4(const char[] p, ref pxtnDescriptor p_doc) @system {
38 	p_doc.w_asfile(cast(int)p.length);
39 	p_doc.w_asfile(p);
40 }
41 
42 struct pxtnText {
43 private:
44 	char[] _p_comment_buf;
45 	int _comment_size;
46 
47 	char[] _p_name_buf;
48 	int _name_size;
49 
50 public:
51 	 ~this() nothrow @system {
52 		_p_comment_buf = null;
53 		_comment_size = 0;
54 		_p_name_buf = null;
55 		_name_size = 0;
56 	}
57 
58 	bool set_comment_buf(const(char)* comment, int buf_size) nothrow @system {
59 		if (!comment) {
60 			return false;
61 		}
62 		_p_comment_buf = null;
63 		if (buf_size <= 0) {
64 			_comment_size = 0;
65 			return true;
66 		}
67 		_p_comment_buf = new char[](buf_size + 1);
68 		if (!(_p_comment_buf)) {
69 			return false;
70 		}
71 		_p_comment_buf[0 .. buf_size] = comment[0 .. buf_size];
72 		_p_comment_buf[buf_size] = '\0';
73 		_comment_size = buf_size;
74 		return true;
75 	}
76 
77 	const(char)[] get_comment_buf() const nothrow @safe {
78 		return _p_comment_buf[0 .. _comment_size];
79 	}
80 
81 	bool is_comment_buf() const nothrow @safe {
82 		if (_comment_size > 0) {
83 			return true;
84 		}
85 		return false;
86 	}
87 
88 	bool set_name_buf(const(char)* name, int buf_size) nothrow @system {
89 		if (!name) {
90 			return false;
91 		}
92 		_p_name_buf = null;
93 		if (buf_size <= 0) {
94 			_name_size = 0;
95 			return true;
96 		}
97 		_p_name_buf = new char[](buf_size + 1);
98 		if (!(_p_name_buf)) {
99 			return false;
100 		}
101 		_p_name_buf[0 .. buf_size] = name[0 .. buf_size];
102 		_p_name_buf[buf_size] = '\0';
103 		_name_size = buf_size;
104 		return true;
105 	}
106 
107 	const(char)[] get_name_buf() const nothrow @safe {
108 		return _p_name_buf[0 .. _name_size];
109 	}
110 
111 	bool is_name_buf() const nothrow @safe {
112 		if (_name_size > 0) {
113 			return true;
114 		}
115 		return false;
116 	}
117 
118 	void Comment_r(ref pxtnDescriptor p_doc) @system {
119 		_read4_malloc(&_p_comment_buf, &_comment_size, p_doc);
120 	}
121 
122 	bool Comment_w(ref pxtnDescriptor p_doc) @system {
123 		if (!_p_comment_buf) {
124 			return false;
125 		}
126 		_write4(_p_comment_buf, p_doc);
127 		return true;
128 	}
129 
130 	void Name_r(ref pxtnDescriptor p_doc) @system {
131 		_read4_malloc(&_p_name_buf, &_name_size, p_doc);
132 	}
133 
134 	bool Name_w(ref pxtnDescriptor p_doc) @system {
135 		if (!_p_name_buf) {
136 			return false;
137 		}
138 		_write4(_p_name_buf, p_doc);
139 		return true;
140 	}
141 }