⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml.c

📁 Coware的LISA指令集描述语言开发包
💻 C
📖 第 1 页 / 共 2 页
字号:

	return node;
}//parseXML





// processXMLStream:
//	Process a XML stream in a specified method.
void processXML( struct lisa_database *database, int method )
{
	struct lisa_node *tag, *value, *last;

	if ( method == LISA_XML_ROOT )
	{
		// get the <?xml?> tag...
		tag = parseXML();
		if ( tag->type != XML_DEFINITION_TAG )
		{
			error( "bad XML file, needed <?xml?> tag" );
		}
		destroyNode( tag );
	}

	// process lisa tags
	tag = parseXML();

	// skip remark tags...
	// ...and definition tags...
	while ( ( tag->type == XML_REMARK_TAG ) || ( tag->type == XML_DEFINITION_TAG ) )
	{
		destroyNode( tag );
		tag = parseXML();
	}

	if ( tag->type != XML_LISA_TAG )
		error( "bad XML file" );

	switch ( method )
	{
		case LISA_XML_ROOT:
		{
			destroyNode( tag );
			last = parseXML();
			while ( last->type != XML_LISA_ENDTAG )
			{
				pushbackXMLNode( last );
				// process database struct...
				processXML( database, LISA_XML_DATABASE );
				// check for </database>...
				last = parseXML();
			}
			if ( strcmp( last->element, "database" ) != 0 )
			{
				debug( last->element );
				error( "bad XML file, needed </database> tag" );
			}
			destroyNode( last );
			return;
		}
		case LISA_XML_DATABASE:
		{
			int i;

			if ( strcmp( tag->element, "db-dsn" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// check for dsn...
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->dsn, value->element );
				}
				// check for </db-dsn>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-dsn" ) != 0 ) )
					error( "bad XML file, needed </db-dsn> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-host" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					strcpy( database->host, "127.0.0.1" );
					destroyNode( value );
					break;
				}
				// NOTE: (2002-08-10 Gabriele Budelacci)
				//	No host name means localhost.
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->host, value->element );
				}
				else
				{
					strcpy( database->host, "127.0.0.1" );
				}
				// check for </db-host>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-host" ) != 0 ) )
					error( "bad XML file, needed </db-host> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-name" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					error( "no database name specified" );
					destroyNode( value );
					break;
				}
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->name, value->element );
				}
				else
				{
					error( "no database name specified" );
					break;
				}
				// check for </db-name>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-name" ) != 0 ) )
					error( "bad XML file, needed </db-name> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-password" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// check for password...
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->password, value->element );
				}
				// check for </db-passord>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-password" ) != 0 ) )
					error( "bad XML file, needed </db-password> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-port" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// NOTE: (2002-08-10 Gabriele Budelacci)
				//	Port number 0 means default port for specified database.
				database->port = atoi( value->element );
				// check for </db-port>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-port" ) != 0 ) )
					error( "bad XML file, needed </db-port> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-type" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					error( "no database type specified" );
					destroyNode( value );
					break;
				}
				// lowercasing value...
				for ( i=0 ; i<strlen( value->element ) ; i++ )
				{
					char ch = value->element[i];
					if ( ch >= 'A' && ch <= 'Z' )
						ch |= 32;
					value->element[i] = ch;
				}
				// testing types match...
				for ( i=0 ; 1 ; i++ )
				{
					char *s = dbtypes[ i ];
					if ( strcmp( s, value->element ) == 0 )
					{
						// founded a valid tag!!!
						// Note: (2002-09-05 Gabriele Budelacci)
						//	I write the index i plus 1, because 0 value means
						//	'no database' (see main.h database.types defines).
						database->type = i + 1;
						break;
					}
					if ( strcmp( s, "END" ) == 0 )
					{
						// no types match...
						debug( value->element );
						error( "unsupported database type tag" );
						break;
					}
				}
				// check for </db-type>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-type" ) != 0 ) )
					error( "bad XML file, needed </db-type> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "db-username" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// check for username...
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->username, value->element );
				}
				// check for </db-username>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "db-username" ) != 0 ) )
					error( "bad XML file, needed </db-username> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "jdbc" ) == 0 )
			{
				destroyNode( tag );
				last = parseXML();
				while ( last->type != XML_LISA_ENDTAG )
				{
					pushbackXMLNode( last );
					// process jdbc struct...
					processXML( database, LISA_XML_JDBC );
					// check for </jdbc>...
					last = parseXML();
				}
				if ( strcmp( last->element, "jdbc" ) != 0 )
				{
					error( "bad XML file, needed </jdbc> tag" );
				}
				destroyNode( last );
				break;
			}

			break;
		}
		case LISA_XML_JDBC:
		{
			if ( strcmp( tag->element, "jdbc-driver" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// check for driver name...
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->jdbc_driver, value->element );
				}
				// check for </jdbc-driver>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "jdbc-driver" ) != 0 ) )
					error( "bad XML file, needed </jdbc-driver> tag" );
				destroyNode( last );
			}

			if ( strcmp( tag->element, "jdbc-string" ) == 0 )
			{
				// get the value
				value = parseXML();
				if ( value->type == XML_LISA_ENDTAG )
				{
					destroyNode( value );
					break;
				}
				// check for connection string...
				if ( strlen( value->element ) != 0 )
				{
					strcpy( database->jdbc_string, value->element );
				}
				// check for </jdbc-string>...
				last = parseXML();
				if ( ( last->type != XML_LISA_ENDTAG ) || ( strcmp( last->element, "jdbc-string" ) != 0 ) )
					error( "bad XML file, needed </jdbc-string> tag" );
				destroyNode( last );
			}

			break;
		}
	}
}//processXML




// processXMLDatabase:
//	this function process the database XML configuration file
//	and place the values into the specified structure.
void processXMLDatabase( struct lisa_database *database, char *filename )
{
	FILE *f;

	// firsts, empty database struct values...
	memset( database, 0, sizeof( struct lisa_database ) );

	// open the XML stream
	f = fopen( filename, "r" );
	// process the XML stream
	if ( f )
	{
		// initialize the XML parser...
		initXMLParser( f );

		// process the XML stream...
		processXML( database, LISA_XML_ROOT );

		// close the XML stream
		fclose( f );
	}

	// validating database info...
	database->valid = 1;

}//processXMLDatabase





// pushbackXMLChar:
//	Perform a push-back of last char on
//	the xml stream.
//	NOTE: This method is criticable!
void pushbackXMLChar( void )
{
	if ( line_index > 0 )
		line_index -= 1;
}//pushbackXMLChar





// pushbackXMLNode:
//	Perform a push-back of specified node to the parser.
//	NOTE: This method is criticable!
void pushbackXMLNode( struct lisa_node *node )
{
	pushback_XML_node = node;
}//pushbackXMLNode

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -