| 1 |
/* gtkmm example Copyright (C) 2002 gtkmm development team |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <giomm.h> |
| 18 |
#include <iostream> |
| 19 |
#include <cstring> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
Glib::RefPtr<Glib::MainLoop> mainloop; |
| 24 |
Glib::RefPtr<Gio::File> file; |
| 25 |
Glib::RefPtr<Gio::FileInputStream> stream; |
| 26 |
gchar buffer[1000]; //TODO: This is unpleasant. |
| 27 |
|
| 28 |
void on_stream_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result) |
| 29 |
{ |
| 30 |
std::cout << "Stream Async handler called." << std::endl; |
| 31 |
|
| 32 |
try |
| 33 |
{ |
| 34 |
const gsize bytes_read = stream->read_finish(result); |
| 35 |
|
| 36 |
if(bytes_read) |
| 37 |
std::cout << "File contents read: " << buffer << std::endl; |
| 38 |
else |
| 39 |
std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl; |
| 40 |
|
| 41 |
} |
| 42 |
catch(const Glib::Error& ex) |
| 43 |
{ |
| 44 |
std::cerr << "Exception caught: " << ex.what() << std::endl; |
| 45 |
} |
| 46 |
|
| 47 |
mainloop->quit(); |
| 48 |
} |
| 49 |
|
| 50 |
void on_file_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result) |
| 51 |
{ |
| 52 |
std::cout << "File Async handler called." << std::endl; |
| 53 |
|
| 54 |
try |
| 55 |
{ |
| 56 |
stream = file->read_finish(result); |
| 57 |
if(!stream) |
| 58 |
std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl; |
| 59 |
} |
| 60 |
catch(const Glib::Error& ex) |
| 61 |
{ |
| 62 |
std::cerr << "Exception caught: " << ex.what() << std::endl; |
| 63 |
mainloop->quit(); |
| 64 |
} |
| 65 |
|
| 66 |
if(stream) |
| 67 |
{ |
| 68 |
memset(buffer, 0, 1000); |
| 69 |
stream->read_async(buffer, 1000, |
| 70 |
[](auto& stream_result) |
| 71 |
{ on_stream_read_async_ready(stream_result); } |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
int main(int /* argc */, char** /* argv */) |
| 77 |
{ |
| 78 |
Gio::init(); |
| 79 |
|
| 80 |
try |
| 81 |
{ |
| 82 |
file = Gio::File::create_for_path("/etc/fstab"); |
| 83 |
if(!file) |
| 84 |
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl; |
| 85 |
} |
| 86 |
catch(const Glib::Error& ex) |
| 87 |
{ |
| 88 |
std::cerr << "Exception caught: " << ex.what() << std::endl; |
| 89 |
} |
| 90 |
|
| 91 |
mainloop = Glib::MainLoop::create(); |
| 92 |
|
| 93 |
file->read_async( |
| 94 |
[](auto& result) |
| 95 |
{ on_file_read_async_ready(result); } |
| 96 |
); |
| 97 |
mainloop->run(); |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |