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