Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _MTP_STRING_BUFFER_H |
| 18 | #define _MTP_STRING_BUFFER_H |
| 19 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 21 | #include <stdint.h> |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 22 | #include <string> |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 23 | |
Yin Liu | 014897f | 2012-12-04 09:19:53 +0100 | [diff] [blame] | 24 | // Max Character number of a MTP String |
| 25 | #define MTP_STRING_MAX_CHARACTER_NUMBER 255 |
| 26 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 27 | namespace android { |
| 28 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 29 | class MtpDataPacket; |
| 30 | |
| 31 | // Represents a utf8 string, with a maximum of 255 characters |
| 32 | class MtpStringBuffer { |
| 33 | |
| 34 | private: |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 35 | std::string mString; |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 36 | |
| 37 | public: |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 38 | MtpStringBuffer() {}; |
| 39 | ~MtpStringBuffer() {}; |
| 40 | |
Chih-Hung Hsieh | a039c88 | 2016-08-09 14:16:10 -0700 | [diff] [blame] | 41 | explicit MtpStringBuffer(const char* src); |
| 42 | explicit MtpStringBuffer(const uint16_t* src); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 43 | MtpStringBuffer(const MtpStringBuffer& src); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 44 | |
| 45 | void set(const char* src); |
Mike Lockwood | dde3720 | 2010-09-25 21:21:05 -0400 | [diff] [blame] | 46 | void set(const uint16_t* src); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 47 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 48 | inline void append(const char* other); |
| 49 | inline void append(MtpStringBuffer &other); |
| 50 | |
Mike Lockwood | ab06384 | 2014-11-12 14:20:06 -0800 | [diff] [blame] | 51 | bool readFromPacket(MtpDataPacket* packet); |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 52 | void writeToPacket(MtpDataPacket* packet) const; |
| 53 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 54 | inline bool isEmpty() const { return mString.empty(); } |
| 55 | inline int size() const { return mString.length(); } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 56 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 57 | inline operator const char*() const { return mString.c_str(); } |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
Jerry Zhang | bc1d4b4 | 2018-03-27 15:25:03 -0700 | [diff] [blame] | 60 | inline void MtpStringBuffer::append(const char* other) { |
| 61 | mString += other; |
| 62 | } |
| 63 | |
| 64 | inline void MtpStringBuffer::append(MtpStringBuffer &other) { |
| 65 | mString += other.mString; |
| 66 | } |
| 67 | |
Mike Lockwood | 7850ef9 | 2010-05-14 10:10:36 -0400 | [diff] [blame] | 68 | }; // namespace android |
| 69 | |
Mike Lockwood | 16864ba | 2010-05-11 17:16:59 -0400 | [diff] [blame] | 70 | #endif // _MTP_STRING_BUFFER_H |