test/harness/testcaseoutput.c
author ali <ali@juiblex.co.uk>
Wed Oct 16 22:51:29 2013 +0100 (2013-10-16)
changeset 104 70cc629ec1e0
permissions -rw-r--r--
Fix bug #28: Don't report ., as double punctuation after "etc" or "&c"
ali@102
     1
#include <stdlib.h>
ali@102
     2
#include <string.h>
ali@102
     3
#include <errno.h>
ali@102
     4
#include <glib.h>
ali@102
     5
#include <bl/bl.h>
ali@102
     6
#include "testcase.h"
ali@102
     7
#include "testcaseoutput.h"
ali@102
     8
ali@102
     9
/*
ali@102
    10
 * Replace \r\n with \n, \n with U+240A (visible symbol for LF)
ali@102
    11
 * and \r with U+240D (visible symbol for CR).
ali@102
    12
 */
ali@102
    13
static char *dos2unix(const char *text)
ali@102
    14
{
ali@102
    15
    gunichar c;
ali@102
    16
    gboolean cr=FALSE;
ali@102
    17
    const gunichar visible_lf=0x240A;
ali@102
    18
    const gunichar visible_cr=0x240D;
ali@102
    19
    GString *string;
ali@102
    20
    string=g_string_new(NULL);
ali@102
    21
    while(*text)
ali@102
    22
    {
ali@102
    23
	c=g_utf8_get_char(text);
ali@102
    24
	text=g_utf8_next_char(text);
ali@102
    25
	if (cr)
ali@102
    26
	{
ali@102
    27
	    cr=FALSE;
ali@102
    28
	    if (c=='\n')
ali@102
    29
	    {
ali@102
    30
		g_string_append_c(string,'\n');
ali@102
    31
		continue;
ali@102
    32
	    }
ali@102
    33
	    else
ali@102
    34
		g_string_append_unichar(string,visible_cr);
ali@102
    35
	}
ali@102
    36
	if (c=='\r')
ali@102
    37
	    cr=TRUE;
ali@102
    38
	else if (c=='\n')
ali@102
    39
	    g_string_append_unichar(string,visible_lf);
ali@102
    40
	else
ali@102
    41
	    g_string_append_unichar(string,c);
ali@102
    42
    }
ali@102
    43
    if (cr)
ali@102
    44
	g_string_append_unichar(string,visible_cr);
ali@102
    45
    return g_string_free(string,FALSE);
ali@102
    46
}
ali@102
    47
ali@102
    48
/*
ali@102
    49
 * Read an output file needed for a testcase (as specified in <output>).
ali@102
    50
 * The file is read in the encoding specified for communicating with
ali@102
    51
 * bookloupe.
ali@102
    52
 */
ali@102
    53
gboolean testcase_output_read(Testcase *testcase,TestcaseOutput *output,
ali@102
    54
  gchar **contents,gsize *length,GError **error)
ali@102
    55
{
ali@102
    56
    char *filename,*s,*t;
ali@102
    57
    gboolean retval;
ali@102
    58
    GError *tmp_err=NULL;
ali@102
    59
    if (!strcmp(output->name,"stdout"))
ali@102
    60
    {
ali@102
    61
	*contents=g_strdup(testcase->test_output);
ali@102
    62
	if (length)
ali@102
    63
	    *length=strlen(testcase->test_output);
ali@102
    64
    }
ali@102
    65
    else
ali@102
    66
    {
ali@102
    67
	if (testcase->tmpdir)
ali@102
    68
	    filename=g_build_filename(testcase->tmpdir,output->name,NULL);
ali@102
    69
	else
ali@102
    70
	    filename=g_strdup(output->name);
ali@102
    71
	if (!g_file_get_contents(filename,&s,NULL,error))
ali@102
    72
	{
ali@102
    73
	    g_free(filename);
ali@102
    74
	    return FALSE;
ali@102
    75
	}
ali@102
    76
	g_free(filename);
ali@102
    77
	if (testcase->encoding)
ali@102
    78
	{
ali@102
    79
	    t=dos2unix(s);
ali@102
    80
	    g_free(s);
ali@102
    81
	    s=g_convert(t,-1,"UTF-8",testcase->encoding,NULL,length,&tmp_err);
ali@102
    82
	    g_free(t);
ali@102
    83
	    if (!s)
ali@102
    84
	    {
ali@102
    85
		g_propagate_prefixed_error(error,tmp_err,
ali@102
    86
		  "Conversion from %s failed: ",testcase->encoding);
ali@102
    87
		return FALSE;
ali@102
    88
	    }
ali@102
    89
	    *contents=s;
ali@102
    90
	}
ali@102
    91
	else
ali@102
    92
	{
ali@102
    93
	    *contents=dos2unix(s);
ali@102
    94
	    if (length)
ali@102
    95
		*length=strlen(*contents);
ali@102
    96
	}
ali@102
    97
    }
ali@102
    98
    return TRUE;
ali@102
    99
}
ali@102
   100
ali@102
   101
/*
ali@102
   102
 * Remove an output file created by program under test.
ali@102
   103
 */
ali@102
   104
gboolean testcase_output_remove(Testcase *testcase,TestcaseOutput *output,
ali@102
   105
  GError **error)
ali@102
   106
{
ali@102
   107
    char *filename;
ali@102
   108
    if (!strcmp(output->name,"stdout"))
ali@102
   109
	return TRUE;
ali@102
   110
    if (testcase->tmpdir)
ali@102
   111
	filename=g_build_filename(testcase->tmpdir,output->name,NULL);
ali@102
   112
    else
ali@102
   113
	filename=g_strdup(output->name);
ali@102
   114
    if (g_unlink(filename)<0)
ali@102
   115
    {
ali@102
   116
	g_set_error(error,G_FILE_ERROR,g_file_error_from_errno(errno),
ali@102
   117
	  "%s: %s",filename,g_strerror(errno));
ali@102
   118
	return FALSE;
ali@102
   119
    }
ali@102
   120
    g_free(filename);
ali@102
   121
    return TRUE;
ali@102
   122
}
ali@102
   123
ali@102
   124
/* Create a new description of an output file expected by a testcase */
ali@102
   125
TestcaseOutput *testcase_output_new(const char *name,const char *contents)
ali@102
   126
{
ali@102
   127
    TestcaseOutput *output;
ali@102
   128
    output=g_new0(TestcaseOutput,1);
ali@102
   129
    output->name=g_strdup(name);
ali@102
   130
    output->contents=g_strdup(contents);
ali@102
   131
    return output;
ali@102
   132
}
ali@102
   133
ali@102
   134
/* Free the description of a testcase output file */
ali@102
   135
void testcase_output_free(TestcaseOutput *output)
ali@102
   136
{
ali@102
   137
    g_free(output->name);
ali@102
   138
    g_free(output->contents);
ali@102
   139
    g_free(output);
ali@102
   140
}