"Fossies" - the Fresh Open Source Software Archive 
Member "encfs-1.9.5/encfs/autosprintf.cpp" (27 Apr 2018, 2361 Bytes) of package /linux/misc/encfs-1.9.5.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "autosprintf.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1.9.4_vs_1.9.5.
1 /* Class autosprintf - formatted output to an ostream.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2002.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 /* Tell glibc's <stdio.h> to provide a prototype for vasprintf().
19 This must come before <config.h> because <config.h> may include
20 <features.h>, and once <features.h> has been included, it's too late. */
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 /* Specification. */
26 #include "autosprintf.h"
27
28 #include <cstdarg> // for va_list
29 #include <cstdio> // for NULL, vasprintf
30 #include <cstdlib> // for free
31 #include <cstring> // for strdup
32
33 namespace gnu {
34
35 /* Constructor: takes a format string and the printf arguments. */
36 autosprintf::autosprintf(const char *format, ...) { // NOLINT (cert-dcl50-cpp) as it's not critical
37 va_list args;
38 va_start(args, format);
39 if (vasprintf(&str, format, args) < 0) {
40 str = nullptr;
41 }
42 va_end(args);
43 }
44
45 /* Copy constructor. Necessary because the destructor is nontrivial. */
46 autosprintf::autosprintf(const autosprintf &src) {
47 str = (src.str != nullptr ? strdup(src.str) : nullptr);
48 }
49
50 /* Destructor: frees the temporarily allocated string. */
51 autosprintf::~autosprintf() {
52 free(str); // NOLINT (cppcoreguidelines-no-malloc) as it's a consequence of vasprintf / variadic function above
53 }
54
55 /* Conversion to string. */
56 autosprintf::operator char *() const {
57 if (str != nullptr) {
58 size_t length = strlen(str) + 1;
59 auto *copy = new char[length];
60 memcpy(copy, str, length);
61 return copy;
62 }
63 return nullptr;
64 }
65 autosprintf::operator std::string() const {
66 return std::string(str != nullptr ? str : "(error in autosprintf)");
67 }
68 } // namespace gnu