Guest User

Untitled

a guest
Nov 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <string.h>
  4.  
  5. #include <ctype.h>
  6.  
  7. #include <errno.h>
  8.  
  9. #include <limits.h>
  10.  
  11.  
  12.  
  13. long strtol(const char *s, char *endptr, int base)
  14.  
  15. {
  16.  
  17.   int power;
  18.  
  19.   int sign = 1;
  20.  
  21.   const char *ptr = s;
  22.  
  23.   long result = 0;
  24.  
  25.   /*check the base*/
  26.  
  27.   if ((base<2 || base >36) && base!=0)
  28.  
  29.   {
  30.  
  31.     errno=EINVAL;
  32.  
  33.     return 0;
  34.  
  35.   }
  36.  
  37.   if(*ptr == '\n')
  38.  
  39.     return 0;
  40.  
  41.   /*delating spaces and tabulations at the begining*/
  42.  
  43.   while(isspace(ptr))
  44.  
  45.     ptr++;
  46.  
  47.   if(!ptr) return 0;
  48.  
  49.   /*case: base =0, string starts with '0x' or '0X' (16) or '0' (8) or it's (10)*/
  50.  
  51.   if(base == 0)
  52.  
  53.   {
  54.  
  55.     if(*ptr == '0' && (*ptr+1 == 'x' || *ptr+1 == 'X'))
  56.  
  57.       base = 16;
  58.  
  59.     else
  60.  
  61.       if(*ptr == '0')
  62.  
  63.             base = 8;
  64.  
  65.       else
  66.  
  67.             base=10;
  68.  
  69.   }
  70.  
  71.   if(*ptr=='+')
  72.  
  73.     ptr = ptr+1;
  74.  
  75.   else
  76.  
  77.     if(*ptr == '-')
  78.  
  79.     {
  80.  
  81.       sign = -1;
  82.  
  83.       ptr = ptr+1;
  84.  
  85.     }
  86.  
  87.   /*case: base = 16, string starts with '0x' or '0X'*/
  88.  
  89.   if(base == 16 && (*ptr == '0' && (*(ptr+1)== 'x' || *(ptr+1)== 'X')))
  90.  
  91.     ptr = ptr+2;
  92.  
  93.   /*case: base = 8, string starts with 0*/
  94.  
  95.   if(base == 8 && *ptr == '0')
  96.  
  97.     ptr = ptr+1;
  98.  
  99.   /*remove zeros at the begining*/
  100.  
  101.   while(*ptr == '0') ptr++;
  102.  
  103.   if(!ptr)
  104.  
  105.   {
  106.  
  107.     return 0;
  108.  
  109.   }
  110.  
  111.   /*set end pointer*/
  112.  
  113.   for(*endptr = ptr; endptr < (ptr +strlen(ptr)-1); endptr++)
  114.  
  115.     if(converter(*endptr)>(base-1) || converter(*endptr)==-1)
  116.  
  117.       break;
  118.  
  119.   power = 1;
  120.  
  121.   for(endptr-=1; endptr>=ptr; endptr--)
  122.  
  123.   {
  124.  
  125.     result += converter(*endptr) * power;
  126.  
  127.     power *=base;
  128.  
  129.   }
  130.  
  131.   if(result*sign> LONG_MAX)
  132.  
  133.   {
  134.  
  135.     errno = ERANGE;
  136.  
  137.     return LONG_MAX;
  138.  
  139.   }
  140.  
  141.   if(result*sign<LONG_MIN)
  142.  
  143.   {
  144.  
  145.     errno = ERANGE;
  146.  
  147.     return LONG_MIN;
  148.  
  149.   }
  150.  
  151.   return result*sign;
  152.  
  153.    
  154.  
  155. }
  156.  
  157. /**       function converting letters and numbers (char type) to numbers(int type)
  158.  
  159. *          @param sth the character
  160.  
  161. */
  162.  
  163. int converter(char sth)
  164.  
  165. {
  166.  
  167.   if(sth>='0' && sth<='9') return sth - '0';
  168.  
  169.   if(sth>='a' && sth<='z') return sth - 'a' + 10;
  170.  
  171.   if(sth>='A' && sth>='Z') return sth-'A' + 10;
  172.  
  173.   return -1;
  174.  
  175. }
Add Comment
Please, Sign In to add comment