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 
20 #include <log/log.h>
21 #include <stdint.h>
22 #include <string>
23 #include <string_view>
24 
25 // Max Character number of a MTP String
26 #define MTP_STRING_MAX_CHARACTER_NUMBER             255
27 
28 namespace android {
29 
30 class MtpDataPacket;
31 
32 // Represents a utf8 string, with a maximum of 255 characters
33 class MtpStringBuffer {
34 
35 private:
36     std::string     mString;
37 
38 public:
MtpStringBuffer()39                     MtpStringBuffer() {};
~MtpStringBuffer()40                     ~MtpStringBuffer() {};
41 
42     explicit        MtpStringBuffer(const char* src);
43     explicit        MtpStringBuffer(const uint16_t* src);
44                     MtpStringBuffer(const MtpStringBuffer& src);
45 
46     void            set(const char* src);
47     void            set(const uint16_t* src);
48 
49     inline void     append(const char* other);
50     inline void     append(MtpStringBuffer &other);
51 
52     bool            readFromPacket(MtpDataPacket* packet);
53     void            writeToPacket(MtpDataPacket* packet) const;
54 
isEmpty()55     inline bool     isEmpty() const { return mString.empty(); }
size()56     inline int      size() const { return mString.length(); }
57 
58     inline operator const char*() const { return mString.c_str(); }
string_view()59     operator std::string_view() const { return mString; }
60 };
61 
append(const char * other)62 inline void MtpStringBuffer::append(const char* other) {
63     mString += other;
64 }
65 
append(MtpStringBuffer & other)66 inline void MtpStringBuffer::append(MtpStringBuffer &other) {
67     mString += other.mString;
68 }
69 
70 }; // namespace android
71 
72 #endif // _MTP_STRING_BUFFER_H
73