File: main.cc

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (100 lines) | stat: -rw-r--r-- 2,527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* gtkmm example Copyright (C) 2002 gtkmm development team
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <giomm.h>
#include <iostream>
#include <cstring>



Glib::RefPtr<Glib::MainLoop> mainloop;
Glib::RefPtr<Gio::File> file;
Glib::RefPtr<Gio::FileInputStream> stream;
gchar buffer[1000]; //TODO: This is unpleasant.

void on_stream_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result)
{
  std::cout << "Stream Async handler called." << std::endl;

  try
  {
    const gsize bytes_read = stream->read_finish(result);

    if(bytes_read)
      std::cout << "File contents read: " << buffer << std::endl;
    else
      std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;

  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "Exception caught: " << ex.what() << std::endl;
  }

  mainloop->quit();
}

void on_file_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result)
{
  std::cout << "File Async handler called." << std::endl;

  try
  {
    stream = file->read_finish(result);
    if(!stream)
      std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "Exception caught: " << ex.what() << std::endl;
    mainloop->quit();
  }

  if(stream)
  {
    memset(buffer, 0, 1000);
    stream->read_async(buffer, 1000,
      [](auto& stream_result)
      { on_stream_read_async_ready(stream_result); }
    );
  }
}

int main(int /* argc */, char** /* argv */)
{
  Gio::init();

  try
  {
    file = Gio::File::create_for_path("/etc/fstab");
    if(!file)
      std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "Exception caught: " << ex.what() << std::endl;
  }

  mainloop = Glib::MainLoop::create();

  file->read_async(
    [](auto& result)
    { on_file_read_async_ready(result); }
  );
  mainloop->run();

  return 0;
}