Beispiel #1
0
void config_read(const char *fname)
{
    /* make sure the glob function works */
    myglob *g1 = new myglob("*.jpeg");
    myglob *g2 = new myglob("*.jpg");

    assert(g1->match("file.jpeg")==true);
    assert(g1->match("file.jpg")==false);

    assert(g2->match("file.jpeg")==false);
    assert(g2->match("file.jpg")==true);
    delete g1;
    delete g2;

    // Compile the regular expression we will use;
    // Unfortunately the POSIX regex has no support for \s
    regex_t r;
    if(regcomp(&r,"([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^\t\r\n]+)",REG_EXTENDED)) err(1,"regcomp"); 
    FILE *f = fopen(fname,"r");
    if(!f) err(1,"%s",fname);
    char linebuf[1024];
    int linenumber = 0;
    while(fgets(linebuf,sizeof(linebuf),f)){
	linenumber++;
	char *cc = strchr(linebuf,'#');
	if(cc) *cc = 0;			// terminate #'s

	/* if the line all whitespace ignore it */
	if(all_whitespace(linebuf)) continue;

	/* parse the line */
	regmatch_t pmatch[10];
	memset(pmatch,0,sizeof(pmatch));
	int res = regexec(&r,linebuf,10,pmatch,0);
	if(res){
	    fprintf(stderr,"Error in configuration file line %d: %s\n",linenumber,linebuf);
	    exit(1);
	}
	linebuf[pmatch[1].rm_eo] = 0;
	linebuf[pmatch[2].rm_eo] = 0;
	linebuf[pmatch[3].rm_eo] = 0;

	class plugins *plug = new plugins(linebuf+pmatch[1].rm_so, linebuf+pmatch[2].rm_so, linebuf+pmatch[3].rm_so);
	plug->glob = new myglob(plug->pattern.c_str());
	comment("pattern: %s  method: %s  path: %s",plug->pattern.c_str(),plug->method.c_str(),plug->path.c_str());
	plugin_list.push_back(plug);
    }
    fclose(f);
    regfree(&r);
}
static void
text_handler (GMarkupParseContext  *context,
              const gchar          *text,
              gsize                 text_len,
              gpointer              user_data,
              GError              **error)
{
  ParseInfo *info = user_data;
  TextSpan  *span;

  if (all_whitespace (text, text_len)   &&
      peek_state (info) != STATE_MARKUP &&
      peek_state (info) != STATE_TAG    &&
      peek_state (info) != STATE_UNKNOWN)
    return;

  switch (peek_state (info))
    {
    case STATE_START:
      g_assert_not_reached (); /* gmarkup shouldn't do this */
      break;

    case STATE_MARKUP:
    case STATE_TAG:
    case STATE_UNKNOWN:
      if (text_len == 0)
        return;

      span = g_new0 (TextSpan, 1);
      span->text = g_strndup (text, text_len);
      span->tags = g_slist_copy (info->tag_stack);

      info->spans = g_list_prepend (info->spans, span);
      break;

    default:
      g_assert_not_reached ();
      break;
    }
}