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