bin: consolidate clients
[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, memcached_return_t rc) {
58 if (!memcached_success(rc)) {
59 if (!opt.isset("quiet")) {
60 if (!memcached_fatal(rc)) {
61 if (opt.isset("verbose")) {
62 std::cerr << "Failed: " << memcached_strerror(&memc, rc) << "\n";;
63 }
64 } else {
65 std::cerr << "Fatal error: " << memcached_last_error_message(&memc) << "\n";
66 }
67 }
68 return false;
69 }
70 return true;
71 }
72
73 bool check_return(const client_options &opt, memcached_st &memc, const char *key,
74 memcached_return_t rc) {
75 if (!memcached_success(rc)) {
76 if (!opt.isset("quiet")) {
77 if (MEMCACHED_NOTFOUND == rc) {
78 if (opt.isset("verbose")) {
79 std::cerr << "Could not find key '" << key
80 << "': " << memcached_strerror(&memc, rc) << "\n";;
81 }
82 } else {
83 std::cerr << "Fatal error for key '" << key
84 << "': " << memcached_last_error_message(&memc) << "\n";
85 }
86 }
87 return false;
88 }
89 return true;
90 }
91
92 std::ostream *check_ostream(const client_options &opt, const char *file, std::ofstream &stream) {
93 if (file && *file) {
94 if (opt.isset("debug")) {
95 std::cerr << "Opening '" << file << "' for writing.\n";
96 }
97 errno = 0;
98 stream.open(file, std::ios::binary | std::ios::out);
99 if (stream.is_open()) {
100 return &stream;
101 } else if (!opt.isset("quiet")) {
102 std::cerr << "Failed to open '" << file << "' for writing: " << strerror(errno) << ".\n";
103 }
104 }
105 return &std::cout;
106 }
107
108 std::istream *check_istream(const client_options &opt, const char *file, std::ifstream &stream) {
109 if (file && *file) {
110 if (file[0] != '-' || file[1] != 0) {
111 if (opt.isset("debug")) {
112 std::cerr << "Opening '" << file << "' for reading.\n";
113 }
114 errno = 0;
115 stream.open(file, std::ios::binary | std::ios::in);
116 if (stream.is_open()) {
117 return &stream;
118 } else if (!opt.isset("quiet")) {
119 std::cerr << "Failed to open '" << file << "' for reading: " << strerror(errno) << ".\n";
120 }
121 return nullptr;
122 }
123 }
124 return &std::cin;
125 }