Changeset 418 for trunk/icedtea-web/plugin/icedteanp/IcedTeaPluginUtils.cc
- Timestamp:
- Feb 11, 2013, 8:53:47 PM (13 years ago)
- Location:
- trunk/icedtea-web
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/icedtea-web
-
Property svn:mergeinfo
set to
/branches/vendor/sourceforge/icedtea-web/1.3 merged eligible /branches/vendor/sourceforge/icedtea-web/current merged eligible
-
Property svn:mergeinfo
set to
-
trunk/icedtea-web/plugin/icedteanp/IcedTeaPluginUtils.cc
r375 r418 151 151 IcedTeaPluginUtilities::JSIDToString(void* id, std::string* result) 152 152 { 153 154 char* id_str = (char*) malloc(sizeof(char)*20); // max = long long = 8446744073709551615 == 19 chars 153 char id_str[NUM_STR_BUFFER_SIZE]; 155 154 156 155 if (sizeof(void*) == sizeof(long long)) 157 156 { 158 s printf(id_str, "%llu", id);157 snprintf(id_str, NUM_STR_BUFFER_SIZE, "%llu", id); 159 158 } 160 159 else 161 160 { 162 s printf(id_str, "%lu", id); // else use long161 snprintf(id_str, NUM_STR_BUFFER_SIZE, "%lu", id); // else use long 163 162 } 164 163 … … 166 165 167 166 PLUGIN_DEBUG("Converting pointer %p to %s\n", id, id_str); 168 free(id_str);169 167 } 170 168 … … 262 260 IcedTeaPluginUtilities::itoa(int i, std::string* result) 263 261 { 264 // largest possible integer is 10 digits long 265 char* int_str = (char*) malloc(sizeof(char)*11); 266 sprintf(int_str, "%d", i); 262 char int_str[NUM_STR_BUFFER_SIZE]; 263 snprintf(int_str, NUM_STR_BUFFER_SIZE, "%d", i); 267 264 result->append(int_str); 268 269 free(int_str);270 265 } 271 266 … … 297 292 * @param str The string to split 298 293 * @param The delimiters to split on 299 * @return A string vector containing the aplit components294 * @return A string vector containing the split components 300 295 */ 301 296 … … 307 302 char* copy; 308 303 309 // Tokeni ngis done on a copy304 // Tokenization is done on a copy 310 305 copy = (char*) malloc (sizeof(char)*strlen(str) + 1); 311 306 strcpy(copy, str); … … 318 313 // Allocation on heap since caller has no way to knowing how much will 319 314 // be needed. Make sure caller cleans up! 320 321 322 323 tok_ptr = strtok (NULL, " ");315 std::string* s = new std::string(); 316 s->append(tok_ptr); 317 v->push_back(s); 318 tok_ptr = strtok (NULL, delim); 324 319 } 320 free(copy); 325 321 326 322 return v; … … 376 372 ostream << length; 377 373 378 // UTF-8 characters are 4-bytes max + space + '\0' 379 char* hex_value = (char*) malloc(sizeof(char)*10); 374 char hex_value[NUM_STR_BUFFER_SIZE]; 380 375 381 376 for (int i = 0; i < str->length(); i++) 382 377 { 383 s printf(hex_value," %hx", str->at(i));378 snprintf(hex_value, NUM_STR_BUFFER_SIZE," %hx", str->at(i)); 384 379 ostream << hex_value; 385 380 } … … 388 383 *utf_str = ostream.str(); 389 384 390 free(hex_value);391 385 PLUGIN_DEBUG("Converted %s to UTF-8 string %s\n", str->c_str(), utf_str->c_str()); 392 386 } … … 673 667 else if (NPVARIANT_IS_STRING(variant)) 674 668 { 675 #if MOZILLA_VERSION_COLLAPSED < 1090200 676 PLUGIN_DEBUG("STRING: %s\n", NPVARIANT_TO_STRING(variant).utf8characters); 677 #else 678 PLUGIN_DEBUG("STRING: %s\n", NPVARIANT_TO_STRING(variant).UTF8Characters); 679 #endif 669 std::string str = IcedTeaPluginUtilities::NPVariantAsString(variant); 670 PLUGIN_DEBUG("STRING: %s (length=%d)\n", str.c_str(), str.size()); 680 671 } 681 672 else … … 688 679 IcedTeaPluginUtilities::NPVariantToString(NPVariant variant, std::string* result) 689 680 { 690 char* str = (char*) malloc(sizeof(char)*32); // enough for everything except string 691 692 if (NPVARIANT_IS_VOID(variant)) 693 { 694 sprintf(str, "%p", variant); 695 } 696 else if (NPVARIANT_IS_NULL(variant)) 697 { 698 sprintf(str, "NULL"); 699 } 700 else if (NPVARIANT_IS_BOOLEAN(variant)) 701 { 702 if (NPVARIANT_TO_BOOLEAN(variant)) 703 sprintf(str, "true"); 704 else 705 sprintf(str, "false"); 706 } 707 else if (NPVARIANT_IS_INT32(variant)) 708 { 709 sprintf(str, "%d", NPVARIANT_TO_INT32(variant)); 710 } 711 else if (NPVARIANT_IS_DOUBLE(variant)) 712 { 713 sprintf(str, "%f", NPVARIANT_TO_DOUBLE(variant));; 714 } 715 else if (NPVARIANT_IS_STRING(variant)) 716 { 717 free(str); 718 #if MOZILLA_VERSION_COLLAPSED < 1090200 719 str = (char*) malloc(sizeof(char)*NPVARIANT_TO_STRING(variant).utf8length); 720 sprintf(str, "%s", NPVARIANT_TO_STRING(variant).utf8characters); 721 #else 722 str = (char*) malloc(sizeof(char)*NPVARIANT_TO_STRING(variant).UTF8Length); 723 sprintf(str, "%s", NPVARIANT_TO_STRING(variant).UTF8Characters); 724 #endif 725 } 681 char conv_str[NUM_STR_BUFFER_SIZE]; // conversion buffer 682 bool was_string_already = false; 683 684 if (NPVARIANT_IS_STRING(variant)) 685 { 686 result->append(IcedTeaPluginUtilities::NPVariantAsString(variant)); 687 was_string_already = true; 688 } 689 else if (NPVARIANT_IS_VOID(variant)) 690 { 691 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%p", variant); 692 } 693 else if (NPVARIANT_IS_NULL(variant)) 694 { 695 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "NULL"); 696 } 697 else if (NPVARIANT_IS_BOOLEAN(variant)) 698 { 699 if (NPVARIANT_TO_BOOLEAN(variant)) 700 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "true"); 726 701 else 727 { 728 sprintf(str, "[Object %p]", variant); 729 } 730 731 result->append(str); 732 free(str); 702 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "false"); 703 } 704 else if (NPVARIANT_IS_INT32(variant)) 705 { 706 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%d", NPVARIANT_TO_INT32(variant)); 707 } 708 else if (NPVARIANT_IS_DOUBLE(variant)) 709 { 710 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "%f", NPVARIANT_TO_DOUBLE(variant)); 711 } 712 else 713 { 714 snprintf(conv_str, NUM_STR_BUFFER_SIZE, "[Object %p]", variant); 715 } 716 717 if (!was_string_already){ 718 result->append(conv_str); 719 } 733 720 } 734 721 … … 869 856 IcedTeaPluginUtilities::printNPVariant(constructor_str); 870 857 871 std::string constructor_name = std::string(); 872 873 #if MOZILLA_VERSION_COLLAPSED < 1090200 874 constructor_name.append(NPVARIANT_TO_STRING(constructor_str).utf8characters); 875 #else 876 constructor_name.append(NPVARIANT_TO_STRING(constructor_str).UTF8Characters); 877 #endif 858 std::string constructor_name = IcedTeaPluginUtilities::NPVariantAsString(constructor_str); 878 859 879 860 PLUGIN_DEBUG("Constructor for NPObject is %s\n", constructor_name.c_str()); … … 918 899 } 919 900 901 /* Copies a variant data type into a C++ string */ 902 std::string 903 IcedTeaPluginUtilities::NPVariantAsString(NPVariant variant) 904 { 905 #if MOZILLA_VERSION_COLLAPSED < 1090200 906 return std::string( 907 NPVARIANT_TO_STRING(variant).utf8characters, 908 NPVARIANT_TO_STRING(variant).utf8length); 909 #else 910 return std::string( 911 NPVARIANT_TO_STRING(variant).UTF8Characters, 912 NPVARIANT_TO_STRING(variant).UTF8Length); 913 #endif 914 } 920 915 921 916 /** … … 1081 1076 PLUGIN_DEBUG("Error: Unable to initialize message queue mutex: %d\n", ret); 1082 1077 1083 PLUGIN_DEBUG("Mutex s %p and %p initialized\n", &subscriber_mutex, &msg_queue_mutex);1078 PLUGIN_DEBUG("Mutexes %p and %p initialized\n", &subscriber_mutex, &msg_queue_mutex); 1084 1079 } 1085 1080 … … 1145 1140 MessageBus::post(const char* message) 1146 1141 { 1147 char* msg = (char*) malloc(sizeof(char)*strlen(message) + 1);1148 1142 bool message_consumed = false; 1149 1150 // consumer frees this memory1151 strcpy(msg, message);1152 1143 1153 1144 PLUGIN_DEBUG("Trying to lock %p...\n", &msg_queue_mutex); 1154 1145 pthread_mutex_lock(&subscriber_mutex); 1155 1146 1156 PLUGIN_DEBUG("Message %s received on bus. Notifying subscribers.\n", m sg);1147 PLUGIN_DEBUG("Message %s received on bus. Notifying subscribers.\n", message); 1157 1148 1158 1149 std::list<BusSubscriber*>::const_iterator i; 1159 1150 for( i = subscribers.begin(); i != subscribers.end() && !message_consumed; ++i ) { 1160 PLUGIN_DEBUG("Notifying subscriber %p of %s\n", *i, m sg);1161 message_consumed = ((BusSubscriber*) *i)->newMessageOnBus(m sg);1151 PLUGIN_DEBUG("Notifying subscriber %p of %s\n", *i, message); 1152 message_consumed = ((BusSubscriber*) *i)->newMessageOnBus(message); 1162 1153 } 1163 1154 … … 1165 1156 1166 1157 if (!message_consumed) 1167 PLUGIN_DEBUG("Warning: No consumer found for message %s\n", m sg);1158 PLUGIN_DEBUG("Warning: No consumer found for message %s\n", message); 1168 1159 1169 1160 PLUGIN_DEBUG("%p unlocked...\n", &msg_queue_mutex);
Note:
See TracChangeset
for help on using the changeset viewer.