예제 #1
0
void XMLDomParser::Reset()
{
	m_errMsg.clear();
	if(m_parser)
	{
		if(!m_encoding.empty()) XML_ParserReset(m_parser, m_encoding.c_str());
		else XML_ParserReset(m_parser, NULL);
		XML_SetUserData(m_parser, this);
		XML_SetElementHandler(m_parser, &StartElementHandler, &EndElementHandler);
		XML_SetCharacterDataHandler(m_parser, &CharacterDataHandler);
	}
	m_pRootNode = NULL;
	while(!m_stack.empty()) m_stack.pop();
}
예제 #2
0
void LLSDXMLParser::Impl::reset()
{
	mResult.clear();
	mParseCount = 0;

	mInLLSDElement = false;
	mDepth = 0;

	mGracefullStop = false;

	mStack.clear();
	
	mSkipping = false;
	
#if( LL_WINDOWS || __GNUC__ > 2)
	mCurrentKey.clear();
#else
	mCurrentKey = std::string();
#endif

	
	XML_ParserReset(mParser, "utf-8");
	XML_SetUserData(mParser, this);
	XML_SetElementHandler(mParser, sStartElementHandler, sEndElementHandler);
	XML_SetCharacterDataHandler(mParser, sCharacterDataHandler);
}
예제 #3
0
int hxcfe_getXmlLayoutID(XmlFloppyBuilder* context,char * container)
{
	int i;
	AppData	*ad = (AppData *) context->ad;

	i = 0;
	do
	{
		XML_ParserReset(context->xml_parser, NULL);
		XML_SetUserData(context->xml_parser, (void *) ad);
		XML_SetElementHandler(context->xml_parser, start, end);
		XML_SetCharacterDataHandler(context->xml_parser, charhandler);
		XML_SetNamespaceDeclHandler(context->xml_parser, ns_start, ns_end);

		ad->xmlcheck = 1;
		XML_Parse(context->xml_parser, disklayout_list[i]->unpacked_data, disklayout_list[i]->size, 1);

		if(!strcmp((char*)ad->name,container))
		{
			return i;
		}

		memset(ad->name,0,512);

		i++;
	}while(disklayout_list[i]);

	return -1;
}
예제 #4
0
END_TEST

/* Regression test for SF bug #477667.
   This test assures that any 8-bit character followed by a 7-bit
   character will not be mistakenly interpreted as a valid UTF-8
   sequence.
*/
START_TEST(test_illegal_utf8)
{
    char text[100];
    int i;

    for (i = 128; i <= 255; ++i) {
        sprintf(text, "<e>%ccd</e>", i);
        if (XML_Parse(parser, text, strlen(text), XML_TRUE) == XML_STATUS_OK) {
            sprintf(text,
                    "expected token error for '%c' (ordinal %d) in UTF-8 text",
                    i, i);
            fail(text);
        }
        else if (XML_GetErrorCode(parser) != XML_ERROR_INVALID_TOKEN)
            xml_failure(parser);
        /* Reset the parser since we use the same parser repeatedly. */
        XML_ParserReset(parser, NULL);
    }
}
예제 #5
0
//This function operates similarly to the parse() function.
//However, it accepts a file offset to begin parsing at a specific point.
//The parser will halt file reading when stop flag is triggered.
bool mzpSAXHandler::parseOffset(f_off offset){

	if (fptr == NULL){
		cerr << "Error parseOffset(): No open file." << endl;
		return false;
	}
	char buffer[CHUNK]; //CHUNK=16384
	int readBytes = 0;
	bool success = true;
	int chunk=0;
	
	XML_ParserReset(m_parser,"ISO-8859-1");
	XML_SetUserData(m_parser, this);
	XML_SetElementHandler(m_parser, mzp_startElementCallback, mzp_endElementCallback);
	XML_SetCharacterDataHandler(m_parser, mzp_charactersCallback);

	mzpfseek(fptr,offset,SEEK_SET);
	m_bStopParse=false;

	if(m_bGZCompression){
		while (success && (readBytes = gzObj.extract(fptr, offset+chunk*CHUNK, (unsigned char*)buffer, CHUNK))>0) {
			success = (XML_Parse(m_parser, buffer, readBytes, false) != 0);
			chunk++;
			if(m_bStopParse) break;
		}
	} else {
		while (success && (readBytes = (int) fread(buffer, 1, sizeof(buffer), fptr)) != 0) {
			success = (XML_Parse(m_parser, buffer, readBytes, false) != 0);
			if(m_bStopParse) break;
		}
	}

	if (!success && !m_bStopParse)
	{
		XML_Error error = XML_GetErrorCode(m_parser);

		cerr << m_strFileName
			<< "(" << XML_GetCurrentLineNumber(m_parser) << ")"
			<< " : error " << (int) error << ": ";

		switch (error)
		{
			case XML_ERROR_SYNTAX:
			case XML_ERROR_INVALID_TOKEN:
			case XML_ERROR_UNCLOSED_TOKEN:
				cerr << "Syntax error parsing XML." << endl;
				break;

			// TODO: Add more descriptive text for interesting errors.

			default:
				cerr << "Spectrum XML Parsing error:\n";
				cerr << XML_ErrorString(error) << endl;
				break;
		} 
		exit(-7);
		return false;
	}
	return true;
}
예제 #6
0
bool xmpp_parser_reset(struct xmpp_parser *parser, bool is_stream_start) {
    if (XML_ParserReset(parser->parser, NULL) != XML_TRUE) {
        return false;
    }

    init_parser(parser, is_stream_start);
    return true;
}
예제 #7
0
static int parseExpat(const char* buf, unsigned int len) 
{
  XML_Status status = XML_Parse(doc, buf, len, 1);
  if( status != XML_STATUS_OK )
    return 1;
  if( XML_ParserReset(doc, NULL) == XML_FALSE )
    return 1;
  return 0;
}
예제 #8
0
bool_t reset_parser(parser_t *parser) {
  if( parser && parser->p ) {
    parser->cur.rstatus = XML_ParserReset(parser->p, NULL);
    XML_SetUserData(parser->p, parser); /* no need to free this later */
    reset_handlers_parser(parser);
    return (bool_t)(parser->cur.rstatus == XML_STATUS_OK);
  }
  return FALSE;
}
예제 #9
0
XmlFloppyBuilder* hxcfe_initXmlFloppy(HXCFLOPPYEMULATOR* floppycontext)
{
	AppData *ad;
	XmlFloppyBuilder * rfw;
	int i;

	rfw = malloc(sizeof(XmlFloppyBuilder));
	if(rfw)
	{
		memset(rfw,0,sizeof(XmlFloppyBuilder));

		rfw->xml_parser = XML_ParserCreate(NULL);
		ad = malloc(sizeof(AppData));
		if(!ad)
		{
			return 0;
		}
		memset(ad,0,sizeof(AppData));
		
		ad->interface_mode = -1;
		
		i=0;
		while( disklayout_list[i])
		{
			if(disklayout_list[i]->unpacked_data) 
			{
				free(disklayout_list[i]->unpacked_data);
				disklayout_list[i]->unpacked_data = 0;
			}

			disklayout_list[i]->unpacked_data = data_unpack(disklayout_list[i]->data,disklayout_list[i]->csize,0,disklayout_list[i]->size);
			i++;
		}

		ad->floppycontext = floppycontext;
		ad->current_state = ENTRY_STATE;
		ad->stack_ptr = 0;
		memset(ad->statestack,0xFF,sizeof(int) * 32);
		ad->statestack[0].state = ad->current_state;
		ad->p = rfw->xml_parser;

		memset(ad->ts,0,sizeof(track_state)*256);

		XML_ParserReset(rfw->xml_parser, NULL);

		XML_SetUserData(rfw->xml_parser, (void *) ad);
		XML_SetElementHandler(rfw->xml_parser, start, end);

		XML_SetCharacterDataHandler(rfw->xml_parser, charhandler);
		XML_SetNamespaceDeclHandler(rfw->xml_parser, ns_start, ns_end);

		rfw->ad = ad;
	}

	return rfw;
}
예제 #10
0
파일: pdb-xml.c 프로젝트: bpeel/prevodb
void
pdb_xml_parser_reset (PdbXmlParser *parser)
{
  XML_Parser xml_parser = pdb_xml_get_parser (parser);

  g_assert (parser->stack->len == 1);

  XML_ParserReset (xml_parser, NULL);
  pdb_xml_init_parser (parser, xml_parser);
}
예제 #11
0
int
main(int argc, char **argv)
{
    XML_Parser parser;

    size_t i;
    size_t j;

    nst_cfg_allocator = nst_mem_stat_register("NST CFG");

    parser = XML_ParserCreate(NULL);
    expat_stack_top->parser = parser;

    if(!parser) {
        assert(0 && "Cannot allocate memory during XML_ParserCreate\n");
        return -1;
    }

    for(i = 0;
            i < sizeof(xml_test_strings) / sizeof(const char *);
            i++) {
        const char *xml_test_string = xml_test_strings[i];
        enum XML_Status xml_status;

        XML_SetStartElementHandler(parser, root_start_handler);
        XML_SetEndElementHandler(parser, root_end_handler);
        XML_SetCharacterDataHandler(parser, root_char_handler);
        XML_SetUserData(parser, (void*)&expat_stack_top);

        for(j = 0; xml_test_string[j]; j++) {
            xml_status = XML_Parse(parser, xml_test_string+j, 1, 0);
            if(xml_status != XML_STATUS_OK) {
                fprintf(stderr, "Parse error at line %lu: %s\n",
                        XML_GetCurrentLineNumber(parser),
                        XML_ErrorString(XML_GetErrorCode(parser)));
                return -1;
            }
        }

        xml_status = XML_Parse(parser, NULL, 0, 1);
        if(xml_status != XML_STATUS_OK) {
            fprintf(stderr, "Parse error at line %lu: %s\n",
                    XML_GetCurrentLineNumber(parser),
                    XML_ErrorString(XML_GetErrorCode(parser)));
            return -1;
        }

        XML_ParserReset(parser, NULL);
    };

    XML_ParserFree(parser);

    assert(nst_mem_stat_get_allocated_nbytes(nst_cfg_allocator.data) == 0);
    return 0;
}
예제 #12
0
//----------------------
///////////////////////////////////////////////////////
//
// ChercheLaTag() -- better to rewrite this!!
//
///////////////////////////////////////////////////////
void simpleXMLparser::CherchezLaTag()
{
  XML_ParserReset(XMLp, NULL);

  XML_SetElementHandler(XMLp, simpleXMLparser::openingHndl, simpleXMLparser::closingHndl);
  XML_SetCharacterDataHandler(XMLp, simpleXMLparser::textHndl);
  XML_SetUserData(XMLp, this);   

  FILE * inputFile;
  if  ( ! (inputFile = fopen(xmlFileName,"r")) )
    {
      std::cout << "Error: File \"" << xmlFileName << "\" does not exist\n" ;
      exit(-1);
    }
	

  searchTag=tXMLpathList.front();     // tag for search
  currentTagNumber=0;                

  Depth = 0;
  prevDepth = 0;
  searchDepth=  tXMLpathList.size(); 

  currentAttr.clear();
  returnAttr.clear();

  ifStop=false;
 
  for (;;) {
    int done;
    int len;
  
    len = fread(Buff, 1, BUFFSIZE, inputFile);
    if (ferror(inputFile)) {
      std::cout << "\nError: read error\n\n";
      exit(-1);
    }
    done = feof(inputFile);

    if (XML_Parse(XMLp, Buff, len, done) == XML_STATUS_ERROR) {
      std::cout << "\nError: parse error at line " 
		<< XML_GetCurrentLineNumber(XMLp) << ": "
		<< XML_ErrorString(XML_GetErrorCode(XMLp)) << "\n\n";
      exit(-1);
    }

    if (done)
      break;
  }

  fclose(inputFile);
  tXMLpathList.clear();
}
예제 #13
0
END_TEST

/* Regression test for SF bug #481609, #774028. */
START_TEST(test_latin1_umlauts)
{
    char *text =
        "<?xml version='1.0' encoding='iso-8859-1'?>\n"
        "<e a='ä ö ü &#228; &#246; &#252; &#x00E4; &#x0F6; &#xFC; >'\n"
        "  >ä ö ü &#228; &#246; &#252; &#x00E4; &#x0F6; &#xFC; ></e>";
    char *utf8 =
        "\xC3\xA4 \xC3\xB6 \xC3\xBC "
        "\xC3\xA4 \xC3\xB6 \xC3\xBC "
        "\xC3\xA4 \xC3\xB6 \xC3\xBC >";
    run_character_check(text, utf8);
    XML_ParserReset(parser, NULL);
    run_attribute_check(text, utf8);
}
예제 #14
0
BrokerMessage *sb_parser_process(XML_Parser p, char *buffer, int len)
{
    BrokerParserData *bp = (BrokerParserData *) XML_GetUserData(p);
    bp->msg = calloc(1, sizeof(BrokerMessage));
    XML_ParserReset(p, NULL);
    XML_SetUserData(p, bp);
    XML_SetElementHandler(p, sbx_start, sbx_end);
    XML_SetCharacterDataHandler(p, getText);

    if (!XML_Parse(p, buffer, len, 1)) {
        fprintf(stderr, "Parse error at line %d:\n%s\n",
                XML_GetCurrentLineNumber(p),
                XML_ErrorString(XML_GetErrorCode(p)));
        exit(-1);
    }

    return bp->msg;
}
예제 #15
0
파일: exml_event.c 프로젝트: erszcz/exml
static ERL_NIF_TERM reset_parser(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    XML_Parser **parser;

    assert(argc == 1);

    if (!enif_get_resource(env, argv[0], PARSER_POINTER, (void **)&parser))
        return enif_make_badarg(env);

    expat_parser *parser_data = XML_GetUserData((XML_Parser)(*parser));
    parser_data->result = enif_make_list(env, 0);
    parser_data->xmlns = (ERL_NIF_TERM)NULL;
    parser_data->env = env;

    assert(XML_TRUE == XML_ParserReset((XML_Parser)(*parser), "UTF-8"));
    init_parser((XML_Parser)(*parser), parser_data);

    return OK;
};
예제 #16
0
void ZLXMLReaderInternal::init(const char *encoding) {
	if (myInitialized) {
		XML_ParserReset(myParser, encoding);
	}

	myInitialized = true;
	XML_UseForeignDTD(myParser, XML_TRUE);

	setupEntities();

	XML_SetUserData(myParser, &myReader);
	if (encoding != 0) {
		XML_SetEncoding(myParser, encoding);
	}
	XML_SetStartElementHandler(myParser, fStartElementHandler);
	XML_SetEndElementHandler(myParser, fEndElementHandler);
	XML_SetCharacterDataHandler(myParser, fCharacterDataHandler);
	XML_SetUnknownEncodingHandler(myParser, fUnknownEncodingHandler, 0);
}
예제 #17
0
const char* hxcfe_getXmlLayoutName(XmlFloppyBuilder* context,int moduleID)
{
	AppData	*ad = (AppData *) context->ad;

	if(hxcfe_numberOfXmlLayout(context) > moduleID)
	{
		ad->xmlcheck = 1;
		XML_ParserReset(context->xml_parser, NULL);
		XML_SetUserData(context->xml_parser, (void *) ad);
		XML_SetElementHandler(context->xml_parser, start, end);
		XML_SetCharacterDataHandler(context->xml_parser, charhandler);
		XML_SetNamespaceDeclHandler(context->xml_parser, ns_start, ns_end);

		XML_Parse(context->xml_parser, disklayout_list[moduleID]->unpacked_data, disklayout_list[moduleID]->size, 1);

		return (const char*)ad->name;
	}

	return NULL;
}
예제 #18
0
파일: parser.c 프로젝트: aconchillo/scew
void
scew_parser_reset (scew_parser *parser)
{
  assert (parser != NULL);

  /* Free stack (to avoid memory leak if last load went wrong). */
  scew_parser_stack_free_ (parser);

  /* Free last loaded preamble. */
  free (parser->preamble);

  /* Reset Expat parser. */
  XML_ParserReset (parser->parser, NULL);
  scew_parser_expat_install_handlers_ (parser);

  /* Initialise structure fields to NULL. */
  parser->tree = NULL;
  parser->preamble = NULL;
  parser->stack = NULL;
}
예제 #19
0
FLOPPY* hxcfe_generateXmlFloppy (XmlFloppyBuilder* context,unsigned char * rambuffer,unsigned buffersize)
{
	AppData *ad;

	ad = context->ad;

	ad->xmlcheck = 0;
	
	ad->image_data = rambuffer;
	ad->buffer_size = buffersize;

	XML_ParserReset(context->xml_parser, NULL);
	XML_SetUserData(context->xml_parser, (void *) ad);
	XML_SetElementHandler(context->xml_parser, start, end);
	XML_SetCharacterDataHandler(context->xml_parser, charhandler);
	XML_SetNamespaceDeclHandler(context->xml_parser, ns_start, ns_end);

	XML_Parse(context->xml_parser, disklayout_list[ad->layout_id]->unpacked_data, disklayout_list[ad->layout_id]->size, 1);

	return ad->floppy;
}
void LLSDXMLParser::Impl::reset()
{
	mResult.clear();
	mParseCount = 0;

	mInLLSDElement = false;
	mDepth = 0;

	mGracefullStop = false;

	mStack.clear();
	
	mSkipping = false;
	
	mCurrentKey.clear();
	
	XML_ParserReset(mParser, "utf-8");
	XML_SetUserData(mParser, this);
	XML_SetElementHandler(mParser, sStartElementHandler, sEndElementHandler);
	XML_SetCharacterDataHandler(mParser, sCharacterDataHandler);
}
예제 #21
0
void ZLXMLReaderInternal::init(const char *encoding) {
	if (myInitialized) {
		XML_ParserReset(myParser, encoding);
	}

	myInitialized = true;
	XML_UseForeignDTD(myParser, XML_TRUE);

	const std::vector<std::string> &dtds = myReader.externalDTDs();
	for (std::vector<std::string>::const_iterator it = dtds.begin(); it != dtds.end(); ++it) {
		myDTDStreamLocks.insert(ZLFile(*it).inputStream());
		parseDTD(myParser, *it);
	}

	XML_SetUserData(myParser, &myReader);
	if (encoding != 0) {
		XML_SetEncoding(myParser, encoding);
	}
	XML_SetStartElementHandler(myParser, fStartElementHandler);
	XML_SetEndElementHandler(myParser, fEndElementHandler);
	XML_SetCharacterDataHandler(myParser, fCharacterDataHandler);
	XML_SetUnknownEncodingHandler(myParser, fUnknownEncodingHandler, 0);
}
예제 #22
0
int cpi_process_xml(struct _cpi_t *p_cpi, char *inp_xml_str, char *out_xml_str)
{
    int success = 0;

    enum XML_Status status;

    /*! create parser instance */
    XML_Parser xml_parser = XML_ParserCreate(expat_char_encoding);

    /*! validate XML */
    {
        /*! initialize parser context, in validate mode */
        parser_context context = { p_cpi, out_xml_str, PARSER_STATE_CPI_BEG, 1 };

        /*! set user data */
        XML_SetUserData(xml_parser, &context);

        /*! set start and end handlers for parsing */
        XML_SetElementHandler(xml_parser, expat_handler_element_start, expat_handler_element_end);

        /*! parse document, validating syntax */
        status = XML_Parse(xml_parser, inp_xml_str, strlen(inp_xml_str), 1);

        /*! update success state, if appropriate */
        success = (context.cur_state == PARSER_STATE_SUCCESS);
    }

    /*! if validation went okay, parse again while executing queries */
    if( (status == XML_STATUS_OK) && success && (XML_ParserReset(xml_parser, expat_char_encoding) == XML_TRUE) )
    {
        /*! initialize parser context, in execution mode */
        parser_context context = { p_cpi, out_xml_str, PARSER_STATE_CPI_BEG, 0 };

        /*! write XML header */
        strcpy(out_xml_str, "<?xml version='1.0'?>\n");
        
        /*! begin basic XML template */
        strncat(out_xml_str, "<cpi version='1.0'>\n", max_size);
        strncat(out_xml_str, "  <response_list>\n", max_size);

        /*! set user data */
        XML_SetUserData(xml_parser, &context);

        /*! set start and end handlers for parsing */
        XML_SetElementHandler(xml_parser, expat_handler_element_start, expat_handler_element_end);

        /*! parse document, executing queries */
        status = XML_Parse(xml_parser, inp_xml_str, strlen(inp_xml_str), 1);

        /*! finish basic XML template */
        strncat(out_xml_str, "  </response_list>\n", max_size);
        strncat(out_xml_str, "</cpi>\n", max_size);

        /*! update success state, if appropriate */
        success = (context.cur_state == PARSER_STATE_SUCCESS);
    }

    /*! free parser instance */
    XML_ParserFree(xml_parser);

    /*! check XML parsing return code */
    if( (status != XML_STATUS_OK) || !success ) { return CPI_FAIL; }

    return CPI_OK;
}
예제 #23
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);
}
예제 #24
0
XML_Bool _Expat_XML_ParserReset(struct ExpatIFace * Self, XML_Parser parser, const XML_Char *encoding)
{
	return XML_ParserReset(parser, encoding);
}
예제 #25
0
파일: compile.c 프로젝트: kristapsdz/sblg
int
compile(XML_Parser p, const char *templ, 
	const char *src, const char *dst)
{
	char		*out, *cp, *buf;
	size_t		 sz, sargsz;
	int		 fd, rc;
	FILE		*f;
	struct pargs	 arg;
	struct article	*sargs;

	memset(&arg, 0, sizeof(struct pargs));

	rc = 0;
	buf = out = NULL;
	fd = -1;
	f = NULL;
	sz = 0;
	sargs = NULL;
	sargsz = 0;

	if ( ! sblg_parse(p, src, &sargs, &sargsz))
		goto out;

	if (0 == sargsz) {
		fprintf(stderr, "%s: contains no article\n", src);
		goto out;
	} else if (sargsz > 1)
		fprintf(stderr, "%s: contains multiple "
			"articles (using the first)\n", src);

	arg.article = &sargs[0];

	if (NULL == dst) {
		/*
		 * If we have no output file name, then name it the same
		 * as the input but with ".html" at the end.
		 * However, if we have ".xml", then replace that with
		 * ".html".
		 */
		sz = strlen(src);
		if (NULL == (cp = strrchr(src, '.')) ||
				strcasecmp(cp + 1, "xml")) {
			/* Append .html to input name. */
			out = xmalloc(sz + 6);
			strlcpy(out, src, sz + 6);
			strlcat(out, ".html", sz + 6);
		} else {
			/* Replace .xml with .html. */
			out = xmalloc(sz + 2);
			strlcpy(out, src, sz - 2);
			strlcat(out, "html", sz + 2);
		} 
	} else
		out = xstrdup(dst);

	f = stdout;
	if (strcmp(out, "-") && NULL == (f = fopen(out, "w"))) {
		perror(out);
		goto out;
	} 
	if ( ! mmap_open(templ, &fd, &buf, &sz))
		goto out;

	arg.f = f;
	arg.src = src;
	arg.dst = strcmp(out, "-") ? out : NULL;
	arg.p = p;

	XML_ParserReset(p, NULL);
	XML_SetElementHandler(p, template_begin, template_end);
	XML_SetDefaultHandlerExpand(p, template_text);
	XML_SetUserData(p, &arg);

	if (XML_STATUS_OK != XML_Parse(p, buf, (int)sz, 1)) {
		fprintf(stderr, "%s:%zu:%zu: %s\n", templ, 
			XML_GetCurrentLineNumber(p),
			XML_GetCurrentColumnNumber(p),
			XML_ErrorString(XML_GetErrorCode(p)));
		goto out;
	} 

	xmltextx(arg.f, arg.buf, arg.dst, arg.article, 1, 0);
	xmlstrflush(arg.buf, &arg.bufsz);
	fputc('\n', f);
	rc = 1;
out:
	mmap_close(fd, buf, sz);
	if (NULL != f && stdin != f)
		fclose(f);

	sblg_free(sargs, sargsz);
	free(out);
	free(arg.buf);
	return(rc);
}
예제 #26
0
int main (int argc, char *argv[])
{
  XML_Parser  parser;
  char        *XMLBuf, *XMLBufEnd, *XMLBufPtr;
  FILE        *fd;
  struct stat fileAttr;
  int         nrOfLoops, bufferSize, fileSize, i, isFinal;
  int         j = 0, ns = 0;
  clock_t     tstart, tend;
  double      cpuTime = 0.0;

  if (argc > 1) {
    if (argv[1][0] == '-') {
      if (argv[1][1] == 'n' && argv[1][2] == '\0') {
        ns = 1;
        j = 1;
      }
      else
        usage(argv[0], 1);
    }
  }

  if (argc != j + 4)
    usage(argv[0], 1);

  if (stat (argv[j + 1], &fileAttr) != 0) {
    fprintf (stderr, "could not access file '%s'\n", argv[j + 1]);
    return 2;
  }

  fd = fopen (argv[j + 1], "r");
  if (!fd) {
    fprintf (stderr, "could not open file '%s'\n", argv[j + 1]);
    exit(2);
  }

  bufferSize = atoi (argv[j + 2]);
  nrOfLoops = atoi (argv[j + 3]);
  if (bufferSize <= 0 || nrOfLoops <= 0) {
    fprintf (stderr,
             "buffer size and nr of loops must be greater than zero.\n");
    exit(3);
  }

  XMLBuf = malloc (fileAttr.st_size);
  fileSize = fread (XMLBuf, sizeof (char), fileAttr.st_size, fd);
  fclose (fd);

  if (ns)
    parser = XML_ParserCreateNS(NULL, '!');
  else
    parser = XML_ParserCreate(NULL);

  i = 0;
  XMLBufEnd = XMLBuf + fileSize;
  while (i < nrOfLoops) {
    XMLBufPtr = XMLBuf;
    isFinal = 0;
    tstart = clock();
    do {
      int parseBufferSize = XMLBufEnd - XMLBufPtr;
      if (parseBufferSize <= bufferSize)
        isFinal = 1;
      else
        parseBufferSize = bufferSize;
      if (!XML_Parse (parser, XMLBufPtr, parseBufferSize, isFinal)) {
        fprintf (stderr, "error '%s' at line %" XML_FMT_INT_MOD \
                     "u character %" XML_FMT_INT_MOD "u\n",
                 XML_ErrorString (XML_GetErrorCode (parser)),
                 XML_GetCurrentLineNumber (parser),
                 XML_GetCurrentColumnNumber (parser));
        free (XMLBuf);
        XML_ParserFree (parser);
        exit (4);
      }
      XMLBufPtr += bufferSize;
    } while (!isFinal);
    tend = clock();
    cpuTime += ((double) (tend - tstart)) / CLOCKS_PER_SEC;
    XML_ParserReset(parser, NULL);
    i++;
  }

  XML_ParserFree (parser);
  free (XMLBuf);

  printf ("%d loops, with buffer size %d. Average time per loop: %f\n",
          nrOfLoops, bufferSize, cpuTime / (double) nrOfLoops);
  return 0;
}