test/harness/testcaseparser.c
author ali <ali@juiblex.co.uk>
Fri Jan 27 16:18:02 2012 +0000 (2012-01-27)
changeset 6 faab25d520dd
parent 5 f600b0d1fc5d
child 7 721e468c10f3
permissions -rw-r--r--
Rely on glib being available everywhere
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@6
    93
    parser=g_new0(TestcaseParser,1);
ali@0
    94
    if (!file_get_contents_text(filename,&parser->contents,NULL))
ali@0
    95
    {
ali@6
    96
	g_free(parser);
ali@0
    97
	return NULL;
ali@0
    98
    }
ali@6
    99
    parser->filename=g_strdup(filename);
ali@0
   100
    return parser;
ali@0
   101
}
ali@0
   102
ali@0
   103
/*
ali@0
   104
 * Free a testcase parser.
ali@0
   105
 */
ali@0
   106
void testcase_parser_free(TestcaseParser *parser)
ali@0
   107
{
ali@6
   108
    g_free(parser->filename);
ali@6
   109
    g_free(parser->contents);
ali@0
   110
    if (parser->flag)
ali@6
   111
	g_string_free(parser->flag,TRUE);
ali@6
   112
    g_free(parser->tag);
ali@6
   113
    g_free(parser->tag_text);
ali@6
   114
    g_free(parser);
ali@0
   115
}