23
23
24
24
#include <libnex/decls.h>
25
25
#include <libnex/object.h>
26
+ #include <stdbool.h>
26
27
#include <stddef.h>
27
28
#include <stdint.h>
28
29
#include <stdio.h>
49
50
#define TEXT_SYS_ERROR 2 ///< errno contains the error
50
51
#define TEXT_INVALID_PARAMETER 3 ///< User passed an invalid parameter
51
52
#define TEXT_BAD_BOM 4 ///< A bad BOM was encountered
52
- #define TEXT_NARROW_WCHAR 5 ///< Attempting to parse UTF-32 on a system with a narrow wchar_t
53
+ #define TEXT_NARROW_WCHAR 5 ///< Attempting to parse UTF-32 on system with narrow wchar_t
53
54
#define TEXT_INVALID_CHAR 6 ///< Character doesn't fit in destination character set
55
+ #define TEXT_BUF_TOO_SMALL 7 ///< Character won't fit in buffer
56
+ #define TEXT_NO_SURROGATE \
57
+ 8 ///< User specified that no surrogates should be expanded. Only affect systems where sizeof(wchar_t) == 2
54
58
55
59
__DECL_START
56
60
@@ -70,6 +74,8 @@ typedef struct _TextStream
70
74
size_t bufSize ; ///< Size of above buffer (defaults to 512 bytes)
71
75
char encoding ; ///< Underlying encoding of the stream
72
76
char order ; ///< Order of bytes for multi byte character sets
77
+ bool expandSur ; ///< Wheter surrogate pairs should be expanded.
78
+ char encSize ; ///< Size of one char in the encoding
73
79
} TextStream_t ;
74
80
75
81
/**
@@ -104,12 +110,18 @@ PUBLIC void TextClose (TextStream_t* stream);
104
110
* TextRead takes a buffer, character count, and stream object,
105
111
* and reads / decodes count codepoints into buf from stream.
106
112
* Data is intially read into a staging buffer, and then the staging buffer is
107
- * decoded into the main buffer specified by buf
113
+ * decoded into the main buffer specified by buf.
114
+ *
115
+ * WARNING: If you are on a platform where sizeof(wchar_t) == 2 and you are decoding a UTF-16 stream,
116
+ * you SHALL make buf's size equal to the number of characters you want to decode times 2.
117
+ * This is in case buf contains surrogate pairs; those are copied as one character and take 4 bytes.
118
+ * Ensure count is equal to the number of characters, NOT the size you malloc'ed.
119
+ * Else, TextRead will fail with error TEXT_BUF_TOO_SMALL
108
120
*
109
121
* @param[in] stream the stream to read from
110
122
* @param[out] buf a buffer of wchar_t's to decode into
111
123
* @param[in] count the number of wchar_t's to decode
112
- * @param[out] the number or characters read
124
+ * @param[out] charsRead the number or characters read
113
125
* @return a result code
114
126
*/
115
127
PUBLIC short TextRead (TextStream_t * stream , wchar_t * buf , const size_t count , size_t * charsRead );
@@ -169,6 +181,13 @@ PUBLIC long TextSize (TextStream_t* stream);
169
181
*/
170
182
PUBLIC void TextSetBufSz (TextStream_t * stream , size_t sz );
171
183
184
+ /**
185
+ * @brief Returns t a textual representation of code
186
+ * @param code the error code turn into a string
187
+ * @return the string message
188
+ */
189
+ PUBLIC const char * TextError (int code );
190
+
172
191
__DECL_END
173
192
174
193
// Helper macros
@@ -179,5 +198,7 @@ __DECL_END
179
198
#define TextLock (item ) (ObjLock (&(item)->obj)) ///< Locks this stream
180
199
#define TextUnlock (item ) (ObjUnlock (&(item)->obj)) ///< Unlocks the stream
181
200
#define TextDeRef (item ) (ObjDestroy (&(item)->obj)) ///< Dereferences this stream
201
+ #define TextNoSurroate (stream ) \
202
+ ((stream)->expandSur = false) ///< Necessary if the user doesn't want to deal with surrogate pairs
182
203
183
204
#endif
0 commit comments