00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <limits.h>
00039 #include <malloc.h>
00040
00041 #ifdef _LONGLONG
00042
00043 #define __LONGLONG_MAX LONGLONG_MAX
00044 #define __ULONGLONG_MAX ULONGLONG_MAX
00045 #define __LONGLONG_MIN LONGLONG_MIN
00046
00047 #else
00048
00049 #define __LONGLONG_MAX LONG_MAX
00050 #define __ULONGLONG_MAX ULONG_MAX
00051 #define __LONGLONG_MIN LONG_MIN
00052 #endif
00053
00054 #include <cmplrs/fio.h>
00055 #include <stdlib.h>
00056 #include "fmtlib.h"
00057
00058 char *icvt(int value, int *ndigit, int *sign, char *buf)
00059 {
00060 char *abuf;
00061
00062
00063 short minint = 0;
00064
00065 if (value > 0)
00066 *sign = 0;
00067 else if (value < 0) {
00068
00069 if (value == INT_MIN) {
00070 minint++;
00071 value++;
00072 }
00073 value = -value;
00074 *sign = 1;
00075 } else {
00076 *sign = 0;
00077 *ndigit = 1;
00078 *buf = '0';
00079 *(buf + 1) = '\0';
00080 return (buf);
00081 }
00082 abuf = buf + MAXOCTLENGTH - 1;
00083 (*abuf--) = '\0';
00084 while (value > 0) {
00085 (*abuf--) = (char) (value % 10 + '0');
00086 value /= 10;
00087 }
00088 *ndigit = buf + MAXOCTLENGTH - 2 - abuf;
00089
00090 if (minint) {
00091 char *ch;
00092
00093 ch = buf + MAXOCTLENGTH - 2;
00094 (*ch)++;
00095 }
00096 return (abuf + 1);
00097 }
00098
00099 char *llcvt(ftnll value, int *ndigit, int *sign, char *buf)
00100 {
00101 char *abuf;
00102
00103
00104 short minint = 0;
00105
00106 if (value > 0)
00107 *sign = 0;
00108 else if (value < 0) {
00109
00110 if (value == (long long) -__LONGLONG_MAX - 1) {
00111 minint++;
00112 value++;
00113 }
00114 value = -value;
00115 *sign = 1;
00116 } else {
00117 *sign = 0;
00118 *ndigit = 1;
00119 *buf = '0';
00120 *(buf + 1) = '\0';
00121 return (buf);
00122 }
00123 abuf = buf + MAXOCTLENGTH - 1;
00124 (*abuf--) = '\0';
00125 #if 0
00126 while ((*(long *) &value > 0) || (*(long *) &value == 0 &&
00127 *((long *) &value + 1) != 0))
00128
00129 #endif
00130 while (value > 0LL) {
00131 long long temp;
00132
00133 temp = value % 10;
00134 (*abuf--) = (char) (temp + '0');
00135 value = value / 10;
00136 }
00137 *ndigit = buf + MAXOCTLENGTH - 2 - abuf;
00138
00139 if (minint) {
00140 char *ch;
00141
00142 ch = buf + MAXOCTLENGTH - 2;
00143 (*ch)++;
00144 }
00145 return (abuf + 1);
00146 }
00147
00148 char *ozcvt(unsigned char *value, int len, int *ndigit, int base, char *buf)
00149 {
00150 register char *abuf;
00151 register unsigned char c, *vbuf;
00152 register unsigned int d;
00153 register int ibits, bits, maxbytes;
00154
00155 bits = ((--base) < 8) ? 3 : 4;
00156 maxbytes = (len * 8) / bits + 3;
00157 #ifdef _MIPSEB
00158 vbuf = value;
00159 while (*vbuf == '\0' && len > 0) {
00160 vbuf++;
00161 len--;
00162 }
00163 vbuf += len - 1;
00164 #else
00165 vbuf = value + len - 1;
00166 while (*vbuf == '\0' && len > 0) {
00167 vbuf--;
00168 len--;
00169 }
00170 vbuf = value;
00171 #endif
00172 if (len == 0) {
00173 *buf = '0';
00174 *(buf + 1) = '\0';
00175 *ndigit = 1;
00176 return (buf);
00177 }
00178 abuf = buf + maxbytes - 1;
00179 (*abuf--) = '\0';
00180 d = 0;
00181 ibits = 0;
00182
00183 while (len--) {
00184 #ifdef _MIPSEB
00185 c = (*vbuf--);
00186 #else
00187 c = *vbuf++;
00188 #endif
00189 d |= c << ibits;
00190 ibits += 8;
00191 while (ibits >= bits) {
00192 c = (unsigned char) (d & base);
00193 (*abuf--) = (char) (c + (c > 9 ? ('A' - 10) : '0'));
00194 d >>= bits;
00195 ibits -= bits;
00196 }
00197 }
00198 if (ibits) {
00199 c = (unsigned char) (d & base);
00200 (*abuf--) = (char) (c + (c > 9 ? ('A' - 10) : '0'));
00201 }
00202 while (*++abuf == '0');
00203 *ndigit = buf + maxbytes - abuf - 1;
00204 return (abuf);
00205 }
00206
00207 #ifdef I90
00208 char *bcvt(unsigned char *value, int len, int *ndigit, char *buf)
00209 {
00210 register char *abuf;
00211 register unsigned char c, *vbuf;
00212 register int maxbytes;
00213
00214 maxbytes = len * 8;
00215
00216 #ifdef _MIPSEB
00217 vbuf = value;
00218 while (*vbuf == '\0' && len > 0) {
00219 vbuf++;
00220 len--;
00221 }
00222 vbuf += len - 1;
00223 #else
00224 vbuf = value + len - 1;
00225 while (*vbuf == '\0' && len > 0) {
00226 vbuf--;
00227 len--;
00228 }
00229 vbuf = value;
00230 #endif
00231
00232 if (len == 0) {
00233 *ndigit = 0;
00234 return (0);
00235 }
00236
00237 abuf = buf + maxbytes;
00238 (*abuf--) = '\0';
00239
00240 while (len--) {
00241
00242 #ifdef _MIPSEB
00243 c = (*vbuf--);
00244 #else
00245 c = *vbuf++;
00246 #endif
00247
00248 if (c & 1) (*abuf--) = '1';
00249 else (*abuf--) = '0';
00250 if (c & 2) (*abuf--) = '1';
00251 else (*abuf--) = '0';
00252 if (c & 4) (*abuf--) = '1';
00253 else (*abuf--) = '0';
00254 if (c & 8) (*abuf--) = '1';
00255 else (*abuf--) = '0';
00256 if (c & 16) (*abuf--) = '1';
00257 else (*abuf--) = '0';
00258 if (c & 32) (*abuf--) = '1';
00259 else (*abuf--) = '0';
00260 if (c & 64) (*abuf--) = '1';
00261 else (*abuf--) = '0';
00262 if (c & 128) (*abuf--) = '1';
00263 else (*abuf--) = '0';
00264
00265 }
00266
00267 while (*(++abuf) == '0' );
00268 *ndigit = buf + maxbytes - abuf;
00269 return (abuf);
00270
00271 }
00272 #endif