static void process_data_from_server() { /* enough data present for the length field ? */ if (3 <= in_data_used) { Uint8 *pData = in_data; Uint16 size; do { /* while (3 <= in_data_used) (enough data present for the length field) */ static const int foo = 1; /* used for run-time byteorder check */ /* make a copy of the length field...watch alignment/byteorder */ if (*(char *)&foo) { /* little-endian ? */ ((Uint8 *)&size)[0] = pData[1]; ((Uint8 *)&size)[1] = pData[2]; } else { /* big-endian */ ((Uint8 *)&size)[0] = pData[2]; ((Uint8 *)&size)[1] = pData[1]; } if (sizeof (in_data) - 3 >= size) { /* buffer big enough ? */ size += 2; /* add length field size */ if (size <= in_data_used) { /* do we have a complete message ? */ process_message_from_server(pData, size); if (log_conn_data) log_conn(pData, size); /* advance to next message */ pData += size; in_data_used -= size; } else break; } else { /* sizeof (in_data) - 3 < size */ log_to_console(c_red2, packet_overrun); log_to_console(c_red2, disconnected_from_server); log_to_console(c_red2, alt_x_quit); in_data_used = 0; disconnected = 1; // clear buddy list clear_buddy(); } } while (3 <= in_data_used); /* move the remaining data to the start of the buffer...(not tested...never happened to me) */ if (in_data_used && pData != in_data) memmove(in_data, pData, in_data_used); } }
void change_vertex_array(int value) { use_vertex_array=(value>0); if(use_vertex_array) { log_to_console(c_green2,enabled_vertex_arrays); } }
void change_point_particles(int value) { use_point_particles=(value>0); if(!use_point_particles) { log_to_console(c_green2,disabled_point_particles); } }
void change_particles_percentage(int value) { if(value>0 && value <=100) particles_percentage=value; else { particles_percentage=0; log_to_console(c_green2,disabled_particles_str); } }
void switch_vidmode(int mode) { if(mode>10 || mode<=0) { //warn about this error log_to_console(c_red2,invalid_video_mode); return; } else video_mode=mode; if(!video_mode_set) return; set_new_video_mode(full_screen,video_mode); }
void get_message_from_server() { /* data available for reading ? */ if (!disconnected && SDLNet_CheckSockets(set, 0) && SDLNet_SocketReady(my_socket)) { int received; if (0 < (received = SDLNet_TCP_Recv(my_socket, &in_data[in_data_used], sizeof (in_data) - in_data_used))) { in_data_used += received; process_data_from_server(); } else { /* 0 >= received (EOF or some error) */ if (received) log_to_console(c_red2, SDLNet_GetError()); //XXX: SDL[Net]_GetError used by timer thread ? i bet its not reentrant... log_to_console(c_red2, disconnected_from_server); log_to_console(c_red2, alt_x_quit); in_data_used = 0; disconnected = 1; // clear buddy list clear_buddy(); } } }
void update_all_actors() { Uint8 str[40]; #ifdef EXTRA_DEBUG ERR(); #endif //we got a nasty error, log it log_to_console(c_red2,resync_server); destroy_all_actors(); str[0]=SEND_ME_MY_ACTORS; my_tcp_send(my_socket,str,1); }
internal bool32 al_log(bool32 error, const char *message, ...) { va_list argptr; va_start(argptr, message); log_to_file(AL_LOG_FILE, message, argptr); va_end(argptr); if (error) { va_start(argptr, message); log_to_console(AL_LOG_FILE, message, argptr); va_end(argptr); } return true; }
void connect_to_server() { IPaddress ip; if(this_version_is_invalid)return; if(set) { SDLNet_FreeSocketSet(set); set=0; } if(my_socket) { SDLNet_TCP_Close(my_socket); my_socket=0; } log_to_console(c_red1,connect_to_server_str); draw_scene(); // update the screen set=SDLNet_AllocSocketSet(1); if(!set) { char str[120]; sprintf(str,"SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); log_error(str); SDLNet_Quit(); SDL_Quit(); exit(4); //most of the time this is a major error, but do what you want. } if(SDLNet_ResolveHost(&ip,server_address,port)==-1) { log_to_console(c_red2,failed_resolve); return; } my_socket=SDLNet_TCP_Open(&ip); if(!my_socket) { //check to see if the player is a moron... if(server_address[0]=='1' && server_address[1]=='9' && server_address[2]=='2' && server_address[3]=='.' && server_address[4]=='1' && server_address[5]=='6' && server_address[6]=='8') { log_to_console(c_red1,license_check); log_to_console(c_red1,alt_x_quit); } else { log_to_console(c_red1,failed_connect); log_to_console(c_red1,reconnect_str); log_to_console(c_red1,alt_x_quit); } return; } if(SDLNet_TCP_AddSocket(set,my_socket)==-1) { char str[120]; sprintf(str,"SDLNet_TCP_AddSocket: %s\n",SDLNet_GetError()); log_error(str); SDLNet_Quit(); SDL_Quit(); exit(2); } disconnected=0; //ask for the opening screen if(!previously_logged_in) { Uint8 str[1]; str[0]=SEND_OPENING_SCREEN; my_tcp_send(my_socket,str,1); } else { yourself=-1; you_sit=0; destroy_all_actors(); send_login_info(); } //send the current version to the server send_version_to_server(&ip); last_heart_beat=cur_time; hide_window(trade_win); }
void process_message_from_server(unsigned char *in_data, int data_lenght) { //see what kind of data we got switch (in_data[PROTOCOL]) { case RAW_TEXT: { // do filtering and ignoring data_lenght=filter_or_ignore_text(&in_data[3],data_lenght-3)+3; if(data_lenght > 3) { //how to display it if(interface_mode!=interface_opening) put_text_in_buffer(&in_data[3],data_lenght-3,0); else put_text_in_buffer(&in_data[3],data_lenght-3,54); //lets log it write_to_log(&in_data[3],data_lenght-3); } } break; case SMALL_WINDOW_TEXT: { add_text_to_small_text_buffer(in_data+3, data_lenght-3); display_small_text_window(); } break; case ADD_NEW_ACTOR: { add_actor_from_server(&in_data[3]); } break; case ADD_NEW_ENHANCED_ACTOR: { add_enhanced_actor_from_server(&in_data[3]); } break; case ADD_ACTOR_COMMAND: { add_command_to_actor(*((short *)(in_data+3)),in_data[5]); } break; case REMOVE_ACTOR: { destroy_actor(*((short *)(in_data+3))); } break; case KILL_ALL_ACTORS: { destroy_all_actors(); } break; case NEW_MINUTE: { game_minute=*((short *)(in_data+3)); new_minute(); } break; case LOG_IN_OK: { interface_mode=interface_game; previously_logged_in=1; } break; case HERE_YOUR_STATS: { get_the_stats((Sint16 *)(in_data+3)); } break; case SEND_PARTIAL_STAT: { get_partial_stat(*((Uint8 *)(in_data+3)),*((Sint32 *)(in_data+4))); } break; case GET_KNOWLEDGE_LIST: { get_knowledge_list(*(Uint16 *)(in_data+1)-1, in_data+3); } break; case GET_NEW_KNOWLEDGE: { get_new_knowledge(*(Uint16 *)(in_data+3)); } break; case HERE_YOUR_INVENTORY: { get_your_items(in_data+3); } break; case GET_NEW_INVENTORY_ITEM: { get_new_inventory_item(in_data+3); } break; case REMOVE_ITEM_FROM_INVENTORY: { remove_item_from_inventory(*((Uint8 *)(in_data+3))); } break; case INVENTORY_ITEM_TEXT: { put_small_text_in_box(&in_data[3],data_lenght-3,6*51+100,items_string); if(!(get_show_window(items_win)||get_show_window(trade_win))) { put_text_in_buffer(&in_data[3],data_lenght-3,0); } } break; case GET_KNOWLEDGE_TEXT: { put_small_text_in_box(&in_data[3],data_lenght-3,6*51+150,knowledge_string); } break; case CHANGE_MAP: { current_sector=-1; if(map_file_name[0]!=0) save_map(map_file_name); object_under_mouse=-1;//to prevent a nasty crash, while looking for bags, when we change the map close_dialogue(); // close the dialogue window if open destroy_all_particles(); if(!load_map(&in_data[4])){ // creating map if it does not exist int size=(in_data[3]&0x1f)<<4; new_map(size,size); dungeon=(in_data[3]&0x20)?1:0; strcpy(map_file_name,&in_data[4]); save_map(map_file_name); } kill_local_sounds(); #ifndef NO_MUSIC playing_music=0; #endif //NO_MUSIC get_map_playlist(); have_a_map=1; //also, stop the rain seconds_till_rain_starts=-1; seconds_till_rain_stops=-1; is_raining=0; rain_sound=0;//kill local sounds also kills the rain sound weather_light_offset=0; rain_light_offset=0; } break; case GET_TELEPORTERS_LIST: { add_teleporters_from_list(&in_data[3]); } break; case PLAY_MUSIC: { if(!no_sound)play_music(*((short *)(in_data+3))); } break; case PLAY_SOUND: { if(!no_sound)add_sound_object(*((short *)(in_data+3)),*((short *)(in_data+5)),*((short *)(in_data+7)),*((char *)(in_data+9)),*((short *)(in_data+10))); } break; case TELEPORT_OUT: { add_particle_sys_at_tile("./particles/teleport_in.part",*((short *)(in_data+3)),*((short *)(in_data+5))); if(!no_sound)add_sound_object(snd_tele_out,*((short *)(in_data+3)),*((short *)(in_data+5)),1,0); } break; case TELEPORT_IN: { add_particle_sys_at_tile("./particles/teleport_in.part",*((short *)(in_data+3)),*((short *)(in_data+5))); if(!no_sound)add_sound_object(snd_tele_in,*((short *)(in_data+3)),*((short *)(in_data+5)),1,0); } break; case LOG_IN_NOT_OK: { sprintf(log_in_error_str,"%s: %s",reg_error_str,invalid_pass); } break; case REDEFINE_YOUR_COLORS: { strcpy(log_in_error_str,redefine_your_colours); } break; case YOU_DONT_EXIST: { sprintf(log_in_error_str,"%s: %s",reg_error_str,char_dont_exist); } break; case CREATE_CHAR_NOT_OK: { sprintf(create_char_error_str,"%s: %s",reg_error_str,char_name_in_use); return; } break; case CREATE_CHAR_OK: { login_from_new_char(); } break; case YOU_ARE: { yourself=*((short *)(in_data+3)); } break; case START_RAIN: { seconds_till_rain_starts=*((Uint8 *)(in_data+3)); seconds_till_rain_stops=-1; } break; case STOP_RAIN: { seconds_till_rain_stops=*((Uint8 *)(in_data+3)); seconds_till_rain_starts=-1; } break; case THUNDER: { add_thunder(rand()%5,*((Uint8 *)(in_data+3))); } break; case SYNC_CLOCK: { server_time_stamp=*((int *)(in_data+3)); client_time_stamp=SDL_GetTicks(); client_server_delta_time=server_time_stamp-client_time_stamp; } break; case PONG: { Uint8 str[160]; sprintf(str,"%s: %i MS",server_latency, SDL_GetTicks()-*((Uint32 *)(in_data+3))); log_to_console(c_green1,str); } break; case UPGRADE_NEW_VERSION: { log_to_console(c_red1,update_your_client); log_to_console(c_red1,(char*)web_update_address); } break; case UPGRADE_TOO_OLD: { log_to_console(c_red1,client_ver_not_supported); log_to_console(c_red1,(char*)web_update_address); this_version_is_invalid=1; } break; case GET_NEW_BAG: { put_bag_on_ground(*((Uint16 *)(in_data+3)),*((Uint16 *)(in_data+5)),*((Uint8 *)(in_data+7))); } break; case GET_BAGS_LIST: { add_bags_from_list(&in_data[3]); } break; case SPAWN_BAG_PARTICLES: { add_particle_sys_at_tile("./particles/bag_in.part",*((Uint16 *)(in_data+3)),*((Uint16 *)(in_data+5))); } break; case GET_NEW_GROUND_ITEM: { get_bag_item(in_data+3); } break; case HERE_YOUR_GROUND_ITEMS: { get_bags_items_list(&in_data[3]); } break; case CLOSE_BAG: { hide_window(ground_items_win); } break; case REMOVE_ITEM_FROM_GROUND: { remove_item_from_ground(in_data[3]); } break; case DESTROY_BAG: { remove_bag(in_data[3]); } break; case NPC_TEXT: { put_small_text_in_box(&in_data[3],data_lenght-3,dialogue_menu_x_len-70,dialogue_string); display_dialogue(); if(in_data[3]>=127 && in_data[4]>=127) { add_questlog(&in_data[4],data_lenght-4); } } break; case SEND_NPC_INFO: { my_strcp(npc_name,&in_data[3]); cur_portrait=in_data[23]; } break; case NPC_OPTIONS_LIST: { build_response_entries(&in_data[3],*((Uint16 *)(in_data+1))); } break; case GET_TRADE_ACCEPT: { if(!in_data[3])trade_you_accepted=1; else trade_other_accepted=1; } break; case GET_TRADE_REJECT: { if(!in_data[3])trade_you_accepted=0; else trade_other_accepted=0; } break; case GET_TRADE_EXIT: { hide_window(trade_win); } break; case GET_YOUR_TRADEOBJECTS: { get_your_trade_objects(in_data+3); } break; case GET_TRADE_OBJECT: { put_item_on_trade(in_data+3); } break; case REMOVE_TRADE_OBJECT: { remove_item_from_trade(in_data+3); } break; case GET_TRADE_PARTNER_NAME: { get_trade_partner_name(&in_data[3],*((Uint16 *)(in_data+1))-1); } break; case GET_ACTOR_DAMAGE: { get_actor_damage(*((Uint16 *)(in_data+3)),in_data[5]); } break; case GET_ACTOR_HEAL: { get_actor_heal(*((Uint16 *)(in_data+3)),in_data[5]); } break; case ACTOR_UNWEAR_ITEM: { unwear_item_from_actor(*((Uint16 *)(in_data+3)),in_data[5]); } break; case ACTOR_WEAR_ITEM: { actor_wear_item(*((Uint16 *)(in_data+3)),in_data[5],in_data[6]); } break; case NPC_SAY_OVERTEXT: { add_displayed_text_to_actor( get_actor_ptr_from_id( *((Uint16 *)(in_data+3)) ), in_data+5 ); } break; case BUDDY_EVENT: { if(in_data[3]==1) add_buddy(&in_data[5],in_data[4],data_lenght-5); else if(in_data[3]==0) del_buddy(&in_data[4],data_lenght-4); } break; // BARREN MOON NEW MESSAGES case THIS_IS_ACTIVE_SECTOR: active_sector=*((Uint16 *)(in_data+3)); break; case GET_TILE_DATA: get_tile_data(in_data+3); break; case GET_3D_OBJECTS: get_3d_objects(in_data+3); break; case GET_2D_OBJECTS: get_2d_objects(in_data+3); break; case GET_LIGHT_OBJECTS: get_light_objects(in_data+3); break; case GET_PARTICLE_OBJECTS: get_particle_objects(in_data+3); break; case GET_3D_OBJECTS_FULL_ROTATION: get_3d_objects_full_rotation(in_data+3); break; case GET_CHECKSUMS: { actor *actor=pf_get_our_actor(); get_checksums(in_data+3, sector_get(actor->x_pos,actor->y_pos)); break; } case ADD_3D_OBJECT: add_3d_object(in_data+3); break; case ADD_3D_OBJECT_FULL_ROTATION: add_3d_object_fullrotation(in_data+3); break; case DELETE_3D_OBJECT: delete_3d_object(in_data+3); break; case REPLACE_3D_OBJECT: replace_3d_object(in_data+3); break; case ADD_2D_OBJECT: add_2d_object(in_data+3); break; case DELETE_2D_OBJECT: delete_2d_object(in_data+3); break; case REPLACE_2D_OBJECT: replace_2d_object(in_data+3); break; case ADD_LIGHT: add_lights(in_data+3); break; case DELETE_LIGHT: delete_light(in_data+3); break; case ADD_PARTICLE: add_particle(in_data+3); break; case DELETE_PARTICLE: delete_particle(in_data+3); break; case REPLACE_PARTICLE: replace_particle(in_data+3); break; default: { /* Unknown data type?? */; } break; } }
/** * Example using STL adaptors */ void STLMoblet::STL_adaptors() { LOG("\n"); LOG("========================= STL adaptors =========================================================================="); LOG("/**"); LOG("* Function object adaptors are used to create a function object from another function object." ); LOG("* The created function object, is not the same as the original functor, but is adapted to a certain need." ); LOG("* For example if we have a function object like std::less, and we want to compare all the elements" ); LOG("* in a range against 10, then be can use an STL adaptor (bind2nd), that bounds the second argument" ); LOG("* of std::less to 10. Then we can use std::less with algorithms like remove_copy_if," ); LOG("* that need a unary predicate." ); LOG("*" ); LOG("* STL provides two functor adaptors: bind1st and bind2nd." ); LOG("*" ); LOG("* bind1st: constructs an unary function object from a binary function object, by binding" ); LOG("* the first argument to a fixed value." ); LOG("*" ); LOG("* bind1st template function is defined like this:" ); LOG("*" ); LOG("* binder1st<SomeFunctor> bind1st (const SomeFunctor& fun, const T& fixedValue" ); LOG("* {" ); LOG("* return binder1st<SomeFunctor>(fun, x);" ); LOG("* }" ); LOG("*" ); LOG("* bind1st returns an binder1st object, which is actually a functor that forwards the function calls" ); LOG("* to the \"fun\" argument it takes as a parameter, when constructed:" ); LOG("*" ); LOG("* template <class Functor> class binder1st {" ); LOG("*" ); LOG("* binder1st(const Functor &fun, Functor::first_argument_type &fixed)" ); LOG("* {" ); LOG("* mFun = fun;" ); LOG("* mFixedValue = fixed;" ); LOG("* }" ); LOG("*" ); LOG("* Functor::result_type operator()(Functor::second_argument_type &someValue){" ); LOG("* return mFun(mFixedValue, someValue);" ); LOG("* }" ); LOG("* };" ); LOG("*" ); LOG("* bind2nd is implemented in a similar way, but instead of binding the first argument," ); LOG("* bind2nd it will bind the second one to a fixed value." ); LOG("*" ); LOG("* bind1st and bind2nd function templates are defined in the <functional> header." ); LOG("*/"); LOG("\n"); LOG(" Example using adaptors "); LOG("\n "); LOG("/**" ); LOG("* bind1st function template: constructs an unary function object from a" ); LOG("* binary function object, by binding the first parameter to a certain value." ); LOG("\n */" ); log_to_console("\n Example using std::bind1st:\n"); int array[] = { 1, -99, 2, -100 }; int arraySize = sizeof(array)/sizeof(array[0]); std::vector<int> vec1(array, array + arraySize); log_to_console(vec1, "vec1 contains: "); LOG("\n" ); LOG("/**std::remove_if calls std::less(0, element). If less(0,element) returns" ); LOG("* true => removes that element." ); LOG("* less(0,element) is equivalent to 0<element." ); LOG("*/" ); LOG("\n" ); TRACE(std::vector<int>::iterator newEnd = std::remove_if(vec1.begin(), vec1.end(), bind1st(std::less<int>(), 1))); log_to_console("vec1 after calling std::remove_if(vec1.begin(), vec1.end(), " "bind1st(std::less<int>(), 0)): "); for(std::vector<int>::iterator it = vec1.begin(); it != newEnd; ++it) { log_to_console(*it); } LOG("\n" ); LOG("/**" ); LOG("* bind2nd function template: constructs an unary function object from a"); LOG("* binary function object, by binding the second parameter to a certain" ); LOG("* value." ); LOG("*/" ); log_to_console("\n Example using std::bind2nd:\n"); std::vector<int> vec2(array, array + arraySize); log_to_console(vec2, "vec2 contains: "); LOG("\n" ); LOG("/** std::remove_if calls std::greater(element, 0)." ); LOG("* If std::greater(element, 0) returns true => removes that element." ); LOG("* std::greater(element, 0) is equivalent to element > 0" ); LOG("*/" ); LOG("\n"); TRACE(newEnd = std::remove_if(vec2.begin(), vec2.end(), bind2nd(std::greater<int>(), 0))); log_to_console("vec2 after calling: std::remove_if(vec2.begin(), vec2.end(), " "bind2nd(std::greater<int>(), 0));"); for(std::vector<int>::iterator it = vec2.begin(); it != newEnd; ++it) { log_to_console(*it); } LOG("\n"); }
void STLMoblet::STL_iterators() { LOG("========================= STL iterators ========================================================================"); LOG("/**"); LOG("* Input iterators: allow only reading elements from a sequence and moving forward, one step,"); LOG("* using operator++. The elements are read with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::istream."); LOG("*/"); LOG("\n"); LOG("/**"); LOG("* Output iterators: allow only writing elements from a sequence and only moving forward, one step,"); LOG("* using operator++. They elements are written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::ostream."); LOG("*/"); LOG("\n"); LOG("/**"); LOG("* Forward iterators: allow reading and writing elements from a sequence. Only moving forward, one step,"); LOG("* using operator++ is possible. They elements are read and written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the one provided by std::ostream."); LOG("*/"); LOG("/**"); LOG("* Bidirectional iterators: allow reading and writing elements from a sequence. Allows moving forward and backward,"); LOG("* one step,using operator++/operator--. Their elements are read and written with operator*."); LOG("* Provides also operator== and operator!=."); LOG("* An input iterator implemented in STL is for example the ones provided by list, multiset, map, multimap."); LOG("*/"); LOG("\n"); LOG(" Example using iterators "); int sArray[] = { 0, 11, 22, 33, 44 }; int sArraySize = sizeof(sArray)/sizeof(sArray[0]); std::list<int> myList(sArray, sArray + sArraySize); LOG("Create a list object (myList) and fill it with: 0, 11, 22, 33, 44\n"); LOG("\n"); LOG("/**"); LOG("* create an bidirectional iterator"); LOG("*/"); LOG("std::list<int>::iterator iter;"); std::list<int>::iterator iter; iter = myList.begin(); LOG("iter = myList.begin();"); LOG("\n"); LOG("/**"); LOG("* read operation"); LOG("*/"); int r = *iter; log_to_console(r, "int r = *iter //r = "); LOG("\n"); LOG("/**"); LOG("* write operation"); LOG("*/"); *iter = 99; LOG("*iter = 99; //myList will contain now: 99, 11, 22, 33, 44"); LOG("\n"); LOG("/**"); LOG("* operator++"); LOG("*/"); iter++; log_to_console(*iter, "iter++; //*iter is now: "); LOG("\n"); LOG("/**"); LOG("* operator--"); LOG("*/"); --iter; log_to_console(*iter, "iter--; //*iter is now: "); LOG("\n"); LOG("/**"); LOG("* Random access iterators: allows all operations a normal pointer does: add and subtract integral values,"); LOG("* move forward and backward, one or more steps, use operator[] on it, subtract one iterator from another."); LOG("* It overloads operator<, operator>, operator==, operator!=."); LOG("* An input iterator implemented in STL is for example the ones provided by vector, deque, string."); LOG("*/"); LOG("\n"); LOG("Create vector object (myVector) and fill with: 0, 11, 22, 33, 44"); std::vector<int> myVector(sArray, sArray + sArraySize); //myVector contains now: 0, 11, 22, 33, 44 LOG("\n"); LOG("/**"); LOG("* create an random iterator"); LOG("*/"); std::vector<int>::iterator randomAcessIter; randomAcessIter = myVector.begin(); LOG("std::vector<int>::iterator randomAcessIter;"); LOG("randomAcessIter = myVector.begin();"); LOG("\n"); LOG("/**"); LOG("* read operation"); LOG("*/"); r = *randomAcessIter; log_to_console(r, "r = *randomAcessIter; //r = "); LOG("\n"); LOG("/**"); LOG("* write operation"); LOG("*/"); *randomAcessIter = 99; log_to_console(myVector, "*randomAcessIter = 99;//myVector will contain now: "); LOG("\n"); LOG("/**"); LOG("* operator++"); LOG("*/"); randomAcessIter++; log_to_console(*randomAcessIter, "randomAcessIter++; //*randomAcessIter = "); LOG("\n"); LOG("/**"); LOG("* operator--"); LOG("*/"); --randomAcessIter; log_to_console(*randomAcessIter, "randomAcessIter--; //*randomAcessIter = "); LOG("\n"); LOG("/**"); LOG("* move two steps forward"); LOG("*/"); LOG("\n"); randomAcessIter += 2; log_to_console(*randomAcessIter, "randomAcessIter +=2; //*randomAcessIter = "); LOG("\n"); }
int check_var(char * str, int type) { int i,*p; char * ptr=str; float foo; for(i=0;i<our_vars.no;i++) { if(type?!strncmp(str,our_vars.var[i]->name,our_vars.var[i]->nlen):!strncmp(str,our_vars.var[i]->shortname,our_vars.var[i]->snlen)) { //Allright, it's the right variable... now move ptr forward ptr+=type?our_vars.var[i]->nlen:our_vars.var[i]->snlen; while(*ptr && (*ptr==' '||*ptr=='='))ptr++;//go to the string occurence if(!*ptr||*ptr=='\n')return -1;//hmm, why would you do such a stupid thing? if(*ptr=='"') { //Accurate quoting char *tptr=++ptr; while(*tptr && *tptr!='"') { if(*tptr==0x0a||*tptr==0x0d) { #ifdef ELC char str[200]; snprintf(str,200,"Reached newline without an ending \" in %s",our_vars.var[i]->name); log_to_console(c_red2,str); #endif break; } tptr++; } *tptr=0; } else { //Strip it char our_string[200]; char *tptr=our_string; while(*ptr && *ptr!=0x0a && *ptr!=0x0d) { if(*ptr!=' ')*tptr++=*ptr++; //Strip all spaces else ptr++; } *tptr=0; ptr=our_string; } switch(our_vars.var[i]->type) { case SPECINT: our_vars.var[i]->func(atoi(ptr)); return 1; case SPECCHAR: our_vars.var[i]->func(ptr); return 1; case INT: our_vars.var[i]->func(our_vars.var[i]->var,atoi(ptr)); return 1; case SPEC: p=our_vars.var[i]->var; if(*p!=atoi(ptr))our_vars.var[i]->func();//the variable has changed return 1; case BOOL: p=our_vars.var[i]->var; if((atoi(ptr)>0)!=*p) our_vars.var[i]->func(our_vars.var[i]->var); return 1; case STRING: our_vars.var[i]->func(our_vars.var[i]->var,ptr,our_vars.var[i]->len); return 1; case FLOAT: foo=atof(ptr); our_vars.var[i]->func(our_vars.var[i]->var,&foo); return 1; } } } return -1;//no variable was found }
/** * Function for illustrating predicates */ void STLMoblet::predicates_explained() { LOG("\n"); LOG("========================= about predicates ======================================================================"); LOG("/**"); LOG("* A predicate is a function that returns a boolean value based on some"); LOG("* comparison criterion."); LOG("* Predicates are used usually with STL algorithms."); LOG("* Binary predicates - predicates that take two arguments."); LOG("* Unary predicates - predicates taking one argument."); LOG("* Usually binary predicates are used with algorithms that need to compare two"); LOG("* elements (e.g sorting algorithms)."); LOG("*/"); LOG("\n"); LOG(" Example using predicates: "); LOG("\n Unary predicate example..."); int array[] = { 0, 1, 2, 3, 4}; int sizeArray = sizeof(array)/sizeof(array[0]); std::vector<int> myVector(array, array + sizeArray); //fill a vector log_to_console(myVector, "myVector contains: "); LOG("/**"); LOG("* std::countf_if algorithm: counts the number of elements that satisfy the"); LOG("* condition we supply (e.g isEven, isNegative)"); LOG("*/"); log_to_console("Calling: std::count_if(myVector.begin(), myVector.end(), " "isEven);"); int res = std::count_if(myVector.begin(), myVector.end(), NumericUtilities::isEven); //returns the how many numbers are even log_to_console(res, "int res = std::count_if(myVector.begin(), myVector.end(), " "isEven); //res = "); log_to_console("\n Binary predicate example..."); Employee staff[] = { Employee(3, "Jonny"), Employee(2, "Bob"), Employee(20, "Filomela")}; int staffSize = sizeof(staff)/sizeof(staff[0]); std::vector<Employee> myStaff( staff, staff + staffSize); log_to_console(myStaff, "myStaff contains: "); LOG("\n"); LOG("/**"); LOG("* std::sort algorithm: sorts the elements, from a range, into ascending"); LOG("* order"); LOG("* lessExperience - a binary predicate for comparing employees"); LOG("*/"); LOG("\n"); TRACE(std::sort(myStaff.begin(), myStaff.end(), lessExperiencePredicate));; log_to_console(myStaff, "myStaff contains now: "); LOG("\n"); }