test/harness/testcaseparser.c
author ali <ali@juiblex.co.uk>
Sun Feb 19 21:08:53 2012 +0000 (2012-02-19)
changeset 31 20c603ca5649
parent 7 721e468c10f3
child 69 1016349e619f
permissions -rw-r--r--
Add testcase for words that shouldn't immediately preceed periods
ali@0
     1
#include <stdlib.h>
ali@0
     2
#include <stdio.h>
ali@0
     3
#include <string.h>
ali@6
     4
#include <glib.h>
ali@5
     5
#include <bl/bl.h>
ali@0
     6
#include "testcaseparser.h"
ali@0
     7
ali@0
     8
/*
ali@0
     9
 * Get the flag (the string of characters which bracket tags in test cases).
ali@0
    10
 */
ali@0
    11
const char *testcase_parser_get_flag(TestcaseParser *parser)
ali@0
    12
{
ali@0
    13
    char *s=parser->contents;
ali@0
    14
    if (!parser->flag)
ali@0
    15
    {
ali@6
    16
	parser->flag=g_string_new(NULL);
ali@0
    17
	while(*s>' ' && *s<='~')
ali@6
    18
	    g_string_append_c(parser->flag,*s++);
ali@0
    19
    }
ali@0
    20
    return parser->flag->str;
ali@0
    21
}
ali@0
    22
ali@0
    23
/*
ali@0
    24
 * Test if the parser has reached the end of the input file
ali@0
    25
 */
ali@6
    26
gboolean testcase_parser_at_eof(TestcaseParser *parser)
ali@0
    27
{
ali@0
    28
    return !parser->contents[parser->pos];
ali@0
    29
}
ali@0
    30
ali@0
    31
/*
ali@0
    32
 * Get the next tag (and its associated text, if any) from a test case.
ali@0
    33
 * Returns: TRUE if successful and FALSE if no more valid tags are present.
ali@0
    34
 * Callers can call testcase_parser_at_eof() when testcase_parser_get_next_tag()
ali@0
    35
 * to distinguish EOF and text which isn't a valid tag.
ali@0
    36
 */
ali@6
    37
gboolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag,
ali@0
    38
  const char **text)
ali@0
    39
{
ali@0
    40
    size_t n;
ali@0
    41
    char *eol,*endp;
ali@6
    42
    GString *string;
ali@6
    43
    g_free(parser->tag);
ali@0
    44
    parser->tag=NULL;
ali@6
    45
    g_free(parser->tag_text);
ali@0
    46
    parser->tag_text=NULL;
ali@0
    47
    (void)testcase_parser_get_flag(parser);
ali@0
    48
    if (strncmp(parser->contents+parser->pos,parser->flag->str,
ali@0
    49
      parser->flag->len))
ali@0
    50
	return FALSE;
ali@0
    51
    eol=strchr(parser->contents+parser->pos,'\n');
ali@0
    52
    if (!eol)
ali@0
    53
	return FALSE;
ali@0
    54
    endp=eol-parser->flag->len;
ali@0
    55
    if (strncmp(endp,parser->flag->str,parser->flag->len))
ali@0
    56
	return FALSE;
ali@6
    57
    while(endp>parser->contents && g_ascii_isspace(endp[-1]))
ali@0
    58
	endp--;
ali@0
    59
    parser->pos+=parser->flag->len;
ali@6
    60
    while(g_ascii_isspace(parser->contents[parser->pos]))
ali@0
    61
	parser->pos++;
ali@6
    62
    parser->tag=g_strndup(parser->contents+parser->pos,
ali@0
    63
      endp-(parser->contents+parser->pos));
ali@0
    64
    parser->pos=eol-parser->contents+1;
ali@6
    65
    string=g_string_new(NULL);
ali@0
    66
    while (!testcase_parser_at_eof(parser) &&
ali@0
    67
      strncmp(parser->contents+parser->pos,parser->flag->str,parser->flag->len))
ali@0
    68
    {
ali@0
    69
	eol=strchr(parser->contents+parser->pos,'\n');
ali@0
    70
	if (eol)
ali@0
    71
	    n=eol-(parser->contents+parser->pos)+1;
ali@0
    72
	else
ali@0
    73
	    n=strlen(parser->contents+parser->pos);
ali@6
    74
	g_string_append_len(string,parser->contents+parser->pos,n);
ali@0
    75
	parser->pos+=n;
ali@0
    76
    }
ali@6
    77
    parser->tag_text=g_string_free(string,FALSE);
ali@0
    78
    if (!parser->tag_text)
ali@6
    79
	parser->tag_text=g_strdup("");
ali@0
    80
    if (tag)
ali@0
    81
	*tag=parser->tag;
ali@0
    82
    if (text)
ali@0
    83
	*text=parser->tag_text;
ali@0
    84
    return TRUE;
ali@0
    85
}
ali@0
    86
ali@0
    87
/*
ali@0
    88
 * Create a testcase parser to read a regular file.
ali@0
    89
 */
ali@0
    90
TestcaseParser *testcase_parser_new_from_file(const char *filename)
ali@0
    91
{
ali@0
    92
    TestcaseParser *parser;
ali@7
    93
    gsize len;
ali@6
    94
    parser=g_new0(TestcaseParser,1);
ali@7
    95
    if (!file_get_contents_text(filename,&parser->contents,&len))
ali@0
    96
    {
ali@6
    97
	g_free(parser);
ali@0
    98
	return NULL;
ali@0
    99
    }
ali@7
   100
    if (!g_utf8_validate(parser->contents,len,NULL))
ali@7
   101
    {
ali@11
   102
	g_printerr("%s: Does not contain valid UTF-8\n",filename);
ali@7
   103
	g_free(parser->contents);
ali@7
   104
	g_free(parser);
ali@7
   105
	return NULL;
ali@7
   106
    }
ali@6
   107
    parser->filename=g_strdup(filename);
ali@0
   108
    return parser;
ali@0
   109
}
ali@0
   110
ali@0
   111
/*
ali@0
   112
 * Free a testcase parser.
ali@0
   113
 */
ali@0
   114
void testcase_parser_free(TestcaseParser *parser)
ali@0
   115
{
ali@6
   116
    g_free(parser->filename);
ali@6
   117
    g_free(parser->contents);
ali@0
   118
    if (parser->flag)
ali@6
   119
	g_string_free(parser->flag,TRUE);
ali@6
   120
    g_free(parser->tag);
ali@6
   121
    g_free(parser->tag_text);
ali@6
   122
    g_free(parser);
ali@0
   123
}