⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.c

📁 Intrisyc 公司的PXA255-bootloader,源码易懂
💻 C
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////////////// Copyright(c) 2001 Intrinsyc Software Inc. All rights reserved.//// Module name:////      string.c//// Description:////      Various string manipulation functions.//// Author:////      Mike Kirkland//// Created:////      October 2001//////////////////////////////////////////////////////////////////////////////////#include <stdarg.h>#include <types.h>#include <serial.h>#include <util.h>#include <string.h>////////////////////////////////////////////////////////////////////////////////// cmpstr// PURPOSE: Checks for a token at the start of a string, for the purpose of//          extracting a command from a commandline.// PARAMS:  (IN) char *str   - string to search in.//          (IN) char *token - token to search for.// RETURNS: int - 1 for match, 0 for non match////////////////////////////////////////////////////////////////////////////////intcmpstr(char const *str,       char const *token){   if(*str == 0 && *token == 0)   {      return 1;   }   do   {      if(*str++ != *token++)      {         return 0;      }   } while(*token != 0);   if(*str == ' ' || *str == 0 || *str == ':')   {      return 1;   }   else   {      return 0;   }}////////////////////////////////////////////////////////////////////////////////// itc_strcmp// PURPOSE: Checks for a string at the beginning of another string.// PARAMS:  (IN) char *str   - string to search in.//          (IN) char *token - token to search for.// RETURNS: int - 1 for match, 0 for non match////////////////////////////////////////////////////////////////////////////////intitc_strcmp(char const *str1,	   char const *str2){   while(*str1++ == *str2++ && *str1 != 0){;}   if(*str1 == 0)   {      return 1;   }   else   {      return 0;   }}////////////////////////////////////////////////////////////////////////////////// itc_strlen// PURPOSE: Returns the length, in characters, of a given string.// PARAMS:  (IN) char *str - string to count.// RETURNS: int - number of characters in string.////////////////////////////////////////////////////////////////////////////////intitc_strlen(char const *str){   int i = 0;   while(*str++ != '\0')   {      ++i;   }   return i;}/*////////////////////////////////////////////////////////////////////////////////// itc_strlcat// PURPOSE: Catenates a string so the total length is that given-1 and//          nul terminates// PARAMS:  (OUT) char *dest - buffer to append to.//          (IN)  char *src  - string to copy.//          (IN)  size_t size- size of dest buffer// RETURNS: Length of the original dest string plus length of the src string.////////////////////////////////////////////////////////////////////////////////size_titc_strlcat(char *dest,            char const *src,            size_t size){   const size_t len = itc_strlen(dest);   return itc_strlcpy(dest + len, src, size - len) + len;}*/////////////////////////////////////////////////////////////////////////////////// itc_strlcpy// PURPOSE: Copies a string up to the maximum length given and nul terminates// PARAMS:  (OUT) char *dest - buffer to copy into.//          (IN)  char *src  - string to copy.//          (IN)  size_t size- size of dest buffer// RETURNS: Length of the src string.////////////////////////////////////////////////////////////////////////////////size_titc_strlcpy(char *dest,            char const *src,            size_t size){   size_t count = 0;   if (size > 0)   {       // Leave space for nul termination       --size;       while(*src != 0 && count < size)       {          *dest++ = *src++;          ++count;       }       // Nul terminate the destination       *dest = 0;       // Finish counting the length of the source       while(*src++ != 0)       {            ++count;       }   }   return count;}////////////////////////////////////////////////////////////////////////////////// itc_strcpy// PURPOSE: Copies a string// PARAMS:  (OUT) char *dest - buffer to copy into.//          (IN)  char *src  - string to copy.// RETURNS: Nothing.////////////////////////////////////////////////////////////////////////////////voiditc_strcpy(char *dest,           char const *src){   while(*src != 0)   {      *dest++ = *src++;   }   *dest = 0;}////////////////////////////////////////////////////////////////////////////////// itc_printf// PURPOSE: Prints data based on a formatted string.// PARAMS: (IN) char *format - String with codes in it.//         (IN) ...          - variable list, based on the codes in format.// NOTES:  Supports://         %i,%d - integer in decimal format.//         %s    - string//         %x    - u32 in hexadecimal format.//         %p    - void * in hexadecimal format.//         %c    - ASCII character//         %I    - same as %i, except that it splices into the string,//                 overwriting to the right of it.////////////////////////////////////////////////////////////////////////////////voiditc_printf(char const *format,           ...){   va_list vl;   char buf[MAX_STRING_SIZE];   char *formatc = buf;   itc_strlcpy(formatc, format, MAX_STRING_SIZE);   va_start(vl, format);   for(;*formatc != 0;formatc++)   {      if(*formatc != '%')      {         output_byte_serial(*formatc);      }      else      {         switch(*++formatc)         {            case 'd':            case 'i':            {               char temp[12];               temp[itoa(va_arg(vl, int), (char *)temp)] = 0;               output_string_serial((char *)temp);               break;            }            case 'I':            {               itoa(va_arg(vl, int), formatc + 1);               break;            }            case 'p':            case 'x':            {               char temp[9];               u32toa(va_arg(vl, u32), (char *)temp);               temp[8] = 0;               output_string_serial((char *)temp);               break;            }            case 'c':            {               output_byte_serial(va_arg(vl, u8));               break;            }            case 's':            {               output_string_serial(va_arg(vl, char *));               break;            }            case 'b':            {               char temp[3];               u8toa(va_arg(vl, u8), temp);               output_string_serial(temp);               break;            }            default:            {               // Unsupported format character, or %%               output_byte_serial(*formatc);               break;            }         }      }   }   va_end(vl);}////////////////////////////////////////////////////////////////////////////////// itoa// PURPOSE: Converts an int into its ASCII decimal representation.// PARAMS:  (IN)  int number - number to convert.//          (OUT) char *buf  - buffer to write string into.// RETURNS: int - number of characters written into buf.// NOTES:   Does NOT null terminate the string, to allow for replacing %I and//          trailing spaces with the string in itc_printf.//          Does NOT work when number == INT_MIN////////////////////////////////////////////////////////////////////////////////intitoa(int number,     char *buf){   int divisor;   int digit = 0;   int started = 0;   int i = 0;   if (number < 0)   {      // Convert negative number to positive to display      // Note that this will fail for the most negative possible integer      // because that one number doesn't have a positive equivalent.      *buf++ = '-';      ++i;      number = -number;   }   for(divisor = 1000000000; divisor > 0; divisor /= 10)   {      digit = number / divisor;      number %= divisor;       if(digit >= 0 && digit < 10)      {         if(started || digit > 0)         {            *buf++ = (char)('0' + digit);            started = 1;            i++;         }      }   }   if(!started)   {      *buf = '0';      return 1;   }   return i;}////////////////////////////////////////////////////////////////////////////////// u32toa// PURPOSE: Converts a u32 into its ASCII hexadecimal representation.// PARAMS:  (IN)  u32 number - number to convert.//          (OUT) char *buf  - buffer to write string into.// RETURNS: Nothing. (Number of characters is always 8.)// NOTES:   Does NOT null terminate the string, to allow for replacing %X and//          trailing spaces with the string in itc_printf.////////////////////////////////////////////////////////////////////////////////voidu32toa(u32 number,       char *buf){   int i;   for(i = (sizeof(u32) * 2) - 1;i >= 0;i--)   {      u32 temp = (number >> (i * 4)) & 0x0F;      *buf++ = temp + ((temp <= 9) ? '0' : 'A' - 10);   }}#if 0////////////////////////////////////////////////////////////////////////////////// atou8// PURPOSE: Converts an ASCII hexadecimal string into a u8.// PARAMS:  (IN)  char *number - number to convert.//          (OUT) u8 *value    - Number as a u8.// RETURNS: Number of characters converted.////////////////////////////////////////////////////////////////////////////////intatou8(char const *number,      u8 *value){   int count = sizeof(u8) * 2;    *value = 0;    while(*number != 0 && count)   {      if(*number >= '0' && *number <= '9')      {         *value <<= 4;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -