Line data Source code
1 : #ifndef OPENDDS_DCPS_SAFETYPROFILESTREAMS_H 2 : #define OPENDDS_DCPS_SAFETYPROFILESTREAMS_H 3 : 4 : #include "dcps_export.h" 5 : #include "PoolAllocator.h" 6 : 7 : #include <ace/INET_Addr.h> 8 : #include <ace/OS_NS_stdio.h> 9 : 10 : #ifdef OPENDDS_SAFETY_PROFILE 11 : # include <cstdlib> // For strto* 12 : #else 13 : # include <fstream> 14 : # include <iostream> 15 : # include <iomanip> 16 : # include <sstream> 17 : #endif 18 : 19 : OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL 20 : 21 : namespace OpenDDS { 22 : namespace DCPS { 23 : 24 : OpenDDS_Dcps_Export String to_dds_string(unsigned short to_convert); 25 : OpenDDS_Dcps_Export String to_dds_string(int to_convert); 26 : OpenDDS_Dcps_Export String to_dds_string(unsigned int to_convert, bool as_hex = false); 27 : OpenDDS_Dcps_Export String to_dds_string(long to_convert); 28 : OpenDDS_Dcps_Export String to_dds_string(long long to_convert); 29 : OpenDDS_Dcps_Export String to_dds_string(unsigned long long to_convert, bool as_hex = false); 30 : OpenDDS_Dcps_Export String to_dds_string(unsigned long to_convert, bool as_hex = false); 31 : OpenDDS_Dcps_Export String to_dds_string(const unsigned char* array, size_t length); 32 : // Pass-through for conditional compilation situtations, i.e., type may be an integer or string. 33 : inline String to_dds_string(const String& to_convert) 34 : { 35 : return to_convert; 36 : } 37 : 38 : //@{ 39 : /** 40 : * Converts a series of bytes at data to a optionally delimited String 41 : * of hexadecimal numbers. 42 : * 43 : * If delim is '\0' (the default) or delim_every is 0, then the output will not 44 : * be delimited. 45 : */ 46 : OpenDDS_Dcps_Export String to_hex_dds_string( 47 : const unsigned char* data, size_t size, char delim = '\0', size_t delim_every = 1); 48 : OpenDDS_Dcps_Export String to_hex_dds_string( 49 : const char* data, size_t size, char delim = '\0', size_t delim_every = 1); 50 : //@} 51 : 52 : /// Convert Pointer to String 53 : template <typename T> 54 : inline 55 0 : String to_dds_string(const T* to_convert) 56 : { 57 0 : const char* fmt = "%p"; 58 0 : const int buff_size = 20 + 1; // note +1 for null terminator 59 : char buf[buff_size]; 60 0 : ACE_OS::snprintf(&buf[0], buff_size, fmt, to_convert); 61 0 : return String(buf); 62 : } 63 : 64 : /** 65 : * Convert string s to value of integral type T. 66 : * 67 : * Returns true for success, false for error 68 : */ 69 : template <typename T> 70 2 : bool convertToInteger(const String& s, T& value) 71 : { 72 : #ifdef OPENDDS_SAFETY_PROFILE 73 : char* end; 74 : const long conv = std::strtol(s.c_str(), &end, 10); 75 : if (end == s.c_str()) return false; 76 : value = static_cast<T>(conv); 77 : #else 78 4 : std::stringstream istr(s.c_str()); 79 2 : if (!(istr >> value) || (istr.peek() != EOF)) return false; 80 : #endif 81 2 : return true; 82 2 : } 83 : 84 : /** 85 : * Convert string s to value of double type T. 86 : * 87 : * Returns true for success, false for error 88 : */ 89 : template <typename T> 90 : bool convertToDouble(const String& s, T& value) 91 : { 92 : #ifdef OPENDDS_SAFETY_PROFILE 93 : char* end; 94 : const double conv = std::strtod(s.c_str(), &end); 95 : if (end == s.c_str()) return false; 96 : value = static_cast<T>(conv); 97 : #else 98 : std::stringstream istr(s.c_str()); 99 : if (!(istr >> value) || (istr.peek() != EOF)) return false; 100 : #endif 101 : return true; 102 : } 103 : 104 : } // namespace DCPS 105 : } // namespace OpenDDS 106 : 107 : OPENDDS_END_VERSIONED_NAMESPACE_DECL 108 : 109 : #endif // OPENDDS_DCPS_SAFETYPROFILESTREAMS_H