test/harness/testcaseparser.c
author ali <ali@juiblex.co.uk>
Thu Jan 26 23:35:52 2012 +0000 (2012-01-26)
changeset 3 ec364358631b
child 5 f600b0d1fc5d
permissions -rw-r--r--
Non-win32 platforms might need mkstemp too
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <ctype.h>
     5 #include <gclib/gclib.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=string_new(NULL);
    17 	while(*s>' ' && *s<='~')
    18 	    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 boolean 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 boolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag,
    38   const char **text)
    39 {
    40     size_t n;
    41     char *eol,*endp;
    42     String *string;
    43     mem_free(parser->tag);
    44     parser->tag=NULL;
    45     mem_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 && isspace(endp[-1]))
    58 	endp--;
    59     parser->pos+=parser->flag->len;
    60     while(isspace(parser->contents[parser->pos]))
    61 	parser->pos++;
    62     parser->tag=str_ndup(parser->contents+parser->pos,
    63       endp-(parser->contents+parser->pos));
    64     parser->pos=eol-parser->contents+1;
    65     string=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 	string_append_len(string,parser->contents+parser->pos,n);
    75 	parser->pos+=n;
    76     }
    77     parser->tag_text=string_free(string,FALSE);
    78     if (!parser->tag_text)
    79 	parser->tag_text=str_dup("");
    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     parser=mem_new0(TestcaseParser,1);
    94     if (!file_get_contents_text(filename,&parser->contents,NULL))
    95     {
    96 	mem_free(parser);
    97 	return NULL;
    98     }
    99     parser->filename=str_dup(filename);
   100     return parser;
   101 }
   102 
   103 /*
   104  * Free a testcase parser.
   105  */
   106 void testcase_parser_free(TestcaseParser *parser)
   107 {
   108     mem_free(parser->filename);
   109     mem_free(parser->contents);
   110     if (parser->flag)
   111 	string_free(parser->flag,TRUE);
   112     mem_free(parser->tag);
   113     mem_free(parser->tag_text);
   114     mem_free(parser);
   115 }