#include <glib.h>
#include <gio/gio.h>

#define FILENAME "/tmp/test"

int
main (int argc, const char* argv[])
{
  GFile *file;
  GError *error = NULL;
  GOutputStream *stream;
  gchar *buffer;

  // Create file with content
  g_print ("g_file_set_contents: %s %s\n", FILENAME, "abcdef");
  g_file_set_contents (FILENAME, "abcdef", 6, &error);
  g_assert_no_error (error);

  // Open file for appending
  g_print ("g_file_append_to: %s\n", FILENAME);
  file = g_file_new_for_path (FILENAME);
  stream = G_OUTPUT_STREAM (g_file_append_to (file, 0, NULL, &error));
  g_assert_no_error (error);

  // Seek to beginning
  g_print ("g_seekable_can_seek: %d\n", g_seekable_can_seek (G_SEEKABLE (stream)));
  g_print ("g_seekable_seek: %d\n", 0);
  g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
  g_assert_no_error (error);
  g_print ("g_seekable_tell: %ld\n", g_seekable_tell (G_SEEKABLE (stream)));

  // Write some content
  g_print ("g_output_stream_write: %s\n", "012345");
  g_output_stream_write (stream, "012345", 6, NULL, &error);
  g_assert_no_error (error);
  g_print ("g_seekable_tell: %ld\n", g_seekable_tell (G_SEEKABLE (stream)));

  g_print ("g_output_stream_close: %s\n", FILENAME);
  g_output_stream_close (stream, NULL, &error);
  g_assert_no_error (error);

  // Read file content
  g_file_get_contents (FILENAME, &buffer, NULL, &error);
  g_assert_no_error (error);
  g_print ("g_file_get_contents: %s %s\n", FILENAME, buffer);
}
