Gaudi Framework, version v20r4

Generated: 8 Jan 2009

xtoa.cpp

Go to the documentation of this file.
00001 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/src/Lib/xtoa.cpp,v 1.3 2003/11/27 10:20:59 mato Exp $
00002 #if !defined(_WIN32)
00003 #include <stdlib.h>
00004 #include <limits.h>
00005 #include "GaudiKernel/xtoa.h"
00006 
00007 /***
00008 *xtoa.c - convert integers/longs to ASCII string
00009 *
00010 *   Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
00011 *
00012 *Purpose:
00013 *   The module has code to convert integers/longs to ASCII strings.  See
00014 *
00015 *******************************************************************************/
00016 
00017 /***
00018 *char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII
00019 *   string
00020 *
00021 *Purpose:
00022 *   Converts an int to a character string.
00023 *
00024 *Entry:
00025 *   val - number to be converted (int, long or unsigned long)
00026 *   int radix - base to convert into
00027 *   char *buf - ptr to buffer to place result
00028 *
00029 *Exit:
00030 *   fills in space pointed to by buf with string result
00031 *   returns a pointer to this buffer
00032 *
00033 *Exceptions:
00034 *
00035 *******************************************************************************/
00036 
00037 /* helper routine that does the main job. */
00038 
00039 static void __cdecl xtoa (unsigned long val,char *buf,unsigned radix,int is_neg)  {
00040   char *p;    /* pointer to traverse string */
00041   char *firstdig;   /* pointer to first digit */
00042   char temp;    /* temp char */
00043   unsigned digval;  /* value of digit */
00044 
00045   p = buf;
00046 
00047   if (is_neg) {
00048     /* negative, so output '-' and negate */
00049     *p++ = '-';
00050     val = (unsigned long)(-(long)val);
00051   }
00052 
00053   firstdig = p;     /* save pointer to first digit */
00054 
00055   do {
00056     digval = (unsigned) (val % radix);
00057     val /= radix;   /* get next digit */
00058 
00059     /* convert to ascii and store */
00060     if (digval > 9)
00061       *p++ = (char) (digval - 10 + 'a');  /* a letter */
00062     else
00063       *p++ = (char) (digval + '0');       /* a digit */
00064   } while (val > 0);
00065 
00066   /* We now have the digit of the number in the buffer, but in reverse
00067      order.  Thus we reverse them now. */
00068 
00069   *p-- = '\0';    /* terminate string; p points to last digit */
00070 
00071   do {
00072     temp = *p;
00073     *p = *firstdig;
00074     *firstdig = temp;   /* swap *p and *firstdig */
00075     --p;
00076     ++firstdig;   /* advance to next two digits */
00077   } while (firstdig < p); /* repeat until halfway */
00078 }
00079 
00080 /* Actual functions just call conversion helper with neg flag set correctly,
00081    and return pointer to buffer. */
00082 
00083 extern "C" char * __cdecl _itoa (int val,char *buf,int radix)    {
00084   if (radix == 10 && val < 0)
00085     xtoa((unsigned long)val, buf, radix, 1);
00086   else
00087     xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
00088   return buf;
00089 }
00090 
00091 extern "C" char * __cdecl _ltoa (long val,char *buf,int radix)   {
00092   xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
00093   return buf;
00094 }
00095 
00096 extern "C" char * __cdecl _ultoa (unsigned long val,char *buf,int radix)        {
00097   xtoa(val, buf, radix, 0);
00098   return buf;
00099 }
00100 
00101 #if 0 // ndef _NO_INT64
00102 
00103 static void __stdcall x64toa (  /* stdcall is faster and smaller... Might as well use it for the helper. */
00104   unsigned __int64 val,
00105   char *buf,
00106   unsigned radix,
00107   int is_neg
00108   )
00109 {
00110   char *p;  /* pointer to traverse string */
00111   char *firstdig;   /* pointer to first digit */
00112   char temp;  /* temp char */
00113   unsigned digval;  /* value of digit */
00114 
00115   p = buf;
00116 
00117   if ( is_neg )  {
00118     *p++ = '-';   /* negative, so output '-' and negate */
00119   }
00120 
00121   firstdig = p;   /* save pointer to first digit */
00122 
00123   do {
00124     digval = (unsigned) (val % radix);
00125     val /= radix;   /* get next digit */
00126 
00127     /* convert to ascii and store */
00128     if (digval > 9)
00129       *p++ = (char) (digval - 10 + 'a');  /* a letter */
00130     else
00131       *p++ = (char) (digval + '0');     /* a digit */
00132   } while (val > 0);
00133 
00134   /* We now have the digit of the number in the buffer, but in reverse
00135    order.  Thus we reverse them now. */
00136 
00137   *p-- = '\0';  /* terminate string; p points to last digit */
00138 
00139   do {
00140     temp = *p;
00141     *p = *firstdig;
00142     *firstdig = temp;   /* swap *p and *firstdig */
00143     --p;
00144     ++firstdig;   /* advance to next two digits */
00145   } while (firstdig < p); /* repeat until halfway */
00146 }
00147 
00148 /* Actual functions just call conversion helper with neg flag set correctly,
00149    and return pointer to buffer. */
00150 
00151 extern "C" char * __cdecl _i64toa (__int64 val,char *buf,int radix )   {
00152   x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
00153   return buf;
00154 }
00155 
00156 extern "C" char * __cdecl i64toa (__int64 val,char *buf,int radix )   {
00157   x64toa((unsigned __int64)val, buf, radix, (radix == 10 && val < 0));
00158   return buf;
00159 }
00160 
00161 extern "C" char * __cdecl _ui64toa (unsigned __int64 val,char *buf,int radix)    {
00162   x64toa(val, buf, radix, 0);
00163   return buf;
00164 }
00165 
00166 extern "C" char * __cdecl ui64toa (unsigned __int64 val,char *buf,int radix)    {
00167   x64toa(val, buf, radix, 0);
00168   return buf;
00169 }
00170 
00171 #endif  /* _NO_INT64 */
00172 
00173 #endif /* WIN32   */

Generated at Thu Jan 8 17:44:22 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004