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