]> pd.if.org Git - pdclib/blobdiff - opt/basecodecs/_PDCLIB_ascii.c
PDCLib includes with quotes, not <>.
[pdclib] / opt / basecodecs / _PDCLIB_ascii.c
index 2036eb382f58ff5d83b7a805d847cb02038cfce9..ac5a593d963266367c61b18f814b834eda2fff08 100644 (file)
@@ -7,23 +7,30 @@
 #include <stdbool.h>
 #ifndef REGTEST
 #include <uchar.h>
+#include "_PDCLIB_encoding.h"
+
+static bool ascii_mbsinit( const mbstate_t *ps )
+{ return 1; }
 
 static bool asciitoc32(
-    char32_t       **restrict   p_outbuf,
-    size_t          *restrict   p_outsz,
-    const char     **restrict   p_inbuf,
-    size_t          *restrict   p_insz,
-    mbstate_t       *restrict   p_ps
+    char32_t       *restrict *restrict   p_outbuf,
+    size_t                   *restrict   p_outsz,
+    const char     *restrict *restrict   p_inbuf,
+    size_t                   *restrict   p_insz,
+    mbstate_t                *restrict   p_ps
 )
 {
     while(*p_outsz && *p_insz) {
         unsigned char c = **p_inbuf;
-        if(c > 128)
+        if(c > 127)
             return false;
-        **p_outbuf = c;
+        
+        if(p_outbuf) {
+            **p_outbuf = c;
+            (*p_outbuf)++; 
+        }
 
         (*p_inbuf)++;
-        (*p_outbuf)++; 
         (*p_insz)--; 
         (*p_outsz)--;
     }
@@ -31,30 +38,41 @@ static bool asciitoc32(
 }
 
 static bool c32toascii(
-    char           **restrict  p_outbuf,
-    size_t          *restrict  p_outsz,
-    const char32_t **restrict  p_inbuf,
-    size_t          *restrict  p_insz,
-    mbstate_t       *restrict  p_ps
+    char           *restrict *restrict  p_outbuf,
+    size_t                   *restrict  p_outsz,
+    const char32_t *restrict *restrict  p_inbuf,
+    size_t                   *restrict  p_insz,
+    mbstate_t                *restrict  p_ps
 )
 {
     while(*p_outsz && *p_insz) {
         char32_t c = **p_inbuf;
-        if(c > 128)
+        if(c > 127)
             return false;
-        **p_outbuf = c;
+
+        if(p_outbuf) {
+            **p_outbuf = c;
+            (*p_outbuf)++; 
+        }
 
         (*p_inbuf)++;
-        (*p_outbuf)++; 
         (*p_insz)--; 
         (*p_outsz)--;        
     }
     return true;
 }
+
+const struct _PDCLIB_charcodec_t _PDCLIB_ascii_codec = {
+    .__mbsinit   = ascii_mbsinit,
+    .__mbstoc32s = asciitoc32,
+    .__c32stombs = c32toascii,
+    .__mb_max    = 1,
+};
+
 #endif
 
 #ifdef TEST
-#include <_PDCLIB_test.h>
+#include "_PDCLIB_test.h"
 
 int main( void )
 {