Example #1
1
void parse_file( bool do_html, istream& input_stream, ostream& output_stream )
{
	DomParser parser;
	parser.set_substitute_entities( true );
	parser.parse_stream( input_stream );
	if ( parser )
	{
		/* if succesfull create output */
		const Element * rootNode = parser.get_document()->get_root_node();
		if ( rootNode == NULL )
		{
			throw runtime_error( "get_root_node() failed" );
		}

		OutputBuilder* b;
		if ( do_html )
		{
			b = new HtmlBuilder( output_stream );
		} else
		{
			b = new LatexBuilder( output_stream );
		}

		/* do stuff */
		{
			const Element & root_in = dynamic_cast<const Element &>( *rootNode );
			if ( root_in.get_name() != "document" )
			{
				throw runtime_error( "root node must be document" );
			}
			OutputState * s = b->create_root();
			Node::NodeList list = root_in.get_children();
			for ( Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter )
			{
				if ( *iter != NULL )
				{
					parse_node( **iter, * s );
				}
			}
			s->finish();
			delete s;
		}
		delete b;
	}
}
Example #2
0
solid_object *parse_tree(ast_node *tree)
{
	solid_bytecode *bcode = (solid_bytecode *) malloc(
			sizeof(solid_bytecode) * 1024);
	int i = parse_node(tree, bcode, 0);
	dbc(OP_END, 0, 0, NULL);
	return define_function(bcode, NULL);
}
Example #3
0
int
map_collect_data_osm(FILE *in, struct maptool_osm *osm)
{
	int size=BUFFER_SIZE;
	char buffer[BUFFER_SIZE];
	char *p;
	sig_alrm(0);
	if (!fgets(buffer, size, in) || !xml_declaration_in_line(buffer)){
		fprintf(stderr,"FATAL: First line does not start with XML declaration;\n"
			       "this does not look like a valid OSM file.\n");
		exit(EXIT_FAILURE);
	}
	while (fgets(buffer, size, in)) {
		p=strchr(buffer,'<');
		if (! p) {
			fprintf(stderr,"FATAL: wrong line in input data (does not start with '<'): %s\n", buffer);
			fprintf(stderr,"This does not look like a valid OSM file.\n"
		                "Note that maptool can only process OSM files without wrapped or empty lines.\n");
			exit(EXIT_FAILURE);
		}
		if (!strncmp(p, "<osm ",5)) {
		} else if (!strncmp(p, "<bound ",7)) {
		} else if (!strncmp(p, "<node ",6)) {
			if (!parse_node(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
			processed_nodes++;
		} else if (!strncmp(p, "<tag ",5)) {
			if (!parse_tag(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
		} else if (!strncmp(p, "<way ",5)) {
			if (!parse_way(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
			processed_ways++;
		} else if (!strncmp(p, "<nd ",4)) {
			if (!parse_nd(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
		} else if (!strncmp(p, "<relation ",10)) {
			if (!parse_relation(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
			processed_relations++;
		} else if (!strncmp(p, "<member ",8)) {
			if (!parse_member(p))
				fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
		} else if (!strncmp(p, "</node>",7)) {
			osm_end_node(osm);
		} else if (!strncmp(p, "</way>",6)) {
			osm_end_way(osm);
		} else if (!strncmp(p, "</relation>",11)) {
			osm_end_relation(osm);
		} else if (!strncmp(p, "</osm>",6)) {
		} else {
			fprintf(stderr,"WARNING: unknown tag in %s\n", buffer);
		}
	}
	sig_alrm(0);
	sig_alrm_end();
	return 1;
}
Example #4
0
static VALUE
parse_sequence(rb_yaml_parser_t *parser)
{
    VALUE arr = rb_ary_new();

    VALUE node;
    while ((node = parse_node(parser)) != Qundef) {
	rb_ary_push(arr, node);
    }
    return make_yaml_node("seq", arr);
}
Example #5
0
void Config::parse(const char *pos)
{
	while (1) {
		char *eol = strchr(pos, '\n');
		if (!eol)
			break;

		// parse
		if (!parse_blank(pos) && !parse_prefix(pos) && !parse_partition(pos) && !parse_node(pos))
			fatal("unexpected line in config file:\n%s<END>", pos);

		pos = eol + 1;
	}
}
Example #6
0
static int parse_document(xmlDocPtr document, Octstr *charset, 
                          simple_binary_t **otabxml)
{
    xmlNodePtr node;

    (*otabxml)->wbxml_version = 0x01; /* WBXML Version number 1.1  */
    (*otabxml)->public_id = 0x01; /* Public id for an unknown document type */
    
    charset = octstr_create("UTF-8");
    (*otabxml)->charset = parse_charset(charset);
    octstr_destroy(charset);

    node = xmlDocGetRootElement(document);
    return parse_node(node, otabxml);
}
Example #7
0
static int parse_document(xmlDocPtr document, Octstr *charset, 
                          simple_binary_t **sibxml)
{
    xmlNodePtr node;

    (*sibxml)->wbxml_version = 0x02; /* WBXML Version number 1.2  */
    (*sibxml)->public_id = 0x05; /* SI 1.0 Public ID */
    
    charset = octstr_create("UTF-8");
    (*sibxml)->charset = parse_charset(charset);
    octstr_destroy(charset);

    node = xmlDocGetRootElement(document);
    return parse_node(node, sibxml);
}
Example #8
0
struct tree_node* get_tree_from_file(const char *filename)
{
    FILE *file;
    assert(BUFF_SIZE >= NODE_NAME_SIZE);
    struct tree_node *root;

    file = fopen(filename, "r");
    if (file == NULL) {
        perror(filename);
        exit(1);
    }

    root = parse_node(file, NULL);

    fclose(file);

    return root;
}
Example #9
0
/**
 * maps_osm_parse:
 * @content: XML data
 * @length: Length of data
 * @error: Error handle
 * Returns: (transfer full): A MapsOSMObject
 */
MapsOSMObject *
maps_osm_parse (const char *content, guint length, GError **error)
{
  xmlDocPtr doc;
  xmlNodePtr sub_node;
  MapsOSMObject *object;

  doc = read_xml_doc (content, length, error);

  if (!doc)
    return NULL;

  sub_node = get_sub_node (doc);

  if (!sub_node)
    {
      *error = g_error_new_literal (MAPS_OSM_ERROR, 0,
                                   _("Could not find OSM element"));
      return NULL;
    }

  if (g_str_equal (sub_node->name, "node"))
    {
      object = MAPS_OSMOBJECT (parse_node (sub_node, error));
    }
  else if (g_str_equal (sub_node->name, "way"))
    {
      object = MAPS_OSMOBJECT (parse_way (sub_node, error));
    }
  else if (g_str_equal (sub_node->name, "relation"))
    {
      object = MAPS_OSMOBJECT (parse_relation (sub_node, error));
    }

  xmlFreeNode (sub_node);
  xmlFreeDoc (doc);

  return object;
}
Example #10
0
//---------------------------------------------------------------------------
int Json::parse(const std::string& data, Value &v)
{
    json_error_t err;
    json_t       *elements = NULL;

    elements = json_loads(data.c_str(), 0, &err);

    if (!elements)
    {
        std::stringstream ss;
        if (err.line >= 0)
            ss << "line " << err.line << ": ";
        ss << err.text;
        error = ss.str();
        return -1;
    }
    current_node = elements;

    int ret = parse_node(v);
    json_decref(elements);
    current_node = NULL;
    return ret;
}
void parse_line(char *buff) {
  switch(*buff) {
    case '?': 
      parse_ro_pred(buff+1); 
      break;
    case '=': 
      parse_copy_pred(buff+1);
      break;
    case '&':
      parse_binop_pred(buff+1, create_and); 
      break;
    case '|':
      parse_binop_pred(buff+1, create_or); 
      break;
    case '~':
      parse_not(buff+1); 
      break;
    case 'P':
      parse_psi(buff+1);
      break;
    case 'H':
      parse_eta(buff+1);
      break;
    case '@': 
      parse_updateuse(buff+1);
    case 'N': 
      parse_node(buff+1); 
      break;
    case 'E':
      parse_edge(buff+1);
      break;
    case 'C': 
      parse_cond_edge(buff+1);
      break;
  }
}
Example #12
0
//----------------------------------------------------------------------------
// SAXParsor::parse_node
//----------------------------------------------------------------------------
void SAXParsor::parse_node(void *_pNode, INodeAnalyser* pAnalyser)
{
  xmlNode *pNode = (xmlNode *)_pNode;
  xmlNode *pCurrentNode;
  yat::String current_file;

  for( pCurrentNode = pNode; pCurrentNode != NULL; pCurrentNode = pCurrentNode->next )
  {
    // Default : next target is the same target
    INodeAnalyser *pNextAnalyser = pAnalyser;

    yat::String element_name;
    if( pCurrentNode->type == XML_ELEMENT_NODE )
    {
      element_name = (const char *)(pCurrentNode->name);

      // Search for 'xml:base' attribute
      for( xmlAttr *pAttr = pCurrentNode->properties; pAttr; pAttr = pAttr->next )
      {
        if( yat::String((const char *)(pAttr->name)).is_equal_no_case("base") )
        {
          yat::String content((const char *)(pAttr->children->content));
          if( content.find('/') != std::string::npos )
            // Remove path part of the base attribute value
            content.extract_token_right('/', &current_file);
        }
      }

      Attributes attributes;
      xmlAttr *pAttr = NULL;
      for( pAttr = pCurrentNode->properties; pAttr; pAttr = pAttr->next )
      {
        yat::String value((const char *)(pAttr->children->content));
        yat::String name((const char *)(pAttr->name));

        // Search for substitute in defines dictionnary
        attributes[name] = value;
      }

      if( NULL != pAnalyser )
        pNextAnalyser = pAnalyser->on_element(element_name, attributes, current_file);
    }

    else if (pCurrentNode->type == XML_TEXT_NODE && !xmlIsBlankNode(pCurrentNode) )
    {
      // Retreives element name
      yat::String element_name((const char *)(pCurrentNode->parent->name));
      yat::String content((const char *)(pCurrentNode->content));
      // Remove wasted white spaces
      content.trim();

      // Process content
      if( NULL != pAnalyser )
        pAnalyser->on_element_content(element_name, content, current_file);
    }

    if( NULL != pNextAnalyser )
    {
      // Parse children node using the next analyser
      parse_node(pCurrentNode->children, pNextAnalyser);
      
      if( pNextAnalyser != pAnalyser )
      {
        // Analyser created on previous call to OnElement no longer in use
        pNextAnalyser->release();
      }
    }

    // Notify target for end parsing
    if( pCurrentNode->type == XML_ELEMENT_NODE && NULL != pNextAnalyser )
      pAnalyser->on_end_element(element_name);
  }

}
Example #13
0
File: tree.c Project: georgeha/OS
/*
 * recursively parse tree file, creating nodes
 */
static struct tree_node *
parse_node(FILE *file, struct tree_node *node)
{
	char buff[BUFF_SIZE], *name, *num_str;
	unsigned nr_children;
	int i;

	name = find_block_start(file, buff, BUFF_SIZE);
	if (name == NULL){ /* EOF */
		 /* empty file, do nothing */
		if (node == NULL)
			return NULL;
		/* otherwise, terminate parsing */
		fprintf(stderr, "expecting: %s and got EOF\n", node->name);
		exit(1);
	}

	/* If no node given, allocate one -- this is used for root
	 * If node is given, check that the names match */
	if (node == NULL){
		node = calloc(1, sizeof(struct tree_node));
		if (node == NULL){
			fprintf(stderr, "node allocation failed\n");
			exit(1);
		}
		snprintf(node->name, NODE_NAME_SIZE, "%s", name);
	} else if (strncmp(node->name, name, NODE_NAME_SIZE) != 0){
		fprintf(stderr, "nodes must be placed in a DFS order\n");
		fprintf(stderr, "expecting: %s and got: %s\n", node->name, name);
		exit(1);
	}

	/* read number of children */
	num_str = read_non_empty_line(file, buff, BUFF_SIZE);
	nr_children = node->nr_children = atol(num_str);

	/* allocate children */
	if (nr_children != 0){
		node->children = malloc(sizeof(struct tree_node)*nr_children);
		if (node->children == NULL){
			fprintf(stderr, "allocate children failed\n");
			exit(1);
		}
	}

	/* read children names */
	for (i=0; i<nr_children; i++){
		name = read_non_empty_line(file, buff, BUFF_SIZE);
		snprintf(node->children[i].name, NODE_NAME_SIZE, "%s", name);
		//printf("%s\n", node->children[i].name);
	}

	read_empty_line(file, buff, BUFF_SIZE);

	/* parse children */
	for (i=0; i<nr_children; i++){
		parse_node(file, &node->children[i]);
	}

	return node;
}
Example #14
0
int cmd_opt::parse(int argc, char **argv)
{
    auto mode = CMD_OPT;
    for (auto i = 1; i < argc; i++) {
        std::string arg(argv[i]);
        switch (mode) {
        case CMD_OPT:
            if (arg == "-o" || arg == "--output") {
                mode = CMD_OUTPUT_ARG;
            }
            else if (arg == "-h" || arg == "--help") {
                return 1;
            }
            else if (arg == "-t" || arg == "--threshold") {
                mode = CMD_THRESHOLD_ARG;
            }
            else if (arg == "-n" || arg == "--node") {
                mode = CMD_NODE_ARG;
            }
            else if (arg == "-l" || arg == "--l") {
                mode = CMD_LABEL_ARG;
            }
            else if (arg == "-d" || arg == "--depth") {
                mode = CMD_DEPTH_ARG;
            }
            else if (arg == "-m" || arg == "--max-subnodes") {
                mode = CMD_MAX_SUBNODES_ARG;
            }
            else if (arg == "-e" || arg == "--export") {
                mode = CMD_EXPORT_ARG;
            }
            else if (arg == "-c" || arg == "--critical") {
                critical_only = true;
            }
            else {
                if (!ifile.empty()) {
                    return -3;
                }
                ifile = arg;
            }

            break;
        case CMD_OUTPUT_ARG:
            ofile = arg;
            mode = CMD_OPT;
            break;
        case CMD_THRESHOLD_ARG:
        {
            char *p = argv[i];
            threshold = std::strtod(argv[i], &p);
            if (p == argv[i]) {
                return -2;
            }
        }
        mode = CMD_OPT;
        break;
        case CMD_MAX_SUBNODES_ARG:
            try {
                max_subnodes = std::stoi(argv[i]);
            }
            catch (...) {
                return -10;
            }
            mode = CMD_OPT;
            break;
        case CMD_DEPTH_ARG:
            try {
                depth = std::stoi(argv[i]);
            }
            catch (...) {
                return -3;
            }
            mode = CMD_OPT;
            break;
        case CMD_EXPORT_ARG:
            if (!strcasecmp("DOT", argv[i])) {
                export_type = EXPORT_DOT;
            }
            else if (!strcasecmp("GML", argv[i])) {
                export_type = EXPORT_GML;
            }
            else if (!strcasecmp("GRAPHML", argv[i])) {
                export_type = EXPORT_GRAPHML;
            }
            else {
                return -4;
            }
            mode = CMD_OPT;
            break;
        case CMD_NODE_ARG:
            parse_node(argv[i]);
            mode = CMD_OPT;
            break;
        case CMD_LABEL_ARG:
        {
            std::string s((argv[i][1] == 'x' || argv[i][1] == 'X')? &argv[i][2] : argv[i]);
            auto i = std::stoll(s, NULL, 16);
            labels.push_back(i);
        }
            mode = CMD_OPT;
            break;
        default:
            return -1;
        }
    }

    return 0;
}
Example #15
0
void parse_node( const Node& in_node, OutputState& state )
{
	if ( dynamic_cast<const ContentNode*>( &in_node ) != NULL )
	{
		const ContentNode & content = dynamic_cast<const ContentNode &>( in_node );
		state.put_text( content.get_content() );
	}
	else
	{
		if ( dynamic_cast<const TextNode*>( &in_node ) != NULL )
		{
			const TextNode & text = dynamic_cast<const TextNode &>( in_node );
			if ( text.is_white_space() ) {
				return;
			}
		} else if ( dynamic_cast<const Element*>( &in_node ) != NULL )
		{
			const Element & element = dynamic_cast<const Element&>( in_node );
			OutputState* out = NULL;
			string name = element.get_name();
			if ( name == "b" )
			{
				out = state.bold();
			} else if ( name == "math" )
			{
				out = state.math();
			} else if ( name == "plot" )
			{
				out = state.plot();
			} else if ( ( name == "section" ) || ( name == "subsection" ) || ( name == "subsubsection" ) )
			{
				string section_name = element.get_attribute_value( "name" );
				if ( section_name.length() == 0 )
				{
					throw runtime_error( "Sections must have a name attribute" );
				}
				unsigned int level;
				if ( name == "subsection" )
				{
					level = 1;
				} else if ( name == "subsubsection" )
				{
					level = 2;
				} else
				{
					level = 0;
				}
				out = state.section( section_name, level );
			}
			else
			{
				stringstream ss;
				ss << "Unknown element with name \"" << name << "\" found!" << endl;
				throw runtime_error( ss.str().c_str() );
			}
			Node::NodeList list = in_node.get_children();
			for ( Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter )
			{
				if ( *iter != NULL )
				{
					parse_node( **iter, *out );
				}
			}
			out->finish();
			delete out;
		}
	}
}
/********************************************************************
 * FUNCTION handle_config_input
 * (config mode input received)
 * 
 * e.g.,
 *  nacm
 *  interface eth0
 *  interface eth0 mtu 1500
 *
 * INPUTS:
 *    server_cb == server control block to use
 *    session_cb == session control block to use
 *    line == CLI input in progress; this line is passed to the
 *           tk_parse functions which expects non-const ptr
 *
 * RETURNS:
 *   status
 *********************************************************************/
status_t
    handle_config_input (server_cb_t *server_cb,
                         session_cb_t *session_cb,
                         xmlChar *line)
{
    if (LOGDEBUG2) {
        log_debug2("\nconfig mode input for line '%s'\n", line);
    }

    /* get a token chain and parse the line of input into tokens */
    tk_chain_t *tkc = tk_new_chain();
    if (tkc == NULL) {
        log_error("\nError: could not malloc token chain\n");
        return ERR_INTERNAL_MEM;
    }
    tk_setup_chain_cli(tkc, line);
    status_t res = tk_tokenize_input(tkc, NULL);
    if (res != NO_ERR) {
        tk_free_chain(tkc);
        return res;
    }

    /* check the number of tokens parsed */
    uint32 tkcount = tk_token_count(tkc);
    if (LOGDEBUG2) {
        log_debug2("\nconfig token count: %u", tkcount);
    }
    if (tkcount == 0) {
        tk_free_chain(tkc);
        return NO_ERR;
    }

    obj_template_t *startobj = session_cb->config_curobj;
    val_value_t *startval = session_cb->config_curval;
    val_value_t *startroot = session_cb->config_etree;
    val_value_t *starteval = session_cb->config_ecurval;

    session_cb->config_no_active = FALSE;
    session_cb->config_estartval = starteval;
    session_cb->config_firstnew = NULL;

    boolean gotleafys = FALSE;
    boolean gotexit = FALSE;
    boolean gotapply = FALSE;
    boolean gotdo = FALSE;
    boolean done = FALSE;
    while (!done && res == NO_ERR) {
        /* parse nodes and possibly keys until a value node or the end
         * of the token chain is reached    */
        res = parse_node(tkc, session_cb, &done, &gotexit, &gotapply, &gotdo);
        obj_template_t *obj = session_cb->config_curobj;
        if (res == NO_ERR && done && !gotdo && !gotexit && !gotapply && obj &&
            obj_is_leafy(obj)) {
            /* get the next node as a value node */
            res = parse_value(session_cb, tkc);
            gotleafys = TRUE;
            if (tk_next_typ(tkc) != TK_TT_NONE) {
                res = ERR_NCX_EXTRA_NODE;
                log_error("\nError: unexpected input at end of line\n");
            }
            if (res == NO_ERR && !session_cb->config_no_active) {
                if (obj->parent && !obj_is_root(obj->parent)) {
                    session_cb->config_curobj = obj->parent;
                } else {
                    session_cb->config_curobj = NULL;
                }
                /* reset to parent of the leaf just parsed */
                session_cb->config_ecurval = session_cb->config_ecurval->parent;
            }
        }
    }
    
    /* check exit conditions */
    if (res != NO_ERR || done || gotexit || gotapply || gotdo) {
        if (res == NO_ERR && gotexit) {
            res = process_exit(server_cb, session_cb);
            if (res == NO_ERR) {
                res = set_config_path(session_cb);
            }
        } else if (res == NO_ERR && gotapply) {
            if (session_cb->config_curobj &&
                dlq_empty(&session_cb->config_editQ) &&
                session_cb->config_edit_mode != CFG_EDITMODE_LINE) {
                /* add an edit just because the user entered a
                 * sub-mode and then invoked apply
                 */
                res = add_edit(session_cb);
            }
            clear_current_level(session_cb);
            if (res == NO_ERR) {
                res = process_apply(server_cb, session_cb);
            }
        } else if (res == NO_ERR && gotdo) {
            res = process_do_command(server_cb, session_cb, tkc, line);
        } else if (res == NO_ERR && done) {
            res = process_line_done(server_cb, session_cb, startobj, gotleafys);
            if (res == NO_ERR && session_cb->config_curobj &&
                session_cb->config_curobj != startobj) {
                res = set_config_path(session_cb);
            }
        }
        if (res != NO_ERR) {
            /* reset current node pointers */
            session_cb->config_curobj = startobj;
            session_cb->config_curval = startval;
            if (startroot == NULL) {
                val_free_value(session_cb->config_etree);
                session_cb->config_etree = NULL;
                session_cb->config_ecurval = NULL;
                session_cb->config_firstnew = NULL;
            } else {
                if (session_cb->config_firstnew) {
                    if (session_cb->config_firstnew->parent) {
                        val_remove_child(session_cb->config_firstnew);
                    }
                    val_free_value(session_cb->config_firstnew);
                    session_cb->config_firstnew = NULL;
                }
                session_cb->config_etree = startroot;
                session_cb->config_ecurval = starteval;
            }
            (void)set_config_path(session_cb);
        }
    }

    tk_free_chain(tkc);
    return res;

}  /* handle_config_input */
Example #17
0
static int parse_node(xmlNodePtr node, simple_binary_t **sibxml)
{
    int status = 0;
    
    /* Call for the parser function of the node type. */
    switch (node->type) {
    case XML_ELEMENT_NODE:
	status = parse_element(node, sibxml);
	break;
    case XML_TEXT_NODE:
	status = parse_text(node, sibxml);
	break;
    case XML_CDATA_SECTION_NODE:
	status = parse_cdata(node, sibxml);
	break;
    case XML_COMMENT_NODE:
    case XML_PI_NODE:
	/* Comments and PIs are ignored. */
	break;
	/*
	 * XML has also many other node types, these are not needed with 
	 * SI. Therefore they are assumed to be an error.
	 */
    default:
	error(0, "SI compiler: Unknown XML node in the SI source.");
	return -1;
	break;
    }

    /* 
     * If node is an element with content, it will need an end tag after it's
     * children. The status for it is returned by parse_element.
     */
    switch (status) {
    case 0:

	if (node->children != NULL)
	    if (parse_node(node->children, sibxml) == -1)
		return -1;
	break;
    case 1:
	if (node->children != NULL)
	    if (parse_node(node->children, sibxml) == -1)
		return -1;
	parse_end(sibxml);
	break;

    case -1: /* Something went wrong in the parsing. */
	return -1;
    default:
	warning(0,"SI compiler: undefined return value in a parse function.");
	return -1;
	break;
    }

    if (node->next != NULL)
	if (parse_node(node->next, sibxml) == -1)
	    return -1;

    return 0;
}