Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App

Dependencies:   NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers strlib.cpp Source File

strlib.cpp

00001 /*****************************************************************************
00002 *
00003 *  strlib.c - String Library functions implementation
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 
00036 /*#include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>*/
00039 #include "mbed.h"
00040 #include "strlib.h"
00041 
00042 //*****************************************************************************
00043 //
00044 //! \brief   Integer to ASCII
00045 //!
00046 //! \param  n is the number to be converted to ASCII
00047 //! \param s is a pointer to an array where the ASCII string will be placed
00048 //! \param b is the base (10 for decimal)
00049 //!
00050 //! \return none
00051 //
00052 //*****************************************************************************
00053 char *itoa(int n, char *s, int b)
00054 {
00055     const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
00056     unsigned int i = 0;
00057     int sign;
00058     
00059     if ((sign = n) < 0)
00060         n = -n;
00061 
00062     do {
00063         s[i++] = digits[n % b];
00064     } while ((n /= b) > 0);
00065 
00066     if (sign < 0)
00067         s[i++] = '-';
00068     s[i] = '\0';
00069 
00070     return strrev(s);
00071 }
00072 
00073 //*****************************************************************************
00074 //
00075 //! \brief   Reverses a string
00076 //!
00077 //! \param  str is a pointer to the string to be reversed
00078 //!
00079 //! \return none
00080 //
00081 //*****************************************************************************
00082 char *strrev(char *str)
00083 {
00084     char *p1, *p2;
00085 
00086     if (!str || !*str)
00087         return str;
00088 
00089     for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
00090         *p1 ^= *p2;
00091         *p2 ^= *p1;
00092         *p1 ^= *p2;
00093     }
00094 
00095     return str;
00096 }
00097 
00098 //*****************************************************************************
00099 //
00100 //! atoc
00101 //!
00102 //! \param  none
00103 //!
00104 //! \return none
00105 //!
00106 //! \brief  Convert nibble to hexdecimal from ASCII
00107 //
00108 //*****************************************************************************
00109 unsigned char
00110 atoc(char data)
00111 {
00112     unsigned char ucRes = 0;
00113 
00114     if ((data >= 0x30) && (data <= 0x39))
00115     {
00116         ucRes = data - 0x30;
00117     }
00118     else
00119     {
00120         if (data == 'a')
00121         {
00122             ucRes = 0x0a;;
00123         }
00124         else if (data == 'b')
00125         {
00126             ucRes = 0x0b;
00127         }
00128         else if (data == 'c')
00129         {
00130             ucRes = 0x0c;
00131         }
00132         else if (data == 'd')
00133         {
00134             ucRes = 0x0d;
00135         }
00136         else if (data == 'e')
00137         {
00138             ucRes = 0x0e;
00139         }
00140         else if (data == 'f')
00141         {
00142             ucRes = 0x0f;
00143         }
00144     }
00145 
00146 
00147     return ucRes;
00148 }
00149 
00150 
00151 //*****************************************************************************
00152 //
00153 //! atoshort
00154 //!
00155 //! \param  none
00156 //!
00157 //! \return none
00158 //!
00159 //! \brief  Convert 2 nibbles in ASCII into a short number
00160 //
00161 //*****************************************************************************
00162 
00163 unsigned short
00164 atoshort(char b1, char b2)
00165 {
00166     unsigned short usRes;
00167 
00168     usRes = (atoc(b1)) * 16 | atoc(b2);
00169 
00170     return usRes;
00171 }
00172 
00173 //*****************************************************************************
00174 //
00175 //! ascii_to_char
00176 //!
00177 //! \param  none
00178 //!
00179 //! \return none
00180 //!
00181 //! \brief  Convert 2 bytes in ASCII into one character
00182 //
00183 //*****************************************************************************
00184 
00185 unsigned char
00186 ascii_to_char(char b1, char b2)
00187 {
00188     unsigned char ucRes;
00189 
00190     ucRes = (atoc(b1)) << 4 | (atoc(b2));
00191 
00192     return ucRes;
00193 }