1.1 --- a/bookloupe/bookloupe.c Sun Oct 27 16:36:46 2013 +0000
1.2 +++ b/bookloupe/bookloupe.c Sun Oct 27 16:58:50 2013 +0000
1.3 @@ -32,6 +32,9 @@
1.4 #include "pending.h"
1.5 #include "HTMLentities.h"
1.6
1.7 +gchar *charset; /* Or NULL for auto (ISO_8859-1/ASCII or UNICODE) */
1.8 +GIConv charset_validator=(GIConv)-1;
1.9 +
1.10 gchar *prevline;
1.11
1.12 /* Common typos. */
1.13 @@ -127,6 +130,7 @@
1.14 };
1.15
1.16 gboolean pswit[SWITNO]; /* program switches */
1.17 +gchar *opt_charset;
1.18
1.19 gboolean typo_compat,paranoid_compat;
1.20
1.21 @@ -211,6 +215,8 @@
1.22 "Defaults for use on www upload", NULL },
1.23 { "dump-config", 0, 0, G_OPTION_ARG_NONE, pswit+DUMP_CONFIG_SWITCH,
1.24 "Dump current config settings", NULL },
1.25 + { "charset", 0, 0, G_OPTION_ARG_STRING, &opt_charset,
1.26 + "Set of characters valid for this ebook", "NAME" },
1.27 { NULL }
1.28 };
1.29
1.30 @@ -423,6 +429,49 @@
1.31 g_free(path);
1.32 }
1.33
1.34 +gboolean set_charset(const char *name,GError **err)
1.35 +{
1.36 + /* The various UNICODE encodings all share the same character set. */
1.37 + const char *unicode_aliases[]={ "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4",
1.38 + "UCS-4BE", "UCS-4LE", "UCS2", "UCS4", "UNICODE", "UNICODEBIG",
1.39 + "UNICODELITTLE", "UTF-7", "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE",
1.40 + "UTF-32", "UTF-32BE", "UTF-32LE", "UTF7", "UTF8", "UTF16", "UTF16BE",
1.41 + "UTF16LE", "UTF32", "UTF32BE", "UTF32LE" };
1.42 + int i;
1.43 + if (charset)
1.44 + g_free(charset);
1.45 + if (charset_validator!=(GIConv)-1)
1.46 + g_iconv_close(charset_validator);
1.47 + if (!name || !g_strcasecmp(name,"auto"))
1.48 + {
1.49 + charset=NULL;
1.50 + charset_validator=(GIConv)-1;
1.51 + return TRUE;
1.52 + }
1.53 + else
1.54 + charset=g_strdup(name);
1.55 + for(i=0;i<G_N_ELEMENTS(unicode_aliases);i++)
1.56 + if (!g_strcasecmp(charset,unicode_aliases[i]))
1.57 + {
1.58 + g_free(charset);
1.59 + charset=g_strdup("UTF-8");
1.60 + break;
1.61 + }
1.62 + if (!strcmp(charset,"UTF-8"))
1.63 + charset_validator=(GIConv)-1;
1.64 + else
1.65 + {
1.66 + charset_validator=g_iconv_open(charset,"UTF-8");
1.67 + if (charset_validator==(GIConv)-1)
1.68 + {
1.69 + g_set_error(err,G_CONVERT_ERROR,G_CONVERT_ERROR_NO_CONVERSION,
1.70 + "Unknown character set \"%s\"",charset);
1.71 + return FALSE;
1.72 + }
1.73 + }
1.74 + return TRUE;
1.75 +}
1.76 +
1.77 void parse_options(int *argc,char ***argv)
1.78 {
1.79 GError *err=NULL;
1.80 @@ -475,11 +524,18 @@
1.81 pswit[USERTYPO_SWITCH]=FALSE;
1.82 pswit[DP_SWITCH]=FALSE;
1.83 }
1.84 + if (opt_charset && !set_charset(opt_charset,&err))
1.85 + {
1.86 + g_printerr("%s\n",err->message);
1.87 + exit(1);
1.88 + }
1.89 if (pswit[DUMP_CONFIG_SWITCH])
1.90 {
1.91 dump_config();
1.92 exit(0);
1.93 }
1.94 + g_free(opt_charset);
1.95 + opt_charset=NULL;
1.96 if (pswit[OVERVIEW_SWITCH])
1.97 /* just print summary; don't echo */
1.98 pswit[ECHO_SWITCH]=FALSE;
1.99 @@ -542,7 +598,11 @@
1.100 exit(1);
1.101 }
1.102 if (g_utf8_validate(contents,len,NULL))
1.103 + {
1.104 utf8=g_utf8_normalize(contents,len,G_NORMALIZE_DEFAULT_COMPOSE);
1.105 + if (!charset)
1.106 + (void)set_charset("UNICODE",NULL);
1.107 + }
1.108 else
1.109 utf8=g_convert(contents,len,"UTF-8","WINDOWS-1252",NULL,&nb,NULL);
1.110 g_free(contents);
1.111 @@ -674,6 +734,7 @@
1.112 g_free(running_from);
1.113 if (usertypo)
1.114 g_tree_unref(usertypo);
1.115 + set_charset(NULL,NULL);
1.116 if (config)
1.117 g_key_file_free(config);
1.118 return 0;
1.119 @@ -1024,25 +1085,32 @@
1.120 "Not reporting them.\n",
1.121 results->spacedash+results->emdash.non_PG_space);
1.122 }
1.123 - /* If more than a quarter of characters are hi-bit, bug out. */
1.124 - warnings.bin=1;
1.125 - if (results->binlen*4>results->totlen)
1.126 + if (charset)
1.127 + warnings.bin=0;
1.128 + else
1.129 {
1.130 - g_print(" --> This file does not appear to be ASCII. "
1.131 - "Terminating. Best of luck with it!\n");
1.132 - exit(1);
1.133 - }
1.134 - if (results->alphalen*4<results->totlen)
1.135 - {
1.136 - g_print(" --> This file does not appear to be text. "
1.137 - "Terminating. Best of luck with it!\n");
1.138 - exit(1);
1.139 - }
1.140 - if (results->binlen*100>results->totlen || results->binlen>100)
1.141 - {
1.142 - g_print(" --> There are a lot of foreign letters here. "
1.143 - "Not reporting them.\n");
1.144 - warnings.bin=0;
1.145 + /* Charset ISO_8859-1/ASCII checks for compatibility with gutcheck */
1.146 + warnings.bin=1;
1.147 + /* If more than a quarter of characters are hi-bit, bug out. */
1.148 + if (results->binlen*4>results->totlen)
1.149 + {
1.150 + g_print(" --> This file does not appear to be ASCII. "
1.151 + "Terminating. Best of luck with it!\n");
1.152 + exit(1);
1.153 + }
1.154 + if (results->alphalen*4<results->totlen)
1.155 + {
1.156 + g_print(" --> This file does not appear to be text. "
1.157 + "Terminating. Best of luck with it!\n");
1.158 + exit(1);
1.159 + }
1.160 + if (results->binlen*100>results->totlen || results->binlen>100)
1.161 + {
1.162 + g_print(" --> There are a lot of foreign letters here. "
1.163 + "Not reporting them.\n");
1.164 + if (!pswit[VERBOSE_SWITCH])
1.165 + warnings.bin=0;
1.166 + }
1.167 }
1.168 warnings.isDutch=FALSE;
1.169 if (results->Dutchcount>50)
1.170 @@ -1070,7 +1138,6 @@
1.171 g_print("\n");
1.172 if (pswit[VERBOSE_SWITCH])
1.173 {
1.174 - warnings.bin=1;
1.175 warnings.shortline=1;
1.176 warnings.dotcomma=1;
1.177 warnings.longline=1;
1.178 @@ -1265,14 +1332,17 @@
1.179 gboolean isemptyline)
1.180 {
1.181 /* Don't repeat multiple warnings on one line. */
1.182 - gboolean eNon_A=FALSE,eTab=FALSE,eTilde=FALSE;
1.183 + gboolean eInvalidChar=FALSE,eTab=FALSE,eTilde=FALSE;
1.184 gboolean eCarat=FALSE,eFSlash=FALSE,eAst=FALSE;
1.185 const char *s;
1.186 gunichar c;
1.187 + gsize nb;
1.188 + gchar *t;
1.189 for (s=aline;*s;s=g_utf8_next_char(s))
1.190 {
1.191 c=g_utf8_get_char(s);
1.192 - if (!eNon_A && (c<CHAR_SPACE && c!='\t' && c!='\n' || c>127))
1.193 + if (warnings->bin && !eInvalidChar &&
1.194 + (c<CHAR_SPACE && c!='\t' && c!='\n' || c>127))
1.195 {
1.196 if (pswit[ECHO_SWITCH])
1.197 g_print("\n%s\n",aline);
1.198 @@ -1287,7 +1357,57 @@
1.199 linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
1.200 else
1.201 cnt_bin++;
1.202 - eNon_A=TRUE;
1.203 + eInvalidChar=TRUE;
1.204 + }
1.205 + if (!eInvalidChar && charset)
1.206 + {
1.207 + if (charset_validator==(GIConv)-1)
1.208 + {
1.209 + if (!g_unichar_isdefined(c))
1.210 + {
1.211 + if (pswit[ECHO_SWITCH])
1.212 + g_print("\n%s\n",aline);
1.213 + if (!pswit[OVERVIEW_SWITCH])
1.214 + g_print(" Line %ld column %ld - Unassigned UNICODE "
1.215 + "code point U+%04" G_GINT32_MODIFIER "X\n",
1.216 + linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
1.217 + else
1.218 + cnt_bin++;
1.219 + eInvalidChar=TRUE;
1.220 + }
1.221 + else if (c>=0xE000 && c<=0xF8FF || c>=0xF0000 && c<=0xFFFFD ||
1.222 + c>=100000 && c<=0x10FFFD)
1.223 + {
1.224 + if (pswit[ECHO_SWITCH])
1.225 + g_print("\n%s\n",aline);
1.226 + if (!pswit[OVERVIEW_SWITCH])
1.227 + g_print(" Line %ld column %ld - Private Use "
1.228 + "character U+%04" G_GINT32_MODIFIER "X\n",
1.229 + linecnt,g_utf8_pointer_to_offset(aline,s)+1,c);
1.230 + else
1.231 + cnt_bin++;
1.232 + eInvalidChar=TRUE;
1.233 + }
1.234 + }
1.235 + else
1.236 + {
1.237 + t=g_convert_with_iconv(s,g_utf8_next_char(s)-s,
1.238 + charset_validator,NULL,&nb,NULL);
1.239 + if (t)
1.240 + g_free(t);
1.241 + else
1.242 + {
1.243 + if (pswit[ECHO_SWITCH])
1.244 + g_print("\n%s\n",aline);
1.245 + if (!pswit[OVERVIEW_SWITCH])
1.246 + g_print(" Line %ld column %ld - Non-%s "
1.247 + "character %u\n",linecnt,
1.248 + g_utf8_pointer_to_offset(aline,s)+1,charset,c);
1.249 + else
1.250 + cnt_bin++;
1.251 + eInvalidChar=TRUE;
1.252 + }
1.253 + }
1.254 }
1.255 if (!eTab && c==CHAR_TAB)
1.256 {
1.257 @@ -2975,8 +3095,7 @@
1.258 if (s>=aline && g_utf8_get_char(s)=='-')
1.259 enddash=TRUE;
1.260 check_for_control_characters(aline);
1.261 - if (warnings->bin)
1.262 - check_for_odd_characters(aline,warnings,isemptyline);
1.263 + check_for_odd_characters(aline,warnings,isemptyline);
1.264 if (warnings->longline)
1.265 check_for_long_line(aline);
1.266 if (warnings->shortline)