dc0ed92cb976d243d3318adbae9ae78945a23ae7
[m6w6/libmemcached] / src / bin / common / checks.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "options.hpp"
19 #include "libmemcached/common.h"
20
21 #include <cerrno>
22 #include <cstring>
23 #include <fstream>
24
25 bool check_buffering(const client_options &opt, memcached_st &memc) {
26 if (opt.isset("buffer")) {
27 if (MEMCACHED_SUCCESS != memcached_flush_buffers(&memc)) {
28 if (!opt.isset("quiet")) {
29 std::cerr << memcached_last_error_message(&memc) << "\n";
30 }
31 return false;
32 }
33 }
34 return true;
35 }
36
37 bool check_argp(const client_options &opt, char **argp, const char *error_msg) {
38 if (argp && *argp) {
39 return true;
40 }
41 if (!opt.isset("quiet")) {
42 std::cerr << error_msg << "\n";
43 }
44 return false;
45 }
46
47 bool check_memcached(const client_options &opt, memcached_st &memc) {
48 if (!memcached_create(&memc)) {
49 if (!opt.isset("quiet")) {
50 std::cerr << "Failed to initialize memcached client.\n";
51 }
52 return false;
53 }
54 return true;
55 }
56
57 bool check_return(const client_options &opt, memcached_st &memc, const char *key,
58 memcached_return_t rc) {
59 if (!memcached_success(rc)) {
60 if (!opt.isset("quiet")) {
61 if (MEMCACHED_NOTFOUND == rc) {
62 if (opt.isset("verbose")) {
63 std::cerr << "Could not find key '" << key
64 << "': " << memcached_strerror(&memc, rc) << "\n";;
65 }
66 } else {
67 std::cerr << "Fatal error for key '" << key
68 << "': " << memcached_last_error_message(&memc) << "\n";
69 }
70 }
71 return false;
72 }
73 return true;
74 }
75
76 std::ostream *check_ostream(const client_options &opt, const char *file, std::ofstream &stream) {
77 if (file && *file) {
78 if (opt.isset("debug")) {
79 std::cerr << "Opening '" << file << "' for writing.\n";
80 }
81 errno = 0;
82 stream.open(file, std::ios::binary | std::ios::out);
83 if (stream.is_open()) {
84 return &stream;
85 } else if (!opt.isset("quiet")) {
86 std::cerr << "Failed to open '" << file << "' for writing: " << strerror(errno) << ".\n";
87 }
88 }
89 return &std::cout;
90 }
91
92 std::istream *check_istream(const client_options &opt, const char *file, std::ifstream &stream) {
93 if (file && *file) {
94 if (file[0] != '-' || file[1] != 0) {
95 if (opt.isset("debug")) {
96 std::cerr << "Opening '" << file << "' for reading.\n";
97 }
98 errno = 0;
99 stream.open(file, std::ios::binary | std::ios::in);
100 if (stream.is_open()) {
101 return &stream;
102 } else if (!opt.isset("quiet")) {
103 std::cerr << "Failed to open '" << file << "' for reading: " << strerror(errno) << ".\n";
104 }
105 return nullptr;
106 }
107 }
108 return &std::cin;
109 }