Subversion Repositories Kolibri OS

Rev

Rev 6857 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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