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