bookloupe/pending.c
author ali <ali@juiblex.co.uk>
Mon Sep 23 21:18:27 2013 +0100 (2013-09-23)
changeset 103 adc06e9e8470
child 111 f805130deb6f
child 123 ddb5ddba6ef3
permissions -rw-r--r--
Fix bug #12: Balanced square brackets test should recognize multi-line [Illustration] tags
     1 #include <stdlib.h>
     2 #include <string.h>
     3 #include <glib.h>
     4 #include "bookloupe.h"
     5 #include "pending.h"
     6 
     7 /*
     8  * print_pending:
     9  *
    10  * If we are in a state of unbalanced quotes, and this line
    11  * doesn't begin with a quote, output the stored error message.
    12  * If the -p switch was used, print the warning even if the
    13  * new para starts with quotes.
    14  */
    15 void print_pending(const char *aline,const char *parastart,
    16   struct pending *pending)
    17 {
    18     const char *s;
    19     gunichar c;
    20     if (aline)
    21     {
    22 	s=aline;
    23 	while (*s==' ')
    24 	    s++;
    25 	c=g_utf8_get_char(s);
    26     }
    27     else
    28     {
    29 	s=NULL;
    30 	c='\0';
    31     }
    32     if (pending->illustration.warning_text)
    33     {
    34 	if (aline)
    35 	{
    36 	    if (pswit[ECHO_SWITCH] && !pending->illustration.queried_line)
    37 		pending->illustration.queried_line=g_strdup(parastart);
    38 	}
    39 	else
    40 	{
    41 	    if (!pswit[OVERVIEW_SWITCH])
    42 	    {
    43 		if (pending->illustration.queried_line)
    44 		    g_print("\n%s\n",pending->illustration.queried_line);
    45 		g_print("%s\n",pending->illustration.warning_text);
    46 	    }
    47 	    else
    48 		cnt_brack++;
    49 	    g_free(pending->illustration.warning_text);
    50 	    pending->illustration.warning_text=NULL;
    51 	    g_free(pending->illustration.queried_line);
    52 	    pending->illustration.queried_line=NULL;
    53 	}
    54     }
    55     if (pending->dquote)
    56     {
    57 	if (c!=CHAR_DQUOTE || pswit[QPARA_SWITCH])
    58 	{
    59 	    if (!pswit[OVERVIEW_SWITCH])
    60 	    {
    61 		if (pswit[ECHO_SWITCH])
    62 		    g_print("\n%s\n",parastart);
    63 		g_print("%s\n",pending->dquote);
    64 	    }
    65 	    else
    66 		cnt_dquot++;
    67 	}
    68 	g_free(pending->dquote);
    69 	pending->dquote=NULL;
    70     }
    71     if (pending->squote)
    72     {
    73 	if (!CHAR_IS_SQUOTE(c) || pswit[QPARA_SWITCH] || pending->squot)
    74 	{
    75 	    if (!pswit[OVERVIEW_SWITCH])
    76 	    {
    77 		if (pswit[ECHO_SWITCH])
    78 		    g_print("\n%s\n",parastart);
    79 		g_print("%s\n",pending->squote);
    80 	    }
    81 	    else
    82 		cnt_squot++;
    83 	}
    84 	g_free(pending->squote);
    85 	pending->squote=NULL;
    86     }
    87     if (pending->rbrack)
    88     {
    89 	if (!pswit[OVERVIEW_SWITCH])
    90 	{
    91 	    if (pswit[ECHO_SWITCH])
    92 		g_print("\n%s\n",parastart);
    93 	    g_print("%s\n",pending->rbrack);
    94 	}
    95 	else
    96 	    cnt_brack++;
    97 	g_free(pending->rbrack);
    98 	pending->rbrack=NULL;
    99     }
   100     if (pending->sbrack)
   101     {
   102 	if (!pswit[OVERVIEW_SWITCH])
   103 	{
   104 	    if (pswit[ECHO_SWITCH])
   105 		g_print("\n%s\n",parastart);
   106 	    g_print("%s\n",pending->sbrack);
   107 	}
   108 	else
   109 	    cnt_brack++;
   110 	g_free(pending->sbrack);
   111 	pending->sbrack=NULL;
   112     }
   113     if (pending->cbrack)
   114     {
   115 	if (!pswit[OVERVIEW_SWITCH])
   116 	{
   117 	    if (pswit[ECHO_SWITCH])
   118 		g_print("\n%s\n",parastart);
   119 	    g_print("%s\n",pending->cbrack);
   120 	}
   121 	else
   122 	    cnt_brack++;
   123 	g_free(pending->cbrack);
   124 	pending->cbrack=NULL;
   125     }
   126     if (pending->unders)
   127     {
   128 	if (!pswit[OVERVIEW_SWITCH])
   129 	{
   130 	    if (pswit[ECHO_SWITCH])
   131 		g_print("\n%s\n",parastart);
   132 	    g_print("%s\n",pending->unders);
   133 	}
   134 	else
   135 	    cnt_brack++;
   136 	g_free(pending->unders);
   137 	pending->unders=NULL;
   138     }
   139 }
   140 
   141 void reset_pending(struct pending *pending)
   142 {
   143     memset(pending,0,sizeof(*pending));
   144 }
   145 
   146 /*
   147  * check_for_mismatched_quotes:
   148  *
   149  * At end of paragraph, check for mismatched quotes.
   150  *
   151  * We don't want to report an error immediately, since it is a
   152  * common convention to omit the quotes at end of paragraph if
   153  * the next paragraph is a continuation of the same speaker.
   154  * Where this is the case, the next para should begin with a
   155  * quote, so we store the warning message and only display it
   156  * at the top of the next iteration if the new para doesn't
   157  * start with a quote.
   158  * The -p switch overrides this default, and warns of unclosed
   159  * quotes on _every_ paragraph, whether the next begins with a
   160  * quote or not.
   161  */
   162 void check_for_mismatched_quotes(const struct counters *counters,
   163   struct pending *pending)
   164 {
   165     int squote_straight,squote_curved,difference;
   166     if (counters->quot%2)
   167 	pending->dquote=
   168 	  g_strdup_printf("    Line %ld - Mismatched quotes",linecnt);
   169     if (pswit[SQUOTE_SWITCH])
   170     {
   171 	if (matching_count(counters,CHAR_SQUOTE,TRUE))
   172 	    squote_straight=matching_difference(counters,CHAR_SQUOTE);
   173 	else
   174 	    squote_straight=0;
   175 	if (matching_count(counters,CHAR_LS_QUOTE,TRUE))
   176 	    squote_curved=matching_difference(counters,CHAR_LS_QUOTE);
   177 	else
   178 	    squote_curved=0;
   179 	if (squote_straight || squote_curved)
   180 	    pending->squote=
   181 	      g_strdup_printf("    Line %ld - Mismatched singlequotes?",
   182 	      linecnt);
   183 	if (squote_straight && squote_straight!=1 ||
   184 	  squote_curved && squote_curved!=1)
   185 	    /*
   186 	     * Flag it to be noted regardless of the
   187 	     * first char of the next para.
   188 	     */
   189 	    pending->squot=1;
   190     }
   191     difference=matching_difference(counters,COUNTER_ILLUSTRATION);
   192     if (difference)
   193     {
   194 	if (difference<0 && pending->illustration.warning_text)
   195 	{
   196 	    difference++;
   197 	    g_free(pending->illustration.queried_line);
   198 	    pending->illustration.queried_line=NULL;
   199 	    g_free(pending->illustration.warning_text);
   200 	    pending->illustration.warning_text=NULL;
   201 	}
   202 	if (difference<0)
   203 	{
   204 	    increment_matching(counters,CHAR_OPEN_SBRACK,FALSE);
   205 	    difference++;
   206 	}
   207 	if (difference)
   208 	{
   209 	    if (pending->illustration.warning_text)
   210 	    {
   211 		if (!pswit[OVERVIEW_SWITCH])
   212 		{
   213 		    if (pending->illustration.queried_line)
   214 			g_print("\n%s\n",pending->illustration.queried_line);
   215 		    g_print("%s\n",pending->illustration.warning_text);
   216 		}
   217 		else
   218 		    cnt_brack++;
   219 		g_free(pending->illustration.warning_text);
   220 	    }
   221 	    pending->illustration.warning_text=g_strdup_printf(
   222 	      "    Line %ld - Mismatched illustration tag?",linecnt);
   223 	    g_free(pending->illustration.queried_line);
   224 	    pending->illustration.queried_line=NULL;
   225 	}
   226     }
   227     if (matching_difference(counters,CHAR_OPEN_RBRACK))
   228 	pending->rbrack=
   229 	  g_strdup_printf("    Line %ld - Mismatched round brackets?",linecnt);
   230     if (matching_difference(counters,CHAR_OPEN_SBRACK))
   231 	pending->sbrack=
   232 	  g_strdup_printf("    Line %ld - Mismatched square brackets?",linecnt);
   233     if (matching_difference(counters,CHAR_OPEN_CBRACK))
   234 	pending->cbrack=
   235 	  g_strdup_printf("    Line %ld - Mismatched curly brackets?",linecnt);
   236     if (counters->c_unders%2)
   237 	pending->unders=
   238 	  g_strdup_printf("    Line %ld - Mismatched underscores?",linecnt);
   239 }