blob: 30d1bc16bbc580be43fc796d6cd53cf8ed30cddc [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
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 Zhangbc1d4b42018-03-27 15:25:03 -070020#include <log/log.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040021#include <stdint.h>
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070022#include <string>
T.J. Mercier4e8c5162022-07-23 18:05:08 +000023#include <string_view>
Mike Lockwood16864ba2010-05-11 17:16:59 -040024
Yin Liu014897f2012-12-04 09:19:53 +010025// Max Character number of a MTP String
26#define MTP_STRING_MAX_CHARACTER_NUMBER 255
27
Mike Lockwood7850ef92010-05-14 10:10:36 -040028namespace android {
29
Mike Lockwood16864ba2010-05-11 17:16:59 -040030class MtpDataPacket;
31
32// Represents a utf8 string, with a maximum of 255 characters
33class MtpStringBuffer {
34
35private:
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070036 std::string mString;
Mike Lockwood16864ba2010-05-11 17:16:59 -040037
38public:
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070039 MtpStringBuffer() {};
40 ~MtpStringBuffer() {};
41
Chih-Hung Hsieha039c882016-08-09 14:16:10 -070042 explicit MtpStringBuffer(const char* src);
43 explicit MtpStringBuffer(const uint16_t* src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040044 MtpStringBuffer(const MtpStringBuffer& src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040045
46 void set(const char* src);
Mike Lockwooddde37202010-09-25 21:21:05 -040047 void set(const uint16_t* src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040048
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070049 inline void append(const char* other);
50 inline void append(MtpStringBuffer &other);
51
Mike Lockwoodab063842014-11-12 14:20:06 -080052 bool readFromPacket(MtpDataPacket* packet);
Mike Lockwood16864ba2010-05-11 17:16:59 -040053 void writeToPacket(MtpDataPacket* packet) const;
54
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070055 inline bool isEmpty() const { return mString.empty(); }
56 inline int size() const { return mString.length(); }
Mike Lockwood16864ba2010-05-11 17:16:59 -040057
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070058 inline operator const char*() const { return mString.c_str(); }
T.J. Mercier4e8c5162022-07-23 18:05:08 +000059 operator std::string_view() const { return mString; }
Mike Lockwood16864ba2010-05-11 17:16:59 -040060};
61
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070062inline void MtpStringBuffer::append(const char* other) {
63 mString += other;
64}
65
66inline void MtpStringBuffer::append(MtpStringBuffer &other) {
67 mString += other.mString;
68}
69
Mike Lockwood7850ef92010-05-14 10:10:36 -040070}; // namespace android
71
Mike Lockwood16864ba2010-05-11 17:16:59 -040072#endif // _MTP_STRING_BUFFER_H