/**************************************************************************************************
*
*  StringBuilder
*
*   Version:      1.0.1 - Mai 2009
*   Author:       Etienne Ribeiro     / tutorial assistant caad      /  eribeiro[at]ethz.ch
*   Supervisor:   Christoph Wartmann  / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*
*   Desc:         Basic library to concate String/String, String/int, String/long and String/char.
*                 Wiring doesn't support "string1" + "string2" or "string" + number, so use this
*                 functions instead.
*
*   Methodes:     int BuildString (char* ret, char* str1, char* str2)
*                    ret: array of char. must be large enough otherwise your sketch will CRASH!!!
*                    str1: first string
*                    str2: string to append
*                    return the length of the new string
*                 int BuildString (char* ret, char* str1, char str2)
*                    ret: array of char. must be large enough otherwise your sketch will CRASH!!!
*                    str1: first string
*                    str2: char to append
*                    return the length of the new string
*                 int BuildString (char* ret, char* string, int number)
*                    ret: array of char. must be large enough otherwise your sketch will CRASH!!!
*                    string: first string
*                    number: number to append
*                    return the length of the new string
*                 int BuildString (char* ret, char* string, long number)
*                    ret: array of char. must be large enough otherwise your sketch will CRASH!!!
*                    string: first string
*                    number: number to append
*                    return the length of the new string
*
***************************************************************************************************/


int BuildString (char* ret, char* str1, char* str2) {

        int count = 0;
        while (*str1 != '\0') { // add str1 to ret
                *ret++ = *str1++;
                count++;
        }
        while (*str2 != '\0') { // add str2 to ret
                *ret++ = *str2++;
                count++;
        }
        *ret = 0;
        return count;

}
int BuildString (char* ret, char* str1, char str2) {

        char tmp[2];
        tmp[0] = str2;
        tmp[1] = 0;
        return BuildString (ret, str1, tmp);

}
int BuildString (char* ret, char* string, int number) {

        char buff[20];
        BuildString_Parse (number, buff);
        return BuildString (ret, string, buff);

}
int BuildString (char* ret, char* string, long number) {

        char buff[20];
        BuildString_Parse (number, buff);
        return BuildString (ret, string, buff);

}








int BuildString_Parse (long Value, char* ret) {

        int base = 10;
        int buf[8 * sizeof(long)]; // Assumes 8-bit chars.
        int i = 0;

        // Value = "0"
        if (Value == 0) {
                *ret++ = '0';
                *ret++ = 0;
                return 1;
        }

        // Sign (negative numbers)
        if (Value < 0) {
                *ret++ = '-';
                Value *= -1;
        }

        //
        while (Value > 0) {
                buf[i++] = Value % base;
                Value /= base;
        }
        int len = i;
        for (i--; i >= 0; i--){
                *ret++ =  (char)(buf[i] < 10 ? '0' + buf[i] : 'A' + buf[i] - 10);
        }
        *ret++ = 0;
        return len;

}