SAM3S8/SD8 Software Package: sam-ba_applets/common/samba_string.c Source File

SAM3S8/SD8 Software Package 1.2

# samba_string.c

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2010, Atmel Corporation
    00005  *
    00006  * All rights reserved.
    00007  *
    00008  * Redistribution and use in source and binary forms, with or without
    00009  * modification, are permitted provided that the following conditions are met:
    00010  *
    00011  * - Redistributions of source code must retain the above copyright notice,
    00012  * this list of conditions and the disclaimer below.
    00013  *
    00014  * Atmel's name may not be used to endorse or promote products derived from
    00015  * this software without specific prior written permission.
    00016  *
    00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
    00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
    00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
    00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00027  * ----------------------------------------------------------------------------
    00028  */
    00029
    00030 /**
    00031  * \section Purpose
    00032  *
    00033  * Implementation of several methods defined in string.h, for reducing the
    00034  * memory footprint when using them (since the whole libc.o file gets included
    00035  * even when using a single method).
    00036  *
    00037  * \section Usage
    00038  *
    00039  * Add string.c to the list of files to compile for the project. This will
    00040  * automatically replace standard libc methods by the custom ones.
    00041  */
    00042
    00043 /*----------------------------------------------------------------------------
    00044  *        Headers
    00045  *----------------------------------------------------------------------------*/
    00046
    00047 #include <string.h>
    00048
    00049 /*----------------------------------------------------------------------------
    00050  *        Exported functions
    00051  *----------------------------------------------------------------------------*/
    00052
    00053 /**
    00054  * \brief Copies data from a source buffer into a destination buffer. The two buffers
    00055  * must NOT overlap. Returns the destination buffer.
    00056  *
    00057  * \param pDestination  Destination buffer.
    00058  * \param pSource  Source buffer.
    00059  * \param num  Number of bytes to copy.
    00060  */
    00061 void * memcpy(void *pDestination, const void *pSource, size_t num)
    00062 {
    00063     unsigned char *pByteDestination;
    00064     unsigned char *pByteSource;
    00065     unsigned int *pAlignedSource = (unsigned int *) pSource;
    00066     unsigned int *pAlignedDestination = (unsigned int *) pDestination;
    00067
    00068     /* If num is more than 4 bytes, and both dest. and source are aligned, */
    00069     /* then copy dwords */
    00070     if ((((unsigned int) pAlignedDestination & 0x3) == 0)
    00071         && (((unsigned int) pAlignedSource & 0x3) == 0)
    00072         && (num >= 4)) {
    00073
    00074         while (num >= 4) {
    00075
    00076             *pAlignedDestination++ = *pAlignedSource++;
    00077             num -= 4;
    00078         }
    00079     }
    00080
    00081     /* Copy remaining bytes */
    00082     pByteDestination = (unsigned char *) pAlignedDestination;
    00083     pByteSource = (unsigned char *) pAlignedSource;
    00084     while (num--) {
    00085
    00086         *pByteDestination++ = *pByteSource++;
    00087     }
    00088
    00089     return pDestination;
    00090 }
    00091
    00092 /**
    00093  * \brief Fills a memory region with the given value. Returns a pointer to the
    00094  * memory region.
    00095  *
    00096  * \param pBuffer  Pointer to the start of the memory region to fill
    00097  * \param value    Value to fill the region with
    00098  * \param num      Size to fill in bytes
    00099  */
    00100 void * memset(void *pBuffer, int value, size_t num)
    00101 {
    00102     unsigned char *pByteDestination;
    00103     unsigned int  *pAlignedDestination = (unsigned int *) pBuffer;
    00104     unsigned int  alignedValue = (value << 24) | (value << 16) | (value << 8) | value;
    00105
    00106     /* Set words if possible */
    00107     if ((((unsigned int) pAlignedDestination & 0x3) == 0) && (num >= 4)) {
    00108         while (num >= 4) {
    00109             *pAlignedDestination++ = alignedValue;
    00110             num -= 4;
    00111         }
    00112     }
    00113     /* Set remaining bytes */
    00114     pByteDestination = (unsigned char *) pAlignedDestination;
    00115     while (num--) {
    00116         *pByteDestination++ = value;
    00117     }
    00118     return pBuffer;
    00119 }
    00120
    00121 /**
    00122  * \brief Search a character in the given string.
    00123  *
    00124  * \param pString   Pointer to the start of the string to search.
    00125  * \param character The character to find.
    00126  *
    00127  * \return Returns a pointer to the character location.
    00128  */
    00129 char * strchr(const char *pString, int character)
    00130 {
    00131     char * p = (char *)pString;
    00132     char   c = character & 0xFF;
    00133
    00134     while(*p != c) {
    00135         if (*p == 0) {
    00136             return 0;
    00137         }
    00138         p++;
    00139     }
    00140     return p;
    00141 }
    00142
    00143 /**
    00144  * \brief Get the length of a given string
    00145  *
    00146  * \param pString Pointer to the start of the string.
    00147  *
    00148  * \return Return the length of a given string.
    00149  */
    00150 size_t strlen(const char *pString)
    00151 {
    00152     unsigned int length = 0;
    00153
    00154     while(*pString++ != 0) {
    00155         length++;
    00156     }
    00157     return length;
    00158 }
    00159
    00160
    00161 /**
    00162  * \brief Search a character backword from the end of given string.
    00163  *
    00164  * \param pString   Pointer to the start of the string to search.
    00165  * \param character The character to find.
    00166  *
    00167  * \return Returns a pointer to the character location.
    00168  */
    00169 char * strrchr(const char *pString, int character)
    00170 {
    00171     char *p = 0;
    00172
    00173     while(*pString != 0) {
    00174         if (*pString++ == character) {
    00175             p = (char*)pString;
    00176         }
    00177     }
    00178     return p;
    00179 }
    00180
    00181 /**
    00182  * \breif Copy from source string to destination string.
    00183  *
    00184  * \param pDestination Pointer to the destination string.
    00185  * \param pSource      Pointer to the source string.
    00186  *
    00187  * \return Return a pointer to the destination string.
    00188  */
    00189 char * strcpy(char *pDestination, const char *pSource)
    00190 {
    00191     char *pSaveDest = pDestination;
    00192
    00193     for(; (*pDestination = *pSource) != 0; ++pSource, ++pDestination);
    00194     return pSaveDest;
    00195 }
    00196
    00197 /**
    00198  * \brief Compare the first specified bytes of 2 given strings
    00199  *
    00200  * \param pString1 Pointer to the start of the 1st string.
    00201  * \param pString2 Pointer to the start of the 2nd string.
    00202  * \param count    Number of bytes that should be compared.
    00203  *
    00204  * \return Return 0 if equals,
    00205  * Return >0 if 1st string > 2nd string
    00206  * Return <0 if 1st string < 2nd string
    00207  */
    00208 int strncmp(const char *pString1, const char *pString2, size_t count)
    00209 {
    00210     int r;
    00211
    00212     while(count) {
    00213         r = *pString1 - *pString2;
    00214         if (r == 0) {
    00215             if (*pString1 == 0) {
    00216                 break;
    00217             }
    00218             pString1++;
    00219             pString2++;
    00220             count--;
    00221             continue;
    00222         }
    00223         return r;
    00224     }
    00225     return 0;
    00226 }
    00227
    00228 /**
    00229  * \brief Copy the first number of bytes from source string to destination string
    00230  *
    00231  * \param pDestination Pointer to the start of destination string.
    00232  * \param pSource      Pointer to the start of the source string.
    00233  * \param count        Number of bytes that should be copied.
    00234  *
    00235  * \return Return the pointer to the destination string.
    00236  */
    00237 char * strncpy(char *pDestination, const char *pSource, size_t count)
    00238 {
    00239     char *pSaveDest = pDestination;
    00240
    00241     while (count) {
    00242         *pDestination = *pSource;
    00243         if (*pSource == 0) {
    00244             break;
    00245         }
    00246         pDestination++;
    00247         pSource++;
    00248         count--;
    00249     }
    00250     return pSaveDest;
    00251 }
    00252

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines