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