bookloupe/pending.c
changeset 103 adc06e9e8470
child 111 f805130deb6f
child 123 ddb5ddba6ef3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bookloupe/pending.c	Mon Sep 23 21:18:27 2013 +0100
     1.3 @@ -0,0 +1,239 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include <glib.h>
     1.7 +#include "bookloupe.h"
     1.8 +#include "pending.h"
     1.9 +
    1.10 +/*
    1.11 + * print_pending:
    1.12 + *
    1.13 + * If we are in a state of unbalanced quotes, and this line
    1.14 + * doesn't begin with a quote, output the stored error message.
    1.15 + * If the -p switch was used, print the warning even if the
    1.16 + * new para starts with quotes.
    1.17 + */
    1.18 +void print_pending(const char *aline,const char *parastart,
    1.19 +  struct pending *pending)
    1.20 +{
    1.21 +    const char *s;
    1.22 +    gunichar c;
    1.23 +    if (aline)
    1.24 +    {
    1.25 +	s=aline;
    1.26 +	while (*s==' ')
    1.27 +	    s++;
    1.28 +	c=g_utf8_get_char(s);
    1.29 +    }
    1.30 +    else
    1.31 +    {
    1.32 +	s=NULL;
    1.33 +	c='\0';
    1.34 +    }
    1.35 +    if (pending->illustration.warning_text)
    1.36 +    {
    1.37 +	if (aline)
    1.38 +	{
    1.39 +	    if (pswit[ECHO_SWITCH] && !pending->illustration.queried_line)
    1.40 +		pending->illustration.queried_line=g_strdup(parastart);
    1.41 +	}
    1.42 +	else
    1.43 +	{
    1.44 +	    if (!pswit[OVERVIEW_SWITCH])
    1.45 +	    {
    1.46 +		if (pending->illustration.queried_line)
    1.47 +		    g_print("\n%s\n",pending->illustration.queried_line);
    1.48 +		g_print("%s\n",pending->illustration.warning_text);
    1.49 +	    }
    1.50 +	    else
    1.51 +		cnt_brack++;
    1.52 +	    g_free(pending->illustration.warning_text);
    1.53 +	    pending->illustration.warning_text=NULL;
    1.54 +	    g_free(pending->illustration.queried_line);
    1.55 +	    pending->illustration.queried_line=NULL;
    1.56 +	}
    1.57 +    }
    1.58 +    if (pending->dquote)
    1.59 +    {
    1.60 +	if (c!=CHAR_DQUOTE || pswit[QPARA_SWITCH])
    1.61 +	{
    1.62 +	    if (!pswit[OVERVIEW_SWITCH])
    1.63 +	    {
    1.64 +		if (pswit[ECHO_SWITCH])
    1.65 +		    g_print("\n%s\n",parastart);
    1.66 +		g_print("%s\n",pending->dquote);
    1.67 +	    }
    1.68 +	    else
    1.69 +		cnt_dquot++;
    1.70 +	}
    1.71 +	g_free(pending->dquote);
    1.72 +	pending->dquote=NULL;
    1.73 +    }
    1.74 +    if (pending->squote)
    1.75 +    {
    1.76 +	if (!CHAR_IS_SQUOTE(c) || pswit[QPARA_SWITCH] || pending->squot)
    1.77 +	{
    1.78 +	    if (!pswit[OVERVIEW_SWITCH])
    1.79 +	    {
    1.80 +		if (pswit[ECHO_SWITCH])
    1.81 +		    g_print("\n%s\n",parastart);
    1.82 +		g_print("%s\n",pending->squote);
    1.83 +	    }
    1.84 +	    else
    1.85 +		cnt_squot++;
    1.86 +	}
    1.87 +	g_free(pending->squote);
    1.88 +	pending->squote=NULL;
    1.89 +    }
    1.90 +    if (pending->rbrack)
    1.91 +    {
    1.92 +	if (!pswit[OVERVIEW_SWITCH])
    1.93 +	{
    1.94 +	    if (pswit[ECHO_SWITCH])
    1.95 +		g_print("\n%s\n",parastart);
    1.96 +	    g_print("%s\n",pending->rbrack);
    1.97 +	}
    1.98 +	else
    1.99 +	    cnt_brack++;
   1.100 +	g_free(pending->rbrack);
   1.101 +	pending->rbrack=NULL;
   1.102 +    }
   1.103 +    if (pending->sbrack)
   1.104 +    {
   1.105 +	if (!pswit[OVERVIEW_SWITCH])
   1.106 +	{
   1.107 +	    if (pswit[ECHO_SWITCH])
   1.108 +		g_print("\n%s\n",parastart);
   1.109 +	    g_print("%s\n",pending->sbrack);
   1.110 +	}
   1.111 +	else
   1.112 +	    cnt_brack++;
   1.113 +	g_free(pending->sbrack);
   1.114 +	pending->sbrack=NULL;
   1.115 +    }
   1.116 +    if (pending->cbrack)
   1.117 +    {
   1.118 +	if (!pswit[OVERVIEW_SWITCH])
   1.119 +	{
   1.120 +	    if (pswit[ECHO_SWITCH])
   1.121 +		g_print("\n%s\n",parastart);
   1.122 +	    g_print("%s\n",pending->cbrack);
   1.123 +	}
   1.124 +	else
   1.125 +	    cnt_brack++;
   1.126 +	g_free(pending->cbrack);
   1.127 +	pending->cbrack=NULL;
   1.128 +    }
   1.129 +    if (pending->unders)
   1.130 +    {
   1.131 +	if (!pswit[OVERVIEW_SWITCH])
   1.132 +	{
   1.133 +	    if (pswit[ECHO_SWITCH])
   1.134 +		g_print("\n%s\n",parastart);
   1.135 +	    g_print("%s\n",pending->unders);
   1.136 +	}
   1.137 +	else
   1.138 +	    cnt_brack++;
   1.139 +	g_free(pending->unders);
   1.140 +	pending->unders=NULL;
   1.141 +    }
   1.142 +}
   1.143 +
   1.144 +void reset_pending(struct pending *pending)
   1.145 +{
   1.146 +    memset(pending,0,sizeof(*pending));
   1.147 +}
   1.148 +
   1.149 +/*
   1.150 + * check_for_mismatched_quotes:
   1.151 + *
   1.152 + * At end of paragraph, check for mismatched quotes.
   1.153 + *
   1.154 + * We don't want to report an error immediately, since it is a
   1.155 + * common convention to omit the quotes at end of paragraph if
   1.156 + * the next paragraph is a continuation of the same speaker.
   1.157 + * Where this is the case, the next para should begin with a
   1.158 + * quote, so we store the warning message and only display it
   1.159 + * at the top of the next iteration if the new para doesn't
   1.160 + * start with a quote.
   1.161 + * The -p switch overrides this default, and warns of unclosed
   1.162 + * quotes on _every_ paragraph, whether the next begins with a
   1.163 + * quote or not.
   1.164 + */
   1.165 +void check_for_mismatched_quotes(const struct counters *counters,
   1.166 +  struct pending *pending)
   1.167 +{
   1.168 +    int squote_straight,squote_curved,difference;
   1.169 +    if (counters->quot%2)
   1.170 +	pending->dquote=
   1.171 +	  g_strdup_printf("    Line %ld - Mismatched quotes",linecnt);
   1.172 +    if (pswit[SQUOTE_SWITCH])
   1.173 +    {
   1.174 +	if (matching_count(counters,CHAR_SQUOTE,TRUE))
   1.175 +	    squote_straight=matching_difference(counters,CHAR_SQUOTE);
   1.176 +	else
   1.177 +	    squote_straight=0;
   1.178 +	if (matching_count(counters,CHAR_LS_QUOTE,TRUE))
   1.179 +	    squote_curved=matching_difference(counters,CHAR_LS_QUOTE);
   1.180 +	else
   1.181 +	    squote_curved=0;
   1.182 +	if (squote_straight || squote_curved)
   1.183 +	    pending->squote=
   1.184 +	      g_strdup_printf("    Line %ld - Mismatched singlequotes?",
   1.185 +	      linecnt);
   1.186 +	if (squote_straight && squote_straight!=1 ||
   1.187 +	  squote_curved && squote_curved!=1)
   1.188 +	    /*
   1.189 +	     * Flag it to be noted regardless of the
   1.190 +	     * first char of the next para.
   1.191 +	     */
   1.192 +	    pending->squot=1;
   1.193 +    }
   1.194 +    difference=matching_difference(counters,COUNTER_ILLUSTRATION);
   1.195 +    if (difference)
   1.196 +    {
   1.197 +	if (difference<0 && pending->illustration.warning_text)
   1.198 +	{
   1.199 +	    difference++;
   1.200 +	    g_free(pending->illustration.queried_line);
   1.201 +	    pending->illustration.queried_line=NULL;
   1.202 +	    g_free(pending->illustration.warning_text);
   1.203 +	    pending->illustration.warning_text=NULL;
   1.204 +	}
   1.205 +	if (difference<0)
   1.206 +	{
   1.207 +	    increment_matching(counters,CHAR_OPEN_SBRACK,FALSE);
   1.208 +	    difference++;
   1.209 +	}
   1.210 +	if (difference)
   1.211 +	{
   1.212 +	    if (pending->illustration.warning_text)
   1.213 +	    {
   1.214 +		if (!pswit[OVERVIEW_SWITCH])
   1.215 +		{
   1.216 +		    if (pending->illustration.queried_line)
   1.217 +			g_print("\n%s\n",pending->illustration.queried_line);
   1.218 +		    g_print("%s\n",pending->illustration.warning_text);
   1.219 +		}
   1.220 +		else
   1.221 +		    cnt_brack++;
   1.222 +		g_free(pending->illustration.warning_text);
   1.223 +	    }
   1.224 +	    pending->illustration.warning_text=g_strdup_printf(
   1.225 +	      "    Line %ld - Mismatched illustration tag?",linecnt);
   1.226 +	    g_free(pending->illustration.queried_line);
   1.227 +	    pending->illustration.queried_line=NULL;
   1.228 +	}
   1.229 +    }
   1.230 +    if (matching_difference(counters,CHAR_OPEN_RBRACK))
   1.231 +	pending->rbrack=
   1.232 +	  g_strdup_printf("    Line %ld - Mismatched round brackets?",linecnt);
   1.233 +    if (matching_difference(counters,CHAR_OPEN_SBRACK))
   1.234 +	pending->sbrack=
   1.235 +	  g_strdup_printf("    Line %ld - Mismatched square brackets?",linecnt);
   1.236 +    if (matching_difference(counters,CHAR_OPEN_CBRACK))
   1.237 +	pending->cbrack=
   1.238 +	  g_strdup_printf("    Line %ld - Mismatched curly brackets?",linecnt);
   1.239 +    if (counters->c_unders%2)
   1.240 +	pending->unders=
   1.241 +	  g_strdup_printf("    Line %ld - Mismatched underscores?",linecnt);
   1.242 +}