]> Dogcows Code - chaz/yoink/blob - src/stlplus/strings/string_float.cpp
build system enhancements
[chaz/yoink] / src / stlplus / strings / string_float.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "string_float.hpp"
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14
15 namespace stlplus
16 {
17
18 // added as a local copy to break the dependency on the portability library
19 static std::string local_dformat(const char* format, ...) throw(std::invalid_argument)
20 {
21 std::string formatted;
22 va_list args;
23 va_start(args, format);
24 #ifdef MSWINDOWS
25 int length = 0;
26 char* buffer = 0;
27 for(int buffer_length = 256; ; buffer_length*=2)
28 {
29 buffer = (char*)malloc(buffer_length);
30 if (!buffer) throw std::invalid_argument("string_float");
31 length = _vsnprintf(buffer, buffer_length-1, format, args);
32 if (length >= 0)
33 {
34 buffer[length] = 0;
35 formatted += std::string(buffer);
36 free(buffer);
37 break;
38 }
39 free(buffer);
40 }
41 #else
42 char* buffer = 0;
43 int length = vasprintf(&buffer, format, args);
44 if (!buffer) throw std::invalid_argument("string_float");
45 if (length >= 0)
46 formatted += std::string(buffer);
47 free(buffer);
48 #endif
49 va_end(args);
50 if (length < 0) throw std::invalid_argument("string_float");
51 return formatted;
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55 // floating-point types
56
57 std::string float_to_string(float f, real_display_t display, unsigned width, unsigned precision)
58 throw(std::invalid_argument)
59 {
60 return double_to_string((double)f, display, width, precision);
61 }
62
63 std::string double_to_string(double f, real_display_t display, unsigned width, unsigned precision)
64 throw(std::invalid_argument)
65 {
66 switch(display)
67 {
68 case display_fixed:
69 return local_dformat("%*.*f", width, precision, f);
70 case display_floating:
71 return local_dformat("%*.*e", width, precision, f);
72 case display_mixed:
73 return local_dformat("%*.*g", width, precision, f);
74 default:
75 throw std::invalid_argument("invalid radix display value");
76 }
77 }
78
79 ////////////////////////////////////////////////////////////////////////////////
80
81 float string_to_float(const std::string& value)
82 throw(std::invalid_argument)
83 {
84 return (float)string_to_double(value);
85 }
86
87 double string_to_double(const std::string& value)
88 throw(std::invalid_argument)
89 {
90 // TODO - error checking
91 return strtod(value.c_str(), 0);
92 }
93
94 ////////////////////////////////////////////////////////////////////////////////
95
96 } // end namespace stlplus
This page took 0.037526 seconds and 4 git commands to generate.