Provide means to avoid check and fallback creation and usage.
authorbarbieri <barbieri@gmail.com>
Fri, 8 Feb 2008 17:58:51 +0000 (17:58 +0000)
committerbarbieri <barbieri@gmail.com>
Fri, 8 Feb 2008 17:58:51 +0000 (17:58 +0000)
lightmediascanner/src/lib/lightmediascanner_charset_conv.c
lightmediascanner/src/lib/lightmediascanner_charset_conv.h

index 6297ea4..370b2eb 100644 (file)
@@ -35,7 +35,7 @@ struct lms_charset_conv {
 };
 
 /**
- * Create a new charset conversion tool.
+ * Create a new charset conversion tool controlling its behavior.
  *
  * Conversion tool will try to convert provided strings to UTF-8, just need
  * to register known charsets with lms_charset_conv_add() and then call
@@ -44,7 +44,7 @@ struct lms_charset_conv {
  * @return newly allocated conversion tool or NULL on error.
  */
 lms_charset_conv_t *
-lms_charset_conv_new(void)
+lms_charset_conv_new_full(int use_check, int use_fallback)
 {
     lms_charset_conv_t *lcc;
 
@@ -54,16 +54,24 @@ lms_charset_conv_new(void)
         return NULL;
     }
 
-    lcc->check = iconv_open("UTF-8", "UTF-8");
-    if (lcc->check == (iconv_t)-1) {
-        perror("ERROR: could not create conversion checker");
-        goto error_check;
+    if (!use_check)
+        lcc->check = (iconv_t)-1;
+    else {
+        lcc->check = iconv_open("UTF-8", "UTF-8");
+        if (lcc->check == (iconv_t)-1) {
+            perror("ERROR: could not create conversion checker");
+            goto error_check;
+        }
     }
 
-    lcc->fallback = iconv_open("UTF-8//IGNORE", "UTF-8");
-    if (lcc->fallback == (iconv_t)-1) {
-        perror("ERROR: could not create conversion fallback");
-        goto error_fallback;
+    if (!use_fallback)
+        lcc->fallback = (iconv_t)-1;
+    else {
+        lcc->fallback = iconv_open("UTF-8//IGNORE", "UTF-8");
+        if (lcc->fallback == (iconv_t)-1) {
+            perror("ERROR: could not create conversion fallback");
+            goto error_fallback;
+        }
     }
 
     lcc->size = 0;
@@ -72,7 +80,8 @@ lms_charset_conv_new(void)
     return lcc;
 
   error_fallback:
-    iconv_close(lcc->check);
+    if (lcc->check != (iconv_t)-1)
+        iconv_close(lcc->check);
   error_check:
     free(lcc);
 
@@ -80,6 +89,21 @@ lms_charset_conv_new(void)
 }
 
 /**
+ * Create a new charset conversion tool.
+ *
+ * Conversion tool will try to convert provided strings to UTF-8, just need
+ * to register known charsets with lms_charset_conv_add() and then call
+ * lms_charset_conv().
+ *
+ * @return newly allocated conversion tool or NULL on error.
+ */
+lms_charset_conv_t *
+lms_charset_conv_new(void)
+{
+    return lms_charset_conv_new_full(1, 1);
+}
+
+/**
  * Free existing charset conversion tool.
  *
  * @param lcc existing Light Media Scanner charset conversion.
@@ -92,8 +116,10 @@ lms_charset_conv_free(lms_charset_conv_t *lcc)
     if (!lcc)
         return;
 
-    iconv_close(lcc->check);
-    iconv_close(lcc->fallback);
+    if (lcc->check != (iconv_t)-1)
+        iconv_close(lcc->check);
+    if (lcc->fallback != (iconv_t)-1)
+        iconv_close(lcc->fallback);
 
     for (i = 0; i < lcc->size; i++) {
         iconv_close(lcc->convs[i]);
@@ -230,6 +256,9 @@ _check(lms_charset_conv_t *lcc, const char *istr, unsigned int ilen, char *ostr,
     char *inbuf, *outbuf;
     size_t r, inlen, outlen;
 
+    if (lcc->check == (iconv_t)-1)
+        return -1;
+
     inbuf = (char *)istr;
     inlen = ilen;
     outbuf = ostr;
@@ -326,6 +355,9 @@ lms_charset_conv(lms_charset_conv_t *lcc, char **p_str, unsigned int *p_len)
         if (_conv(lcc->convs[i], p_str, p_len, outstr, outlen) == 0)
             return 0;
 
+    if (lcc->fallback == (iconv_t)-1)
+        return -5;
+
     fprintf(stderr,
             "WARNING: could not convert '%*s' to any charset, use fallback\n",
             *p_len, *p_str);
@@ -376,6 +408,9 @@ lms_charset_conv_force(lms_charset_conv_t *lcc, char **p_str, unsigned int *p_le
         if (_conv(lcc->convs[i], p_str, p_len, outstr, outlen) == 0)
             return 0;
 
+    if (lcc->fallback == (iconv_t)-1)
+        return -5;
+
     fprintf(stderr,
             "WARNING: could not convert '%*s' to any charset, use fallback\n",
             *p_len, *p_str);
index 6c74273..09af11b 100644 (file)
@@ -71,6 +71,7 @@ extern "C" {
 
     typedef struct lms_charset_conv lms_charset_conv_t;
 
+    API lms_charset_conv_t *lms_charset_conv_new_full(int use_check, int use_fallback) GNUC_MALLOC GNUC_WARN_UNUSED_RESULT;
     API lms_charset_conv_t *lms_charset_conv_new(void) GNUC_MALLOC GNUC_WARN_UNUSED_RESULT;
     API void lms_charset_conv_free(lms_charset_conv_t *lcc) GNUC_NON_NULL(1);
     API int lms_charset_conv_add(lms_charset_conv_t *lcc, const char *charset) GNUC_NON_NULL(1, 2);