Just4Trionic - CAN and BDM FLASH programmer for Saab cars

Dependencies:   mbed

strings.cpp

Committer:
Just4pLeisure
Date:
2010-05-19
Revision:
0:e0b964252a05
Child:
1:d5452e398b76

File content as of revision 0:e0b964252a05:

/*******************************************************************************

Strings.cpp - By Sophie Dexter, April 2010

This C++ module provides functions for working with 'strings' of ascii 'char's

C++ should have functions like these, but it doesn't seem to so these are my
own ones. They are very simple and do not check for any errors e.g. StrCpy
does not check that s1 is big enough to store all of s2.

********************************************************************************

WARNING: Use at your own risk, sadly this software comes with no guarantees.
This software is provided 'free' and in good faith, but the author does not
accept liability for any damage arising from its use.

*******************************************************************************/
#include "strings.h"

// copies a string, s2, (array of chars) to another string, s1.
char *StrCpy(char *s1, char *s2) {
//    while (*s1++ = *s2++);
    while (*s2)
        *s1++ = *s2++;
    return s1;
}

// returns the number of chars in a string
int StrLen(char *s) {
    int x = 0;
    while (*s++)
        x++;
    return (x);
}

// checks s1 to see if it the same as s2
// returns TRUE if there is a match
// WARNING actually only checks that s1 starts with s2!
bool StrCmp(char *s1, char *s2) {
//    while (*s2 != '\0') {
    while (*s2) {
        if (*s1++ != *s2++) {
            return FALSE;
        }
    }
    return TRUE;
}

// Converts lower case ascii letters a-z to upper case
// leaves other characters unchanged
char ToUpper(char c) {
    if (c >= 'a' && c <= 'z')
        c -= 32;
    return c;
}

// Converts upper case ascii letters A-Z to lower case
// leaves other characters unchanged
char ToLower(char c) {
    if (c >= 'A' && c <= 'Z')
        c += 32;
    return c;
}

// Converts ASCII numbers 0-9 and letters a-f (and A-F) to hex values 0x00-0x0F
// leaves other characters unchanged
// ASCII '0' is worth 0x30 so subtract 0x30 if ascii character is a number
// Lower case ASCII letter 'a' is worth 0x61, but we want this to be worth 0x0A
// Subtract 0x57 (0x61 + 0x0A = 0x57) from lower case letters
// Upper case ASCII letter 'A' is worth 0x41, but we want this to be worth 0x0A
// Subtract 0x37 (0x41 + 0x0A = 0x37) from upper case letters

char *aToh(char *s) {
    while (*s) {
        if ((*s >= '0') && (*s <='9'))
            *s -= '0';
        else if ((*s >= 'a') && (*s <='f'))
            *s -= ('a' - 0x0A);
        else if ((*s >= 'A') && (*s <='F'))
            *s -= ('A' - 0x0A);
        s++;
    }
    return s;
}