From 676010f50539a6cd8a94d5cd2211a611b8156f19 Mon Sep 17 00:00:00 2001 From: miker Date: Sat, 27 Oct 2007 01:48:03 +0000 Subject: [PATCH] Patch from Scott McKellar to provide more const correctness to functions using osrfJSON objects git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1116 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_message.h | 19 ++++++++++--------- src/libopensrf/osrf_message.c | 34 ++++++++++++++++------------------ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/opensrf/osrf_message.h b/include/opensrf/osrf_message.h index e8d5b75..57f1aa5 100644 --- a/include/opensrf/osrf_message.h +++ b/include/opensrf/osrf_message.h @@ -103,15 +103,16 @@ const char* osrf_message_get_current_locale(void); osrf_message* osrf_message_init( enum M_TYPE type, int thread_trace, int protocol ); //void osrf_message_set_request_info( osrf_message*, char* param_name, json* params ); -void osrf_message_set_status_info( osrf_message*, char* status_name, char* status_text, int status_code ); -void osrf_message_set_result_content( osrf_message*, char* json_string ); +void osrf_message_set_status_info( osrf_message*, + const char* status_name, const char* status_text, int status_code ); +void osrf_message_set_result_content( osrf_message*, const char* json_string ); void osrfMessageFree( osrfMessage* ); void osrf_message_free( osrf_message* ); char* osrf_message_to_xml( osrf_message* ); -char* osrf_message_serialize(osrf_message*); +char* osrf_message_serialize(const osrf_message*); /* count is the max number of messages we'll put into msgs[] */ -int osrf_message_deserialize(char* json, osrf_message* msgs[], int count); +int osrf_message_deserialize(const char* json, osrf_message* msgs[], int count); @@ -121,10 +122,10 @@ int osrf_message_deserialize(char* json, osrf_message* msgs[], int count); */ int osrf_message_from_xml( char* xml, osrf_message* msgs[] ); -void osrf_message_set_params( osrf_message* msg, jsonObject* o ); -void osrf_message_set_method( osrf_message* msg, char* method_name ); -void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ); -void osrf_message_add_param( osrf_message*, char* param_string ); +void osrf_message_set_params( osrf_message* msg, const jsonObject* o ); +void osrf_message_set_method( osrf_message* msg, const char* method_name ); +void osrf_message_add_object_param( osrf_message* msg, const jsonObject* o ); +void osrf_message_add_param( osrf_message*, const char* param_string ); jsonObject* osrfMessageGetResult( osrfMessage* msg ); @@ -133,7 +134,7 @@ jsonObject* osrfMessageGetResult( osrfMessage* msg ); Returns the message as a jsonObject @return The jsonObject which must be freed by the caller. */ -jsonObject* osrfMessageToJSON( osrfMessage* msg ); +jsonObject* osrfMessageToJSON( const osrfMessage* msg ); char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ); diff --git a/src/libopensrf/osrf_message.c b/src/libopensrf/osrf_message.c index c7c7efc..50938d1 100644 --- a/src/libopensrf/osrf_message.c +++ b/src/libopensrf/osrf_message.c @@ -30,20 +30,19 @@ char* osrf_message_set_locale( osrf_message* msg, const char* locale ) { const char* osrf_message_set_default_locale( const char* locale ) { if( locale == NULL ) return NULL; - if( strlen(locale) > 16 ) return NULL; + if( strlen(locale) > sizeof(default_locale) - 1 ) return NULL; - memcpy( default_locale, locale, strlen(locale) ); - default_locale[strlen(locale)] = '\0'; + strcpy( default_locale, locale ); return (const char*) default_locale; } -void osrf_message_set_method( osrf_message* msg, char* method_name ) { +void osrf_message_set_method( osrf_message* msg, const char* method_name ) { if( msg == NULL || method_name == NULL ) return; msg->method_name = strdup( method_name ); } -void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) { +void osrf_message_add_object_param( osrf_message* msg, const jsonObject* o ) { if(!msg|| !o) return; if(!msg->_params) msg->_params = jsonParseString("[]"); @@ -52,16 +51,15 @@ void osrf_message_add_object_param( osrf_message* msg, jsonObject* o ) { free(j); } -void osrf_message_set_params( osrf_message* msg, jsonObject* o ) { +void osrf_message_set_params( osrf_message* msg, const jsonObject* o ) { if(!msg || !o) return; if(o->type != JSON_ARRAY) { osrfLogDebug( OSRF_LOG_MARK, "passing non-array to osrf_message_set_params(), fixing..."); - jsonObject* clone = jsonObjectClone(o); - o = jsonNewObject(NULL); - jsonObjectPush(o, clone); if(msg->_params) jsonObjectFree(msg->_params); - msg->_params = o; + jsonObject* clone = jsonObjectClone(o); + msg->_params = jsonNewObject(NULL); + jsonObjectPush(msg->_params, clone); return; } @@ -71,15 +69,15 @@ void osrf_message_set_params( osrf_message* msg, jsonObject* o ) { /* only works if parse_json_params is false */ -void osrf_message_add_param( osrf_message* msg, char* param_string ) { +void osrf_message_add_param( osrf_message* msg, const char* param_string ) { if(msg == NULL || param_string == NULL) return; if(!msg->_params) msg->_params = jsonParseString("[]"); jsonObjectPush(msg->_params, jsonParseString(param_string)); } -void osrf_message_set_status_info( - osrf_message* msg, char* status_name, char* status_text, int status_code ) { +void osrf_message_set_status_info( osrf_message* msg, + const char* status_name, const char* status_text, int status_code ) { if(!msg) return; if( status_name != NULL ) @@ -92,7 +90,7 @@ void osrf_message_set_status_info( } -void osrf_message_set_result_content( osrf_message* msg, char* json_string ) { +void osrf_message_set_result_content( osrf_message* msg, const char* json_string ) { if( msg == NULL || json_string == NULL) return; msg->result_string = strdup(json_string); if(json_string) msg->_result_content = jsonParseString(json_string); @@ -138,7 +136,7 @@ char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) { char* j; int i = 0; - osrfMessage* msg = NULL; + const osrfMessage* msg = NULL; jsonObject* wrapper = jsonNewObject(NULL); while( ((msg = msgs[i]) && (i++ < count)) ) @@ -151,7 +149,7 @@ char* osrfMessageSerializeBatch( osrfMessage* msgs [], int count ) { } -char* osrf_message_serialize(osrf_message* msg) { +char* osrf_message_serialize(const osrf_message* msg) { if( msg == NULL ) return NULL; char* j = NULL; @@ -169,7 +167,7 @@ char* osrf_message_serialize(osrf_message* msg) { } -jsonObject* osrfMessageToJSON( osrfMessage* msg ) { +jsonObject* osrfMessageToJSON( const osrfMessage* msg ) { jsonObject* json = jsonNewObject(NULL); jsonObjectSetClass(json, "osrfMessage"); @@ -240,7 +238,7 @@ jsonObject* osrfMessageToJSON( osrfMessage* msg ) { } -int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) { +int osrf_message_deserialize(const char* string, osrf_message* msgs[], int count) { if(!string || !msgs || count <= 0) return 0; int numparsed = 0; -- 2.11.0