These patches tidy up a few things.  They are independent of the patches
I submitted yesterday.

1. In the header: removed a commented-out prototype for
   message_init_xml(), to which I could find no reference elsewhere.

2. Since message_prepare_xml() was a trivial wrapper for message_to_xml(), 
   which is not called anywhere else, I combined them into a single
   function message_prepare_xml().

3. In message_set_osrf_xid() and message_set_router_info(): added code to
   free the various members before replacing them, in order to avoid a
   potential memory leak.

------------------------------

In message_set_osrf_xid() and message_set_router_info() I not only added
some new code but also replaced the existing if/else code with the 
ternary conditional operator ('?' and ':').  I am not a big fan of the
ternary conditional operator because it usually makes the code more
cryptic.  In this case, however, the results are compact, repetitive,
and stereotyped.  To my eyes the new version is easier to read.

Scott McKellar
http://home.swbell.net/mck9/ct/

Developer's Certificate of Origin 1.1 By making a contribution to
this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license indicated
in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source license
and I have the right under that license to submit that work with
modifications, whether created in whole or in part by me, under the
same open source license (unless I am permitted to submit under a
different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person
who certified (a), (b) or (c) and I have not modified it; and

(d) In the case of each of (a), (b), or (c), I understand and agree
that this project and the contribution are public and that a record
of the contribution (including all personal information I submit
with it, including my sign-off) is maintained indefinitely and may
be redistributed consistent with this project or the open source
license indicated in the file.
*** ./trunk/include/opensrf/transport_message.h	2008-12-07 08:07:52.000000000 -0600
--- ./trunk-mod/include/opensrf/transport_message.h	2008-12-09 18:30:03.000000000 -0600
***************
*** 55,67 ****
  void message_set_osrf_xid( transport_message* msg, const char* osrf_xid );
  
  // ---------------------------------------------------------------------------------
- // Formats the Jabber message as XML for encoding. 
- // Returns NULL on error
- // ---------------------------------------------------------------------------------
- char* message_to_xml( const transport_message* msg );
- 
- 
- // ---------------------------------------------------------------------------------
  // Call this to create the encoded XML for sending on the wire.
  // This is a seperate function so that encoding will not necessarily have
  // to happen on all messages (i.e. typically only occurs outbound messages).
--- 56,61 ----
***************
*** 75,85 ****
  int message_free( transport_message* msg );
  
  // ---------------------------------------------------------------------------------
- // Prepares the shared XML document
- // ---------------------------------------------------------------------------------
- //int message_init_xml();
- 
- // ---------------------------------------------------------------------------------
  // Determines the username of a Jabber ID.  This expects a pre-allocated char 
  // array for the return value.
  // ---------------------------------------------------------------------------------
--- 69,74 ----
*** ./trunk/src/libopensrf/transport_message.c	2008-12-07 08:07:55.000000000 -0600
--- ./trunk-mod/src/libopensrf/transport_message.c	2008-12-09 18:39:01.000000000 -0600
***************
*** 160,221 ****
  		new_msg->body = strdup("");
  
  	new_msg->msg_xml = xmlDocToString(msg_doc, 0);
!    xmlFreeDoc(msg_doc);
!    xmlCleanupParser();
  
  	return new_msg;
  }
  
  void message_set_osrf_xid( transport_message* msg, const char* osrf_xid ) {
!    if(!msg) return;
!    if( osrf_xid )
!       msg->osrf_xid = strdup(osrf_xid);
!    else msg->osrf_xid = strdup("");
  }
  
  void message_set_router_info( transport_message* msg, const char* router_from,
  		const char* router_to, const char* router_class, const char* router_command,
  		int broadcast_enabled ) {
  
! 	if( !msg ) return;
! 	
! 	if(router_from)
! 		msg->router_from		= strdup(router_from);
! 	else
! 		msg->router_from		= strdup("");
! 
! 	if(router_to)
! 		msg->router_to			= strdup(router_to);
! 	else
! 		msg->router_to			= strdup("");
! 
! 	if(router_class)
! 		msg->router_class		= strdup(router_class);
! 	else 
! 		msg->router_class		= strdup("");
! 	
! 	if(router_command)
! 		msg->router_command	= strdup(router_command);
! 	else
! 		msg->router_command	= strdup("");
! 
! 	msg->broadcast = broadcast_enabled;
! 
! 	if( msg->router_from == NULL || msg->router_to == NULL ||
! 			msg->router_class == NULL || msg->router_command == NULL ) 
! 		osrfLogError(OSRF_LOG_MARK,  "message_set_router_info(): Out of Memory" );
! 
! 	return;
! }
! 
! 
  
! /* encodes the message for traversal */
! int message_prepare_xml( transport_message* msg ) {
! 	if( !msg ) return 0;
! 	if( msg->msg_xml == NULL )
! 		msg->msg_xml = message_to_xml( msg );
! 	return 1;
  }
  
  
--- 161,202 ----
  		new_msg->body = strdup("");
  
  	new_msg->msg_xml = xmlDocToString(msg_doc, 0);
! 	xmlFreeDoc(msg_doc);
! 	xmlCleanupParser();
  
  	return new_msg;
  }
  
  void message_set_osrf_xid( transport_message* msg, const char* osrf_xid ) {
! 	if( msg ) {
! 		if( msg->osrf_xid ) free( msg->osrf_xid );
! 		msg->osrf_xid = strdup( osrf_xid ? osrf_xid : "" );
! 	}
  }
  
  void message_set_router_info( transport_message* msg, const char* router_from,
  		const char* router_to, const char* router_class, const char* router_command,
  		int broadcast_enabled ) {
  
! 	if( msg ) {
  
! 		/* free old values, if any */
! 		if( msg->router_from    ) free( msg->router_from );
! 		if( msg->router_to      ) free( msg->router_to );
! 		if( msg->router_class   ) free( msg->router_class );
! 		if( msg->router_command ) free( msg->router_command );
! 
! 		/* install new values */
! 		msg->router_from     = strdup( router_from     ? router_from     : "" );
! 		msg->router_to       = strdup( router_to       ? router_to       : "" );
! 		msg->router_class    = strdup( router_class    ? router_class    : "" );
! 		msg->router_command  = strdup( router_command  ? router_command  : "" );
! 		msg->broadcast = broadcast_enabled;
! 
! 		if( msg->router_from == NULL || msg->router_to == NULL ||
! 				msg->router_class == NULL || msg->router_command == NULL ) 
! 			osrfLogError(OSRF_LOG_MARK,  "message_set_router_info(): Out of Memory" );
! 	}
  }
  
  
***************
*** 240,255 ****
  	free(msg);
  	return 1;
  }
! 	
  // ---------------------------------------------------------------------------------
! // Allocates a char* holding the XML representation of this jabber message
  // ---------------------------------------------------------------------------------
! char* message_to_xml( const transport_message* msg ) {
! 
! 	//int			bufsize;
! 	//xmlChar*		xmlbuf;
! 	//char*			encoded_body;
  
  	xmlNodePtr	message_node;
  	xmlNodePtr	body_node;
  	xmlNodePtr	thread_node;
--- 221,236 ----
  	free(msg);
  	return 1;
  }
! 
! 
  // ---------------------------------------------------------------------------------
! // Encodes the message as XML for traversal; stores in msg_xml member
  // ---------------------------------------------------------------------------------
! int message_prepare_xml( transport_message* msg ) {
  
+ 	if( !msg ) return 0;
+ 	if( msg->msg_xml ) return 1;   /* already done */
+ 	
  	xmlNodePtr	message_node;
  	xmlNodePtr	body_node;
  	xmlNodePtr	thread_node;
***************
*** 260,270 ****
  
  	xmlKeepBlanksDefault(0);
  
- 	if( ! msg ) { 
- 		osrfLogWarning(OSRF_LOG_MARK,  "Passing NULL message to message_to_xml()"); 
- 		return NULL; 
- 	}
- 
  	doc = xmlReadDoc( BAD_CAST "<message/>", NULL, NULL, XML_PARSE_NSCLEAN );
  	message_node = xmlDocGetRootElement(doc);
  
--- 241,246 ----
***************
*** 318,328 ****
  
  	xmlBufferPtr xmlbuf = xmlBufferCreate();
  	xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
! 	char* xml = strdup((const char*) (xmlBufferContent(xmlbuf)));
  	xmlBufferFree(xmlbuf);
  	xmlFreeDoc( doc );		 
  	xmlCleanupParser();
! 	return xml;
  }
  
  
--- 294,306 ----
  
  	xmlBufferPtr xmlbuf = xmlBufferCreate();
  	xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
! 	msg->msg_xml = strdup((const char*) (xmlBufferContent(xmlbuf)));
! 	
  	xmlBufferFree(xmlbuf);
  	xmlFreeDoc( doc );		 
  	xmlCleanupParser();
! 	
! 	return 1;
  }
  
  

Reply via email to