Пример #1
0
static int parse_tileset(xmlTextReaderPtr reader, tmx_tileset **ts_headadr, const char *filename) {
	tmx_tileset *res = NULL;
	int ret;
	char *value, *ab_path;
	xmlTextReaderPtr sub_reader;

	if (!(res = alloc_tileset())) return 0;
	res->next = *ts_headadr;
	*ts_headadr = res;

	/* parses each attribute */
	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"firstgid"))) { /* fisrtgid */
		res->firstgid = atoi(value);
		tmx_free_func(value);
	} else {
		tmx_err(E_MISSEL, "xml parser: missing 'firstgid' attribute in the 'tileset' element");
		return 0;
	}

	if ((value = (char*)xmlTextReaderGetAttribute(reader, (xmlChar*)"source"))) { /* source */
		if (!(ab_path = mk_absolute_path(filename, value))) return 0;
		tmx_free_func(value);
		if (!(sub_reader = create_parser(ab_path))) return 0; /* opens */
		ret = parse_tileset_sub(sub_reader, res, ab_path); /* and parses the tsx file */
		xmlFreeTextReader(sub_reader);
		tmx_free_func(ab_path);
		return ret;
	}

	return parse_tileset_sub(reader, res, filename);
}
Пример #2
0
static int initialize_parser(ap_filter_t *f, saxctxt *ctxt, const char **bufp, apr_size_t bytes)
{
  const char *buf = *bufp;

  /* initialize parser */
  if(buf[bytes] != 0) {
    /* make a string for parse routines to play with */
    char *buf1 = apr_palloc(ctxt->f->r->pool, bytes + 1);
    memcpy(buf1, buf, bytes);
    buf1[bytes] = 0;
    buf = *bufp = buf1;
  }

  if(ctxt->cfg->skipto != NULL)
    *bufp = skip_to_start(ctxt, bufp, &bytes);

  if(ctxt->cfg->skipto != NULL && buf != *bufp) {
    /* skip_to_start skipped some bytes, dump them directly */
    ap_fwrite(f->next, ctxt->bb, buf, *bufp - buf);
  }

  if(!create_parser(ctxt, bufp, &bytes))
    return 0;

  return 1;
}
Пример #3
0
static void
assert_log_message_dropped(const gchar *input)
{
  LogParser *parser = create_parser(NULL);
  LogMessage *msg = copy_str_into_log_message(input);

  cr_assert_not(_process_log_message(parser, msg));

  log_msg_unref(msg);
  destroy_parser(parser);
}
Пример #4
0
static void
assert_log_message_name_values_with_options(TestParserOptions *options, const gchar *input,
                                            TestNameValue *expected, gsize number_of_expected)
{
  LogParser *parser = create_parser(options);
  LogMessage *msg = parse_str_into_log_message(parser, input);

  for (int i=0; i < number_of_expected; i++)
    assert_log_message_value_by_name(msg, expected[i].name, expected[i].value);

  log_msg_unref(msg);
  destroy_parser(parser);
}
Пример #5
0
tmx_map *parse_xml(const char *filename) {
	xmlTextReaderPtr reader;
	tmx_map *res = NULL;

	xmlMemSetup((xmlFreeFunc)tmx_free_func, (xmlMallocFunc)tmx_malloc, (xmlReallocFunc)tmx_alloc_func, (xmlStrdupFunc)tmx_strdup);

	if ((reader = create_parser(filename))) {
		res = parse_root_map(reader, filename);
		xmlFreeTextReader(reader);
	}

	return res;
}
Пример #6
0
/* Parse used by load */
object_type *parse_chain(interp_core_type *interp) {
    reset(interp);
    
    if(!interp->scanner) {
	create_parser(interp);
    }
    
    if(parse_internal(interp, peek_scanner)) {
	return 0;
    }

    return interp->added;
}
Пример #7
0
/* Parse a string */
object_type *parse_string(interp_core_type *interp, char *in) {
    reset(interp);

    if(!interp->scanner) {
	create_parser(interp);
    }
    
    yy_scan_string(in, peek_scanner);
    
    if(parse_internal(interp, peek_scanner)) {
	return 0;
    }

    TRACE("\n")
    
    return interp->added;
}
Пример #8
0
/* Parse a file */
object_type *parse(interp_core_type *interp, FILE *in) {
    reset(interp);
    
    if(!interp->scanner) {
	create_parser(interp);
    }
    
    yyset_in(in, peek_scanner);
    
    if(parse_internal(interp, peek_scanner)) {
	return 0;
    }

    TRACE("\n")
    
    return interp->added;
}
Пример #9
0
static int
exmpp_xml_control(ErlDrvData drv_data, unsigned int command,
    char *buf, int len, char **rbuf, int rlen)
{
	struct exmpp_xml_data *edd;
	ei_x_buff *to_return;
	ErlDrvBinary *bin;
	int size, ret;

	edd = (struct exmpp_xml_data *)drv_data;
	size = 0;
	bin = NULL;
	to_return = NULL;

	switch (command) {
	/*
	 * Parsing.
	 */

	case COMMAND_PARSE:
	case COMMAND_PARSE_FINAL:
		if (edd->parser == NULL) {
			/* Start a parser. */
			if (create_parser(edd) != 0) {
				to_return = exmpp_new_xbuf();
				if (to_return == NULL)
					return (-1);

				ret = RET_ERROR;
				ei_x_encode_atom(to_return,
				    "parser_setup_failed");

				break;
			}
		}

		/* Control the total size of data to parse. */
		if (!is_data_size_under_limit(&edd->ctx, len)) {
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_atom(to_return, "stanza_too_big");

			break;
		}

		/* Run XML document parsing. */
		ret = XML_Parse(edd->parser, buf, len,
		    command == COMMAND_PARSE_FINAL);

		if (!ret) {
			enum XML_Error errcode;
			const char *errmsg;

			/* An error occured during parsing; most probably,
			 * XML wasn't well-formed. */
			errcode = XML_GetErrorCode(edd->parser);
			errmsg = XML_ErrorString(errcode);

			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ret = RET_ERROR;
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_atom(to_return, "parsing_failed");
			ei_x_encode_tuple_header(to_return, 2);
			ei_x_encode_long(to_return, errcode);
			ei_x_encode_string(to_return, errmsg);

			break;
		}

		/* Return the complete tree(s). */
		ret = RET_OK;
		if (edd->ctx.complete_trees_ready) {
			/* Terminate the complete trees list. */
			ei_x_encode_empty_list(edd->ctx.complete_trees);

			to_return = edd->ctx.complete_trees;
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			memcpy(bin->orig_bytes + 1,
			    to_return->buff, to_return->index);
		} else {
			/* We need more data to produce a tree. */
			to_return = exmpp_new_xbuf();
			if (to_return == NULL)
				return (-1);

			ei_x_encode_atom(to_return,
			    command == COMMAND_PARSE ? "continue" : "done");
		}

		if (command == COMMAND_PARSE) {
			/* Update the size of processed data. */
			add_data_size(&edd->ctx, len);

			/* Reset the complete trees list. */
			reset_complete_trees(&edd->ctx);
		} else {
			/* We're done with the parser. */
			destroy_parser(edd);
		}

		break;

	case COMMAND_RESET_PARSER:
		if (edd->parser != NULL) {
			reset_context(&edd->ctx);
			XML_ParserReset(edd->parser, "UTF-8");
			init_parser(edd);
		}

		ret = RET_OK;

		break;

	/*
	 * Misc.
	 */

	case COMMAND_PORT_REVISION:
		/* Store the revision in the buffer. */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = RET_OK;
		ei_x_encode_string(to_return, "$Revision$");

		break;

	default:
		/* Other commands are handled in 'exmpp_xml.c' */
		to_return = exmpp_new_xbuf();
		if (to_return == NULL)
			return (-1);

		ret = control(&edd->ctx, command, buf, to_return);
		if (ret < 0)
			return (-1);
	}

	if (bin == NULL) {
		if (to_return != NULL) {
			size = 1 + to_return->index;
			bin = driver_alloc_binary(size);
			if (bin == NULL)
				return (-1);
			bin->orig_bytes[0] = (char)ret;
			if (to_return->index > 0)
				memcpy(bin->orig_bytes + 1,
				    to_return->buff, to_return->index);
			exmpp_free_xbuf(to_return);
		} else {
			/* The command called doesn't return anything. */
			size = 1;
			bin = driver_alloc_binary(size);
			bin->orig_bytes[0] = RET_OK;
		}
	}

	/* Set the returned buffer. */
	*rbuf = (char *)bin;

	/* Return the size of this buffer. */
	return (size);
}
Пример #10
0
static int dive_cb(const unsigned char *data, unsigned int size,
	const unsigned char *fingerprint, unsigned int fsize,
	void *userdata)
{
	int rc;
	parser_t *parser = NULL;
	device_data_t *devdata = userdata;
	dc_datetime_t dt = {0};
	struct tm tm;
	struct dive *dive;

	rc = create_parser(devdata, &parser);
	if (rc != PARSER_STATUS_SUCCESS) {
		fprintf(stderr, "Unable to create parser for %s", devdata->name);
		return rc;
	}

	rc = parser_set_data(parser, data, size);
	if (rc != PARSER_STATUS_SUCCESS) {
		fprintf(stderr, "Error registering the data.");
		parser_destroy(parser);
		return rc;
	}

	dive = alloc_dive();
	rc = parser_get_datetime(parser, &dt);
	if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) {
		fprintf(stderr, "Error parsing the datetime.");
		parser_destroy (parser);
		return rc;
	}

	tm.tm_year = dt.year;
	tm.tm_mon = dt.month-1;
	tm.tm_mday = dt.day;
	tm.tm_hour = dt.hour;
	tm.tm_min = dt.minute;
	tm.tm_sec = dt.second;
	dive->when = utc_mktime(&tm);

	// Parse the divetime.
	printf("Parsing the divetime.\n");
	unsigned int divetime = 0;
	rc = parser_get_field (parser, FIELD_TYPE_DIVETIME, 0, &divetime);
	if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) {
		fprintf(stderr, "Error parsing the divetime.");
		parser_destroy(parser);
		return rc;
	}
	dive->duration.seconds = divetime;

	// Parse the maxdepth.
	printf("Parsing the maxdepth.\n");
	double maxdepth = 0.0;
	rc = parser_get_field(parser, FIELD_TYPE_MAXDEPTH, 0, &maxdepth);
	if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) {
		fprintf(stderr, "Error parsing the maxdepth.");
		parser_destroy(parser);
		return rc;
	}
	dive->maxdepth.mm = maxdepth * 1000 + 0.5;

	// Parse the gas mixes.
	printf("Parsing the gas mixes.\n");
	unsigned int ngases = 0;
	rc = parser_get_field(parser, FIELD_TYPE_GASMIX_COUNT, 0, &ngases);
	if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) {
		fprintf(stderr, "Error parsing the gas mix count.");
		parser_destroy(parser);
		return rc;
	}

	rc = parse_gasmixes(dive, parser, ngases);
	if (rc != PARSER_STATUS_SUCCESS) {
		fprintf(stderr, "Error parsing the gas mix.");
		parser_destroy(parser);
		return rc;
	}

	// Initialize the sample data.
	rc = parse_samples(&dive, parser);
	if (rc != PARSER_STATUS_SUCCESS) {
		fprintf(stderr, "Error parsing the samples.");
		parser_destroy(parser);
		return rc;
	}

	parser_destroy(parser);

	/* If we already saw this dive, abort. */
	if (find_dive(dive, devdata))
		return 0;

	record_dive(dive);
	return 1;
}
Пример #11
0
int main(int argc, char *argv[]){
  int i;
  char c;
  key_t *sortKeys;
  struct sigaction action;
  action.sa_handler = sigHandler;
  num_sort = 1;

  while ((c = getopt (argc, argv, "n:")) != -1)
  {
    switch (c)
    {
    case 'n':
      if(atoi(optarg) < 0 || atoi(optarg) > 50){
        printf("invalid No. of sort processes, defaulting to 1\n");
        num_sort = 1;
      }
      else{
        num_sort = atoi(optarg);
      }
      break;
    default:
      break;
    }
  }

  sigaction(SIGINT, &action, NULL);

  // create memory for key and queue arrays
  sortKeys = malloc(sizeof(key_t)*num_sort);
  assert(sortKeys);
  combiner_queues = malloc(sizeof(int)*num_sort);
  assert(combiner_queues);


  create_parser_queue( /* no args */);

  create_combiner_queues(sortKeys);

  // calling sort before the parser
  // seem to give better results.. i.e. it works
  for(i = 0; i < num_sort; i++){
    sort(sortKeys[i], i+1);
  }

  // create parser
  create_parser(/* no args */);

  // wait for the parser to finish
  wait(NULL);

  combiner(/* no args */);

  // wait for n - sort processes to finish
  for(i = 0; i < num_sort; i++){
    wait(NULL);
  }

  // remove combiner queues
  for(i = 0; i < num_sort; i++){
   msgctl(combiner_queues[i], IPC_RMID, NULL);
  }

  // remove parser queue
  msgctl(message_queue, IPC_RMID, NULL);

  exit(EXIT_SUCCESS);
}
Пример #12
0
static int dive_cb(const unsigned char *data, unsigned int size,
	const unsigned char *fingerprint, unsigned int fsize,
	void *userdata)
{
	int rc;
	dc_parser_t *parser = NULL;
	device_data_t *devdata = userdata;
	dc_datetime_t dt = {0};
	struct tm tm;
	struct dive *dive;

	rc = create_parser(devdata, &parser);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Unable to create parser for %s %s"), devdata->vendor, devdata->product);
		return rc;
	}

	rc = dc_parser_set_data(parser, data, size);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error registering the data"));
		dc_parser_destroy(parser);
		return rc;
	}

	import_dive_number++;
	dive = alloc_dive();
	rc = dc_parser_get_datetime(parser, &dt);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the datetime"));
		dc_parser_destroy(parser);
		return rc;
	}

	tm.tm_year = dt.year;
	tm.tm_mon = dt.month-1;
	tm.tm_mday = dt.day;
	tm.tm_hour = dt.hour;
	tm.tm_min = dt.minute;
	tm.tm_sec = dt.second;
	dive->when = utc_mktime(&tm);

	// Parse the divetime.
	dev_info(devdata, _("Dive %d: %s %d %04d"), import_dive_number,
		monthname(tm.tm_mon), tm.tm_mday, year(tm.tm_year));
	unsigned int divetime = 0;
	rc = dc_parser_get_field (parser, DC_FIELD_DIVETIME, 0, &divetime);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the divetime"));
		dc_parser_destroy(parser);
		return rc;
	}
	dive->duration.seconds = divetime;

	// Parse the maxdepth.
	double maxdepth = 0.0;
	rc = dc_parser_get_field(parser, DC_FIELD_MAXDEPTH, 0, &maxdepth);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the maxdepth"));
		dc_parser_destroy(parser);
		return rc;
	}
	dive->maxdepth.mm = maxdepth * 1000 + 0.5;

	// Parse the gas mixes.
	unsigned int ngases = 0;
	rc = dc_parser_get_field(parser, DC_FIELD_GASMIX_COUNT, 0, &ngases);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error parsing the gas mix count"));
		dc_parser_destroy(parser);
		return rc;
	}

	// Check if the libdivecomputer version already supports salinity
	double salinity = 1.03;
#ifdef DC_FIELD_SALINITY
	rc = dc_parser_get_field(parser, DC_FIELD_SALINITY, 0, &salinity);
	if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {
		dev_info(devdata, _("Error obtaining water salinity"));
		dc_parser_destroy(parser);
		return rc;
	}
#endif
	dive->salinity = salinity * 10000.0 + 0.5;

	rc = parse_gasmixes(devdata, dive, parser, ngases);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error parsing the gas mix"));
		dc_parser_destroy(parser);
		return rc;
	}

	// Initialize the sample data.
	rc = parse_samples(devdata, &dive, parser);
	if (rc != DC_STATUS_SUCCESS) {
		dev_info(devdata, _("Error parsing the samples"));
		dc_parser_destroy(parser);
		return rc;
	}

	dc_parser_destroy(parser);

	/* If we already saw this dive, abort. */
	if (!devdata->force_download && find_dive(dive, devdata))
		return 0;

	dive->downloaded = TRUE;
	record_dive(dive);
	mark_divelist_changed(TRUE);
	return 1;
}