Library to use Arduino USB host shield on mbed

Dependents:   USBHOST_PS5

ArduinoのUSB Host Shield 2.0をmbedで使えるようにしたライブラリです。
大体のコードがArduinoからそのまま移植可能です。

Arduino UNOやMega用のホストシールド以外にもミニサイズのホストシールドでも使用可能です https://os.mbed.com/media/uploads/kotakku/dffgfddswa.png

シールドについて

3.3VのI/O用にシールドの改造が必要になりますがネット上に記事がたくさんあるのでそちらを参考にしてください

接続例

https://os.mbed.com/media/uploads/kotakku/esgsvfvhjrekldkcjxvb.png

使い方

Arduinoのコードと違うのはUSBのインスタンスの宣言部分のみです。
ピンを自分で指定できるようにしたので使いやすくなりました。

仕様

  • Arduinoのmillis関数、micros関数の移植のために内部でTimerクラスを使用しています。

main.cpp

#include "mbed.h"
#include <PS3BT.h>
#include <usbhub.h>

Serial pc(USBTX, USBRX, 115200);

//Nucleo f303k8用
USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr
BTD Btd(&Usb);
PS3BT PS3(&Btd);

int main()
{
    bool printAngle = false;

    if (Usb.Init() == -1)
    {
        pc.printf("\r\nOSC did not start");
        while (1); // Halt
    }
    pc.printf("\r\nPS3 USB Library Started");

    while (1)
    {
        Usb.Task();
        
        if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
            if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117)
            {
                pc.printf("\r\nLeftHatX: %d", PS3.getAnalogHat(LeftHatX));
                pc.printf("\tLeftHatY: %d", PS3.getAnalogHat(LeftHatY));
                if (PS3.PS3Connected)
                { // The Navigation controller only have one joystick
                    pc.printf("\tRightHatX: %d", PS3.getAnalogHat(RightHatX));
                    pc.printf("\tRightHatY: %d", PS3.getAnalogHat(RightHatY));
                }
            }
            // Analog button values can be read from almost all buttons
            if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2))
            {
                pc.printf("\r\nL2: %d", PS3.getAnalogButton(L2));
                if (!PS3.PS3NavigationConnected)
                {
                    pc.printf("\tR2: %d", PS3.getAnalogButton(R2));
                }
            }
            if (PS3.getButtonClick(PS))
            {
                PS3.disconnect();
                pc.printf("\r\nPS");
            }
    
            if (PS3.getButtonClick(TRIANGLE))
                pc.printf("\r\nTriangle");
            if (PS3.getButtonClick(CIRCLE))
                pc.printf("\r\nCircle");
            if (PS3.getButtonClick(CROSS))
                pc.printf("\r\nCross");
            if (PS3.getButtonClick(SQUARE))
                pc.printf("\r\nSquare");
    
            if (PS3.getButtonClick(UP))
            {
                pc.printf("\r\nUp");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED4);
            }
            if (PS3.getButtonClick(RIGHT))
            {
                pc.printf("\r\nRight");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED1);
            }
            if (PS3.getButtonClick(DOWN))
            {
                pc.printf("\r\nDown");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED2);
            }
            if (PS3.getButtonClick(LEFT))
            {
                pc.printf("\r\nLeft");
                PS3.setLedOff();
                PS3.setLedOn(CONTROLLER_LED3);
            }
    
            if (PS3.getButtonClick(L1))
                pc.printf("\r\nL1");
            if (PS3.getButtonClick(L3))
                pc.printf("\r\nL3");
            if (PS3.getButtonClick(R1))
                pc.printf("\r\nR1");
            if (PS3.getButtonClick(R3))
                pc.printf("\r\nR3");
    
            if (PS3.getButtonClick(SELECT))
            {
                pc.printf("\r\nSelect - ");
                PS3.printStatusString();
            }
            if (PS3.getButtonClick(START))
            {
                pc.printf("\r\nStart");
                printAngle = !printAngle;
            }
            if (printAngle)
            {
                pc.printf("\r\nPitch: %.3lf", PS3.getAngle(Pitch));
                pc.printf("\tRoll: %.3lf", PS3.getAngle(Roll));
            }
        }
        else
        {
            pc.printf("not connect\n");
        }
    }
}
Committer:
robo_ichinoseki_a
Date:
Sat May 02 05:56:48 2020 +0000
Revision:
1:da31140f2a1c
Parent:
0:b1ce54272580
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kotakku 0:b1ce54272580 1 /*
kotakku 0:b1ce54272580 2 WString.cpp - String library for Wiring & Arduino
kotakku 0:b1ce54272580 3 ...mostly rewritten by Paul Stoffregen...
kotakku 0:b1ce54272580 4 Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
kotakku 0:b1ce54272580 5 Copyright 2011, Paul Stoffregen, paul@pjrc.com
kotakku 0:b1ce54272580 6
kotakku 0:b1ce54272580 7 This library is free software; you can redistribute it and/or
kotakku 0:b1ce54272580 8 modify it under the terms of the GNU Lesser General Public
kotakku 0:b1ce54272580 9 License as published by the Free Software Foundation; either
kotakku 0:b1ce54272580 10 version 2.1 of the License, or (at your option) any later version.
kotakku 0:b1ce54272580 11
kotakku 0:b1ce54272580 12 This library is distributed in the hope that it will be useful,
kotakku 0:b1ce54272580 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
kotakku 0:b1ce54272580 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kotakku 0:b1ce54272580 15 Lesser General Public License for more details.
kotakku 0:b1ce54272580 16
kotakku 0:b1ce54272580 17 You should have received a copy of the GNU Lesser General Public
kotakku 0:b1ce54272580 18 License along with this library; if not, write to the Free Software
kotakku 0:b1ce54272580 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
kotakku 0:b1ce54272580 20 */
kotakku 0:b1ce54272580 21
kotakku 0:b1ce54272580 22 #include "WString.h"
kotakku 0:b1ce54272580 23 #include "itoa.h"
kotakku 0:b1ce54272580 24 #include "avr/dtostrf.h"
kotakku 0:b1ce54272580 25
kotakku 0:b1ce54272580 26 /*********************************************/
kotakku 0:b1ce54272580 27 /* Constructors */
kotakku 0:b1ce54272580 28 /*********************************************/
kotakku 0:b1ce54272580 29
kotakku 0:b1ce54272580 30 String::String(const char *cstr)
kotakku 0:b1ce54272580 31 {
kotakku 0:b1ce54272580 32 init();
kotakku 0:b1ce54272580 33 if (cstr) copy(cstr, strlen(cstr));
kotakku 0:b1ce54272580 34 }
kotakku 0:b1ce54272580 35
kotakku 0:b1ce54272580 36 String::String(const String &value)
kotakku 0:b1ce54272580 37 {
kotakku 0:b1ce54272580 38 init();
kotakku 0:b1ce54272580 39 *this = value;
kotakku 0:b1ce54272580 40 }
kotakku 0:b1ce54272580 41
kotakku 0:b1ce54272580 42 String::String(const __FlashStringHelper *pstr)
kotakku 0:b1ce54272580 43 {
kotakku 0:b1ce54272580 44 init();
kotakku 0:b1ce54272580 45 *this = pstr;
kotakku 0:b1ce54272580 46 }
kotakku 0:b1ce54272580 47
kotakku 0:b1ce54272580 48 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
kotakku 0:b1ce54272580 49 String::String(String &&rval)
kotakku 0:b1ce54272580 50 {
kotakku 0:b1ce54272580 51 init();
kotakku 0:b1ce54272580 52 move(rval);
kotakku 0:b1ce54272580 53 }
kotakku 0:b1ce54272580 54 String::String(StringSumHelper &&rval)
kotakku 0:b1ce54272580 55 {
kotakku 0:b1ce54272580 56 init();
kotakku 0:b1ce54272580 57 move(rval);
kotakku 0:b1ce54272580 58 }
kotakku 0:b1ce54272580 59 #endif
kotakku 0:b1ce54272580 60
kotakku 0:b1ce54272580 61 String::String(char c)
kotakku 0:b1ce54272580 62 {
kotakku 0:b1ce54272580 63 init();
kotakku 0:b1ce54272580 64 char buf[2];
kotakku 0:b1ce54272580 65 buf[0] = c;
kotakku 0:b1ce54272580 66 buf[1] = 0;
kotakku 0:b1ce54272580 67 *this = buf;
kotakku 0:b1ce54272580 68 }
kotakku 0:b1ce54272580 69
kotakku 0:b1ce54272580 70 String::String(unsigned char value, unsigned char base)
kotakku 0:b1ce54272580 71 {
kotakku 0:b1ce54272580 72 init();
kotakku 0:b1ce54272580 73 char buf[1 + 8 * sizeof(unsigned char)];
kotakku 0:b1ce54272580 74 utoa(value, buf, base);
kotakku 0:b1ce54272580 75 *this = buf;
kotakku 0:b1ce54272580 76 }
kotakku 0:b1ce54272580 77
kotakku 0:b1ce54272580 78 String::String(int value, unsigned char base)
kotakku 0:b1ce54272580 79 {
kotakku 0:b1ce54272580 80 init();
kotakku 0:b1ce54272580 81 char buf[2 + 8 * sizeof(int)];
kotakku 0:b1ce54272580 82 itoa(value, buf, base);
kotakku 0:b1ce54272580 83 *this = buf;
kotakku 0:b1ce54272580 84 }
kotakku 0:b1ce54272580 85
kotakku 0:b1ce54272580 86 String::String(unsigned int value, unsigned char base)
kotakku 0:b1ce54272580 87 {
kotakku 0:b1ce54272580 88 init();
kotakku 0:b1ce54272580 89 char buf[1 + 8 * sizeof(unsigned int)];
kotakku 0:b1ce54272580 90 utoa(value, buf, base);
kotakku 0:b1ce54272580 91 *this = buf;
kotakku 0:b1ce54272580 92 }
kotakku 0:b1ce54272580 93
kotakku 0:b1ce54272580 94 String::String(long value, unsigned char base)
kotakku 0:b1ce54272580 95 {
kotakku 0:b1ce54272580 96 init();
kotakku 0:b1ce54272580 97 char buf[2 + 8 * sizeof(long)];
kotakku 0:b1ce54272580 98 ltoa(value, buf, base);
kotakku 0:b1ce54272580 99 *this = buf;
kotakku 0:b1ce54272580 100 }
kotakku 0:b1ce54272580 101
kotakku 0:b1ce54272580 102 String::String(unsigned long value, unsigned char base)
kotakku 0:b1ce54272580 103 {
kotakku 0:b1ce54272580 104 init();
kotakku 0:b1ce54272580 105 char buf[1 + 8 * sizeof(unsigned long)];
kotakku 0:b1ce54272580 106 ultoa(value, buf, base);
kotakku 0:b1ce54272580 107 *this = buf;
kotakku 0:b1ce54272580 108 }
kotakku 0:b1ce54272580 109
kotakku 0:b1ce54272580 110 String::String(float value, unsigned char decimalPlaces)
kotakku 0:b1ce54272580 111 {
kotakku 0:b1ce54272580 112 init();
kotakku 0:b1ce54272580 113 char buf[33];
kotakku 0:b1ce54272580 114 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
kotakku 0:b1ce54272580 115 }
kotakku 0:b1ce54272580 116
kotakku 0:b1ce54272580 117 String::String(double value, unsigned char decimalPlaces)
kotakku 0:b1ce54272580 118 {
kotakku 0:b1ce54272580 119 init();
kotakku 0:b1ce54272580 120 char buf[33];
kotakku 0:b1ce54272580 121 *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
kotakku 0:b1ce54272580 122 }
kotakku 0:b1ce54272580 123
kotakku 0:b1ce54272580 124 String::~String()
kotakku 0:b1ce54272580 125 {
kotakku 0:b1ce54272580 126 free(buffer);
kotakku 0:b1ce54272580 127 }
kotakku 0:b1ce54272580 128
kotakku 0:b1ce54272580 129 /*********************************************/
kotakku 0:b1ce54272580 130 /* Memory Management */
kotakku 0:b1ce54272580 131 /*********************************************/
kotakku 0:b1ce54272580 132
kotakku 0:b1ce54272580 133 inline void String::init(void)
kotakku 0:b1ce54272580 134 {
kotakku 0:b1ce54272580 135 buffer = NULL;
kotakku 0:b1ce54272580 136 capacity = 0;
kotakku 0:b1ce54272580 137 len = 0;
kotakku 0:b1ce54272580 138 }
kotakku 0:b1ce54272580 139
kotakku 0:b1ce54272580 140 void String::invalidate(void)
kotakku 0:b1ce54272580 141 {
kotakku 0:b1ce54272580 142 if (buffer) free(buffer);
kotakku 0:b1ce54272580 143 buffer = NULL;
kotakku 0:b1ce54272580 144 capacity = len = 0;
kotakku 0:b1ce54272580 145 }
kotakku 0:b1ce54272580 146
kotakku 0:b1ce54272580 147 unsigned char String::reserve(unsigned int size)
kotakku 0:b1ce54272580 148 {
kotakku 0:b1ce54272580 149 if (buffer && capacity >= size) return 1;
kotakku 0:b1ce54272580 150 if (changeBuffer(size)) {
kotakku 0:b1ce54272580 151 if (len == 0) buffer[0] = 0;
kotakku 0:b1ce54272580 152 return 1;
kotakku 0:b1ce54272580 153 }
kotakku 0:b1ce54272580 154 return 0;
kotakku 0:b1ce54272580 155 }
kotakku 0:b1ce54272580 156
kotakku 0:b1ce54272580 157 unsigned char String::changeBuffer(unsigned int maxStrLen)
kotakku 0:b1ce54272580 158 {
kotakku 0:b1ce54272580 159 char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
kotakku 0:b1ce54272580 160 if (newbuffer) {
kotakku 0:b1ce54272580 161 buffer = newbuffer;
kotakku 0:b1ce54272580 162 capacity = maxStrLen;
kotakku 0:b1ce54272580 163 return 1;
kotakku 0:b1ce54272580 164 }
kotakku 0:b1ce54272580 165 return 0;
kotakku 0:b1ce54272580 166 }
kotakku 0:b1ce54272580 167
kotakku 0:b1ce54272580 168 /*********************************************/
kotakku 0:b1ce54272580 169 /* Copy and Move */
kotakku 0:b1ce54272580 170 /*********************************************/
kotakku 0:b1ce54272580 171
kotakku 0:b1ce54272580 172 String & String::copy(const char *cstr, unsigned int length)
kotakku 0:b1ce54272580 173 {
kotakku 0:b1ce54272580 174 if (!reserve(length)) {
kotakku 0:b1ce54272580 175 invalidate();
kotakku 0:b1ce54272580 176 return *this;
kotakku 0:b1ce54272580 177 }
kotakku 0:b1ce54272580 178 len = length;
kotakku 0:b1ce54272580 179 strcpy(buffer, cstr);
kotakku 0:b1ce54272580 180 return *this;
kotakku 0:b1ce54272580 181 }
kotakku 0:b1ce54272580 182
kotakku 0:b1ce54272580 183 String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
kotakku 0:b1ce54272580 184 {
kotakku 0:b1ce54272580 185 if (!reserve(length)) {
kotakku 0:b1ce54272580 186 invalidate();
kotakku 0:b1ce54272580 187 return *this;
kotakku 0:b1ce54272580 188 }
kotakku 0:b1ce54272580 189 len = length;
kotakku 0:b1ce54272580 190 strcpy_P(buffer, (PGM_P)pstr);
kotakku 0:b1ce54272580 191 return *this;
kotakku 0:b1ce54272580 192 }
kotakku 0:b1ce54272580 193
kotakku 0:b1ce54272580 194 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
kotakku 0:b1ce54272580 195 void String::move(String &rhs)
kotakku 0:b1ce54272580 196 {
kotakku 0:b1ce54272580 197 if (buffer) {
kotakku 0:b1ce54272580 198 if (capacity >= rhs.len) {
kotakku 0:b1ce54272580 199 strcpy(buffer, rhs.buffer);
kotakku 0:b1ce54272580 200 len = rhs.len;
kotakku 0:b1ce54272580 201 rhs.len = 0;
kotakku 0:b1ce54272580 202 return;
kotakku 0:b1ce54272580 203 } else {
kotakku 0:b1ce54272580 204 free(buffer);
kotakku 0:b1ce54272580 205 }
kotakku 0:b1ce54272580 206 }
kotakku 0:b1ce54272580 207 buffer = rhs.buffer;
kotakku 0:b1ce54272580 208 capacity = rhs.capacity;
kotakku 0:b1ce54272580 209 len = rhs.len;
kotakku 0:b1ce54272580 210 rhs.buffer = NULL;
kotakku 0:b1ce54272580 211 rhs.capacity = 0;
kotakku 0:b1ce54272580 212 rhs.len = 0;
kotakku 0:b1ce54272580 213 }
kotakku 0:b1ce54272580 214 #endif
kotakku 0:b1ce54272580 215
kotakku 0:b1ce54272580 216 String & String::operator = (const String &rhs)
kotakku 0:b1ce54272580 217 {
kotakku 0:b1ce54272580 218 if (this == &rhs) return *this;
kotakku 0:b1ce54272580 219
kotakku 0:b1ce54272580 220 if (rhs.buffer) copy(rhs.buffer, rhs.len);
kotakku 0:b1ce54272580 221 else invalidate();
kotakku 0:b1ce54272580 222
kotakku 0:b1ce54272580 223 return *this;
kotakku 0:b1ce54272580 224 }
kotakku 0:b1ce54272580 225
kotakku 0:b1ce54272580 226 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
kotakku 0:b1ce54272580 227 String & String::operator = (String &&rval)
kotakku 0:b1ce54272580 228 {
kotakku 0:b1ce54272580 229 if (this != &rval) move(rval);
kotakku 0:b1ce54272580 230 return *this;
kotakku 0:b1ce54272580 231 }
kotakku 0:b1ce54272580 232
kotakku 0:b1ce54272580 233 String & String::operator = (StringSumHelper &&rval)
kotakku 0:b1ce54272580 234 {
kotakku 0:b1ce54272580 235 if (this != &rval) move(rval);
kotakku 0:b1ce54272580 236 return *this;
kotakku 0:b1ce54272580 237 }
kotakku 0:b1ce54272580 238 #endif
kotakku 0:b1ce54272580 239
kotakku 0:b1ce54272580 240 String & String::operator = (const char *cstr)
kotakku 0:b1ce54272580 241 {
kotakku 0:b1ce54272580 242 if (cstr) copy(cstr, strlen(cstr));
kotakku 0:b1ce54272580 243 else invalidate();
kotakku 0:b1ce54272580 244
kotakku 0:b1ce54272580 245 return *this;
kotakku 0:b1ce54272580 246 }
kotakku 0:b1ce54272580 247
kotakku 0:b1ce54272580 248 String & String::operator = (const __FlashStringHelper *pstr)
kotakku 0:b1ce54272580 249 {
kotakku 0:b1ce54272580 250 if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
kotakku 0:b1ce54272580 251 else invalidate();
kotakku 0:b1ce54272580 252
kotakku 0:b1ce54272580 253 return *this;
kotakku 0:b1ce54272580 254 }
kotakku 0:b1ce54272580 255
kotakku 0:b1ce54272580 256 /*********************************************/
kotakku 0:b1ce54272580 257 /* concat */
kotakku 0:b1ce54272580 258 /*********************************************/
kotakku 0:b1ce54272580 259
kotakku 0:b1ce54272580 260 unsigned char String::concat(const String &s)
kotakku 0:b1ce54272580 261 {
kotakku 0:b1ce54272580 262 return concat(s.buffer, s.len);
kotakku 0:b1ce54272580 263 }
kotakku 0:b1ce54272580 264
kotakku 0:b1ce54272580 265 unsigned char String::concat(const char *cstr, unsigned int length)
kotakku 0:b1ce54272580 266 {
kotakku 0:b1ce54272580 267 unsigned int newlen = len + length;
kotakku 0:b1ce54272580 268 if (!cstr) return 0;
kotakku 0:b1ce54272580 269 if (length == 0) return 1;
kotakku 0:b1ce54272580 270 if (!reserve(newlen)) return 0;
kotakku 0:b1ce54272580 271 strcpy(buffer + len, cstr);
kotakku 0:b1ce54272580 272 len = newlen;
kotakku 0:b1ce54272580 273 return 1;
kotakku 0:b1ce54272580 274 }
kotakku 0:b1ce54272580 275
kotakku 0:b1ce54272580 276 unsigned char String::concat(const char *cstr)
kotakku 0:b1ce54272580 277 {
kotakku 0:b1ce54272580 278 if (!cstr) return 0;
kotakku 0:b1ce54272580 279 return concat(cstr, strlen(cstr));
kotakku 0:b1ce54272580 280 }
kotakku 0:b1ce54272580 281
kotakku 0:b1ce54272580 282 unsigned char String::concat(char c)
kotakku 0:b1ce54272580 283 {
kotakku 0:b1ce54272580 284 char buf[2];
kotakku 0:b1ce54272580 285 buf[0] = c;
kotakku 0:b1ce54272580 286 buf[1] = 0;
kotakku 0:b1ce54272580 287 return concat(buf, 1);
kotakku 0:b1ce54272580 288 }
kotakku 0:b1ce54272580 289
kotakku 0:b1ce54272580 290 unsigned char String::concat(unsigned char num)
kotakku 0:b1ce54272580 291 {
kotakku 0:b1ce54272580 292 char buf[1 + 3 * sizeof(unsigned char)];
kotakku 0:b1ce54272580 293 itoa(num, buf, 10);
kotakku 0:b1ce54272580 294 return concat(buf, strlen(buf));
kotakku 0:b1ce54272580 295 }
kotakku 0:b1ce54272580 296
kotakku 0:b1ce54272580 297 unsigned char String::concat(int num)
kotakku 0:b1ce54272580 298 {
kotakku 0:b1ce54272580 299 char buf[2 + 3 * sizeof(int)];
kotakku 0:b1ce54272580 300 itoa(num, buf, 10);
kotakku 0:b1ce54272580 301 return concat(buf, strlen(buf));
kotakku 0:b1ce54272580 302 }
kotakku 0:b1ce54272580 303
kotakku 0:b1ce54272580 304 unsigned char String::concat(unsigned int num)
kotakku 0:b1ce54272580 305 {
kotakku 0:b1ce54272580 306 char buf[1 + 3 * sizeof(unsigned int)];
kotakku 0:b1ce54272580 307 utoa(num, buf, 10);
kotakku 0:b1ce54272580 308 return concat(buf, strlen(buf));
kotakku 0:b1ce54272580 309 }
kotakku 0:b1ce54272580 310
kotakku 0:b1ce54272580 311 unsigned char String::concat(long num)
kotakku 0:b1ce54272580 312 {
kotakku 0:b1ce54272580 313 char buf[2 + 3 * sizeof(long)];
kotakku 0:b1ce54272580 314 ltoa(num, buf, 10);
kotakku 0:b1ce54272580 315 return concat(buf, strlen(buf));
kotakku 0:b1ce54272580 316 }
kotakku 0:b1ce54272580 317
kotakku 0:b1ce54272580 318 unsigned char String::concat(unsigned long num)
kotakku 0:b1ce54272580 319 {
kotakku 0:b1ce54272580 320 char buf[1 + 3 * sizeof(unsigned long)];
kotakku 0:b1ce54272580 321 ultoa(num, buf, 10);
kotakku 0:b1ce54272580 322 return concat(buf, strlen(buf));
kotakku 0:b1ce54272580 323 }
kotakku 0:b1ce54272580 324
kotakku 0:b1ce54272580 325 unsigned char String::concat(float num)
kotakku 0:b1ce54272580 326 {
kotakku 0:b1ce54272580 327 char buf[20];
kotakku 0:b1ce54272580 328 char* string = dtostrf(num, 4, 2, buf);
kotakku 0:b1ce54272580 329 return concat(string, strlen(string));
kotakku 0:b1ce54272580 330 }
kotakku 0:b1ce54272580 331
kotakku 0:b1ce54272580 332 unsigned char String::concat(double num)
kotakku 0:b1ce54272580 333 {
kotakku 0:b1ce54272580 334 char buf[20];
kotakku 0:b1ce54272580 335 char* string = dtostrf(num, 4, 2, buf);
kotakku 0:b1ce54272580 336 return concat(string, strlen(string));
kotakku 0:b1ce54272580 337 }
kotakku 0:b1ce54272580 338
kotakku 0:b1ce54272580 339 unsigned char String::concat(const __FlashStringHelper * str)
kotakku 0:b1ce54272580 340 {
kotakku 0:b1ce54272580 341 if (!str) return 0;
kotakku 0:b1ce54272580 342 int length = strlen_P((const char *) str);
kotakku 0:b1ce54272580 343 if (length == 0) return 1;
kotakku 0:b1ce54272580 344 unsigned int newlen = len + length;
kotakku 0:b1ce54272580 345 if (!reserve(newlen)) return 0;
kotakku 0:b1ce54272580 346 strcpy_P(buffer + len, (const char *) str);
kotakku 0:b1ce54272580 347 len = newlen;
kotakku 0:b1ce54272580 348 return 1;
kotakku 0:b1ce54272580 349 }
kotakku 0:b1ce54272580 350
kotakku 0:b1ce54272580 351 /*********************************************/
kotakku 0:b1ce54272580 352 /* Concatenate */
kotakku 0:b1ce54272580 353 /*********************************************/
kotakku 0:b1ce54272580 354
kotakku 0:b1ce54272580 355 StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
kotakku 0:b1ce54272580 356 {
kotakku 0:b1ce54272580 357 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 358 if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
kotakku 0:b1ce54272580 359 return a;
kotakku 0:b1ce54272580 360 }
kotakku 0:b1ce54272580 361
kotakku 0:b1ce54272580 362 StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
kotakku 0:b1ce54272580 363 {
kotakku 0:b1ce54272580 364 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 365 if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
kotakku 0:b1ce54272580 366 return a;
kotakku 0:b1ce54272580 367 }
kotakku 0:b1ce54272580 368
kotakku 0:b1ce54272580 369 StringSumHelper & operator + (const StringSumHelper &lhs, char c)
kotakku 0:b1ce54272580 370 {
kotakku 0:b1ce54272580 371 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 372 if (!a.concat(c)) a.invalidate();
kotakku 0:b1ce54272580 373 return a;
kotakku 0:b1ce54272580 374 }
kotakku 0:b1ce54272580 375
kotakku 0:b1ce54272580 376 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
kotakku 0:b1ce54272580 377 {
kotakku 0:b1ce54272580 378 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 379 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 380 return a;
kotakku 0:b1ce54272580 381 }
kotakku 0:b1ce54272580 382
kotakku 0:b1ce54272580 383 StringSumHelper & operator + (const StringSumHelper &lhs, int num)
kotakku 0:b1ce54272580 384 {
kotakku 0:b1ce54272580 385 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 386 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 387 return a;
kotakku 0:b1ce54272580 388 }
kotakku 0:b1ce54272580 389
kotakku 0:b1ce54272580 390 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
kotakku 0:b1ce54272580 391 {
kotakku 0:b1ce54272580 392 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 393 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 394 return a;
kotakku 0:b1ce54272580 395 }
kotakku 0:b1ce54272580 396
kotakku 0:b1ce54272580 397 StringSumHelper & operator + (const StringSumHelper &lhs, long num)
kotakku 0:b1ce54272580 398 {
kotakku 0:b1ce54272580 399 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 400 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 401 return a;
kotakku 0:b1ce54272580 402 }
kotakku 0:b1ce54272580 403
kotakku 0:b1ce54272580 404 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
kotakku 0:b1ce54272580 405 {
kotakku 0:b1ce54272580 406 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 407 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 408 return a;
kotakku 0:b1ce54272580 409 }
kotakku 0:b1ce54272580 410
kotakku 0:b1ce54272580 411 StringSumHelper & operator + (const StringSumHelper &lhs, float num)
kotakku 0:b1ce54272580 412 {
kotakku 0:b1ce54272580 413 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 414 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 415 return a;
kotakku 0:b1ce54272580 416 }
kotakku 0:b1ce54272580 417
kotakku 0:b1ce54272580 418 StringSumHelper & operator + (const StringSumHelper &lhs, double num)
kotakku 0:b1ce54272580 419 {
kotakku 0:b1ce54272580 420 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 421 if (!a.concat(num)) a.invalidate();
kotakku 0:b1ce54272580 422 return a;
kotakku 0:b1ce54272580 423 }
kotakku 0:b1ce54272580 424
kotakku 0:b1ce54272580 425 StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
kotakku 0:b1ce54272580 426 {
kotakku 0:b1ce54272580 427 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
kotakku 0:b1ce54272580 428 if (!a.concat(rhs)) a.invalidate();
kotakku 0:b1ce54272580 429 return a;
kotakku 0:b1ce54272580 430 }
kotakku 0:b1ce54272580 431
kotakku 0:b1ce54272580 432 /*********************************************/
kotakku 0:b1ce54272580 433 /* Comparison */
kotakku 0:b1ce54272580 434 /*********************************************/
kotakku 0:b1ce54272580 435
kotakku 0:b1ce54272580 436 int String::compareTo(const String &s) const
kotakku 0:b1ce54272580 437 {
kotakku 0:b1ce54272580 438 if (!buffer || !s.buffer) {
kotakku 0:b1ce54272580 439 if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
kotakku 0:b1ce54272580 440 if (buffer && len > 0) return *(unsigned char *)buffer;
kotakku 0:b1ce54272580 441 return 0;
kotakku 0:b1ce54272580 442 }
kotakku 0:b1ce54272580 443 return strcmp(buffer, s.buffer);
kotakku 0:b1ce54272580 444 }
kotakku 0:b1ce54272580 445
kotakku 0:b1ce54272580 446 unsigned char String::equals(const String &s2) const
kotakku 0:b1ce54272580 447 {
kotakku 0:b1ce54272580 448 return (len == s2.len && compareTo(s2) == 0);
kotakku 0:b1ce54272580 449 }
kotakku 0:b1ce54272580 450
kotakku 0:b1ce54272580 451 unsigned char String::equals(const char *cstr) const
kotakku 0:b1ce54272580 452 {
kotakku 0:b1ce54272580 453 if (len == 0) return (cstr == NULL || *cstr == 0);
kotakku 0:b1ce54272580 454 if (cstr == NULL) return buffer[0] == 0;
kotakku 0:b1ce54272580 455 return strcmp(buffer, cstr) == 0;
kotakku 0:b1ce54272580 456 }
kotakku 0:b1ce54272580 457
kotakku 0:b1ce54272580 458 unsigned char String::operator<(const String &rhs) const
kotakku 0:b1ce54272580 459 {
kotakku 0:b1ce54272580 460 return compareTo(rhs) < 0;
kotakku 0:b1ce54272580 461 }
kotakku 0:b1ce54272580 462
kotakku 0:b1ce54272580 463 unsigned char String::operator>(const String &rhs) const
kotakku 0:b1ce54272580 464 {
kotakku 0:b1ce54272580 465 return compareTo(rhs) > 0;
kotakku 0:b1ce54272580 466 }
kotakku 0:b1ce54272580 467
kotakku 0:b1ce54272580 468 unsigned char String::operator<=(const String &rhs) const
kotakku 0:b1ce54272580 469 {
kotakku 0:b1ce54272580 470 return compareTo(rhs) <= 0;
kotakku 0:b1ce54272580 471 }
kotakku 0:b1ce54272580 472
kotakku 0:b1ce54272580 473 unsigned char String::operator>=(const String &rhs) const
kotakku 0:b1ce54272580 474 {
kotakku 0:b1ce54272580 475 return compareTo(rhs) >= 0;
kotakku 0:b1ce54272580 476 }
kotakku 0:b1ce54272580 477
kotakku 0:b1ce54272580 478 unsigned char String::equalsIgnoreCase( const String &s2 ) const
kotakku 0:b1ce54272580 479 {
kotakku 0:b1ce54272580 480 if (this == &s2) return 1;
kotakku 0:b1ce54272580 481 if (len != s2.len) return 0;
kotakku 0:b1ce54272580 482 if (len == 0) return 1;
kotakku 0:b1ce54272580 483 const char *p1 = buffer;
kotakku 0:b1ce54272580 484 const char *p2 = s2.buffer;
kotakku 0:b1ce54272580 485 while (*p1) {
kotakku 0:b1ce54272580 486 if (tolower(*p1++) != tolower(*p2++)) return 0;
kotakku 0:b1ce54272580 487 }
kotakku 0:b1ce54272580 488 return 1;
kotakku 0:b1ce54272580 489 }
kotakku 0:b1ce54272580 490
kotakku 0:b1ce54272580 491 unsigned char String::startsWith( const String &s2 ) const
kotakku 0:b1ce54272580 492 {
kotakku 0:b1ce54272580 493 if (len < s2.len) return 0;
kotakku 0:b1ce54272580 494 return startsWith(s2, 0);
kotakku 0:b1ce54272580 495 }
kotakku 0:b1ce54272580 496
kotakku 0:b1ce54272580 497 unsigned char String::startsWith( const String &s2, unsigned int offset ) const
kotakku 0:b1ce54272580 498 {
kotakku 0:b1ce54272580 499 if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
kotakku 0:b1ce54272580 500 return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
kotakku 0:b1ce54272580 501 }
kotakku 0:b1ce54272580 502
kotakku 0:b1ce54272580 503 unsigned char String::endsWith( const String &s2 ) const
kotakku 0:b1ce54272580 504 {
kotakku 0:b1ce54272580 505 if ( len < s2.len || !buffer || !s2.buffer) return 0;
kotakku 0:b1ce54272580 506 return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
kotakku 0:b1ce54272580 507 }
kotakku 0:b1ce54272580 508
kotakku 0:b1ce54272580 509 /*********************************************/
kotakku 0:b1ce54272580 510 /* Character Access */
kotakku 0:b1ce54272580 511 /*********************************************/
kotakku 0:b1ce54272580 512
kotakku 0:b1ce54272580 513 char String::charAt(unsigned int loc) const
kotakku 0:b1ce54272580 514 {
kotakku 0:b1ce54272580 515 return operator[](loc);
kotakku 0:b1ce54272580 516 }
kotakku 0:b1ce54272580 517
kotakku 0:b1ce54272580 518 void String::setCharAt(unsigned int loc, char c)
kotakku 0:b1ce54272580 519 {
kotakku 0:b1ce54272580 520 if (loc < len) buffer[loc] = c;
kotakku 0:b1ce54272580 521 }
kotakku 0:b1ce54272580 522
kotakku 0:b1ce54272580 523 char & String::operator[](unsigned int index)
kotakku 0:b1ce54272580 524 {
kotakku 0:b1ce54272580 525 static char dummy_writable_char;
kotakku 0:b1ce54272580 526 if (index >= len || !buffer) {
kotakku 0:b1ce54272580 527 dummy_writable_char = 0;
kotakku 0:b1ce54272580 528 return dummy_writable_char;
kotakku 0:b1ce54272580 529 }
kotakku 0:b1ce54272580 530 return buffer[index];
kotakku 0:b1ce54272580 531 }
kotakku 0:b1ce54272580 532
kotakku 0:b1ce54272580 533 char String::operator[]( unsigned int index ) const
kotakku 0:b1ce54272580 534 {
kotakku 0:b1ce54272580 535 if (index >= len || !buffer) return 0;
kotakku 0:b1ce54272580 536 return buffer[index];
kotakku 0:b1ce54272580 537 }
kotakku 0:b1ce54272580 538
kotakku 0:b1ce54272580 539 void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
kotakku 0:b1ce54272580 540 {
kotakku 0:b1ce54272580 541 if (!bufsize || !buf) return;
kotakku 0:b1ce54272580 542 if (index >= len) {
kotakku 0:b1ce54272580 543 buf[0] = 0;
kotakku 0:b1ce54272580 544 return;
kotakku 0:b1ce54272580 545 }
kotakku 0:b1ce54272580 546 unsigned int n = bufsize - 1;
kotakku 0:b1ce54272580 547 if (n > len - index) n = len - index;
kotakku 0:b1ce54272580 548 strncpy((char *)buf, buffer + index, n);
kotakku 0:b1ce54272580 549 buf[n] = 0;
kotakku 0:b1ce54272580 550 }
kotakku 0:b1ce54272580 551
kotakku 0:b1ce54272580 552 /*********************************************/
kotakku 0:b1ce54272580 553 /* Search */
kotakku 0:b1ce54272580 554 /*********************************************/
kotakku 0:b1ce54272580 555
kotakku 0:b1ce54272580 556 int String::indexOf(char c) const
kotakku 0:b1ce54272580 557 {
kotakku 0:b1ce54272580 558 return indexOf(c, 0);
kotakku 0:b1ce54272580 559 }
kotakku 0:b1ce54272580 560
kotakku 0:b1ce54272580 561 int String::indexOf( char ch, unsigned int fromIndex ) const
kotakku 0:b1ce54272580 562 {
kotakku 0:b1ce54272580 563 if (fromIndex >= len) return -1;
kotakku 0:b1ce54272580 564 const char* temp = strchr(buffer + fromIndex, ch);
kotakku 0:b1ce54272580 565 if (temp == NULL) return -1;
kotakku 0:b1ce54272580 566 return temp - buffer;
kotakku 0:b1ce54272580 567 }
kotakku 0:b1ce54272580 568
kotakku 0:b1ce54272580 569 int String::indexOf(const String &s2) const
kotakku 0:b1ce54272580 570 {
kotakku 0:b1ce54272580 571 return indexOf(s2, 0);
kotakku 0:b1ce54272580 572 }
kotakku 0:b1ce54272580 573
kotakku 0:b1ce54272580 574 int String::indexOf(const String &s2, unsigned int fromIndex) const
kotakku 0:b1ce54272580 575 {
kotakku 0:b1ce54272580 576 if (fromIndex >= len) return -1;
kotakku 0:b1ce54272580 577 const char *found = strstr(buffer + fromIndex, s2.buffer);
kotakku 0:b1ce54272580 578 if (found == NULL) return -1;
kotakku 0:b1ce54272580 579 return found - buffer;
kotakku 0:b1ce54272580 580 }
kotakku 0:b1ce54272580 581
kotakku 0:b1ce54272580 582 int String::lastIndexOf( char theChar ) const
kotakku 0:b1ce54272580 583 {
kotakku 0:b1ce54272580 584 return lastIndexOf(theChar, len - 1);
kotakku 0:b1ce54272580 585 }
kotakku 0:b1ce54272580 586
kotakku 0:b1ce54272580 587 int String::lastIndexOf(char ch, unsigned int fromIndex) const
kotakku 0:b1ce54272580 588 {
kotakku 0:b1ce54272580 589 if (fromIndex >= len) return -1;
kotakku 0:b1ce54272580 590 char tempchar = buffer[fromIndex + 1];
kotakku 0:b1ce54272580 591 buffer[fromIndex + 1] = '\0';
kotakku 0:b1ce54272580 592 char* temp = strrchr( buffer, ch );
kotakku 0:b1ce54272580 593 buffer[fromIndex + 1] = tempchar;
kotakku 0:b1ce54272580 594 if (temp == NULL) return -1;
kotakku 0:b1ce54272580 595 return temp - buffer;
kotakku 0:b1ce54272580 596 }
kotakku 0:b1ce54272580 597
kotakku 0:b1ce54272580 598 int String::lastIndexOf(const String &s2) const
kotakku 0:b1ce54272580 599 {
kotakku 0:b1ce54272580 600 return lastIndexOf(s2, len - s2.len);
kotakku 0:b1ce54272580 601 }
kotakku 0:b1ce54272580 602
kotakku 0:b1ce54272580 603 int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
kotakku 0:b1ce54272580 604 {
kotakku 0:b1ce54272580 605 if (s2.len == 0 || len == 0 || s2.len > len) return -1;
kotakku 0:b1ce54272580 606 if (fromIndex >= len) fromIndex = len - 1;
kotakku 0:b1ce54272580 607 int found = -1;
kotakku 0:b1ce54272580 608 for (char *p = buffer; p <= buffer + fromIndex; p++) {
kotakku 0:b1ce54272580 609 p = strstr(p, s2.buffer);
kotakku 0:b1ce54272580 610 if (!p) break;
kotakku 0:b1ce54272580 611 if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
kotakku 0:b1ce54272580 612 }
kotakku 0:b1ce54272580 613 return found;
kotakku 0:b1ce54272580 614 }
kotakku 0:b1ce54272580 615
kotakku 0:b1ce54272580 616 String String::substring(unsigned int left, unsigned int right) const
kotakku 0:b1ce54272580 617 {
kotakku 0:b1ce54272580 618 if (left > right) {
kotakku 0:b1ce54272580 619 unsigned int temp = right;
kotakku 0:b1ce54272580 620 right = left;
kotakku 0:b1ce54272580 621 left = temp;
kotakku 0:b1ce54272580 622 }
kotakku 0:b1ce54272580 623 String out;
kotakku 0:b1ce54272580 624 if (left >= len) return out;
kotakku 0:b1ce54272580 625 if (right > len) right = len;
kotakku 0:b1ce54272580 626 char temp = buffer[right]; // save the replaced character
kotakku 0:b1ce54272580 627 buffer[right] = '\0';
kotakku 0:b1ce54272580 628 out = buffer + left; // pointer arithmetic
kotakku 0:b1ce54272580 629 buffer[right] = temp; //restore character
kotakku 0:b1ce54272580 630 return out;
kotakku 0:b1ce54272580 631 }
kotakku 0:b1ce54272580 632
kotakku 0:b1ce54272580 633 /*********************************************/
kotakku 0:b1ce54272580 634 /* Modification */
kotakku 0:b1ce54272580 635 /*********************************************/
kotakku 0:b1ce54272580 636
kotakku 0:b1ce54272580 637 void String::replace(char find, char replace)
kotakku 0:b1ce54272580 638 {
kotakku 0:b1ce54272580 639 if (!buffer) return;
kotakku 0:b1ce54272580 640 for (char *p = buffer; *p; p++) {
kotakku 0:b1ce54272580 641 if (*p == find) *p = replace;
kotakku 0:b1ce54272580 642 }
kotakku 0:b1ce54272580 643 }
kotakku 0:b1ce54272580 644
kotakku 0:b1ce54272580 645 void String::replace(const String& find, const String& replace)
kotakku 0:b1ce54272580 646 {
kotakku 0:b1ce54272580 647 if (len == 0 || find.len == 0) return;
kotakku 0:b1ce54272580 648 int diff = replace.len - find.len;
kotakku 0:b1ce54272580 649 char *readFrom = buffer;
kotakku 0:b1ce54272580 650 char *foundAt;
kotakku 0:b1ce54272580 651 if (diff == 0) {
kotakku 0:b1ce54272580 652 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
kotakku 0:b1ce54272580 653 memcpy(foundAt, replace.buffer, replace.len);
kotakku 0:b1ce54272580 654 readFrom = foundAt + replace.len;
kotakku 0:b1ce54272580 655 }
kotakku 0:b1ce54272580 656 } else if (diff < 0) {
kotakku 0:b1ce54272580 657 char *writeTo = buffer;
kotakku 0:b1ce54272580 658 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
kotakku 0:b1ce54272580 659 unsigned int n = foundAt - readFrom;
kotakku 0:b1ce54272580 660 memcpy(writeTo, readFrom, n);
kotakku 0:b1ce54272580 661 writeTo += n;
kotakku 0:b1ce54272580 662 memcpy(writeTo, replace.buffer, replace.len);
kotakku 0:b1ce54272580 663 writeTo += replace.len;
kotakku 0:b1ce54272580 664 readFrom = foundAt + find.len;
kotakku 0:b1ce54272580 665 len += diff;
kotakku 0:b1ce54272580 666 }
kotakku 0:b1ce54272580 667 strcpy(writeTo, readFrom);
kotakku 0:b1ce54272580 668 } else {
kotakku 0:b1ce54272580 669 unsigned int size = len; // compute size needed for result
kotakku 0:b1ce54272580 670 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
kotakku 0:b1ce54272580 671 readFrom = foundAt + find.len;
kotakku 0:b1ce54272580 672 size += diff;
kotakku 0:b1ce54272580 673 }
kotakku 0:b1ce54272580 674 if (size == len) return;
kotakku 0:b1ce54272580 675 if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
kotakku 0:b1ce54272580 676 int index = len - 1;
kotakku 0:b1ce54272580 677 while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
kotakku 0:b1ce54272580 678 readFrom = buffer + index + find.len;
kotakku 0:b1ce54272580 679 memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
kotakku 0:b1ce54272580 680 len += diff;
kotakku 0:b1ce54272580 681 buffer[len] = 0;
kotakku 0:b1ce54272580 682 memcpy(buffer + index, replace.buffer, replace.len);
kotakku 0:b1ce54272580 683 index--;
kotakku 0:b1ce54272580 684 }
kotakku 0:b1ce54272580 685 }
kotakku 0:b1ce54272580 686 }
kotakku 0:b1ce54272580 687
kotakku 0:b1ce54272580 688 void String::remove(unsigned int index){
kotakku 0:b1ce54272580 689 // Pass the biggest integer as the count. The remove method
kotakku 0:b1ce54272580 690 // below will take care of truncating it at the end of the
kotakku 0:b1ce54272580 691 // string.
kotakku 0:b1ce54272580 692 remove(index, (unsigned int)-1);
kotakku 0:b1ce54272580 693 }
kotakku 0:b1ce54272580 694
kotakku 0:b1ce54272580 695 void String::remove(unsigned int index, unsigned int count){
kotakku 0:b1ce54272580 696 if (index >= len) { return; }
kotakku 0:b1ce54272580 697 if (count <= 0) { return; }
kotakku 0:b1ce54272580 698 if (count > len - index) { count = len - index; }
kotakku 0:b1ce54272580 699 char *writeTo = buffer + index;
kotakku 0:b1ce54272580 700 len = len - count;
kotakku 0:b1ce54272580 701 strncpy(writeTo, buffer + index + count,len - index);
kotakku 0:b1ce54272580 702 buffer[len] = 0;
kotakku 0:b1ce54272580 703 }
kotakku 0:b1ce54272580 704
kotakku 0:b1ce54272580 705 void String::toLowerCase(void)
kotakku 0:b1ce54272580 706 {
kotakku 0:b1ce54272580 707 if (!buffer) return;
kotakku 0:b1ce54272580 708 for (char *p = buffer; *p; p++) {
kotakku 0:b1ce54272580 709 *p = tolower(*p);
kotakku 0:b1ce54272580 710 }
kotakku 0:b1ce54272580 711 }
kotakku 0:b1ce54272580 712
kotakku 0:b1ce54272580 713 void String::toUpperCase(void)
kotakku 0:b1ce54272580 714 {
kotakku 0:b1ce54272580 715 if (!buffer) return;
kotakku 0:b1ce54272580 716 for (char *p = buffer; *p; p++) {
kotakku 0:b1ce54272580 717 *p = toupper(*p);
kotakku 0:b1ce54272580 718 }
kotakku 0:b1ce54272580 719 }
kotakku 0:b1ce54272580 720
kotakku 0:b1ce54272580 721 void String::trim(void)
kotakku 0:b1ce54272580 722 {
kotakku 0:b1ce54272580 723 if (!buffer || len == 0) return;
kotakku 0:b1ce54272580 724 char *begin = buffer;
kotakku 0:b1ce54272580 725 while (isspace(*begin)) begin++;
kotakku 0:b1ce54272580 726 char *end = buffer + len - 1;
kotakku 0:b1ce54272580 727 while (isspace(*end) && end >= begin) end--;
kotakku 0:b1ce54272580 728 len = end + 1 - begin;
kotakku 0:b1ce54272580 729 if (begin > buffer) memcpy(buffer, begin, len);
kotakku 0:b1ce54272580 730 buffer[len] = 0;
kotakku 0:b1ce54272580 731 }
kotakku 0:b1ce54272580 732
kotakku 0:b1ce54272580 733 /*********************************************/
kotakku 0:b1ce54272580 734 /* Parsing / Conversion */
kotakku 0:b1ce54272580 735 /*********************************************/
kotakku 0:b1ce54272580 736
kotakku 0:b1ce54272580 737 long String::toInt(void) const
kotakku 0:b1ce54272580 738 {
kotakku 0:b1ce54272580 739 if (buffer) return atol(buffer);
kotakku 0:b1ce54272580 740 return 0;
kotakku 0:b1ce54272580 741 }
kotakku 0:b1ce54272580 742
kotakku 0:b1ce54272580 743 float String::toFloat(void) const
kotakku 0:b1ce54272580 744 {
kotakku 0:b1ce54272580 745 if (buffer) return float(atof(buffer));
kotakku 0:b1ce54272580 746 return 0;
kotakku 0:b1ce54272580 747 }
kotakku 0:b1ce54272580 748