Subversion Repositories HelenOS

Rev

Rev 1896 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup generic
  30.  * @{
  31.  */
  32.  
  33. #include <string.h>
  34.  
  35. /**
  36.  * @file
  37.  * @brief   String manipulation functions.
  38.  */
  39.  
  40. /** Return number of characters in a string.
  41.  *
  42.  * @param str NULL terminated string.
  43.  *
  44.  * @return Number of characters in str.
  45.  */
  46. size_t strlen(const char *str)
  47. {
  48.     int i;
  49.    
  50.     for (i = 0; str[i]; i++)
  51.         ;
  52.    
  53.     return i;
  54. }
  55.  
  56. /** Compare two NULL terminated strings
  57.  *
  58.  * Do a char-by-char comparison of two NULL terminated strings.
  59.  * The strings are considered equal iff they consist of the same
  60.  * characters on the minimum of their lengths and specified maximal
  61.  * length.
  62.  *
  63.  * @param src First string to compare.
  64.  * @param dst Second string to compare.
  65.  * @param len Maximal length for comparison.
  66.  *
  67.  * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
  68.  *
  69.  */
  70. int strncmp(const char *src, const char *dst, size_t len)
  71. {
  72.     int i;
  73.    
  74.     i = 0;
  75.     for (;*src && *dst && i < len;src++,dst++,i++) {
  76.         if (*src < *dst)
  77.             return -1;
  78.         if (*src > *dst)
  79.             return 1;
  80.     }
  81.     if (i == len || *src == *dst)
  82.         return 0;
  83.     if (!*src)
  84.         return -1;
  85.     return 1;
  86. }
  87.  
  88. /** Copy NULL terminated string.
  89.  *
  90.  * Copy at most 'len' characters from string 'src' to 'dest'.
  91.  * If 'src' is shorter than 'len', '\0' is inserted behind the
  92.  * last copied character.
  93.  *
  94.  * @param src Source string.
  95.  * @param dest Destination buffer.
  96.  * @param len Size of destination buffer.
  97.  */
  98. void strncpy(char *dest, const char *src, size_t len)
  99. {
  100.     int i;
  101.     for (i = 0; i < len; i++) {
  102.         if (!(dest[i] = src[i]))
  103.             return;
  104.     }
  105.     dest[i-1] = '\0';
  106. }
  107.  
  108. /** Convert ascii representation to unative_t
  109.  *
  110.  * Supports 0x for hexa & 0 for octal notation.
  111.  * Does not check for overflows, does not support negative numbers
  112.  *
  113.  * @param text Textual representation of number
  114.  * @return Converted number or 0 if no valid number ofund
  115.  */
  116. unative_t atoi(const char *text)
  117. {
  118.     int base = 10;
  119.     unative_t result = 0;
  120.  
  121.     if (text[0] == '0' && text[1] == 'x') {
  122.         base = 16;
  123.         text += 2;
  124.     } else if (text[0] == '0')
  125.         base = 8;
  126.  
  127.     while (*text) {
  128.         if (base != 16 && \
  129.             ((*text >= 'A' && *text <= 'F' )
  130.              || (*text >='a' && *text <='f')))
  131.             break;
  132.         if (base == 8 && *text >='8')
  133.             break;
  134.  
  135.         if (*text >= '0' && *text <= '9') {
  136.             result *= base;
  137.             result += *text - '0';
  138.         } else if (*text >= 'A' && *text <= 'F') {
  139.             result *= base;
  140.             result += *text - 'A' + 10;
  141.         } else if (*text >= 'a' && *text <= 'f') {
  142.             result *= base;
  143.             result += *text - 'a' + 10;
  144.         } else
  145.             break;
  146.         text++;
  147.     }
  148.  
  149.     return result;
  150. }
  151.  
  152. /** @}
  153.  */
  154.