Skip to content

Commit 5f48196

Browse files
committed
Completly remodel implementation of TextStream class
1 parent 92001b0 commit 5f48196

12 files changed

+479
-497
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ BreakConstructorInitializersBeforeComma: false
6666
BreakConstructorInitializers: BeforeColon
6767
BreakAfterJavaFieldAnnotations: false
6868
BreakStringLiterals: true
69-
ColumnLimit: 120
69+
ColumnLimit: 115
7070
CommentPragmas: '^ IWYU pragma:'
7171
CompactNamespaces: false
7272
ConstructorInitializerAllOnOneLineOrOnePerLine: false

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.testout
22
*.code-workspace
33
*.mo
4+
*.po~

CMakeLists.txt

+12-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if(LIBNEX_ENABLE_NLS)
7676
# Validate locales
7777
foreach(locale ${LIBNEX_ENABLED_LOCALES})
7878
# Strip en from list. en_US is the locale that the message IDs are in, meaning that we
79-
# don't need a l12n for that
79+
# don't need a l10n for that
8080
if("${locale}" STREQUAL "en")
8181
list(REMOVE_ITEM LIBNEX_ENABLED_LOCALES "en")
8282
endif()
@@ -169,7 +169,8 @@ list(APPEND LIBNEX_HEADERS_ALWAYS
169169
${CMAKE_SOURCE_DIR}/include/libnex/bits.h
170170
${CMAKE_SOURCE_DIR}/include/libnex/object.h
171171
${CMAKE_SOURCE_DIR}/include/libnex/lock.h
172-
${CMAKE_SOURCE_DIR}/include/libnex/unicode.h)
172+
${CMAKE_SOURCE_DIR}/include/libnex/unicode.h
173+
${CMAKE_SOURCE_DIR}/include/libnex/char32.h)
173174

174175
# Figure out which libnex.h to use
175176
if(LIBNEX_BAREMETAL)
@@ -203,6 +204,9 @@ list(APPEND LIBNEX_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/include ${CMAKE_BINARY_DIR}
203204
${CMAKE_SOURCE_DIR}/src ${Intl_INCLUDE_DIRS}
204205
${NEXNIXSDK_INCLUDE_DIRS})
205206

207+
# Public include directories
208+
list(APPEND LIBNEX_PUBLIC_INCLUDE_DIRS ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
209+
206210
# Setup libraries to link with
207211
if(Intl_LIB)
208212
list(APPEND LIBNEX_LIBRARIES ${Intl_LIB})
@@ -225,7 +229,7 @@ add_library(nex ${LIBNEX_SOURCES})
225229
target_link_libraries(nex PUBLIC ${LIBNEX_LIBRARIES})
226230

227231
# Set SOName
228-
set_target_properties(nex PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION})
232+
set_target_properties(nex PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
229233
# Include the directories
230234
target_include_directories(nex PRIVATE ${LIBNEX_INCLUDE_DIRS})
231235

@@ -252,11 +256,14 @@ install(TARGETS nex)
252256
if(BUILD_SHARED_LIBS)
253257
set(LIBNEX_LIBTYPE "SHARED")
254258
set(LIBNEX_LIBRARY_FILE
255-
"${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}nex.${CMAKE_SHARED_LIBRARY_SUFFIX}")
259+
"${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}nex${CMAKE_SHARED_LIBRARY_SUFFIX}")
260+
set(LIBNEX_HAVE_SONAME TRUE)
261+
set(LIBNEX_SONAME "${CMAKE_SHARED_LIBRARY_PREFIX}nex${CMAKE_SHARED_LIBRARY_SUFFIX}.${PROJECT_VERSION_MAJOR}")
256262
else()
257263
set(LIBNEX_LIBTYPE "STATIC")
264+
set(LIBNEX_HAVE_SONAME FALSE)
258265
set(LIBNEX_LIBRARY_FILE
259-
"${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}nex.${CMAKE_STATIC_LIBRARY_SUFFIX}")
266+
"${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}nex${CMAKE_STATIC_LIBRARY_SUFFIX}")
260267
endif()
261268

262269
# Install header files

data/LibNexConfig.cmake.in

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ set(LIBNEX_LIBRARY "@LIBNEX_LIBRARY_FILE@")
2929

3030
# Create the imported target
3131
add_library(LibNex::nex @LIBNEX_LIBTYPE@ IMPORTED)
32-
set_target_properties(LibNex::nex PROPERTIES IMPORTED_SONAME "@LIBNEX_LIBRARY@")
32+
set_target_properties(LibNex::nex PROPERTIES IMPORTED_LOCATION "@LIBNEX_LIBRARY_FILE@")
3333
if(NOT "@LIBNEX_LIBRARIES@" STREQUAL "")
3434
target_link_libraries(LibNex::nex INTERFACE "@LIBNEX_LIBRARIES@")
3535
endif()
36+
target_include_directories(LibNex::nex INTERFACE "@LIBNEX_PUBLIC_INCLUDE_DIRS@")
37+
38+
# Set SOName
39+
if(@LIBNEX_HAVE_SONAME@)
40+
set_target_properties(LibNex::nex PROPERTIES IMPORTED_SONAME "@LIBNEX_SONAME@")
41+
endif()
3642

3743
check_required_components(LibNex)

include/libnex/textstream.h

+21-29
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <libnex/char32.h>
2525
#include <libnex/decls.h>
2626
#include <libnex/object.h>
27+
#include <libnex/unicode.h>
2728
#include <stdbool.h>
2829
#include <stddef.h>
2930
#include <stdint.h>
@@ -66,15 +67,15 @@ __DECL_START
6667
*/
6768
typedef struct _TextStream
6869
{
69-
Object_t obj; ///< The object for this stream
70-
FILE* file; ///< Pointer to underlying file object
71-
char* fileName; ///< The original file name
72-
uint8_t* buf; ///< Buffer to use for staging
73-
size_t bufSize; ///< Size of above buffer (defaults to 512 bytes)
74-
char encoding; ///< Underlying encoding of the stream
75-
char order; ///< Order of bytes for multi byte character sets
76-
char maxEncSize; ///< Max size of one char in the encoding
77-
char minEncSize; ///< Minimum size of one char in the encoding
70+
Object_t obj; // The object for this stream
71+
FILE* file; // Pointer to underlying file object
72+
const char* fileName; // The original file name
73+
uint8_t* buf; // Buffer to use for staging
74+
size_t bufSize; // Size of above buffer
75+
size_t bufPos; // Read position within buffer. Used only for reading
76+
char encoding; // Underlying encoding of the stream
77+
char order; // Order of bytes for multi byte character sets
78+
char mode; // Mode used to open text stream
7879
} TextStream_t;
7980

8081
/**
@@ -91,7 +92,7 @@ typedef struct _TextStream
9192
* @param[out] stream result variable to put the stream in
9293
* @return TEXT_SUCCESS, otherwise, an error code
9394
*/
94-
PUBLIC short TextOpen (char* file, TextStream_t** stream, char mode, char encoding, bool hasBom, char order);
95+
PUBLIC short TextOpen (const char* file, TextStream_t** stream, char mode, char encoding, bool hasBom, char order);
9596

9697
/**
9798
* @brief Closes a text stream
@@ -101,7 +102,7 @@ PUBLIC short TextOpen (char* file, TextStream_t** stream, char mode, char encodi
101102
*
102103
* @param[in] stream the stream to close
103104
*/
104-
PUBLIC void TextClose (TextStream_t* stream);
105+
PUBLIC short TextClose (TextStream_t* stream);
105106

106107
/**
107108
* @brief Reads data from a text stream
@@ -160,20 +161,6 @@ PUBLIC short TextWrite (TextStream_t* stream, const char32_t* buf, const size_t
160161
*/
161162
PUBLIC long TextSize (TextStream_t* stream);
162163

163-
/**
164-
* @brief Sets the size of the staging buffer
165-
*
166-
* TextSetBufSz resizes the staging buffer to the size specified by sz
167-
* This could used to help optimize performance by selecting a large buffer
168-
* size, or optimizing memory usage by selecting a small buffer size This
169-
* function has a potential exit point. It may call exit(3) in an error
170-
* condition
171-
*
172-
* @param[in] stream the stream to operate on
173-
* @param[in] sz the new buffer size
174-
*/
175-
PUBLIC void TextSetBufSz (TextStream_t* stream, size_t sz);
176-
177164
/**
178165
* @brief Returns a textual representation of a textstream error code
179166
* @param code the error code turn into a string
@@ -184,21 +171,26 @@ PUBLIC const char* TextError (int code);
184171
/**
185172
* @brief Takes a libchardet charset name, and returns a textsream encoding and byte order
186173
* @param encName the name of the encoding
187-
* @param enc the numeric encoding ID to write to
188-
* @param order the byte order to write to
174+
* @param enc the numeric encoding ID pointer to write to
175+
* @param order the byte order pointer to write to
189176
*/
190177
PUBLIC void TextGetEncId (const char* encName, char* enc, char* order);
191178

179+
/**
180+
* @brief Flushes the contents of a text stream when the stream in a writing mode
181+
* @param stream the stream to flush
182+
* @return an error code, or TEXT_SUCCESS
183+
*/
184+
PUBLIC short TextFlush (TextStream_t* stream);
185+
192186
__DECL_END
193187

194188
// Helper macros
195189
#define TextGetEncoding(stream) ((stream)->encoding) ///< Grabs the character encoding of stream
196190
#define TextGetOrder(stream) ((stream)->order) ///< Grabs the byte order of stream
197-
#define TextGetBufSz(stream) ((stream)->bufSize) ///< Grabs the size of the staging buffer in stream
198191
#define TextRef(item) ((TextStream_t*) ObjRef (&(item)->obj)) ///< References the underlying the object
199192
#define TextLock(item) (ObjLock (&(item)->obj)) ///< Locks this stream
200193
#define TextUnlock(item) (ObjUnlock (&(item)->obj)) ///< Unlocks the stream
201194
#define TextDeRef(item) (ObjDestroy (&(item)->obj)) ///< Dereferences this stream
202-
#define TextGetFile(stream) ((stream)->file) ///< Obtains the file handle of this stream
203195

204196
#endif

include/libnex/unicode.h

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef struct _utf8state
4848

4949
#define UnicodeIsAccepted(state) ((state).state == 6) ///< Checks if state has been accepted
5050
#define UnicodeIsAcceptedP(state) ((state)->state == 6) ///< Checks if state has been accepted
51+
#define UnicodeStateInit(state) ((state).state = 0) ///< Initializes state structure
52+
#define UnicodeStateInitP(state) ((state)->state = 0) ///< Initializes state structure
5153

5254
/**
5355
* @brief Decodes a UTF-16 character to UTF-32
@@ -77,6 +79,7 @@ PUBLIC size_t UnicodeEncode16 (uint16_t* out, char32_t in, char endian);
7779
* @param out pointer to the resulting codepoint
7880
* @param in the current byte in the sequence
7981
* @param state pointer to structure maintaining the parser's state
82+
* @return The number of byte decoded from in. 0 on error
8083
*/
8184
PUBLIC size_t UnicodeDecodePart8 (char32_t* out, uint8_t in, Utf8State_t* state);
8285

@@ -85,6 +88,7 @@ PUBLIC size_t UnicodeDecodePart8 (char32_t* out, uint8_t in, Utf8State_t* state)
8588
* @param out the buffer to write the character out to
8689
* @param in pointer to buffer containing sequence to convert
8790
* @param sz size of in
91+
* @return the number of bytes decoded from in. 0 on error
8892
*/
8993
PUBLIC size_t UnicodeDecode8 (char32_t* out, const uint8_t* in, size_t sz);
9094

@@ -93,6 +97,7 @@ PUBLIC size_t UnicodeDecode8 (char32_t* out, const uint8_t* in, size_t sz);
9397
* @param out the buffer to write the encoded UTF-8 out to
9498
* @param in character to encode
9599
* @param sz size of out
100+
* @return the number of bytes encoded. 0 on error
96101
*/
97102
PUBLIC size_t UnicodeEncode8 (uint8_t* out, char32_t in, size_t sz);
98103

src/error.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ void error (char* msg, ...)
3939
va_start (ap, msg);
4040

4141
// Print it out
42-
printf ("%s: ", getprogname());
42+
fprintf (stderr, "%s: ", getprogname());
4343
vfprintf (stderr, msg, ap);
44-
printf ("\n");
44+
fprintf (stderr, "\n");
4545

4646
// Exit
4747
va_end (ap);

src/po/es/libnex.po

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: PACKAGE VERSION\n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2022-03-08 12:39-0500\n"
10+
"POT-Creation-Date: 2022-03-12 16:32-0500\n"
1111
"PO-Revision-Date: 2022-03-07 22:29-0500\n"
1212
"Last-Translator: nexos <[email protected]>\n"
1313
"Language-Team: Language es\n"
@@ -16,26 +16,26 @@ msgstr ""
1616
"Content-Type: text/plain; charset=UTF-8\n"
1717
"Content-Transfer-Encoding: 8bit\n"
1818

19-
#: src/textstream.c:677
19+
#: src/textstream.c:643
2020
msgid "No error"
21-
msgstr "No error"
21+
msgstr "No hay error"
2222

23-
#: src/textstream.c:679
23+
#: src/textstream.c:645
2424
msgid "Invalid parameter"
25-
msgstr "Parametro invalido"
25+
msgstr "Un parametro es invalido"
2626

27-
#: src/textstream.c:680
27+
#: src/textstream.c:646
2828
msgid "Invalid byte order mark"
29-
msgstr "Marca de orden de bytes no válida"
29+
msgstr "La marca de orden de bytes no válida"
3030

31-
#: src/textstream.c:681
31+
#: src/textstream.c:647
3232
msgid "Character can't be encoded by character set"
3333
msgstr "El carácter no se puede codificar por el conjunto de caracteres"
3434

35-
#: src/textstream.c:682
35+
#: src/textstream.c:648
3636
msgid "Result buffer too small"
3737
msgstr "El búfer de resultados es demasiado pequeño"
3838

39-
#: src/textstream.c:683
39+
#: src/textstream.c:649
4040
msgid "Unsupported character encoding"
41-
msgstr "Conjunto de caracteres no soportado"
41+
msgstr "El conjunto de caracteres es no soportado"

src/po/libnex.pot

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2022-03-08 12:39-0500\n"
11+
"POT-Creation-Date: 2022-03-12 16:32-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -17,26 +17,26 @@ msgstr ""
1717
"Content-Type: text/plain; charset=CHARSET\n"
1818
"Content-Transfer-Encoding: 8bit\n"
1919

20-
#: src/textstream.c:677
20+
#: src/textstream.c:643
2121
msgid "No error"
2222
msgstr ""
2323

24-
#: src/textstream.c:679
24+
#: src/textstream.c:645
2525
msgid "Invalid parameter"
2626
msgstr ""
2727

28-
#: src/textstream.c:680
28+
#: src/textstream.c:646
2929
msgid "Invalid byte order mark"
3030
msgstr ""
3131

32-
#: src/textstream.c:681
32+
#: src/textstream.c:647
3333
msgid "Character can't be encoded by character set"
3434
msgstr ""
3535

36-
#: src/textstream.c:682
36+
#: src/textstream.c:648
3737
msgid "Result buffer too small"
3838
msgstr ""
3939

40-
#: src/textstream.c:683
40+
#: src/textstream.c:649
4141
msgid "Unsupported character encoding"
4242
msgstr ""

0 commit comments

Comments
 (0)