/* CString.cpp - Lightweight printable string class Copyright (c) 2009 Mikal Hart. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include "CString.h" //**************************************************************** //* Construction and destuction //**************************************************************** // Basic constructor requires a preallocated buffer CString::CString(char *strBuff, size_t nSize): m_strBuff(strBuff), m_nBuffSize(nSize) { m_strEndPtr = end(); m_strStartPtr = start(); memset(m_strWorking, 0, BUFF_SIZE); } CString::CString(CString& strOther): m_strBuff(strOther.m_strBuff), m_nBuffSize(strOther.m_nBuffSize) { m_strEndPtr = end(); m_strStartPtr = start(); memset(m_strWorking, 0, BUFF_SIZE); } //**************************************************************** //* Concatenation operator functions //**************************************************************** void CString::operator +=(const char cCh) { uint16_t nLen = strlen(m_strBuff); if ((nLen + 1) < m_nBuffSize) { m_strBuff[nLen] = cCh; m_strBuff[nLen + 1] = 0; } } void CString::operator +=(const char* str) { if ((strlen(m_strBuff) + strlen(str)) < m_nBuffSize) strcat(m_strBuff, str); } void CString::operator +=(const __FlashStringHelper* str) { if ((strlen(m_strBuff) + strlen_P((PGM_P)str)) < m_nBuffSize) strcat_P(m_strBuff, (PGM_P)str); } const char* CString::operator +(const char cCh) { m_strWorking[0] = cCh; m_strWorking[1] = 0; return m_strWorking; } const char* CString::operator +(const char* str) { memcpy(m_strWorking, 0, BUFF_SIZE); strcpy(m_strWorking, m_strBuff); strncat(m_strWorking, str, BUFF_SIZE - 2 - strlen(m_strBuff)); return m_strWorking; } const char* CString::operator +(const __FlashStringHelper* str) { memcpy(m_strWorking, 0, BUFF_SIZE); strncpy_P(m_strWorking, (PGM_P)str, BUFF_SIZE - 2); return m_strWorking; } CString operator +(const char* strLHS, CString& strRHS) { static char strBuff[BUFF_SIZE]; CString strLHSRet(strBuff, BUFF_SIZE); strLHSRet = strLHS; strLHSRet += strRHS; return strLHSRet; } //**************************************************************** //* Assignment operator functions //**************************************************************** void CString::operator =(const char cCh) { m_strBuff[0] = cCh; m_strBuff[1] = 0; } void CString::operator =(const char* str) { if (strlen(str) < m_nBuffSize) strcpy(m_strBuff, str); } void CString::operator =(const __FlashStringHelper* str) { if (strlen_P((PGM_P)str) < m_nBuffSize) strcpy_P(m_strBuff, (PGM_P)str); } //**************************************************************** //* Comparaison operator functions //**************************************************************** bool CString::operator ==(const char *str) { return (m_nBuffSize > 0) && !strcmp(m_strBuff, str); } bool CString::operator ==(const __FlashStringHelper *str) { return (m_nBuffSize > 0) && !strcmp_P(m_strBuff, (PGM_P)str); } //**************************************************************** //* Miscellaneous functions //**************************************************************** // Empty the string. void CString::empty() { if (m_nBuffSize > 0) m_strBuff[0] = '\0'; } //**************************************************************** //* Search functions functions //**************************************************************** int16_t CString::indexOf(const char cCh) { int16_t nI = 0; while ((m_strBuff[nI] != cCh) && (nI < m_nBuffSize) && (nI < strlen(m_strBuff))) nI++; if ((nI >= strlen(m_strBuff)) && (nI < m_nBuffSize)) nI = -1; return nI; } int16_t CString::indexOf(const char* str) { int16_t nI = -1; m_strStartPtr = strstr(m_strBuff, str); if (m_strStartPtr) nI = m_strStartPtr - m_strBuff; return nI; } int16_t CString::indexOf(const __FlashStringHelper* str) { int16_t nI = -1; m_strStartPtr = strstr_P(m_strBuff, (PGM_P)str); if (m_strStartPtr) nI = m_strStartPtr - m_strBuff; return nI; } int16_t CString::reverseIndexOf(const char cCh) { int16_t nI = strlen(m_strBuff) - 1; while ((m_strBuff[nI] != cCh) && (nI >= 0)) nI--; return nI; } int16_t CString::reverseIndexOf(const char* str) { int16_t nI = -1; m_strStartPtr = m_strBuff + strlen(m_strBuff) - 1; while (m_strStartPtr >= m_strBuff) { if (strstr(m_strStartPtr, str) != NULL) { nI = m_strStartPtr - m_strBuff; break; } m_strStartPtr--; } return nI; } int16_t CString::reverseIndexOf(const __FlashStringHelper* str) { int16_t nI = -1; m_strStartPtr = m_strBuff + strlen(m_strBuff) - 1; while (m_strStartPtr >= m_strBuff) { if (strstr_P(m_strStartPtr, (PGM_P)str) != NULL) { nI = m_strStartPtr - m_strBuff; break; } m_strStartPtr--; } return nI; } //**************************************************************** //* Formatting and conversion functions //**************************************************************** int CString::format(const char *str, ...) { va_list argptr; va_start(argptr, str); int ret = vsnprintf_P(m_strStartPtr, m_nBuffSize - (m_strStartPtr - m_strBuff), str, argptr); if (m_nBuffSize) while (*m_strStartPtr) ++m_strStartPtr; return ret; } const char* CString::fromBool(const bool bVal) { memset(m_strWorking, 0, BUFF_SIZE); if (bVal) strcpy_P(m_strWorking, (PGM_P)F("true")); else strcpy_P(m_strWorking, (PGM_P)F("false")); return m_strWorking; } void CString::convertHex2Bin(char *strHex, const uint8_t nCapacity) { char strTemp[nCapacity]; memset(strTemp, 0, nCapacity); for (uint16_t nI = 0; nI < strlen(strHex); nI++) { if ((strlen(strTemp) + 4) <= nCapacity) { switch (strHex[nI]) { case '0': strcat_P(strTemp, (PGM_P)F("0000")); break; case '1': strcat_P(strTemp, (PGM_P)F("0001")); break; case '2': strcat_P(strTemp, (PGM_P)F("0010")); break; case '3': strcat_P(strTemp, (PGM_P)F("0011")); break; case '4': strcat_P(strTemp, (PGM_P)F("0100")); break; case '5': strcat_P(strTemp, (PGM_P)F("0101")); break; case '6': strcat_P(strTemp, (PGM_P)F("0110")); break; case '7': strcat_P(strTemp, (PGM_P)F("0111")); break; case '8': strcat_P(strTemp, (PGM_P)F("1000")); break; case '9': strcat_P(strTemp, (PGM_P)F("1001")); break; case 'A': case 'a': strcat_P(strTemp, (PGM_P)F("1010")); break; case 'B': case 'b': strcat_P(strTemp, (PGM_P)F("1011")); break; case 'C': case 'c': strcat_P(strTemp, (PGM_P)F("1100")); break; case 'D': case 'd': strcat_P(strTemp, (PGM_P)F("1101")); break; case 'E': case 'e': strcat_P(strTemp, (PGM_P)F("1110")); break; case 'F': case 'f': strcat_P(strTemp, (PGM_P)F("1111")); break; }; } } strcpy(strHex, strTemp); } const char* CString::fromUint(const uint32_t nNum, const uint8_t nBase) { memset(m_strBuff, 0, m_nBuffSize); utoa(nNum, m_strBuff, nBase); return m_strBuff; } const char* CString::fromInt(const int32_t nNum, const uint8_t nBase) { memset(m_strBuff, 0, m_nBuffSize); itoa(nNum, m_strBuff, nBase); return m_strBuff; } const char* CString::fromReal(const double dNum, const uint8_t nBase) { memset(m_strBuff, 0, m_nBuffSize); ultoa(dNum, m_strBuff, nBase); return m_strBuff; } uint32_t CString::toUint(const uint8_t nBase) { int8_t nI = 0; uint32_t nVal = 0, nMult = 0; if ((nBase >= 2) && (nBase <= 16)) { for (nI = strlen(m_strBuff) - 1, nMult = 1; nI >= 0; nI--, nMult *= nBase) { if ((m_strBuff[nI] >= '0') && (m_strBuff[nI] <= '9')) nVal += (m_strBuff[nI] - '0') * nMult; else if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) nVal += (m_strBuff[nI] - 'W') * nMult; else if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) nVal += (m_strBuff[nI] - '7') * nMult; } } return nVal; } int32_t CString::toInt(const uint8_t nBase) { int8_t nI = 0; int32_t nVal = 0, nMult = 0; if ((nBase >= 2) && (nBase <= 16)) { for (nI = strlen(m_strBuff) - 1, nMult = 1; nI >= 0; nI--, nMult *= nBase) { if (m_strBuff[nI] == '-') nVal *= -1; else if ((m_strBuff[nI] >= '0') && (m_strBuff[nI] <= '9')) nVal += (m_strBuff[nI] - '0') * nMult; else if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) nVal += (m_strBuff[nI] - 'W') * nMult; else if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) nVal += (m_strBuff[nI] - '7') * nMult; } } return nVal; } double CString::toReal(uint8_t nBase) { int8_t nI = 0, nSign = 0; uint8_t nPlaces = 0, nPos = 0, nLen = 0, nPow = 0, nMult = 0; double dVal = 0.0, dMult = 0; nPos = indexOf("."); nPlaces = nPos - 1; nSign = 1; if (m_strBuff[0] == '-') { nSign = -1; m_strBuff[0] = '0'; } else nSign = 1; // EXAMPLE: 123.456e-67 if ((strstr(m_strBuff, "e-") != NULL) || (strstr(m_strBuff, "e+") != NULL) || (strstr(m_strBuff, "E-") != NULL) || (strstr(m_strBuff, "E+") != NULL)) { for (nI = 0, nLen = strlen(m_strBuff), dMult = pow(nBase, nPlaces); nI < nLen; nI++, dMult /= nBase) { if (m_strBuff[nI] == '.') dMult *= nBase; else if ((m_strBuff[nI] == 'e') || (m_strBuff[nI] == 'E')) break; else if ((m_strBuff[nI] >= '0') && (m_strBuff[nI] <= '9')) dVal += (m_strBuff[nI] - '0') * dMult; else if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) dVal += (m_strBuff[nI] - 'W') * dMult; else if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) dVal += (m_strBuff[nI] - '7') * dMult; } dVal *= nSign; nI = nLen - 1; nMult = 1; while ((m_strBuff[nI] != '-') && (m_strBuff[nI] != '+')) { if ((m_strBuff[nI] >= '0') && (m_strBuff[nI] <= '9')) nPow += (m_strBuff[nI] - '0') * nMult; else if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) nPow += (m_strBuff[nI] - 'W') * nMult; else if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) nPow += (m_strBuff[nI] - '7') * nMult; nMult *= nBase; nI--; } if (m_strBuff[nI] == '-') nSign = -1; else nSign = 1; dVal *= pow(nBase, nPow * nSign); } // EXAMPLE: 123.456 else { for (nI = 0, nLen = strlen(m_strBuff), dMult = pow(nBase, nPlaces); nI <= nLen; nI++, dMult /= nBase) { if (m_strBuff[nI] == '.') dMult *= nBase; else if ((m_strBuff[nI] >= '0') && (m_strBuff[nI] <= '9')) dVal += (m_strBuff[nI] - '0') * dMult; else if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) dVal += (m_strBuff[nI] - 'W') * dMult; else if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) dVal += (m_strBuff[nI] - '7') * dMult; } dVal *= nSign; } return dVal; } //**************************************************************** //* Modification functions //**************************************************************** void CString::replace(const char cFind, const char cReplace) { char strFind[2], strReplace[2]; strFind[0] = cFind; strFind[1] = 0; strReplace[0] = cReplace; strReplace[1] = 0; replace_remove(strFind, strReplace); } void CString::removec(const char cFind) { char strFind[2]; strFind[0] = cFind; strFind[1] = 0; replace_remove(strFind, NULL); } void CString::replace_remove(const char* strFind, const char* strReplace) { bool bDone = false; memset(m_strWorking, 0, BUFF_SIZE); m_strStartPtr = m_strEndPtr = start(); while (!bDone) { m_strEndPtr = strstr(m_strStartPtr, strFind); if (m_strEndPtr != NULL) { concat(m_strStartPtr, m_strEndPtr - 1, m_strWorking, BUFF_SIZE); if (strReplace) concat(strReplace, strReplace + strlen(strReplace) - 1, m_strWorking, BUFF_SIZE); m_strStartPtr = m_strEndPtr + strlen(strFind); m_strEndPtr = m_strStartPtr; } else { bDone = true; concat(m_strStartPtr, m_strStartPtr + strlen(m_strStartPtr) - 1, m_strWorking, BUFF_SIZE); memset(m_strBuff, 0, m_nBuffSize); strcpy(m_strBuff, m_strWorking); } } } void CString::remove(const uint16_t nPos, uint16_t nCount) { memset(m_strWorking, 0, BUFF_SIZE); for (uint8_t nI = 0, nLen = strlen(m_strBuff), nJ = 0; nI < nLen; nI++) { if ((nI < nPos) || (nI >= (nPos + nCount))) m_strWorking[nJ++] = m_strBuff[nI]; } memset(m_strBuff, 0, BUFF_SIZE); strcpy(m_strBuff, m_strWorking); } void CString::toLower() { for (uint16_t nI = 0; nI < strlen(m_strBuff); nI++) { if ((m_strBuff[nI] >= 'A') && (m_strBuff[nI] <= 'Z')) m_strBuff[nI] += 32; } } void CString::toUpper() { for (uint16_t nI = 0; nI < strlen(m_strBuff); nI++) { if ((m_strBuff[nI] >= 'a') && (m_strBuff[nI] <= 'z')) m_strBuff[nI] -= 32; } } void CString::concat(const char *strStart, const char* strEnd, char* strDest, const uint16_t nSize) { uint16_t nI = strlen(strDest); while ((strStart <= strEnd) && (nI < nSize)) { strDest[nI] = strStart[0]; strDest[nI + 1] = 0; strStart++; nI++; } } void CString::copy(const char *strStart, const char* strEnd, char* strDest, const uint16_t nSize) { uint16_t nI = 0; memset(strDest, 0, nSize); while ((strStart <= strEnd) && (nI < nSize)) { strDest[nI] = *strStart; strStart++; nI++; } } void CString::trimLeft() { m_strStartPtr = start(); m_strEndPtr = end(); while (isWhitespace(*m_strStartPtr) && (m_strStartPtr <= m_strEndPtr)) m_strStartPtr++; copy(m_strStartPtr, m_strEndPtr, m_strWorking, m_nBuffSize); strcpy(m_strBuff, m_strWorking); } void CString::trimRight() { m_strStartPtr = start(); m_strEndPtr = end(); while (isWhitespace(*m_strEndPtr) && (m_strEndPtr >= m_strStartPtr)) m_strEndPtr--; copy(m_strStartPtr, m_strEndPtr, m_strWorking, m_nBuffSize); strcpy(m_strBuff, m_strWorking); } void CString::trim() { trimLeft(); trimRight(); }