00001 #pragma ident "$Id: NumberFormatter.cpp 2938 2011-10-23 19:39:11Z yanweignss $"
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "NumberFormatter.hpp"
00031 #include <cstdio>
00032 #include "DebugUtils.hpp"
00033
00034 namespace gpstk
00035 {
00036 void NumberFormatter::append(std::string& str, int value)
00037 {
00038 char buffer[64];
00039 std::sprintf(buffer, "%d", value);
00040 str.append(buffer);
00041 }
00042
00043
00044 void NumberFormatter::append(std::string& str, int value, int width)
00045 {
00046 GPSTK_ASSERT (width > 0 && width < 64);
00047
00048 char buffer[64];
00049 std::sprintf(buffer, "%*d", width, value);
00050 str.append(buffer);
00051 }
00052
00053
00054 void NumberFormatter::append0(std::string& str, int value, int width)
00055 {
00056 GPSTK_ASSERT (width > 0 && width < 64);
00057
00058 char buffer[64];
00059 std::sprintf(buffer, "%0*d", width, value);
00060 str.append(buffer);
00061 }
00062
00063
00064 void NumberFormatter::appendHex(std::string& str, int value)
00065 {
00066 char buffer[64];
00067 std::sprintf(buffer, "%X", value);
00068 str.append(buffer);
00069 }
00070
00071
00072 void NumberFormatter::appendHex(std::string& str, int value, int width)
00073 {
00074 GPSTK_ASSERT (width > 0 && width < 64);
00075
00076 char buffer[64];
00077 std::sprintf(buffer, "%0*X", width, value);
00078 str.append(buffer);
00079 }
00080
00081
00082 void NumberFormatter::append(std::string& str, unsigned value)
00083 {
00084 char buffer[64];
00085 std::sprintf(buffer, "%u", value);
00086 str.append(buffer);
00087 }
00088
00089
00090 void NumberFormatter::append(std::string& str, unsigned value, int width)
00091 {
00092 GPSTK_ASSERT (width > 0 && width < 64);
00093
00094 char buffer[64];
00095 std::sprintf(buffer, "%*u", width, value);
00096 str.append(buffer);
00097 }
00098
00099
00100 void NumberFormatter::append0(std::string& str, unsigned int value, int width)
00101 {
00102 GPSTK_ASSERT (width > 0 && width < 64);
00103
00104 char buffer[64];
00105 std::sprintf(buffer, "%0*u", width, value);
00106 str.append(buffer);
00107 }
00108
00109
00110 void NumberFormatter::appendHex(std::string& str, unsigned value)
00111 {
00112 char buffer[64];
00113 std::sprintf(buffer, "%X", value);
00114 str.append(buffer);
00115 }
00116
00117
00118 void NumberFormatter::appendHex(std::string& str, unsigned value, int width)
00119 {
00120 GPSTK_ASSERT (width > 0 && width < 64);
00121
00122 char buffer[64];
00123 std::sprintf(buffer, "%0*X", width, value);
00124 str.append(buffer);
00125 }
00126
00127
00128 void NumberFormatter::append(std::string& str, long value)
00129 {
00130 char buffer[64];
00131 std::sprintf(buffer, "%ld", value);
00132 str.append(buffer);
00133 }
00134
00135
00136 void NumberFormatter::append(std::string& str, long value, int width)
00137 {
00138 GPSTK_ASSERT (width > 0 && width < 64);
00139
00140 char buffer[64];
00141 std::sprintf(buffer, "%*ld", width, value);
00142 str.append(buffer);
00143 }
00144
00145
00146 void NumberFormatter::append0(std::string& str, long value, int width)
00147 {
00148 GPSTK_ASSERT (width > 0 && width < 64);
00149
00150 char buffer[64];
00151 std::sprintf(buffer, "%0*ld", width, value);
00152 str.append(buffer);
00153 }
00154
00155
00156 void NumberFormatter::appendHex(std::string& str, long value)
00157 {
00158 char buffer[64];
00159 std::sprintf(buffer, "%lX", value);
00160 str.append(buffer);
00161 }
00162
00163
00164 void NumberFormatter::appendHex(std::string& str, long value, int width)
00165 {
00166 GPSTK_ASSERT (width > 0 && width < 64);
00167
00168 char buffer[64];
00169 std::sprintf(buffer, "%0*lX", width, value);
00170 str.append(buffer);
00171 }
00172
00173
00174 void NumberFormatter::append(std::string& str, unsigned long value)
00175 {
00176 char buffer[64];
00177 std::sprintf(buffer, "%lu", value);
00178 str.append(buffer);
00179 }
00180
00181
00182 void NumberFormatter::append(std::string& str, unsigned long value, int width)
00183 {
00184 GPSTK_ASSERT (width > 0 && width < 64);
00185
00186 char buffer[64];
00187 std::sprintf(buffer, "%*lu", width, value);
00188 str.append(buffer);
00189 }
00190
00191
00192 void NumberFormatter::append0(std::string& str, unsigned long value, int width)
00193 {
00194 GPSTK_ASSERT (width > 0 && width < 64);
00195
00196 char buffer[64];
00197 std::sprintf(buffer, "%0*lu", width, value);
00198 str.append(buffer);
00199 }
00200
00201
00202 void NumberFormatter::appendHex(std::string& str, unsigned long value)
00203 {
00204 char buffer[64];
00205 std::sprintf(buffer, "%lX", value);
00206 str.append(buffer);
00207 }
00208
00209
00210 void NumberFormatter::appendHex(std::string& str, unsigned long value, int width)
00211 {
00212 GPSTK_ASSERT (width > 0 && width < 64);
00213
00214 char buffer[64];
00215 std::sprintf(buffer, "%0*lX", width, value);
00216 str.append(buffer);
00217 }
00218
00219 void NumberFormatter::append(std::string& str, double value)
00220 {
00221 char buffer[64];
00222 std::sprintf(buffer, "%.*g", 16, value);
00223 str.append(buffer);
00224 }
00225
00226
00227 void NumberFormatter::append(std::string& str, double value, int precision)
00228 {
00229 GPSTK_ASSERT (precision >= 0 && precision < 32);
00230
00231 char buffer[64];
00232 std::sprintf(buffer, "%.*f", precision, value);
00233 str.append(buffer);
00234 }
00235
00236
00237 void NumberFormatter::append(std::string& str, double value, int width, int precision)
00238 {
00239 GPSTK_ASSERT (width > 0 && width < 64 && precision >= 0 && precision < width);
00240
00241 char buffer[64];
00242 std::sprintf(buffer, "%*.*f", width, precision, value);
00243 str.append(buffer);
00244 }
00245
00246 }
00247