Subversion Repositories Kolibri OS

Rev

Rev 2502 | Go to most recent revision | 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.  
  127. void _itoa(int i, char *s)
  128. {
  129. int a, b, c, d;
  130. a = (i - i%1000)/1000;
  131. b = (i - i%100)/100 - a*10;
  132. c = (i - i%10)/10 - a*100 - b*10;
  133. d = i%10;
  134. s[0] = a + '0';
  135. s[1] = b + '0';
  136. s[2] = c + '0';
  137. s[3] = d + '0';
  138. s[4] = 0;
  139. }
  140.  
  141.  
  142.  /* reverse:  ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */
  143.  void reverse(char s[])
  144.  {
  145.      int i, j;
  146.      char c;
  147.  
  148.      for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  149.          c = s[i];
  150.          s[i] = s[j];
  151.          s[j] = c;
  152.      }
  153.  }
  154.  
  155.  
  156.  /* itoa:  êîíâåðòèðóåì n â ñèìâîëû â s */
  157.  void itoa(int n, char s[])
  158.  {
  159.      int i, sign;
  160.  
  161.      if ((sign = n) < 0)  /* çàïèñûâàåì çíàê */
  162.          n = -n;          /* äåëàåì n ïîëîæèòåëüíûì ÷èñëîì */
  163.      i = 0;
  164.      do {       /* ãåíåðèðóåì öèôðû â îáðàòíîì ïîðÿäêå */
  165.          s[i++] = n % 10 + '0';   /* áåðåì ñëåäóþùóþ öèôðó */
  166.      } while ((n /= 10) > 0);     /* óäàëÿåì */
  167.      if (sign < 0)
  168.          s[i++] = '-';
  169.      s[i] = '\0';
  170.      reverse(s);
  171.  }
  172.  
  173.