Line data Source code
1 : // Snap Websites Server -- wrapper of popen()/pclose() with iostream like functions
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : #include "snapwebsites/snap_thread.h"
20 :
21 : #include <sstream>
22 : #include <stdio.h>
23 :
24 : #include <QVector>
25 :
26 : namespace snap
27 : {
28 :
29 0 : class snap_pipe_exception : public snap_exception
30 : {
31 : public:
32 0 : explicit snap_pipe_exception(const char * whatmsg) : snap_exception("snap_pipe", whatmsg) {}
33 : explicit snap_pipe_exception(const std::string & whatmsg) : snap_exception("snap_pipe", whatmsg) {}
34 0 : explicit snap_pipe_exception(const QString & whatmsg) : snap_exception("snap_pipe", whatmsg) {}
35 : };
36 :
37 0 : class snap_pipe_exception_cannot_open : public snap_pipe_exception
38 : {
39 : public:
40 : explicit snap_pipe_exception_cannot_open(const char * whatmsg) : snap_pipe_exception(whatmsg) {}
41 : explicit snap_pipe_exception_cannot_open(const std::string & whatmsg) : snap_pipe_exception(whatmsg) {}
42 0 : explicit snap_pipe_exception_cannot_open(const QString & whatmsg) : snap_pipe_exception(whatmsg) {}
43 : };
44 :
45 0 : class snap_pipe_exception_cannot_write : public snap_pipe_exception
46 : {
47 : public:
48 0 : explicit snap_pipe_exception_cannot_write(const char * whatmsg) : snap_pipe_exception(whatmsg) {}
49 : explicit snap_pipe_exception_cannot_write(const std::string & whatmsg) : snap_pipe_exception(whatmsg) {}
50 : explicit snap_pipe_exception_cannot_write(const QString & whatmsg) : snap_pipe_exception(whatmsg) {}
51 : };
52 :
53 0 : class snap_pipe_exception_cannot_read : public snap_pipe_exception
54 : {
55 : public:
56 0 : explicit snap_pipe_exception_cannot_read(const char * whatmsg) : snap_pipe_exception(whatmsg) {}
57 : explicit snap_pipe_exception_cannot_read(const std::string & whatmsg) : snap_pipe_exception(whatmsg) {}
58 0 : explicit snap_pipe_exception_cannot_read(const QString & whatmsg) : snap_pipe_exception(whatmsg) {}
59 : };
60 :
61 0 : class snap_pipe_exception_no_file : public snap_pipe_exception
62 : {
63 : public:
64 0 : explicit snap_pipe_exception_no_file(const char * whatmsg) : snap_pipe_exception(whatmsg) {}
65 : explicit snap_pipe_exception_no_file(const std::string & whatmsg) : snap_pipe_exception(whatmsg) {}
66 : explicit snap_pipe_exception_no_file(const QString & whatmsg) : snap_pipe_exception(whatmsg) {}
67 : };
68 :
69 :
70 :
71 :
72 : class snap_pipe : public std::streambuf
73 : {
74 : public:
75 : enum class mode_t
76 : {
77 : PIPE_MODE_IN, // you will be writing to the command (<<)
78 : PIPE_MODE_OUT // you will be reading from the command (>>)
79 : };
80 :
81 : snap_pipe(QString const & command, mode_t mode);
82 : ~snap_pipe();
83 :
84 : // prevent copies
85 : snap_pipe(snap_pipe const & ) = delete;
86 : snap_pipe & operator = (snap_pipe const & ) = delete;
87 :
88 : int close_pipe();
89 : QString const & get_command() const;
90 : mode_t get_mode() const;
91 :
92 : protected:
93 : virtual int_type overflow(int_type c) override;
94 : virtual int_type underflow() override;
95 :
96 : private:
97 : // you are not expected to use those
98 : //int write(char const *buf, size_t size);
99 : //int read(char *buf, size_t size);
100 :
101 : QString f_command;
102 : mode_t f_mode = mode_t::PIPE_MODE_IN;
103 : FILE * f_file = nullptr;
104 : };
105 :
106 :
107 : } // namespace snap
108 : // vim: ts=4 sw=4 et
|