00001
00002
00003 #include "rcs_ce.h"
00004
00005
00006 #if defined(DEBUG) && !defined(NO_STDIO)
00007 #include <stdio.h>
00008 #endif
00009
00010 #include <string.h>
00011 #include <stdarg.h>
00012
00013 double RCS_CE_ATOF(const char *str)
00014 {
00015 int mantissa = 0;
00016 double exponent_factor=1;
00017 long exponent = 0;
00018 char period_occured = 0;
00019 char e_occured = 0;
00020 char mantissa_negative = 0;
00021 char e_value_negative = 0;
00022 int e_value = 0;
00023 double return_value;
00024 char c = *str;
00025 while(c != 0)
00026 {
00027 #if defined(DEBUG) && !defined(NO_STDIO)
00028 printf("c=%c, mantissa = %d, exponent=%d\n",c,mantissa,exponent);
00029 #endif
00030 if(c >= '0' && c <= '9')
00031 {
00032 if(!e_occured)
00033 {
00034 mantissa *= 10;
00035 mantissa += (c-'0');
00036 if(period_occured)
00037 {
00038 exponent--;
00039 }
00040 }
00041 else
00042 {
00043 e_value *= 10;
00044 e_value += (c-'0');
00045 }
00046
00047 }
00048 else if(!period_occured && c == '.')
00049 {
00050 period_occured = 1;
00051 }
00052 else if(!e_occured && (c == 'e' || c == 'E'))
00053 {
00054 e_occured = 1;
00055 #if defined(DEBUG) && !defined(NO_STDIO)
00056 printf("e occurred");
00057 #endif
00058 }
00059 else if(c == '-')
00060 {
00061 if(!e_occured)
00062 {
00063 mantissa_negative = 1;
00064 }
00065 else
00066 {
00067 e_value_negative = 1;
00068 }
00069 }
00070 else
00071 {
00072 break;
00073 }
00074 str++;
00075 c = *str;
00076 }
00077 #if defined(DEBUG) && !defined(NO_STDIO)
00078 printf("c=%c, mantissa = %d, exponent=%d\n",c,mantissa,exponent);
00079 #endif
00080 if(e_value_negative)
00081 {
00082 e_value *= -1;
00083 }
00084 if(mantissa_negative)
00085 {
00086 mantissa *= -1;
00087 }
00088 exponent += e_value;
00089 if(exponent > 0)
00090 {
00091 for(int i = 0; i < exponent; i++)
00092 {
00093 exponent_factor *= 10;
00094 }
00095 }
00096 else if(exponent < 0)
00097 {
00098 for(int i = exponent; i < 0; i++)
00099 {
00100 exponent_factor *= 10;
00101 }
00102 }
00103 #if defined(DEBUG) && !defined(NO_STDIO)
00104 printf("exponent_factor=%d\n",exponent_factor);
00105 #endif
00106 if(exponent == 0)
00107 {
00108 exponent_factor = 1;
00109 }
00110 if(exponent > 0)
00111 {
00112 return_value = ((double) mantissa) * ((double) exponent_factor);
00113 }
00114 else if(exponent < 0)
00115 {
00116 return_value = ((double) mantissa) / ((double) exponent_factor);
00117 }
00118 else
00119 {
00120 return_value = (double) mantissa;
00121 }
00122 #if defined(DEBUG) && !defined(NO_STDIO)
00123 printf("c=%c, mantissa = %d, exponent=%d\n",c,mantissa,exponent);
00124 #endif
00125 return(return_value);
00126 }
00127
00128 void RCS_CE_UNICODE_TO_ASCII(char *str, wchar_t *wcs, int len)
00129 {
00130 int chars_converted = 0;
00131 while(*wcs != 0 && chars_converted < len-1)
00132 {
00133 *str = *(((char *) wcs));
00134 str++;
00135 wcs++;
00136 chars_converted++;
00137 }
00138 *str = 0;
00139 }
00140
00141
00142 void RCS_CE_ASCII_TO_UNICODE(wchar_t *wcs, const char *str, int len)
00143 {
00144 int chars_converted = 0;
00145 while(*str != 0 && chars_converted < len -1)
00146 {
00147 *((char *) wcs) = *str;
00148 *(((char *) wcs)+1) = 0;
00149 str++;
00150 wcs++;
00151 chars_converted++;
00152 }
00153 *wcs = 0;
00154 }
00155
00156 int RCS_CE_VSPRINTF(char *str, const char *fmt, va_list args)
00157 {
00158 int pfound = 0;
00159 char *sptr = str;
00160 char *fptr = (char *) fmt;
00161 int dec,sign,exp;
00162 char c = *fptr;
00163 int i = 0;
00164 double d = 0.0;
00165 char *string = NULL;
00166 char *fstring = NULL;
00167
00168 while(c != 0)
00169 {
00170 if(c != '%' && !pfound)
00171 {
00172 *sptr = c;
00173 fptr++;
00174 sptr++;
00175 c = *fptr;
00176 continue;
00177 }
00178 else if(c == '%')
00179 {
00180 if(!pfound)
00181 {
00182 pfound = 1;
00183 fptr++;
00184 c = *fptr;
00185 continue;
00186 }
00187 else
00188 {
00189 *sptr = '%';
00190 sptr++;
00191 pfound = 0;
00192 fptr++;
00193 c = *fptr;
00194 continue;
00195 }
00196 }
00197 else if(pfound)
00198 {
00199 if((c >= '0' && c <= '9') || c == '.' || c == 'l')
00200 {
00201 fptr++;
00202 c = *fptr;
00203 continue;
00204 }
00205 else
00206 {
00207 switch(c)
00208 {
00209 case 'd':
00210 i = va_arg(args,int);
00211 memset(sptr,0,8);
00212 _itoa(i,sptr,10);
00213 while(*sptr != 0)
00214 {
00215 sptr++;
00216 }
00217 break;
00218
00219 case 'o':
00220 i = va_arg(args,int);
00221 memset(sptr,0,8);
00222 _itoa(i,sptr,8);
00223 while(*sptr != 0)
00224 {
00225 sptr++;
00226 }
00227 break;
00228
00229 case 'x':
00230 i = va_arg(args,int);
00231 memset(sptr,0,8);
00232 _itoa(i,sptr,16);
00233 while(*sptr != 0)
00234 {
00235 sptr++;
00236 }
00237
00238 case 's':
00239 string = va_arg(args, char *);
00240 while(*string != 0)
00241 {
00242 *sptr = *string;
00243 sptr++;
00244 string++;
00245 }
00246 break;
00247
00248 case 'f':
00249 d = va_arg(args, double);
00250 fstring = _fcvt(d,5,&dec,&sign);
00251 if(sign)
00252 {
00253 *sptr = '-';
00254 sptr++;
00255 }
00256 exp = 0;
00257 if(dec < -5 || dec > 5)
00258 {
00259 dec = 1;
00260 exp = dec;
00261 }
00262 if(dec < 0)
00263 {
00264 *sptr = '0';
00265 sptr++;
00266 *sptr = '.';
00267 sptr++;
00268 for(i = dec; i < 0; i++)
00269 {
00270 *sptr='0';
00271 sptr++;
00272 }
00273 }
00274 i = 0;
00275 while(*fstring != 0)
00276 {
00277 if(i == dec)
00278 {
00279 *sptr = '.';
00280 sptr++;
00281 }
00282 *sptr = *fstring;
00283 sptr++;
00284 i++;
00285 fstring++;
00286 }
00287 if(exp != 0)
00288 {
00289 *sptr = 'E';
00290 sptr++;
00291 _itoa(exp,sptr,10);
00292 while(*sptr != 0)
00293 {
00294 sptr++;
00295 }
00296 }
00297 break;
00298 }
00299 pfound = 0;
00300 fptr++;
00301 c = *fptr;
00302 }
00303 }
00304 }
00305 *sptr = 0;
00306 return 0;
00307 }