stdlib.c

00001 /***************************************************************************
00002                           stdlib.c  -  description
00003                              -------------------
00004     begin                : Sat Dec 13 2003
00005     copyright            : (C) 2003 by Dynacube Team
00006     email                : mdshah82@yahoo.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017  
00018  #include "common/ktypes.h"
00019  #include "common/kconst.h"
00020  #include "core/kstdio.h"
00021 
00022  #include "common/stdlib.h"
00023  
00024  char* itoa(DD num,DB base)
00025  {
00026   DB* ptr_num = NULL;
00027   DB _num[32] = {0}, temp;
00028   DW index = 0, i = 0, val;
00029 
00030   do
00031   {
00032     val = num%base;
00033 
00034     if(val >= 10)
00035     {
00036      //Works only for Hex
00037      _num[index++] = 'F'-(base-val-1);
00038     }
00039     else
00040     {
00041      _num[index++] = '0'+val;
00042     }
00043 
00044     num/=base;
00045 
00046   }while(num);
00047 
00048 
00049   switch(base)
00050   {
00051    case 16:
00052         base = 8;
00053         break;
00054     
00055    case 2:
00056         base = 32;
00057         break;
00058    
00059    case 10: //For Decimal
00060     base = 0; //Don't want zeroes
00061         break;
00062    
00063    default:
00064 //change        
00065 //      kprint("\nBase not known\tFile Name : \t");
00066 //      kprint(__FILE__);
00067         return NULL;
00068         break; //Useless but just for safety
00069   }
00070 
00071   while(index < base)
00072   {
00073    _num[index++] = '0';
00074   }
00075 
00076   index--;
00077 
00078   i = 0;
00079 
00080   while(i <= (int)(index/2))
00081   {
00082    temp = _num[i];
00083    _num[i] = _num[index-i];
00084    _num[index-i] = temp;
00085    i++;
00086   }
00087 
00088   index++;
00089 
00090   _num[index] = '\0';
00091 
00092   ptr_num = &_num[0];
00093   return ptr_num;
00094  }
00095 
00096  SDD atoi(char *num)
00097   {
00098     SDD no;
00099     DW i;
00100     no = 0;
00101     i = 0;
00102     if(num[0] == '-')
00103      {
00104       i++;
00105      }
00106     for(;i<strlen(num);i++)
00107      {
00108       if(!isnum(num[i]))
00109         return 0;
00110       no = no * 10 + num[i] - '0';
00111      }
00112      if(num[0] == '-')
00113      {
00114       no = no * -1;
00115      }
00116     return no;
00117   }
00118  DD abs(SDD num)
00119  {
00120    if(num >= 0) return num;
00121    else return -num;
00122  }
00123 

Generated on Thu Jul 27 23:52:27 2006 for Dynacube by  doxygen 1.4.7