Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "string.h"
  3.  
  4. void*  memset(void *mem, int c, unsigned size)
  5. {
  6. unsigned i;
  7.  
  8. for ( i = 0; i < size; i++ )
  9.          *((char *)mem+i) = (char) c;
  10.  
  11. return NULL;   
  12. }
  13.  
  14.  
  15. void* memcpy(void *dst, const void *src, unsigned size)
  16. {
  17.  
  18. unsigned i;
  19.  
  20. for ( i = 0; i < size; i++)
  21.         *(char *)(dst+i) = *(char *)(src+i);
  22.  
  23. return NULL;
  24. }
  25.  
  26.  
  27. int memcmp(const void* buf1, const void* buf2, int count)
  28. {
  29. int i;
  30. for (i=0;i<count;i++)
  31.         {
  32.         if (*(unsigned char*)buf1<*(unsigned char*)buf2)
  33.                 return -1;
  34.         if (*(unsigned char*)buf1>*(unsigned char*)buf2)                       
  35.                 return 1;
  36.         }
  37. return 0;
  38. }
  39.  
  40. void strcat(char strDest[], char strSource[])
  41. {
  42.  
  43. int i, j;
  44.  
  45. i = j = 0;
  46. while (strDest[i] != '\0')
  47.         i++;
  48.  
  49. while ((strDest[i++] = strSource[j++]) != '\0')
  50.              ;
  51. }
  52.  
  53.  
  54. int strcmp(const char* string1, const char* string2)
  55. {
  56.  
  57. while (1)
  58. {
  59. if (*string1<*string2)
  60.         return -1;
  61. if (*string1>*string2)
  62.         return 1;
  63.  
  64. if (*string1=='\0')
  65.         return 0;
  66.  
  67. string1++;
  68. string2++;
  69. }
  70.  
  71. }
  72.  
  73.  
  74. void strcpy(char strDest[], const char strSource[])
  75. {
  76. unsigned i;
  77.  
  78. i = 0;
  79. while ((strDest[i] = strSource[i]) != '\0')
  80.         i++;
  81.  
  82. }
  83.  
  84.  
  85. char* strncpy(char *strDest, const char *strSource, unsigned n)
  86. {
  87. unsigned i;
  88.  
  89. if (! n )
  90.         return strDest;
  91.  
  92. i = 0;
  93. while ((strDest[i] = strSource[i]) != '\0')
  94.         if ( (n-1) == i )
  95.                 break;
  96.         else
  97.                 i++;
  98.  
  99. return strDest;
  100. }
  101.  
  102.  
  103. int strlen(const char* string)
  104. {
  105. int i;
  106.  
  107. i=0;
  108. while (*string++) i++;
  109. return i;
  110. }
  111.  
  112.  
  113.  
  114. char* strchr(const char* string, int c)
  115. {
  116.         while (*string)
  117.         {
  118.                 if (*string==c)
  119.                         return (char*)string;
  120.                 string++;
  121.         }      
  122.         return (char*)0;
  123. }
  124.  
  125.  
  126. char* strrchr(const char* string, int c)
  127. {
  128.         char* last_found;
  129.         while (*string)
  130.         {
  131.                 if (*string==c)
  132.                 {
  133.                         last_found = (char*)string;
  134.                 }
  135.                 string++;
  136.         }      
  137.         return last_found;
  138. }
  139.  
  140.  
  141.  
  142. void _itoa(int i, char *s)
  143. {
  144. int a, b, c, d;
  145. a = (i - i%1000)/1000;
  146. b = (i - i%100)/100 - a*10;
  147. c = (i - i%10)/10 - a*100 - b*10;
  148. d = i%10;
  149. s[0] = a + '0';
  150. s[1] = b + '0';
  151. s[2] = c + '0';
  152. s[3] = d + '0';
  153. s[4] = 0;
  154. }
  155.  
  156.  
  157.  /* reverse:  ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */
  158.  void reverse(char s[])
  159.  {
  160.      int i, j;
  161.      char c;
  162.  
  163.      for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  164.          c = s[i];
  165.          s[i] = s[j];
  166.          s[j] = c;
  167.      }
  168.  }
  169.  
  170.  
  171.  /* itoa:  êîíâåðòèðóåì n â ñèìâîëû â s */
  172.  void itoa(int n, char s[])
  173.  {
  174.      int i, sign;
  175.  
  176.      if ((sign = n) < 0)
  177.          n = -n;
  178.      i = 0;
  179.      do {
  180.          s[i++] = n % 10 + '0';
  181.      } while ((n /= 10) > 0);
  182.      if (sign < 0)
  183.          s[i++] = '-';
  184.      s[i] = '\0';
  185.      reverse(s);
  186.  }
  187.  
  188.  
  189.  
  190. int _atoi ( char *s )
  191. {
  192. int i, n;
  193.  
  194. n = 0;
  195. for ( i = 0; s[i]!= '\0'; ++i)
  196.         if ((s[i]<'0') || (s[i]>'9'))
  197.                 return 0;
  198.         else
  199.                 n = 10 * n + s[i] - '0';
  200.  
  201. return n;
  202. }
  203.