Subversion Repositories Kolibri OS

Rev

Rev 4001 | 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. int    strnicmp(const char* string1, const char* string2, unsigned count)
  74. {
  75. int pc = 0;
  76. while (1)
  77.         {
  78.         if (toupper(*string1)<toupper(*string2))
  79.                 return -1;
  80.         if (toupper(*string1)>toupper(*string2))
  81.                 return 1;
  82.  
  83.         if (*string1=='\0' || pc == count)
  84.                 return 0;
  85.  
  86.         string1++;
  87.         string2++;
  88.         pc++;
  89.         }
  90. }
  91.  
  92. void strcpy(char strDest[], const char strSource[])
  93. {
  94. unsigned i;
  95.  
  96. i = 0;
  97. while ((strDest[i] = strSource[i]) != '\0')
  98.         i++;
  99.  
  100. }
  101.  
  102.  
  103. char* strncpy(char *strDest, const char *strSource, unsigned n)
  104. {
  105. unsigned i;
  106.  
  107. if (! n )
  108.         return strDest;
  109.  
  110. i = 0;
  111. while ((strDest[i] = strSource[i]) != '\0')
  112.         if ( (n-1) == i )
  113.                 break;
  114.         else
  115.                 i++;
  116.  
  117. return strDest;
  118. }
  119.  
  120.  
  121. int strlen(const char* string)
  122. {
  123. int i;
  124.  
  125. i=0;
  126. while (*string++) i++;
  127. return i;
  128. }
  129.  
  130.  
  131.  
  132. char* strchr(const char* string, int c)
  133. {
  134.         while (*string)
  135.         {
  136.                 if (*string==c)
  137.                         return (char*)string;
  138.                 string++;
  139.         }      
  140.         return (char*)0;
  141. }
  142.  
  143.  
  144. char* strrchr(const char* string, int c)
  145. {
  146.         char* last_found;
  147.         while (*string)
  148.         {
  149.                 if (*string==c)
  150.                 {
  151.                         last_found = (char*)string;
  152.                 }
  153.                 string++;
  154.         }      
  155.         return last_found;
  156. }
  157.  
  158.  
  159.  
  160. void _itoa(int i, char *s)
  161. {
  162. int a, b, c, d;
  163. a = (i - i%1000)/1000;
  164. b = (i - i%100)/100 - a*10;
  165. c = (i - i%10)/10 - a*100 - b*10;
  166. d = i%10;
  167. s[0] = a + '0';
  168. s[1] = b + '0';
  169. s[2] = c + '0';
  170. s[3] = d + '0';
  171. s[4] = 0;
  172. }
  173.  
  174.  
  175.  /* reverse:  ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */
  176.  void reverse(char s[])
  177.  {
  178.      int i, j;
  179.      char c;
  180.  
  181.      for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  182.          c = s[i];
  183.          s[i] = s[j];
  184.          s[j] = c;
  185.      }
  186.  }
  187.  
  188.  
  189.  /* itoa:  êîíâåðòèðóåì n â ñèìâîëû â s */
  190.  void itoa(int n, char s[])
  191.  {
  192.      int i, sign;
  193.  
  194.      if ((sign = n) < 0)
  195.          n = -n;
  196.      i = 0;
  197.      do {
  198.          s[i++] = n % 10 + '0';
  199.      } while ((n /= 10) > 0);
  200.      if (sign < 0)
  201.          s[i++] = '-';
  202.      s[i] = '\0';
  203.      reverse(s);
  204.  }
  205.  
  206.  
  207.  
  208. int _atoi ( char *s )
  209. {
  210. int i, n;
  211.  
  212. n = 0;
  213. for ( i = 0; s[i]!= '\0'; ++i)
  214.         if ((s[i]<'0') || (s[i]>'9'))
  215.                 return 0;
  216.         else
  217.                 n = 10 * n + s[i] - '0';
  218.  
  219. return n;
  220. }
  221.