1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/harness/testcaseparser.c Wed Jan 25 19:33:43 2012 +0000
1.3 @@ -0,0 +1,115 @@
1.4 +#include <stdlib.h>
1.5 +#include <stdio.h>
1.6 +#include <string.h>
1.7 +#include <ctype.h>
1.8 +#include <gclib/gclib.h>
1.9 +#include "testcaseparser.h"
1.10 +
1.11 +/*
1.12 + * Get the flag (the string of characters which bracket tags in test cases).
1.13 + */
1.14 +const char *testcase_parser_get_flag(TestcaseParser *parser)
1.15 +{
1.16 + char *s=parser->contents;
1.17 + if (!parser->flag)
1.18 + {
1.19 + parser->flag=string_new(NULL);
1.20 + while(*s>' ' && *s<='~')
1.21 + string_append_c(parser->flag,*s++);
1.22 + }
1.23 + return parser->flag->str;
1.24 +}
1.25 +
1.26 +/*
1.27 + * Test if the parser has reached the end of the input file
1.28 + */
1.29 +boolean testcase_parser_at_eof(TestcaseParser *parser)
1.30 +{
1.31 + return !parser->contents[parser->pos];
1.32 +}
1.33 +
1.34 +/*
1.35 + * Get the next tag (and its associated text, if any) from a test case.
1.36 + * Returns: TRUE if successful and FALSE if no more valid tags are present.
1.37 + * Callers can call testcase_parser_at_eof() when testcase_parser_get_next_tag()
1.38 + * to distinguish EOF and text which isn't a valid tag.
1.39 + */
1.40 +boolean testcase_parser_get_next_tag(TestcaseParser *parser,const char **tag,
1.41 + const char **text)
1.42 +{
1.43 + size_t n;
1.44 + char *eol,*endp;
1.45 + String *string;
1.46 + mem_free(parser->tag);
1.47 + parser->tag=NULL;
1.48 + mem_free(parser->tag_text);
1.49 + parser->tag_text=NULL;
1.50 + (void)testcase_parser_get_flag(parser);
1.51 + if (strncmp(parser->contents+parser->pos,parser->flag->str,
1.52 + parser->flag->len))
1.53 + return FALSE;
1.54 + eol=strchr(parser->contents+parser->pos,'\n');
1.55 + if (!eol)
1.56 + return FALSE;
1.57 + endp=eol-parser->flag->len;
1.58 + if (strncmp(endp,parser->flag->str,parser->flag->len))
1.59 + return FALSE;
1.60 + while(endp>parser->contents && isspace(endp[-1]))
1.61 + endp--;
1.62 + parser->pos+=parser->flag->len;
1.63 + while(isspace(parser->contents[parser->pos]))
1.64 + parser->pos++;
1.65 + parser->tag=str_ndup(parser->contents+parser->pos,
1.66 + endp-(parser->contents+parser->pos));
1.67 + parser->pos=eol-parser->contents+1;
1.68 + string=string_new(NULL);
1.69 + while (!testcase_parser_at_eof(parser) &&
1.70 + strncmp(parser->contents+parser->pos,parser->flag->str,parser->flag->len))
1.71 + {
1.72 + eol=strchr(parser->contents+parser->pos,'\n');
1.73 + if (eol)
1.74 + n=eol-(parser->contents+parser->pos)+1;
1.75 + else
1.76 + n=strlen(parser->contents+parser->pos);
1.77 + string_append_len(string,parser->contents+parser->pos,n);
1.78 + parser->pos+=n;
1.79 + }
1.80 + parser->tag_text=string_free(string,FALSE);
1.81 + if (!parser->tag_text)
1.82 + parser->tag_text=str_dup("");
1.83 + if (tag)
1.84 + *tag=parser->tag;
1.85 + if (text)
1.86 + *text=parser->tag_text;
1.87 + return TRUE;
1.88 +}
1.89 +
1.90 +/*
1.91 + * Create a testcase parser to read a regular file.
1.92 + */
1.93 +TestcaseParser *testcase_parser_new_from_file(const char *filename)
1.94 +{
1.95 + TestcaseParser *parser;
1.96 + parser=mem_new0(TestcaseParser,1);
1.97 + if (!file_get_contents_text(filename,&parser->contents,NULL))
1.98 + {
1.99 + mem_free(parser);
1.100 + return NULL;
1.101 + }
1.102 + parser->filename=str_dup(filename);
1.103 + return parser;
1.104 +}
1.105 +
1.106 +/*
1.107 + * Free a testcase parser.
1.108 + */
1.109 +void testcase_parser_free(TestcaseParser *parser)
1.110 +{
1.111 + mem_free(parser->filename);
1.112 + mem_free(parser->contents);
1.113 + if (parser->flag)
1.114 + string_free(parser->flag,TRUE);
1.115 + mem_free(parser->tag);
1.116 + mem_free(parser->tag_text);
1.117 + mem_free(parser);
1.118 +}