Beispiel #1
0
extern void runYamlParser (const yamlCallback callback, void* userData)
{
	yaml_parser_t yaml;
	yaml_token_t token;
	bool done;

	yamlInit (&yaml);

	findRegexTags ();

	done = false;
	while (!done)
	{
		if (!yaml_parser_scan (&yaml, &token))
			break;

		callback (&token, userData);
		verbose("yaml token:%s<%d>@Line:%lu\n", tokenTypeName[token.type], token.type,
				token.start_mark.line + 1);
		if (token.type == YAML_STREAM_END_TOKEN)
			done = true;

		yaml_token_delete (&token);
	}
	yamlFini (&yaml);
}
Beispiel #2
0
int
main (int argc, char* argv[]) {
  int number;

  if (argc < 2) {
    printf ("Usage: %s file1.yaml ...\n", argv[0]);
    return 0;
    }

  for (number = 1; number < argc; number ++) {
    FILE* file;
    yaml_parser_t parser;
    yaml_token_t token;
    int done = 0;
    int count = 0;
    int error = 0;

    printf ("[%d] Scanning '%s': ", number, argv[number]);
    fflush (stdout);

    file = fopen (argv[number], "rb");
    assert (file);

    assert (yaml_parser_initialize (&parser));

    yaml_parser_set_input_file (&parser, file);

    while (!done) {
      if (!yaml_parser_scan (&parser, &token)) {
        error = 1;
        break;
        }

      done = (token.type == YAML_STREAM_END_TOKEN);

      yaml_token_delete (&token);

      count ++;
      }

    yaml_parser_delete (&parser);

    assert (!fclose (file));

    printf ("%s (%d tokens)\n", (error ? "FAILURE" : "SUCCESS"), count);
    }

  return 0;
  }
Beispiel #3
0
static rstatus_t
conf_token_next(struct conf *cf)
{
    int rv;

    ASSERT(cf->valid_parser && !cf->valid_token);

    rv = yaml_parser_scan(&cf->parser, &cf->token);
    if (!rv) {
        log_error("conf: failed (err %d) to scan next token", cf->parser.error);
        return NC_ERROR;
    }
    cf->valid_token = 1;

    return NC_OK;
}
Beispiel #4
0
void uwsgi_yaml_config(char *file, char *magic_table[]) {

	int len = 0;
	char *yaml;

	int in_uwsgi_section = 0;

	char *key = NULL;
	char *val = NULL;

	char *section_asked = "uwsgi";
	char *colon;

	if (uwsgi_check_scheme(file)) {
		colon = uwsgi_get_last_char(file, '/');
		colon = uwsgi_get_last_char(colon, ':');
	}
	else {
		colon = uwsgi_get_last_char(file, ':');
	}

	if (colon) {
		colon[0] = 0;
		if (colon[1] != 0) {
			section_asked = colon + 1;
		}
	}

	uwsgi_log("[uWSGI] getting YAML configuration from %s\n", file);

	yaml = uwsgi_open_and_read(file, &len, 1, magic_table);

#ifdef UWSGI_LIBYAML
	yaml_parser_t parser;
	yaml_token_t token;
	int status = 0;
	int parsing = 1;

	if (!yaml_parser_initialize(&parser)) {
		uwsgi_log("unable to initialize YAML parser (libyaml)\n");
		exit(1);
	}

	yaml_parser_set_input_string(&parser, (const unsigned char *) yaml, (size_t) len - 1);

	while (parsing) {
		if (!yaml_parser_scan(&parser, &token)) {
			uwsgi_log("error parsing YAML file: %s (%c)\n", parser.problem, yaml[parser.problem_offset]);
			exit(1);
		}
		switch (token.type) {
		case YAML_STREAM_END_TOKEN:
			parsing = 0;
			break;
		case YAML_KEY_TOKEN:
			status = 1;
			break;
		case YAML_VALUE_TOKEN:
			status = 2;
			break;
		case YAML_FLOW_SEQUENCE_START_TOKEN:
		case YAML_BLOCK_SEQUENCE_START_TOKEN:
			status = 3;
			break;
		case YAML_BLOCK_MAPPING_START_TOKEN:
			if (!in_uwsgi_section) {
				if (key) {
					if (!strcmp(section_asked, key)) {
						in_uwsgi_section = 1;
					}
				}
			}
			break;
		case YAML_BLOCK_END_TOKEN:
			if (in_uwsgi_section) {
				parsing = 0;
				break;
			}
			break;
		case YAML_SCALAR_TOKEN:
		case YAML_FLOW_ENTRY_TOKEN:
		case YAML_BLOCK_ENTRY_TOKEN:
			if (status == 1) {
				key = (char *) token.data.scalar.value;
			}
			else if (status == 2) {
				val = (char *) token.data.scalar.value;
				if (key && val && in_uwsgi_section) {
					add_exported_option(key, val, 0);
				}
				status = 0;
			}
			else if (status == 3) {
				val = (char *) token.data.scalar.value;
				if (key && val && in_uwsgi_section) {
					add_exported_option(key, val, 0);
				}
			}
			else {
				uwsgi_log("unsupported YAML token in %s block\n", section_asked);
				parsing = 0;
				break;
			}
			break;
		default:
			status = 0;
		}
	}

#else
	int depth;
	int current_depth = 0;
	int lines = 1;
	char *yaml_line;
	char *section = "";


	while (len) {
		yaml_line = yaml_get_line(yaml, len);
		if (yaml_line == NULL) {
			break;
		}
		lines++;

		// skip empty line
		if (yaml[0] == 0)
			goto next;
		depth = yaml_get_depth(yaml);
		if (depth <= current_depth) {
			current_depth = depth;
			// end the parsing cycle
			if (in_uwsgi_section)
				return;
		}
		else if (depth > current_depth && !in_uwsgi_section) {
			goto next;
		}

		key = yaml_lstrip(yaml);
		// skip empty line
		if (key[0] == 0)
			goto next;

		// skip list and {} defined dict
		if (key[0] == '-' || key[0] == '[' || key[0] == '{') {
			if (in_uwsgi_section)
				return;
			goto next;
		}

		if (!in_uwsgi_section) {
			section = strchr(key, ':');
			if (!section)
				goto next;
			section[0] = 0;
			if (!strcmp(key, section_asked)) {
				in_uwsgi_section = 1;
			}
		}
		else {
			// get dict value       
			val = strstr(key, ": ");
			if (!val) {
				val = strstr(key, ":\t");
			}
			if (!val)
				return;
			// get the right key
			val[0] = 0;
			// yeah overengeneering....
			yaml_rstrip(key);

			val = yaml_lstrip(val + 2);
			yaml_rstrip(val);

			//uwsgi_log("YAML: %s = %s\n", key, val);

			add_exported_option((char *) key, val, 0);
		}
next:
		len -= (yaml_line - yaml);
		yaml += (yaml_line - yaml);

	}
#endif


}
Beispiel #5
0
void 
print_config( OlyStatus *status )
{
    yaml_parser_t        config_parser ;
    yaml_token_t         token;
    OlyConfig           *olyconf;
    TokenMark            token_marker;

    zero_token_mark(&token_marker);

    if ( *status != OLY_OKAY )
    {
        HANDLE_OLY_STATUS(*status);
        return;
    }
    else if ( config_file == NULL )
    {
        *status = OLY_ERR_CONFIG_FILE_NOT_FOUND ;
    }
    
    if ( yaml_parser_initialize(&config_parser) != 1 )
    {
        *status = OLY_ERR_LIBYAML_INIT;
         return;
    }
    /* TODO:
     * This encoding set may need to be dynamic at some point.  
     * since this is only in Linux right now on x86, we are fine,
     * but you never know! 
     */
    yaml_parser_set_encoding( &config_parser, YAML_UTF16LE_ENCODING );
    yaml_input_ofile( &config_parser , config_file );
    do {
        if (!yaml_parser_scan(&config_parser, &token)) 
        {
            printf("Parser error %d\n", config_parser.error);
            exit(EXIT_FAILURE);
        }
        switch(token.type)
        { 
            case YAML_STREAM_START_TOKEN:
                printf("STREAM START\n");
                break;
            case YAML_STREAM_END_TOKEN:
                printf("STREAM END\n");
                break;
            case YAML_VERSION_DIRECTIVE_TOKEN:
                printf("VERSION DIRECTIVE TOKEN\n");
                break;
            case YAML_TAG_DIRECTIVE_TOKEN:
                printf("TAG DIRECTIVE TOKEN\n");
                break;
            case YAML_DOCUMENT_START_TOKEN:
                printf("DOCUMENT START\n");
                break;
            case YAML_DOCUMENT_END_TOKEN:
                printf("DOCUMENT END\n");
                break;
            case YAML_BLOCK_SEQUENCE_START_TOKEN:
                printf("BLOCK SEQUENCE START\n");
                break;
            case YAML_BLOCK_MAPPING_START_TOKEN:
                printf("BLOCK MAPPING START\n");
                break;
            case YAML_BLOCK_END_TOKEN:
                puts("BLOCK END");
                break;
            case YAML_FLOW_SEQUENCE_START_TOKEN:
                puts("FLOW SEQUENCE START");
                break;
            case YAML_FLOW_SEQUENCE_END_TOKEN:
                puts("FLOW SEQUENCE END");
                break;
            case YAML_FLOW_MAPPING_START_TOKEN:
                puts("FLOW MAPPING START");
                break;
            case YAML_FLOW_MAPPING_END_TOKEN:
                puts("FLOW MAPPING END");
                break;
            case YAML_BLOCK_ENTRY_TOKEN:
                puts("[Block Entry]");
                break;
            case YAML_FLOW_ENTRY_TOKEN:
                puts("[Flow Entry]");
                break;
            case YAML_KEY_TOKEN:
                token_marker.is_key_token = 1;
                break;
            case YAML_VALUE_TOKEN:
                token_marker.is_value_token = 1;
                break;
            case YAML_ALIAS_TOKEN:
                puts("al - i - as TOKEN!");
                break;
            case YAML_ANCHOR_TOKEN:
                puts("ANCHOR TOKEN!");
                break;
            case YAML_TAG_TOKEN:
                puts("TAG TOKEN!");
                break;
            case YAML_SCALAR_TOKEN:
                if (token_marker.is_key_token == 1) 
                {
                    u_fprintf(u_stdout, "%s : ", token.data.scalar.value);
                }
                else
                {
                    u_fprintf(u_stdout, "%s\n", token.data.scalar.value);
                }
                zero_token_mark(&token_marker);
                break;
            default: 
                break;
        }
        if(token.type != YAML_STREAM_END_TOKEN)
        {
            yaml_token_delete(&token);
        }
    } while(token.type != YAML_STREAM_END_TOKEN) ;
    yaml_token_delete(&token);
  /* END new code */
    return;
}
Beispiel #6
0
/**
 * @brief [brief description]
 * @details [long description]
 *
 * @param fh [description]
 * @param configuration [description]
 *
 * @return [description]
 */
int ocs_configuration_parse(FILE* fh, ocs_configuration_t* configuration)
{
  assert(fh != NULL);
  assert(configuration != NULL);

  yaml_parser_t parser;
  yaml_token_t token;

  if (!yaml_parser_initialize(&parser))
  {
    /* Failed to initialize parser */
    return(OCS_ERROR);
  }

  /* */
  yaml_parser_set_input_file(&parser, fh);

  /* */
  enum ocs_yaml_reader_state_t state = OCS_YAML_READER_STATE_UNDEFINED;

  /* */
  char** data_ptr = NULL;

  /* */
  char* token_scalar_value = NULL;

  do
  {
    yaml_parser_scan(&parser, &token);
    switch(token.type)
    {
    case YAML_KEY_TOKEN:
      state = OCS_YAML_READER_STATE_KEY_TOKEN;
      break;
    case YAML_VALUE_TOKEN:
      state = OCS_YAML_READER_STATE_VALUE_TOKEN;
      break;
    case YAML_SCALAR_TOKEN:
      token_scalar_value = (char*) token.data.scalar.value;
      if (state == OCS_YAML_READER_STATE_KEY_TOKEN) {
        if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_USERNAME))
        {
          data_ptr = &(configuration->username);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_AUTH))
        {
          data_ptr = &(configuration->auth);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_EMAIL))
        {
          data_ptr = &(configuration->email);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_SERVER))
        {
          data_ptr = &(configuration->server);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_PORT))
        {
          data_ptr = &(configuration->port);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_PATH))
        {
          data_ptr = &(configuration->path);
        }
        else if (!strcmp(token_scalar_value, OCS_CONFIGURATION_TOKEN_TAGS))
        {
          data_ptr = &(configuration->tags);
        }
        else
        {
          /* Unrecognised key */
          return(OCS_ERROR);
        }
      }
      else if (state == OCS_YAML_READER_STATE_VALUE_TOKEN)
      {
	     *data_ptr = strdup(token_scalar_value);
	  }
      break;
    default:
      break;
    }
    if (token.type != YAML_STREAM_END_TOKEN)
    {
      yaml_token_delete(&token);
    }
  } while (token.type != YAML_STREAM_END_TOKEN);

  /* Cleanup. */
  yaml_token_delete(&token);
  yaml_parser_delete(&parser);

  return(OCS_SUCCESS);
}
Beispiel #7
0
int getServiceFromYAML(maps* conf, char* file,service** service,char *name){
  FILE *fh = fopen("test.yml", "r");
  if(current_content!=NULL){
    freeMap(&current_content);
    free(current_content);
    current_content=NULL;
  }
#ifdef DEBUG_SERVICE_CONF
  fprintf(stderr,"(STARTING)FREE current_element\n");
#endif
  if(current_element!=NULL){
    freeElements(&current_element);
    free(current_element);
    current_element=NULL;
  }
  my_service=NULL;
  
  my_service=*service;
  my_service->name=strdup(name);
  my_service->content=NULL;
  my_service->metadata=NULL;
  my_service->inputs=NULL;
  my_service->outputs=NULL;
  fh = fopen(file,"r");
  if (fh==NULL){
    fprintf(stderr,"error : file not found\n") ;
    return -1;
  }
  yaml_parser_t parser;
  yaml_token_t  token;   /* new variable */

  /* Initialize parser */
  if(!yaml_parser_initialize(&parser))
    fputs("Failed to initialize parser!\n", stderr);
  if(fh == NULL)
    fputs("Failed to open file!\n", stderr);
  /* Set input file */
  yaml_parser_set_input_file(&parser, fh);
  /* BEGIN new code */
  int level=0;
  int plevel=level;
  int ilevel=-1;
  int blevel=-1;
  int ttype=0;
  int wait_metadata=-1;
  char *cur_key;
  do {
    yaml_parser_scan(&parser, &token);
    switch(token.type)
    {
    /* Stream start/end */
    case YAML_STREAM_START_TOKEN: 
#ifdef DEBUG_YAML
      puts("STREAM START"); 
#endif
      break;
    case YAML_STREAM_END_TOKEN:   
#ifdef DEBUG_YAML
      puts("STREAM END");   
#endif
      break;
    /* Token types (read before actual token) */
    case YAML_KEY_TOKEN:   
#ifdef DEBUG_YAML
      printf("(Key token)   "); 
#endif
      ttype=0;
      break;
    case YAML_VALUE_TOKEN: 
#ifdef DEBUG_YAML
      printf("(Value token) "); 
#endif
      ttype=1;
      break;
    /* Block delimeters */
    case YAML_BLOCK_SEQUENCE_START_TOKEN: 
#ifdef DEBUG_YAML
      puts("<b>Start Block (Sequence)</b>"); 
#endif
      break;
    case YAML_BLOCK_ENTRY_TOKEN:          
#ifdef DEBUG_YAML
      puts("<b>Start Block (Entry)</b>");    
#endif
      break;
    case YAML_BLOCK_END_TOKEN:      
      blevel--;
      if(ilevel>=0)
	ilevel--;
#ifdef DEBUG_YAML
      printf("<b>End block</b> (%d,%d,%d,%d)\n", blevel,level,ilevel,ttype); 
#endif
      break;
    /* Data */
    case YAML_BLOCK_MAPPING_START_TOKEN:  
#ifdef DEBUG_YAML
      puts("[Block mapping]");            
#endif
      blevel++;
      break;
    case YAML_SCALAR_TOKEN:  
      if(ttype==0){
	cur_key=zStrdup((char *)token.data.scalar.value);
      }
      if(ttype==1){
	if(current_content==NULL){
	  current_content=createMap(cur_key,(char *)token.data.scalar.value);
	}else{
	  addToMap(current_content,cur_key,(char *)token.data.scalar.value);
	}
	free(cur_key);
	cur_key=NULL;
      }

      if(ttype==0 && blevel==0 && level==0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0 && blevel==0){
	addMapToMap(&my_service->content,current_content);
#ifdef DEBUG_YAML
	fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	freeMap(&current_content);
	free(current_content);
	current_content=NULL;
	wait_metadata=1;
      }
      if(ttype==0 && blevel>0 && level>0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0){
	if(current_element->content==NULL && current_content!=NULL)
	  addMapToMap(&current_element->content,current_content);
#ifdef DEBUG_YAML
	dumpMap(current_content);
	fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	freeMap(&current_content);
	free(current_content);
	current_content=NULL;
	wait_metadata=1;
      }
      if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"inputs")==0 && blevel==0){
	if(wait_metadata>0){
	  addMapToMap(&my_service->metadata,current_content);
	  wait_metadata=-1;
	}else{
	  if(current_content!=NULL && my_service->content==NULL)
	    addMapToMap(&my_service->content,current_content);
	}
#ifdef DEBUG_YAML
	dumpMap(current_content);
	fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	freeMap(&current_content);
	free(current_content);
	current_content=NULL;
	wait_metadata=false;
	level++;
      }
      if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"outputs")==0 && blevel==1){
	level++;
#ifdef DEBUG_YAML
	dumpMap(current_content);
	printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); 
#endif
	if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){
	  current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
	  current_element->defaults->content=NULL;
	  current_element->defaults->next=NULL;
	  addMapToMap(&current_element->defaults->content,current_content);
#ifdef DEBUG_YAML
	  dumpElements(current_element);
	  dumpMap(current_content);
	  fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	  freeMap(&current_content);
	  free(current_content);
	  current_content=NULL;
	}else{
	  if(current_content!=NULL && ilevel<=0){
	    addMapToIoType(&current_element->supported,current_content);
#ifdef DEBUG_YAML
	    dumpElements(current_element);
	    dumpMap(current_content);
	    fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	    freeMap(&current_content);
	    free(current_content);
	    current_content=NULL;
	  }
	}
      }
      if(level==1 && strcasecmp((char *)token.data.scalar.value,"default")==0){
	ilevel=0;
      }
      if(level==1 && strcasecmp((char *)token.data.scalar.value,"supported")==0){
#ifdef DEBUG_YAML
	dumpMap(current_content);
	printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); 
#endif
	if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){
	  current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
	  current_element->defaults->content=NULL;
	  current_element->defaults->next=NULL;
	  addMapToMap(&current_element->defaults->content,current_content);
#ifdef DEBUG_YAML
	  dumpElements(current_element);
	  dumpMap(current_content);
	  fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	  freeMap(&current_content);
	  free(current_content);
	  current_content=NULL;
	}else{
	  if(current_content!=NULL && ilevel<=0){
	    if(current_element->supported==NULL){
	      current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
	      current_element->supported->content=NULL;
	      current_element->supported->next=NULL;
	    }
	    addMapToMap(&current_element->supported->content,current_content);
#ifdef DEBUG_YAML
	    dumpElements(current_element);
	    fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	    freeMap(&current_content);
	    free(current_content);
	    current_content=NULL;
	  }
	}
	ilevel=1;
      }


      if(strncasecmp((char *)token.data.scalar.value,"ComplexData",11)==0 || 
	 strncasecmp((char *)token.data.scalar.value,"LiteralData",10)==0 || 
	 strncasecmp((char *)token.data.scalar.value,"ComplexOutput",13)==0 || 
	 strncasecmp((char *)token.data.scalar.value,"LiteralOutput",12)==0 || 
	 strncasecmp((char *)token.data.scalar.value,"BoundingBoxOutput",13)==0 || 
	 strncasecmp((char *)token.data.scalar.value,"BoundingBoxData",12)==0){
	current_element->format=zStrdup((char *)token.data.scalar.value);
	free(cur_key);
	cur_key=NULL;
	if(wait_metadata>0 && current_content!=NULL){
	  addMapToMap(&current_element->metadata,current_content);
	  wait_metadata=-1;
	}else{
	  if(current_content!=NULL){
	    addMapToMap(&current_element->content,current_content);
	  }
	}
#ifdef DEBUG_YAML
	dumpMap(current_content);
	fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
	freeMap(&current_content);
	free(current_content);
	current_content=NULL;
#ifdef DEBUG_YAML
	dumpElements(current_element);
#endif
      }

      if(blevel==1 && level==1){
	if(current_element!=NULL && current_content!=NULL){
	  if(current_element->defaults==NULL){
	    current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
	    current_element->defaults->content=NULL;
	    current_element->defaults->next=NULL;
	    addMapToMap(&current_element->defaults->content,current_content);
	  }else{
	    if(current_element->supported==NULL){
	      current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
	      current_element->supported->content=NULL;
	      current_element->supported->next=NULL;
	      addMapToMap(&current_element->supported->content,current_content);
	    }else
	      addMapToIoType(&current_element->supported,current_content);
	  }
	}
	if(current_element!=NULL){
	  if(my_service->inputs==NULL)
	    my_service->inputs=dupElements(current_element);
	  else
	    addToElements(&my_service->inputs,current_element);
	  freeElements(&current_element);
	  free(current_element);
	}
	plevel=level;
	current_element=(elements*)malloc(ELEMENTS_SIZE);
	current_element->name=zStrdup((char *)token.data.scalar.value);
	current_element->content=NULL;
	current_element->metadata=NULL;
	current_element->format=NULL;
	current_element->defaults=NULL;
	current_element->supported=NULL;
	current_element->next=NULL;
	
      }
      if(blevel==1 && level==2){
	if(current_element!=NULL && current_content!=NULL){
	  if(current_element->defaults==NULL){
	    current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
	    current_element->defaults->content=NULL;
	    current_element->defaults->next=NULL;
	    addMapToMap(&current_element->defaults->content,current_content);
	  }else{
	    if(current_element->supported==NULL){
	      current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
	      current_element->supported->content=NULL;
	      current_element->supported->next=NULL;
	      addMapToMap(&current_element->supported->content,current_content);
	    }else
	      addMapToIoType(&current_element->supported,current_content);
	  }
	}
	if(current_element!=NULL){
	  if(plevel==level){
	    if(my_service->outputs==NULL)
	      my_service->outputs=dupElements(current_element);
	    else
	      addToElements(&my_service->outputs,current_element);
	  }else{
	    if(my_service->inputs==NULL)
	      my_service->inputs=dupElements(current_element);
	    else
	      addToElements(&my_service->inputs,current_element);
	  }
	  freeElements(&current_element);
	  free(current_element);
	}
	plevel=level;
	current_element=(elements*)malloc(ELEMENTS_SIZE);
	current_element->name=zStrdup((char *)token.data.scalar.value);
	current_element->content=NULL;
	current_element->metadata=NULL;
	current_element->format=NULL;
	current_element->defaults=NULL;
	current_element->supported=NULL;
	current_element->next=NULL;
	
      }


#ifdef DEBUG_YAML
      printf("scalar %s (%d,%d,%d,%d,%d)\n", token.data.scalar.value,blevel,level,plevel,ilevel,ttype); 
#endif
      break;
    /* Others */
    default:
      if(token.type==0){
	char tmp[1024];
	sprintf(tmp,"Wrong charater found in %s: \\t",name);
	setMapInMaps(conf,"lenv","message",tmp);
	return -1;
      }
#ifdef DEBUG_YAML
      printf("Got token of type %d\n", token.type);
#endif
      break;
    }
    if(token.type != YAML_STREAM_END_TOKEN )
      yaml_token_delete(&token);
  } while(token.type != YAML_STREAM_END_TOKEN);
  yaml_token_delete(&token);


#ifdef DEBUG_YAML
  fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
  if(current_element!=NULL && current_content!=NULL){
    if(current_element->defaults==NULL){
      current_element->defaults=(iotype*)malloc(IOTYPE_SIZE);
      current_element->defaults->content=NULL;
      current_element->defaults->next=NULL;
      addMapToMap(&current_element->defaults->content,current_content);
    }else{
      if(current_element->supported==NULL){
	current_element->supported=(iotype*)malloc(IOTYPE_SIZE);
	current_element->supported->content=NULL;
	current_element->supported->next=NULL;
	addMapToMap(&current_element->supported->content,current_content);
      }else
	addMapToIoType(&current_element->supported,current_content);
    }
#ifdef DEBUG_YAML
    dumpMap(current_content);
    fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__);
#endif
    freeMap(&current_content);
    free(current_content);
    current_content=NULL;
  }
  if(current_element!=NULL){
    if(my_service->outputs==NULL)
      my_service->outputs=dupElements(current_element);
    else
      addToElements(&my_service->outputs,current_element);
    freeElements(&current_element);
    free(current_element);
    current_element=NULL;
  }
  /* END new code */

  /* Cleanup */
  yaml_parser_delete(&parser);
  fclose(fh);

#ifdef DEBUG_YAML
  dumpService(my_service);
#endif
  *service=my_service;

  return 1;
}
Beispiel #8
0
/*
 * Name: KeInitializeEx
 * Desc: Same as above, but initializes more engine components as specified in the settings
 *		 script.  The first param is the path to the YAML based file where the engine settings
 *       are stored.  Any components listed will be initialized in this function.
 */
bool KeInitializeEx( std::string settings_file, IKeRenderDevice** rd, IKeAudioDevice** ad )
{
	/* Set the current directory to our resource directory */
    KeSetCurrentPathToResourceDirectory();
    
    /* Initial debug logging */
    dbg = new NVDebug( KE_DBG_LEVEL, "debug.txt" );
    DISPDBG( KE_DBGLVL(0), "Initialization started\n" );
    
    /* Initialize SDL and the necessary sub-systems. For now, we only want to initialize 
       timing and events. */
    if( SDL_Init( SDL_INIT_EVENTS | SDL_INIT_TIMER ) != 0 )
	{
		DISPDBG( KE_ERROR, "Error initializing SDL timer and events!" );
	}
	else
	{
		DISPDBG( KE_DBGLVL(3), "SDL_Init(SDL_INIT_EVENTS | SDL_INIT_TIMER) = OK\n" );
	}
    
    /* Gather display modes */
    extern int KeGatherAllDisplayInformation();
    KeGatherAllDisplayInformation();
    
    /* Insert event handler for mobile/embedded platforms */
#ifdef __MOBILE_OS__
    extern int KeProcessAppEvents( void *userdata, SDL_Event *event );
    SDL_SetEventFilter( KeProcessAppEvents, KeGetContextPointer() );
#endif
   
    /* Reset keys */
    KeResetKeys();

	/* Open settings file */
	FILE* fp = fopen( settings_file.c_str(), "r" );
	if( !fp )
	{
		DISPDBG( KE_ERROR, "Error opening settings file!\nFile: " << settings_file << std::endl );
		return false;
	}

	/* Initialize YAML parser */
	yaml_parser_t yaml_parser;
	if( !yaml_parser_initialize( &yaml_parser ) )
	{
		fclose(fp);
		DISPDBG( KE_ERROR, "Error intiializing YAML parser!" );
		return false;
	}

	/* Read settings file */
	yaml_parser_set_input_file( &yaml_parser, fp );

	bool init_renderer = No, init_audio = No, init_leap_motion = No, init_kinect = No;
	KeRenderDeviceDesc rddesc;
	KeAudioDeviceDesc  addesc;

	ZeroMemory( &rddesc, sizeof( KeRenderDeviceDesc ) );
	ZeroMemory( &addesc, sizeof( KeAudioDeviceDesc ) );

	/* Parse the data */
	yaml_token_t token;

	do
	{
		static std::string keyval = " ", valueval = " ";
		static int key = No, value = No;

		yaml_parser_scan( &yaml_parser, &token );

		switch (token.type)
		{
		/* Stream start/end */
		case YAML_STREAM_START_TOKEN:	break;
		case YAML_STREAM_END_TOKEN:		break;

		/* Token types (read before actual token) */
        case YAML_KEY_TOKEN:   key = Yes; break;
        case YAML_VALUE_TOKEN: value = Yes; break;

		/* Block delimeters */
		case YAML_BLOCK_SEQUENCE_START_TOKEN: break;
		case YAML_BLOCK_ENTRY_TOKEN:          break;
		case YAML_BLOCK_END_TOKEN:            break;

		/* Data */
		case YAML_BLOCK_MAPPING_START_TOKEN:  
			if( value )
			{
				if( keyval == "RenderDevice" )
					init_renderer = Yes;
				if( keyval == "AudioDevice" )
					init_audio = Yes;
				if( keyval == "LeapMotionDevice" )
					init_leap_motion = Yes;
				if( keyval == "KinectDevice" )
					init_kinect = Yes;

				value = No;
			}
			break;
		case YAML_SCALAR_TOKEN:
                fprintf(stderr, "scalar %s \n", token.data.scalar.value);
                
                /* Key token value */
                if( key )
                {
                    keyval = (const char*) token.data.scalar.value;
                }
                
                /* Value token value */
                if( value )
                {
                    valueval = (const char*) token.data.scalar.value;
                    
					/* RenderDevice settings */
					if( keyval == "WindowWidth" )
						rddesc.width = atoi( valueval.c_str() );
					else if( keyval == "WindowHeight" )
						rddesc.height = atoi( valueval.c_str() );
					else if( keyval == "ColourBpp" )
						rddesc.colour_bpp = atoi( valueval.c_str() );
					else if( keyval == "DepthBpp" )
						rddesc.depth_bpp = atoi( valueval.c_str() );
					else if( keyval == "StencilBpp" )
						rddesc.stencil_bpp = atoi( valueval.c_str() );
					else if( keyval == "BufferCount" )
						rddesc.buffer_count = atoi( valueval.c_str() );
					else if( keyval == "RefreshRate" )
						rddesc.refresh_rate = atoi( valueval.c_str() );
					else if( keyval == "DeviceType" )
					{
						if( valueval == "OpenGL3" )		rddesc.device_type = KE_RENDERDEVICE_OGL3;
						if( valueval == "OpenGL4" )		rddesc.device_type = KE_RENDERDEVICE_OGL4;
						if( valueval == "Direct3D11" )	rddesc.device_type = KE_RENDERDEVICE_D3D11;
                        if( valueval == "Direct3D12" )	rddesc.device_type = KE_RENDERDEVICE_D3D12;
						if( valueval == "OpenGLES2" )	rddesc.device_type = KE_RENDERDEVICE_OGLES2;
						if( valueval == "OpenGLES3" )	rddesc.device_type = KE_RENDERDEVICE_OGLES3;
						if( valueval == "Metal" )		rddesc.device_type = KE_RENDERDEVICE_METAL;
                        if( valueval == "Vulkan" )      rddesc.device_type = KE_RENDERDEVICE_VULKAN;
					}
					else if( keyval == "Fullscreen" )
						rddesc.fullscreen = valueval == "Yes" ? Yes : No;

					/* AudioDevice settings */

						
                    /* Reset key and values */
                    keyval = " ", valueval = " ";
                }
                
                key = No, value = No;
                break;

		/* Others */
		default:
			fprintf( stderr, "Got token of type %d\n", token.type );
		}
		if( token.type != YAML_STREAM_END_TOKEN )
			yaml_token_delete( &token );
	} while( token.type != YAML_STREAM_END_TOKEN );
	yaml_token_delete( &token );

	/* Close YAML parser */
	yaml_parser_delete( &yaml_parser );
	fclose(fp);
 
	/* Create window and rendering device */
	if( init_renderer && rd != NULL )
	{
		if( !KeCreateWindowAndDevice( &rddesc, rd ) )
			return false;
	}

    /* Call user specified initialization routine */
    KeOnInitialize( KeGetContextPointer() );
    
	return true;
}
Beispiel #9
0
int main (
    int     argc,
    char    *argv [])
{
    char    *fname = "/etc/apache2/conf.d/php.conf.yaml";
    FILE    *fh;

    if (argc > 1)
        fname = argv [1];

    fh = fopen(fname, "r");

    printf ("Openned :%s:\n", fname);

    yaml_parser_t parser;
    yaml_token_t  token;   /* New variable */

    /* Initialize parser */
    if(!yaml_parser_initialize(&parser))
        fputs("Failed to initialize parser!\n", stderr);
    if(fh == NULL)
        fputs("Failed to open file!\n", stderr);

    /* Set input file */
    yaml_parser_set_input_file(&parser, fh);

    /* START new code */
    do {
        yaml_parser_scan(&parser, &token);
        switch(token.type)
        {
        /* Stream start/end */
        case YAML_STREAM_START_TOKEN:
            puts("STREAM START");
            break;
        case YAML_STREAM_END_TOKEN:
            puts("STREAM END");
            break;
        /* Token types (read before actual token) */
        case YAML_KEY_TOKEN:
            printf("(Key token)   ");
            break;
        case YAML_VALUE_TOKEN:
            printf("(Value token) ");
            break;
        /* Block delimeters */
        case YAML_BLOCK_SEQUENCE_START_TOKEN:
            puts("<b>Start Block (Sequence)</b>");
            break;
        case YAML_BLOCK_ENTRY_TOKEN:
            puts("<b>Start Block (Entry)</b>");
            break;
        case YAML_BLOCK_END_TOKEN:
            puts("<b>End block</b>");
            break;
        /* Data */
        case YAML_BLOCK_MAPPING_START_TOKEN:
            puts("[Block mapping]");
            break;
        case YAML_SCALAR_TOKEN:
            printf("scalar %s \n", token.data.scalar.value);
            break;
        /* Others */
        default:
            printf("Got token of type %d\n", token.type);
        }

        if(token.type != YAML_STREAM_END_TOKEN)
            yaml_token_delete(&token);
    } while (token.type != YAML_STREAM_END_TOKEN);

    yaml_token_delete(&token);
    /* END new code */

    /* Cleanup */
    yaml_parser_delete(&parser);
    fclose(fh);

    return 0;
}