Give a useful error if input is neither UTF-8 nor Windows-1252
authorali <ali@juiblex.co.uk>
Sat Jul 20 11:07:02 2013 +0100 (2013-07-20)
changeset 764e6e7cc6b50d
parent 75 dab0af6882ee
child 77 9edfe77d747d
Give a useful error if input is neither UTF-8 nor Windows-1252
bookloupe/bookloupe.c
     1.1 --- a/bookloupe/bookloupe.c	Thu May 30 20:27:48 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Sat Jul 20 11:07:02 2013 +0100
     1.3 @@ -404,8 +404,10 @@
     1.4   */
     1.5  gchar *read_etext(const char *filename,GError **err)
     1.6  {
     1.7 +    GError *tmp_err=NULL;
     1.8      gchar *contents,*utf8;
     1.9 -    gsize len,nb;
    1.10 +    gsize len,bytes_read,bytes_written;
    1.11 +    int i,line,col;
    1.12      if (!g_file_get_contents(filename,&contents,&len,err))
    1.13  	return NULL;
    1.14      if (g_utf8_validate(contents,len,NULL))
    1.15 @@ -418,7 +420,27 @@
    1.16      }
    1.17      else
    1.18      {
    1.19 -	utf8=g_convert(contents,len,"UTF-8","WINDOWS-1252",NULL,&nb,NULL);
    1.20 +	utf8=g_convert(contents,len,"UTF-8","WINDOWS-1252",&bytes_read,
    1.21 +	  &bytes_written,&tmp_err);
    1.22 +	if (g_error_matches(tmp_err,G_CONVERT_ERROR,
    1.23 +	  G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
    1.24 +	{
    1.25 +	    line=col=1;
    1.26 +	    for(i=0;i<bytes_read;i++)
    1.27 +		if (contents[i]=='\n')
    1.28 +		{
    1.29 +		    line++;
    1.30 +		    col=1;
    1.31 +		}
    1.32 +		else if (contents[i]!='\r')
    1.33 +		    col++;
    1.34 +	    g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
    1.35 +	      "Input conversion failed. Byte %d at line %d, column %d is not a "
    1.36 +	      "valid Windows-1252 character",
    1.37 +	      ((unsigned char *)contents)[bytes_read],line,col);
    1.38 +	}
    1.39 +	else if (tmp_err)
    1.40 +	    g_propagate_error(err,tmp_err);
    1.41  	g_set_print_handler(print_as_windows_1252);
    1.42  #ifdef __WIN32__
    1.43  	SetConsoleOutputCP(1252);