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