Apache Mesos
format.hpp
Go to the documentation of this file.
1 // Copyright 2015 Mesosphere, Inc.
2 //
3 // This file has been modified from its original form by Mesosphere, Inc.
4 // All modifications made to this file by Mesosphere (the "Modifications")
5 // are copyright 2015 Mesosphere, Inc.
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 // ---
20 //
21 // The original file on which the Modifications have been made were
22 // provided to Mesosphere pursuant to the following terms:
23 //
24 // Copyright (C) 2014 insane coder (http://insanecoding.blogspot.com/,
25 // http://asprintf.insanecoding.org/)
26 //
27 // Permission to use, copy, modify, and distribute this software for any
28 // purpose with or without fee is hereby granted, provided that the above
29 // copyright notice and this permission notice appear in all copies.
30 //
31 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 
39 #ifndef __STOUT_WINDOWS_FORMAT_HPP__
40 #define __STOUT_WINDOWS_FORMAT_HPP__
41 
42 #include <stdio.h> // For '_vscprintf', 'vsnprintf'.
43 #include <stdlib.h> // For 'malloc', 'free'.
44 #include <limits.h> // For 'INT_MAX'.
45 
46 
47 inline int vasprintf(char** buffer, const char* format, va_list args)
48 {
49  int result = -1;
50  int size = _vscprintf(format, args) + 1;
51 
52  if (size >= 0 && size < INT_MAX) {
53  *buffer = (char*) malloc(size);
54 
55  if (*buffer) {
56  result = vsnprintf(*buffer, size, format, args);
57 
58  if (result < 0) {
59  free(*buffer);
60  }
61  }
62  }
63 
64  return result;
65 }
66 
67 
68 #endif // __STOUT_WINDOWS_FORMAT_HPP__
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Try< std::string > format(const std::string &s, const T &...t)
Definition: format.hpp:58
int vasprintf(char **buffer, const char *format, va_list args)
Definition: format.hpp:47