Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenfcvtbuf.c
Go to the documentation of this file.
00001 #include <stdlib.h> 00002 #include <stdio.h> 00003 #include <string.h> 00004 #include <float.h> 00005 #include <math.h> 00006 #include <malloc.h> 00007 // #include <msvcrt/locale.h> 00008 00009 // replace fjgpp fcvtbuf from project http://www.jbox.dk/sanos/source/lib/fcvt.c.html 00010 // with small modification's to match ReactOS arch 00011 00012 // Floating point to string conversion routines 00013 // 00014 // Copyright (C) 2002 Michael Ringgaard. All rights reserved. 00015 // 00016 // Redistribution and use in source and binary forms, with or without 00017 // modification, are permitted provided that the following conditions 00018 // are met: 00019 // 00020 // 1. Redistributions of source code must retain the above copyright 00021 // notice, this list of conditions and the following disclaimer. 00022 // 2. Redistributions in binary form must reproduce the above copyright 00023 // notice, this list of conditions and the following disclaimer in the 00024 // documentation and/or other materials provided with the distribution. 00025 // 3. Neither the name of the project nor the names of its contributors 00026 // may be used to endorse or promote products derived from this software 00027 // without specific prior written permission. 00028 // 00029 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00030 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00031 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00032 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00033 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00034 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00035 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00036 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00038 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00039 // SUCH DAMAGE. 00040 // 00041 00042 00043 //#include <math.h> 00044 #define CVTBUFSIZE 2 * DBL_MAX_10_EXP + 10 00045 static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag) 00046 { 00047 int r2; 00048 double fi, fj; 00049 char *p, *p1; 00050 00051 if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2; 00052 r2 = 0; 00053 *sign = 0; 00054 p = &buf[0]; 00055 if (arg < 0) 00056 { 00057 *sign = 1; 00058 arg = -arg; 00059 } 00060 arg = modf(arg, &fi); 00061 p1 = &buf[CVTBUFSIZE]; 00062 00063 if (fi != 0) 00064 { 00065 p1 = &buf[CVTBUFSIZE]; 00066 while (fi != 0) 00067 { 00068 fj = modf(fi / 10, &fi); 00069 *--p1 = (int)((fj + .03) * 10) + '0'; 00070 r2++; 00071 } 00072 while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++; 00073 } 00074 else if (arg > 0) 00075 { 00076 while ((fj = arg * 10) < 1) 00077 { 00078 arg = fj; 00079 r2--; 00080 } 00081 } 00082 p1 = &buf[ndigits]; 00083 if (eflag == 0) p1 += r2; 00084 *decpt = r2; 00085 if (p1 < &buf[0]) 00086 { 00087 buf[0] = '\0'; 00088 return buf; 00089 } 00090 while (p <= p1 && p < &buf[CVTBUFSIZE]) 00091 { 00092 arg *= 10; 00093 arg = modf(arg, &fj); 00094 *p++ = (int) fj + '0'; 00095 } 00096 if (p1 >= &buf[CVTBUFSIZE]) 00097 { 00098 buf[CVTBUFSIZE - 1] = '\0'; 00099 return buf; 00100 } 00101 p = p1; 00102 *p1 += 5; 00103 while (*p1 > '9') 00104 { 00105 *p1 = '0'; 00106 if (p1 > buf) 00107 ++*--p1; 00108 else 00109 { 00110 *p1 = '1'; 00111 (*decpt)++; 00112 if (eflag == 0) 00113 { 00114 if (p > buf) *p = '0'; 00115 p++; 00116 } 00117 } 00118 } 00119 *p = '\0'; 00120 return buf; 00121 } 00122 00123 char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) 00124 { 00125 return cvt(arg, ndigits, decpt, sign, buf, 0); 00126 } Generated on Sun May 27 2012 04:36:37 for ReactOS by
1.7.6.1
|