bookloupe/bookloupe.c
changeset 169 d14a95cb0aab
parent 168 ff5126920618
child 170 8169aeaa2790
     1.1 --- a/bookloupe/bookloupe.c	Tue Oct 15 09:47:32 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Wed Oct 16 20:54:58 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;