summaryrefslogtreecommitdiff
path: root/tests/giomm_ioerror/main.cc
blob: 797ed22e2760f6ccdb354be529425db71e9cf6c4 (plain)
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
// Some libraries define HOST_NOT_FOUND.  Make sure we can still compile fine
// even if this is the case:
#define HOST_NOT_FOUND 1

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

//Use this line if you want debug output:
//std::ostream& ostr = std::cout;

//This seems nicer and more useful than putting an ifdef around the use of ostr:
std::stringstream debug;
std::ostream& ostr = debug;

// This is just to test a workaround in the error.h header.  We save and #undef
// HOST_NOT_FOUND if it was defined by another header, and then restore it at
// the end of the header.  Here I'm just making sure that our temporary value
// doesn't remain set
#ifdef GIOMM_SAVED_HOST_NOT_FOUND
#error Forgot to #undef GIOMM_SAVED_HOST_NOT_FOUND
#endif

#ifdef G_OS_WIN32
#define TEST_FILE "c:/windows/write.exe"
#else
#define TEST_FILE "/etc/fstab"
#endif

int main(int, char**)
{
  Glib::init();
  Gio::init();

  try
  {
    auto file = Gio::File::create_for_path(TEST_FILE);
    if(!file)
    {
      std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
      return EXIT_FAILURE; 
    }

    auto stream = file->read();
    if(!stream)
    {
      std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
      return EXIT_FAILURE; 
    }

    gchar buffer[1000]; //TODO: This is unpleasant.
    memset(buffer, 0, sizeof buffer);
    const gsize bytes_read = stream->read(buffer, sizeof buffer - 1);

    if(bytes_read)
      ostr << "File contents read: " << buffer << std::endl;
    else
    {
      std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
      return EXIT_FAILURE; 
    }
  }
  catch(const Gio::Error& ex)
  {
    //This is just here to check that HOST_WAS_NOT_FOUND is still in our API,
    //because we hack it into our gio_enums.defs file and there is a risk of 
    //losing it when we regenerate that file. murrayc.
    if(ex.code() == Gio::Error::HOST_WAS_NOT_FOUND)
    {
      std::cerr << "Host was not found." << std::endl;
    }
    else
      std::cerr << "Gio::Error exception caught: " << ex.what() << std::endl; 
  }
  catch(const Glib::Exception& ex)
  {
    std::cerr << "Exception caught: " << ex.what() << std::endl;
    return EXIT_FAILURE; 
  }

  return EXIT_SUCCESS;
}