blob: 4cec58acbc1e64f3746f31f8f44e4d1382e35e46 [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>
Mike Lockwood16864ba2010-05-11 17:16:59 -040023
Yin Liu014897f2012-12-04 09:19:53 +010024// Max Character number of a MTP String
25#define MTP_STRING_MAX_CHARACTER_NUMBER 255
26
Mike Lockwood7850ef92010-05-14 10:10:36 -040027namespace android {
28
Mike Lockwood16864ba2010-05-11 17:16:59 -040029class MtpDataPacket;
30
31// Represents a utf8 string, with a maximum of 255 characters
32class MtpStringBuffer {
33
34private:
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070035 std::string mString;
Mike Lockwood16864ba2010-05-11 17:16:59 -040036
37public:
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070038 MtpStringBuffer() {};
39 ~MtpStringBuffer() {};
40
Chih-Hung Hsieha039c882016-08-09 14:16:10 -070041 explicit MtpStringBuffer(const char* src);
42 explicit MtpStringBuffer(const uint16_t* src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040043 MtpStringBuffer(const MtpStringBuffer& src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040044
45 void set(const char* src);
Mike Lockwooddde37202010-09-25 21:21:05 -040046 void set(const uint16_t* src);
Mike Lockwood16864ba2010-05-11 17:16:59 -040047
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070048 inline void append(const char* other);
49 inline void append(MtpStringBuffer &other);
50
Mike Lockwoodab063842014-11-12 14:20:06 -080051 bool readFromPacket(MtpDataPacket* packet);
Mike Lockwood16864ba2010-05-11 17:16:59 -040052 void writeToPacket(MtpDataPacket* packet) const;
53
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070054 inline bool isEmpty() const { return mString.empty(); }
55 inline int size() const { return mString.length(); }
Mike Lockwood16864ba2010-05-11 17:16:59 -040056
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070057 inline operator const char*() const { return mString.c_str(); }
Mike Lockwood16864ba2010-05-11 17:16:59 -040058};
59
Jerry Zhangbc1d4b42018-03-27 15:25:03 -070060inline void MtpStringBuffer::append(const char* other) {
61 mString += other;
62}
63
64inline void MtpStringBuffer::append(MtpStringBuffer &other) {
65 mString += other.mString;
66}
67
Mike Lockwood7850ef92010-05-14 10:10:36 -040068}; // namespace android
69
Mike Lockwood16864ba2010-05-11 17:16:59 -040070#endif // _MTP_STRING_BUFFER_H