/src/trafficserver/include/tscore/TextBuffer.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | A brief file description |
4 | | |
5 | | @section license License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #pragma once |
25 | | |
26 | | /**************************************************************************** |
27 | | * |
28 | | * TextBuffer.h - A self-expanding buffer, primarily meant for strings |
29 | | * |
30 | | * |
31 | | * |
32 | | ****************************************************************************/ |
33 | | |
34 | | #include "tscore/ink_platform.h" |
35 | | #include "tscore/ink_memory.h" |
36 | | #include "tscore/ink_apidefs.h" |
37 | | |
38 | | #include <cstdarg> |
39 | | class TextBuffer |
40 | | { |
41 | | public: |
42 | 0 | TextBuffer() {} |
43 | | TextBuffer(const TextBuffer &rhs) |
44 | 0 | { |
45 | 0 | if (!rhs.empty()) { |
46 | 0 | copyFrom(rhs.bufPtr(), rhs.spaceUsed()); |
47 | 0 | } |
48 | 0 | } |
49 | | TextBuffer & |
50 | | operator=(TextBuffer &&other) |
51 | 0 | { |
52 | 0 | if (this != &other) { |
53 | 0 | ats_free(bufferStart); |
54 | 0 | bufferStart = other.bufferStart; |
55 | 0 | other.bufferStart = nullptr; |
56 | 0 | } |
57 | 0 | return *this; |
58 | 0 | } |
59 | | |
60 | | TextBuffer(int size); |
61 | | ~TextBuffer(); |
62 | | |
63 | | int rawReadFromFile(int fd); |
64 | | int readFromFD(int fd); |
65 | | int copyFrom(const void *, unsigned num_bytes); |
66 | | void reUse(); |
67 | | void chomp(); |
68 | | void slurp(int); |
69 | | |
70 | | char * |
71 | | bufPtr() |
72 | 0 | { |
73 | 0 | return bufferStart; |
74 | 0 | } |
75 | | |
76 | | const char * |
77 | | bufPtr() const |
78 | 0 | { |
79 | 0 | return bufferStart; |
80 | 0 | } |
81 | | |
82 | | void |
83 | | clear() |
84 | 0 | { |
85 | 0 | this->reUse(); |
86 | 0 | } |
87 | | |
88 | | void |
89 | | resize(unsigned nbytes) |
90 | 0 | { |
91 | 0 | this->enlargeBuffer(nbytes); |
92 | 0 | } |
93 | | |
94 | | size_t |
95 | | spaceUsed() const |
96 | 0 | { |
97 | 0 | return static_cast<size_t>(nextAdd - bufferStart); |
98 | 0 | }; |
99 | | |
100 | | bool |
101 | | empty() const |
102 | 0 | { |
103 | 0 | return this->spaceUsed() == 0; |
104 | 0 | } |
105 | | |
106 | | void format(const char *fmt, ...) TS_PRINTFLIKE(2, 3); |
107 | | void vformat(const char *fmt, va_list ap); |
108 | | |
109 | | char *release(); |
110 | | |
111 | | private: |
112 | | int enlargeBuffer(unsigned N); |
113 | | |
114 | | size_t currentSize = 0; |
115 | | size_t spaceLeft = 0; |
116 | | char *bufferStart = nullptr; |
117 | | char *nextAdd = nullptr; |
118 | | }; |