Bugs #13+14: charsets in configuration files
authorali <ali@juiblex.co.uk>
Sun Oct 27 17:01:47 2013 +0000 (2013-10-27)
changeset 202c25e023cb9fe
parent 201 f1d85b36e188
child 203 53532149d849
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	Sun Oct 27 16:58:50 2013 +0000
     1.2 +++ b/bookloupe/bookloupe.c	Sun Oct 27 17:01:47 2013 +0000
     1.3 @@ -202,6 +202,8 @@
     1.4      { "no-verbose", 0, G_OPTION_FLAG_HIDDEN|G_OPTION_FLAG_REVERSE,
     1.5        G_OPTION_ARG_NONE, pswit+VERBOSE_SWITCH,
     1.6        "Switch off verbose mode", NULL },
     1.7 +    { "charset", 0, 0, G_OPTION_ARG_STRING, &opt_charset,
     1.8 +      "Set of characters valid for this ebook", "NAME" },
     1.9      { NULL }
    1.10  };
    1.11  
    1.12 @@ -215,8 +217,6 @@
    1.13        "Defaults for use on www upload", NULL },
    1.14      { "dump-config", 0, 0, G_OPTION_ARG_NONE, pswit+DUMP_CONFIG_SWITCH,
    1.15        "Dump current config settings", NULL },
    1.16 -    { "charset", 0, 0, G_OPTION_ARG_STRING, &opt_charset,
    1.17 -      "Set of characters valid for this ebook", "NAME" },
    1.18      { NULL }
    1.19  };
    1.20  
    1.21 @@ -268,11 +268,55 @@
    1.22  UINT saved_cp;
    1.23  #endif
    1.24  
    1.25 +gboolean set_charset(const char *name,GError **err)
    1.26 +{
    1.27 +    /* The various UNICODE encodings all share the same character set. */
    1.28 +    const char *unicode_aliases[]={ "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4",
    1.29 +      "UCS-4BE", "UCS-4LE", "UCS2", "UCS4", "UNICODE", "UNICODEBIG",
    1.30 +      "UNICODELITTLE", "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
    1.31 +      "UTF-32", "UTF-32BE", "UTF-32LE", "UTF7", "UTF8", "UTF16", "UTF16BE",
    1.32 +      "UTF16LE", "UTF32", "UTF32BE", "UTF32LE" };
    1.33 +    int i;
    1.34 +    if (charset)
    1.35 +	g_free(charset);
    1.36 +    if (charset_validator!=(GIConv)-1)
    1.37 +	g_iconv_close(charset_validator);
    1.38 +    if (!name || !g_strcasecmp(name,"auto"))
    1.39 +    {
    1.40 +	charset=NULL;
    1.41 +	charset_validator=(GIConv)-1;
    1.42 +	return TRUE;
    1.43 +    }
    1.44 +    else
    1.45 +	charset=g_strdup(name);
    1.46 +    for(i=0;i<G_N_ELEMENTS(unicode_aliases);i++)
    1.47 +	if (!g_strcasecmp(charset,unicode_aliases[i]))
    1.48 +	{
    1.49 +	    g_free(charset);
    1.50 +	    charset=g_strdup("UTF-8");
    1.51 +	    break;
    1.52 +	}
    1.53 +    if (!strcmp(charset,"UTF-8"))
    1.54 +	charset_validator=(GIConv)-1;
    1.55 +    else
    1.56 +    {
    1.57 +	charset_validator=g_iconv_open(charset,"UTF-8");
    1.58 +	if (charset_validator==(GIConv)-1)
    1.59 +	{
    1.60 +	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_NO_CONVERSION,
    1.61 +	      "Unknown character set \"%s\"",charset);
    1.62 +	    return FALSE;
    1.63 +	}
    1.64 +    }
    1.65 +    return TRUE;
    1.66 +}
    1.67 +
    1.68  GKeyFile *config;
    1.69  
    1.70  void config_file_update(GKeyFile *kf)
    1.71  {
    1.72      int i;
    1.73 +    const char *s;
    1.74      gboolean sw;
    1.75      for(i=0;options[i].long_name;i++)
    1.76      {
    1.77 @@ -285,6 +329,13 @@
    1.78  		sw=!sw;
    1.79  	    g_key_file_set_boolean(kf,"options",options[i].long_name,sw);
    1.80  	}
    1.81 +	else if (options[i].arg==G_OPTION_ARG_STRING)
    1.82 +	{
    1.83 +	    s=*(gchar **)options[i].arg_data;
    1.84 +	    if (!s)
    1.85 +		s="auto";
    1.86 +	    g_key_file_set_string(kf,"options",options[i].long_name,s);
    1.87 +	}
    1.88  	else
    1.89  	    g_assert_not_reached();
    1.90      }
    1.91 @@ -381,7 +432,7 @@
    1.92  void parse_config_file(void)
    1.93  {
    1.94      int i,j;
    1.95 -    gchar *path;
    1.96 +    gchar *path,*s;
    1.97      gchar **keys;
    1.98      gboolean sw;
    1.99      GError *err=NULL;
   1.100 @@ -410,9 +461,35 @@
   1.101  			      path,keys[i],err->message);
   1.102  			    g_clear_error(&err);
   1.103  			}
   1.104 -			if (options[j].flags&G_OPTION_FLAG_REVERSE)
   1.105 -			    sw=!sw;
   1.106 -			*(gboolean *)options[j].arg_data=sw;
   1.107 +			else
   1.108 +			{
   1.109 +			    if (options[j].flags&G_OPTION_FLAG_REVERSE)
   1.110 +				sw=!sw;
   1.111 +			    *(gboolean *)options[j].arg_data=sw;
   1.112 +			}
   1.113 +			break;
   1.114 +		    }
   1.115 +		    else if (options[j].arg==G_OPTION_ARG_STRING)
   1.116 +		    {
   1.117 +			s=g_key_file_get_string(config,"options",keys[i],
   1.118 +			  &err);
   1.119 +			if (err)
   1.120 +			{
   1.121 +			    g_printerr("Bookloupe: %s: options.%s: %s\n",
   1.122 +			      path,keys[i],err->message);
   1.123 +			    g_clear_error(&err);
   1.124 +			}
   1.125 +			else
   1.126 +			{
   1.127 +			    g_free(*(gchar **)options[j].arg_data);
   1.128 +			    if (!g_strcmp0(s,"auto"))
   1.129 +			    {
   1.130 +				*(gchar **)options[j].arg_data=NULL;
   1.131 +				g_free(s);
   1.132 +			    }
   1.133 +			    else
   1.134 +				*(gchar **)options[j].arg_data=s;
   1.135 +			}
   1.136  			break;
   1.137  		    }
   1.138  		    else
   1.139 @@ -429,49 +506,6 @@
   1.140  	g_free(path);
   1.141  }
   1.142  
   1.143 -gboolean set_charset(const char *name,GError **err)
   1.144 -{
   1.145 -    /* The various UNICODE encodings all share the same character set. */
   1.146 -    const char *unicode_aliases[]={ "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4",
   1.147 -      "UCS-4BE", "UCS-4LE", "UCS2", "UCS4", "UNICODE", "UNICODEBIG",
   1.148 -      "UNICODELITTLE", "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
   1.149 -      "UTF-32", "UTF-32BE", "UTF-32LE", "UTF7", "UTF8", "UTF16", "UTF16BE",
   1.150 -      "UTF16LE", "UTF32", "UTF32BE", "UTF32LE" };
   1.151 -    int i;
   1.152 -    if (charset)
   1.153 -	g_free(charset);
   1.154 -    if (charset_validator!=(GIConv)-1)
   1.155 -	g_iconv_close(charset_validator);
   1.156 -    if (!name || !g_strcasecmp(name,"auto"))
   1.157 -    {
   1.158 -	charset=NULL;
   1.159 -	charset_validator=(GIConv)-1;
   1.160 -	return TRUE;
   1.161 -    }
   1.162 -    else
   1.163 -	charset=g_strdup(name);
   1.164 -    for(i=0;i<G_N_ELEMENTS(unicode_aliases);i++)
   1.165 -	if (!g_strcasecmp(charset,unicode_aliases[i]))
   1.166 -	{
   1.167 -	    g_free(charset);
   1.168 -	    charset=g_strdup("UTF-8");
   1.169 -	    break;
   1.170 -	}
   1.171 -    if (!strcmp(charset,"UTF-8"))
   1.172 -	charset_validator=(GIConv)-1;
   1.173 -    else
   1.174 -    {
   1.175 -	charset_validator=g_iconv_open(charset,"UTF-8");
   1.176 -	if (charset_validator==(GIConv)-1)
   1.177 -	{
   1.178 -	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_NO_CONVERSION,
   1.179 -	      "Unknown character set \"%s\"",charset);
   1.180 -	    return FALSE;
   1.181 -	}
   1.182 -    }
   1.183 -    return TRUE;
   1.184 -}
   1.185 -
   1.186  void parse_options(int *argc,char ***argv)
   1.187  {
   1.188      GError *err=NULL;
     2.1 --- a/sample.ini	Sun Oct 27 16:58:50 2013 +0000
     2.2 +++ b/sample.ini	Sun Oct 27 17:01:47 2013 +0000
     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	Sun Oct 27 16:58:50 2013 +0000
     3.2 +++ b/test/bookloupe/config-default.tst	Sun Oct 27 17:01:47 2013 +0000
     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	Sun Oct 27 16:58:50 2013 +0000
     4.2 +++ b/test/bookloupe/config-internal.tst	Sun Oct 27 17:01:47 2013 +0000
     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	Sun Oct 27 16:58:50 2013 +0000
     5.2 +++ b/test/bookloupe/config-override.tst	Sun Oct 27 17:01:47 2013 +0000
     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	Sun Oct 27 16:58:50 2013 +0000
     6.2 +++ b/test/bookloupe/config-user.tst	Sun Oct 27 17:01:47 2013 +0000
     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