| 786 | //------------------------------------------------------------------------------ |
| 787 | |
| 788 | vtkVariant ConvertStringToFloat(bool isBinary, const char* rawData) |
| 789 | { |
| 790 | if (isBinary) |
| 791 | { |
| 792 | // As of PostgreSQL version 8.3.0, libpq transmits a float in network |
| 793 | // byte order -- that is, it reinterprets the bits as an unsigned int |
| 794 | // and then transmits them that way. This... frightens me. It assumes |
| 795 | // that both sender and recipient use IEEE floats. Still, I'm not sure |
| 796 | // there's any other good way to do it. |
| 797 | unsigned int intResult; |
| 798 | ConvertFromNetworkOrder(intResult, rawData); |
| 799 | |
| 800 | // This is the idiom that libpq uses internally to convert between the |
| 801 | // two types. |
| 802 | union |
| 803 | { |
| 804 | unsigned int i; |
| 805 | float f; |
| 806 | } swap; |
| 807 | swap.i = intResult; |
| 808 | float floatResult = swap.f; |
| 809 | |
| 810 | return vtkVariant(floatResult); |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | std::string rawString(rawData); |
| 815 | float finalResult; |
| 816 | |
| 817 | // Catch NaN |
| 818 | if (rawData[0] == 'N' || rawData[0] == 'n') |
| 819 | { |
| 820 | if (std::numeric_limits<float>::has_quiet_NaN) |
| 821 | { |
| 822 | finalResult = std::numeric_limits<float>::quiet_NaN(); |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | // C99 defines a NAN macro. If it's there, that solves our problem. |
| 827 | #if defined(NAN) |
| 828 | finalResult = NAN; |
| 829 | #else |
| 830 | float zero = 0.0; |
| 831 | finalResult = zero / zero; |
| 832 | #endif |
| 833 | } |
| 834 | } |
| 835 | else if (rawString == "Infinity") |
| 836 | { |
| 837 | if (std::numeric_limits<float>::has_infinity) |
| 838 | { |
| 839 | finalResult = std::numeric_limits<float>::infinity(); |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | finalResult = VTK_FLOAT_MAX; |
| 844 | } |
| 845 | } |
no test coverage detected