Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers strings.cpp Source File

strings.cpp

00001 /*******************************************************************************
00002 
00003 strings.cpp 
00004 (c) 2010 by Sophie Dexter
00005 
00006 This C++ module provides functions for working with 'strings' of ascii 'char's
00007 
00008 C++ should have functions like these, but it doesn't seem to so these are my
00009 own ones. They are very simple and do not check for any errors e.g. StrCpy
00010 does not check that s1 is big enough to store all of s2.
00011 
00012 ********************************************************************************
00013 
00014 WARNING: Use at your own risk, sadly this software comes with no guarantees.
00015 This software is provided 'free' and in good faith, but the author does not
00016 accept liability for any damage arising from its use.
00017 
00018 *******************************************************************************/
00019 
00020 #include "strings.h"
00021 
00022 // copies a string, s2, (array of chars) to another string, s1.
00023 char *StrCpy(char *s1, char *s2) {
00024 //    while (*s1++ = *s2++);
00025     while (*s2)
00026         *s1++ = *s2++;
00027     return s1;
00028 }
00029 
00030 // returns the number of chars in a string
00031 int StrLen(char *s) {
00032     int x = 0;
00033     while (*s++)
00034         x++;
00035     return (x);
00036 }
00037 
00038 // checks s1 to see if it the same as s2
00039 // returns TRUE if there is a match
00040 // WARNING actually only checks that s1 starts with s2!
00041 bool StrCmp(char *s1, char *s2) {
00042 //    while (*s2 != '\0') {
00043     while (*s2) {
00044         if (*s1++ != *s2++) {
00045             return FALSE;
00046         }
00047     }
00048     return TRUE;
00049 }
00050 
00051 // Converts lower case ascii letters a-z to upper case
00052 // leaves other characters unchanged
00053 char ToUpper(char c) {
00054     if (c >= 'a' && c <= 'z')
00055         c -= 32;
00056     return c;
00057 }
00058 
00059 // Converts upper case ascii letters A-Z to lower case
00060 // leaves other characters unchanged
00061 char ToLower(char c) {
00062     if (c >= 'A' && c <= 'Z')
00063         c += 32;
00064     return c;
00065 }
00066 
00067 // Converts ASCII numbers 0-9 and letters a-f (and A-F) to hex values 0x00-0x0F
00068 // leaves other characters unchanged
00069 // ASCII '0' is worth 0x30 so subtract 0x30 if ascii character is a number
00070 // Lower case ASCII letter 'a' is worth 0x61, but we want this to be worth 0x0A
00071 // Subtract 0x57 (0x61 + 0x0A = 0x57) from lower case letters
00072 // Upper case ASCII letter 'A' is worth 0x41, but we want this to be worth 0x0A
00073 // Subtract 0x37 (0x41 + 0x0A = 0x37) from upper case letters
00074 
00075 char *aToh(char *s) {
00076     while (*s) {
00077         if ((*s >= '0') && (*s <='9'))
00078             *s -= '0';
00079         else if ((*s >= 'a') && (*s <='f'))
00080             *s -= ('a' - 0x0A);
00081         else if ((*s >= 'A') && (*s <='F'))
00082             *s -= ('A' - 0x0A);
00083         s++;
00084     }
00085     return s;
00086 }
00087 
00088 // StrAddc    adds a single char to the end of a string
00089 
00090 char *StrAddc (char *s, const char c) {
00091   char *s1 = s;
00092 
00093 // Find the end of the string 's'
00094   while (*s)
00095     *s++;
00096 // add the new character
00097   *s++ = c;
00098 // put the end of string character at its new position
00099   *s = '\0';
00100   return s1;
00101 }
00102 
00103 //-----------------------------------------------------------------------------
00104 /**
00105     Converts an ASCII character to low nibble of a byte.
00106 
00107     @param        rx_byte            character to convert
00108     
00109     @return                        resulting value            
00110 */
00111 uint8_t ascii2nibble(char str)
00112 {
00113     return str >= 'a' ? (str - 'a' + 10) & 0x0f :
00114         (str >= 'A' ? (str - 'A' + 10) & 0x0f :
00115         (str - '0') & 0x0f);
00116 }
00117 
00118 //-----------------------------------------------------------------------------
00119 /**
00120     Converts an ASCII string to integer (checks string contents beforehand).
00121 
00122     @param        val                destination integer
00123     @param        str                pointer to source string
00124     @param        length            length of source string
00125 
00126     @return                        succ / fail
00127 */
00128 bool ascii2int(uint32_t* val, const char* str, uint8_t length)
00129 {
00130     // nothing to convert
00131     if (!str || length < 1)
00132     {
00133         *val = 0;
00134         return false;
00135     }
00136 
00137     // check string contents
00138     uint8_t shift;
00139     for (shift = 0; shift < length; ++shift)
00140     {
00141         if (!isxdigit(*(str + shift)))
00142         {
00143             // not a hex value
00144             *val = 0;
00145             return false;    
00146         } 
00147     }
00148 
00149     // convert string
00150     *val = ascii2nibble(*(str++));
00151     for (shift = 1; shift < length; ++shift)
00152     {
00153         *val <<= 4;
00154         *val += ascii2nibble(*(str++));
00155     }
00156     return true;
00157 }
00158 
00159 int isxdigit ( int ch )
00160 {
00161     return (unsigned int)( ch         - '0') < 10u  ||
00162            (unsigned int)((ch | 0x20) - 'a') <  6u;
00163 }