Bugs #13+14: charsets in configuration files
authorali <ali@juiblex.co.uk>
Thu Oct 17 08:07:48 2013 +0100 (2013-10-17)
changeset 179589d5af2c38d
parent 178 db7b24d83bed
child 180 fd54c7cb276b
Bugs #13+14: charsets in configuration files
bookloupe/bookloupe.c
sample.ini
test/bookloupe/config-default.tst
test/bookloupe/config-internal.tst
test/bookloupe/config-override.tst
test/bookloupe/config-user.tst
     1.1 --- a/bookloupe/bookloupe.c	Thu Oct 17 08:05:05 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Thu Oct 17 08:07:48 2013 +0100
     1.3 @@ -268,11 +268,55 @@
     1.4  UINT saved_cp;
     1.5  #endif
     1.6  
     1.7 +gboolean set_charset(const char *name,GError **err)
     1.8 +{
     1.9 +    /* The various UNICODE encodings all share the same character set. */
    1.10 +    const char *unicode_aliases[]={ "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4",
    1.11 +      "UCS-4BE", "UCS-4LE", "UCS2", "UCS4", "UNICODE", "UNICODEBIG",
    1.12 +      "UNICODELITTLE", "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
    1.13 +      "UTF-32", "UTF-32BE", "UTF-32LE", "UTF7", "UTF8", "UTF16", "UTF16BE",
    1.14 +      "UTF16LE", "UTF32", "UTF32BE", "UTF32LE" };
    1.15 +    int i;
    1.16 +    if (charset)
    1.17 +	g_free(charset);
    1.18 +    if (charset_validator!=(GIConv)-1)
    1.19 +	g_iconv_close(charset_validator);
    1.20 +    if (!name || !g_strcasecmp(name,"auto"))
    1.21 +    {
    1.22 +	charset=NULL;
    1.23 +	charset_validator=(GIConv)-1;
    1.24 +	return TRUE;
    1.25 +    }
    1.26 +    else
    1.27 +	charset=g_strdup(name);
    1.28 +    for(i=0;i<G_N_ELEMENTS(unicode_aliases);i++)
    1.29 +	if (!g_strcasecmp(charset,unicode_aliases[i]))
    1.30 +	{
    1.31 +	    g_free(charset);
    1.32 +	    charset=g_strdup("UTF-8");
    1.33 +	    break;
    1.34 +	}
    1.35 +    if (!strcmp(charset,"UTF-8"))
    1.36 +	charset_validator=(GIConv)-1;
    1.37 +    else
    1.38 +    {
    1.39 +	charset_validator=g_iconv_open(charset,"UTF-8");
    1.40 +	if (charset_validator==(GIConv)-1)
    1.41 +	{
    1.42 +	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_NO_CONVERSION,
    1.43 +	      "Unknown character set \"%s\"",charset);
    1.44 +	    return FALSE;
    1.45 +	}
    1.46 +    }
    1.47 +    return TRUE;
    1.48 +}
    1.49 +
    1.50  GKeyFile *config;
    1.51  
    1.52  void config_file_update(GKeyFile *kf)
    1.53  {
    1.54      int i;
    1.55 +    const char *s;
    1.56      gboolean sw;
    1.57      for(i=0;options[i].long_name;i++)
    1.58      {
    1.59 @@ -285,6 +329,13 @@
    1.60  		sw=!sw;
    1.61  	    g_key_file_set_boolean(kf,"options",options[i].long_name,sw);
    1.62  	}
    1.63 +	else if (options[i].arg==G_OPTION_ARG_STRING)
    1.64 +	{
    1.65 +	    s=*(gchar **)options[i].arg_data;
    1.66 +	    if (!s)
    1.67 +		s="auto";
    1.68 +	    g_key_file_set_string(kf,"options",options[i].long_name,s);
    1.69 +	}
    1.70  	else
    1.71  	    g_assert_not_reached();
    1.72      }
    1.73 @@ -381,7 +432,7 @@
    1.74  void parse_config_file(void)
    1.75  {
    1.76      int i,j;
    1.77 -    gchar *path;
    1.78 +    gchar *path,*s;
    1.79      gchar **keys;
    1.80      gboolean sw;
    1.81      GError *err=NULL;
    1.82 @@ -410,9 +461,35 @@
    1.83  			      path,keys[i],err->message);
    1.84  			    g_clear_error(&err);
    1.85  			}
    1.86 -			if (options[j].flags&G_OPTION_FLAG_REVERSE)
    1.87 -			    sw=!sw;
    1.88 -			*(gboolean *)options[j].arg_data=sw;
    1.89 +			else
    1.90 +			{
    1.91 +			    if (options[j].flags&G_OPTION_FLAG_REVERSE)
    1.92 +				sw=!sw;
    1.93 +			    *(gboolean *)options[j].arg_data=sw;
    1.94 +			}
    1.95 +			break;
    1.96 +		    }
    1.97 +		    else if (options[j].arg==G_OPTION_ARG_STRING)
    1.98 +		    {
    1.99 +			s=g_key_file_get_string(config,"options",keys[i],
   1.100 +			  &err);
   1.101 +			if (err)
   1.102 +			{
   1.103 +			    g_printerr("Bookloupe: %s: options.%s: %s\n",
   1.104 +			      path,keys[i],err->message);
   1.105 +			    g_clear_error(&err);
   1.106 +			}
   1.107 +			else
   1.108 +			{
   1.109 +			    g_free(*(gchar **)options[j].arg_data);
   1.110 +			    if (!g_strcmp0(s,"auto"))
   1.111 +			    {
   1.112 +				*(gchar **)options[j].arg_data=NULL;
   1.113 +				g_free(s);
   1.114 +			    }
   1.115 +			    else
   1.116 +				*(gchar **)options[j].arg_data=s;
   1.117 +			}
   1.118  			break;
   1.119  		    }
   1.120  		    else
   1.121 @@ -429,49 +506,6 @@
   1.122  	g_free(path);
   1.123  }
   1.124  
   1.125 -gboolean set_charset(const char *name,GError **err)
   1.126 -{
   1.127 -    /* The various UNICODE encodings all share the same character set. */
   1.128 -    const char *unicode_aliases[]={ "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4",
   1.129 -      "UCS-4BE", "UCS-4LE", "UCS2", "UCS4", "UNICODE", "UNICODEBIG",
   1.130 -      "UNICODELITTLE", "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
   1.131 -      "UTF-32", "UTF-32BE", "UTF-32LE", "UTF7", "UTF8", "UTF16", "UTF16BE",
   1.132 -      "UTF16LE", "UTF32", "UTF32BE", "UTF32LE" };
   1.133 -    int i;
   1.134 -    if (charset)
   1.135 -	g_free(charset);
   1.136 -    if (charset_validator!=(GIConv)-1)
   1.137 -	g_iconv_close(charset_validator);
   1.138 -    if (!name || !g_strcasecmp(name,"auto"))
   1.139 -    {
   1.140 -	charset=NULL;
   1.141 -	charset_validator=(GIConv)-1;
   1.142 -	return TRUE;
   1.143 -    }
   1.144 -    else
   1.145 -	charset=g_strdup(name);
   1.146 -    for(i=0;i<G_N_ELEMENTS(unicode_aliases);i++)
   1.147 -	if (!g_strcasecmp(charset,unicode_aliases[i]))
   1.148 -	{
   1.149 -	    g_free(charset);
   1.150 -	    charset=g_strdup("UTF-8");
   1.151 -	    break;
   1.152 -	}
   1.153 -    if (!strcmp(charset,"UTF-8"))
   1.154 -	charset_validator=(GIConv)-1;
   1.155 -    else
   1.156 -    {
   1.157 -	charset_validator=g_iconv_open(charset,"UTF-8");
   1.158 -	if (charset_validator==(GIConv)-1)
   1.159 -	{
   1.160 -	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_NO_CONVERSION,
   1.161 -	      "Unknown character set \"%s\"",charset);
   1.162 -	    return FALSE;
   1.163 -	}
   1.164 -    }
   1.165 -    return TRUE;
   1.166 -}
   1.167 -
   1.168  void parse_options(int *argc,char ***argv)
   1.169  {
   1.170      GError *err=NULL;
     2.1 --- a/sample.ini	Thu Oct 17 08:05:05 2013 +0100
     2.2 +++ b/sample.ini	Thu Oct 17 08:07:48 2013 +0100
     2.3 @@ -29,3 +29,5 @@
     2.4  web=false
     2.5  # Verbose - list everything
     2.6  verbose=false
     2.7 +# Set of characters valid for this ebook
     2.8 +charset=auto
     3.1 --- a/test/bookloupe/config-default.tst	Thu Oct 17 08:05:05 2013 +0100
     3.2 +++ b/test/bookloupe/config-default.tst	Thu Oct 17 08:07:48 2013 +0100
     3.3 @@ -30,6 +30,8 @@
     3.4  usertypo=false
     3.5  # Verbose - list everything
     3.6  verbose=false
     3.7 +# Set of characters valid for this ebook
     3.8 +charset=auto
     3.9  **************** EXPECTED(stdout) ****************
    3.10  # Default configuration for bookloupe
    3.11  
    3.12 @@ -60,3 +62,5 @@
    3.13  usertypo=false
    3.14  # Verbose - list everything
    3.15  verbose=false
    3.16 +# Set of characters valid for this ebook
    3.17 +charset=auto
     4.1 --- a/test/bookloupe/config-internal.tst	Thu Oct 17 08:05:05 2013 +0100
     4.2 +++ b/test/bookloupe/config-internal.tst	Thu Oct 17 08:07:48 2013 +0100
     4.3 @@ -30,3 +30,5 @@
     4.4  usertypo=false
     4.5  # Verbose - list everything
     4.6  verbose=false
     4.7 +# Set of characters valid for this ebook
     4.8 +charset=auto
     5.1 --- a/test/bookloupe/config-override.tst	Thu Oct 17 08:05:05 2013 +0100
     5.2 +++ b/test/bookloupe/config-override.tst	Thu Oct 17 08:07:48 2013 +0100
     5.3 @@ -1,5 +1,6 @@
     5.4  **************** OPTIONS ****************
     5.5  --usertypo
     5.6 +--charset=auto
     5.7  --dump-config
     5.8  **************** INPUT(bookloupe.ini) ****************
     5.9  # Relaxed configuration for bookloupe
    5.10 @@ -31,6 +32,8 @@
    5.11  usertypo=false
    5.12  # Verbose - list everything
    5.13  verbose=false
    5.14 +# Set of characters valid for this ebook
    5.15 +charset=UNICODE
    5.16  **************** EXPECTED(stdout) ****************
    5.17  # Relaxed configuration for bookloupe
    5.18  
    5.19 @@ -61,3 +64,5 @@
    5.20  usertypo=true
    5.21  # Verbose - list everything
    5.22  verbose=false
    5.23 +# Set of characters valid for this ebook
    5.24 +charset=auto
     6.1 --- a/test/bookloupe/config-user.tst	Thu Oct 17 08:05:05 2013 +0100
     6.2 +++ b/test/bookloupe/config-user.tst	Thu Oct 17 08:07:48 2013 +0100
     6.3 @@ -35,6 +35,8 @@
     6.4  usertypo=true
     6.5  # Verbose - list everything - Contrary by name...
     6.6  verbose=true
     6.7 +# Set of characters valid for this ebook - Let's stick with Latin1
     6.8 +charset=ISO-8859-1
     6.9  **************** EXPECTED(stdout) ****************
    6.10  # Mary Contrary's configuration for bookloupe
    6.11  
    6.12 @@ -70,3 +72,5 @@
    6.13  usertypo=true
    6.14  # Verbose - list everything - Contrary by name...
    6.15  verbose=true
    6.16 +# Set of characters valid for this ebook - Let's stick with Latin1
    6.17 +charset=ISO-8859-1