c51ce6cc029093764307a551fd5aa912b0c89576
[folly.git] / folly / io / Cursor.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #include <folly/io/Cursor.h>
17
18 #include <cstdio>
19 #include <folly/ScopeGuard.h>
20
21 namespace folly {
22 namespace io {
23
24 void Appender::printf(const char* fmt, ...) {
25   va_list ap;
26   va_start(ap, fmt);
27   vprintf(fmt, ap);
28   va_end(ap);
29 }
30
31 void Appender::vprintf(const char* fmt, va_list ap) {
32   // Make a copy of ap in case we need to retry.
33   // We use ap on the first attempt, so it always gets advanced
34   // passed the used arguments.  We'll only use apCopy if we need to retry.
35   va_list apCopy;
36   va_copy(apCopy, ap);
37   SCOPE_EXIT {
38     va_end(apCopy);
39   };
40
41   // First try writing into our available data space.
42   int ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
43                       fmt, ap);
44   if (ret < 0) {
45     throw std::runtime_error("error formatting printf() data");
46   }
47   auto len = size_t(ret);
48   // vsnprintf() returns the number of characters that would be printed,
49   // not including the terminating nul.
50   if (len < length()) {
51     // All of the data was successfully written.
52     append(len);
53     return;
54   }
55
56   // There wasn't enough room for the data.
57   // Allocate more room, and then retry.
58   ensure(len + 1);
59   ret = vsnprintf(reinterpret_cast<char*>(writableData()), length(),
60                   fmt, apCopy);
61   if (ret < 0) {
62     throw std::runtime_error("error formatting printf() data");
63   }
64   len = size_t(ret);
65   if (len >= length()) {
66     // This shouldn't ever happen.
67     throw std::runtime_error("unexpectedly out of buffer space on second "
68                              "vsnprintf() attmept");
69   }
70   append(len);
71 }
72 } // namespace io
73 } // namespace folly