yaz_poll: Allow num_fds == 0
[yaz-moved-to-github.git] / src / utf8.c
index 37ee04c..038b744 100644 (file)
@@ -1,7 +1,6 @@
-/*
- * Copyright (C) 1995-2008, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2012 Index Data
  * See the file LICENSE for details.
- *
  */
 /**
  * \file
 #include <assert.h>
 #include <errno.h>
 #include <string.h>
-#include <ctype.h>
 
 #include "iconv-p.h"
 
-size_t yaz_init_UTF8(yaz_iconv_t cd, unsigned char *inp,
-                     size_t inbytesleft, size_t *no_read)
+static size_t init_utf8(yaz_iconv_t cd, yaz_iconv_decoder_t d,
+                        unsigned char *inp,
+                        size_t inbytesleft, size_t *no_read)
 {
-    if (inp[0] != 0xef)
+    if (!inp || inp[0] != 0xef)
     {
         *no_read = 0;
         return 0;
@@ -136,8 +135,9 @@ unsigned long yaz_read_UTF8_char(unsigned char *inp,
     return x;
 }
 
-unsigned long yaz_read_UTF8(yaz_iconv_t cd, unsigned char *inp,
-                            size_t inbytesleft, size_t *no_read)
+static unsigned long read_utf8(yaz_iconv_t cd, yaz_iconv_decoder_t d,
+                               unsigned char *inp,
+                               size_t inbytesleft, size_t *no_read)
 {
     int err = 0;
     int r = yaz_read_UTF8_char(inp, inbytesleft, no_read, &err);
@@ -228,11 +228,43 @@ yaz_iconv_encoder_t yaz_utf8_encoder(const char *tocode,
     return 0;
 }
 
+yaz_iconv_decoder_t yaz_utf8_decoder(const char *fromcode,
+                                     yaz_iconv_decoder_t d)
+{
+    if (!yaz_matchstr(fromcode, "UTF8"))
+    {
+        d->init_handle = init_utf8;
+        d->read_handle = read_utf8;
+        return d;
+    }
+    return 0;
+}
+
+int yaz_utf8_check(const char *str)
+{
+    /* cast OK: yaz_read_UTF8_char is read-only */
+    unsigned char *inp = (unsigned char *) str;
+    size_t inbytesleft = strlen(str);
+
+    while (inbytesleft)
+    {
+        int error = 0;
+        size_t no_read;
+        yaz_read_UTF8_char(inp, inbytesleft, &no_read, &error);
+        if (error)
+            return 0;
+        inp += no_read;
+        inbytesleft -= no_read;
+    }
+    return 1;
+}   
 
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+