xi_context_t* xi_create_context( xi_protocol_t protocol, const char* api_key , int32_t feed_id ) { // allocate the structure to store new context xi_context_t* ret = ( xi_context_t* ) xi_alloc( sizeof( xi_context_t ) ); XI_CHECK_MEMORY( ret ); // copy given numeric parameters as is ret->protocol = protocol; ret->feed_id = feed_id; // copy string parameters carefully if( api_key ) { // duplicate the string ret->api_key = xi_str_dup( api_key ); XI_CHECK_MEMORY( ret->api_key ); } else { ret->api_key = 0; } return ret; err_handling: if( ret ) { XI_SAFE_FREE( ret ); } return 0; }
xi_context_t* xi_create_context( xi_protocol_t protocol, const char* api_key , xi_feed_id_t feed_id ) { // allocate the structure to store new context xi_context_t* ret = ( xi_context_t* ) xi_alloc( sizeof( xi_context_t ) ); XI_CHECK_MEMORY( ret ); // copy given numeric parameters as is ret->protocol = protocol; ret->feed_id = feed_id; // copy string parameters carefully if( api_key ) { // duplicate the string ret->api_key = xi_str_dup( api_key ); XI_CHECK_MEMORY( ret->api_key ); } else { ret->api_key = 0; } switch( protocol ) { case XI_HTTP: { // @TODO make a configurable pool of these // static structures allocated statically static http_layer_data_t http_layer_data; static csv_layer_data_t csv_layer_data; static xi_response_t xi_response; // clean the structures memset( &http_layer_data, 0, sizeof( http_layer_data_t ) ); memset( &csv_layer_data, 0, sizeof( csv_layer_data_t ) ); memset( &xi_response, 0, sizeof( xi_response_t ) ); // the response pointer http_layer_data.response = &xi_response; csv_layer_data.response = &xi_response; // prepare user data description void* user_datas[] = { 0, ( void* ) &http_layer_data, ( void* ) &csv_layer_data }; // create and connect layers store the information in layer_chain member ret->layer_chain = create_and_connect_layers( CONNECTION_SCHEME_1, user_datas, CONNECTION_SCHEME_LENGTH( CONNECTION_SCHEME_1 ) ); } break; default: goto err_handling; } return ret; err_handling: if( ret ) { if( ret->api_key ) { XI_SAFE_FREE( ret->api_key ); } XI_SAFE_FREE( ret ); } return 0; }