gtkmm-documentation r31 - in trunk: . examples/book/giomm examples/book/giomm/directory_list examples/book/giomm/getline examples/book/giomm/monitor_directory examples/book/giomm/read_file examples/book/giomm/volumes
- From: jjongsma svn gnome org
- To: svn-commits-list gnome org
- Subject: gtkmm-documentation r31 - in trunk: . examples/book/giomm examples/book/giomm/directory_list examples/book/giomm/getline examples/book/giomm/monitor_directory examples/book/giomm/read_file examples/book/giomm/volumes
- Date: Sun, 27 Jan 2008 21:34:09 +0000 (GMT)
Author: jjongsma
Date: Sun Jan 27 21:34:09 2008
New Revision: 31
URL: http://svn.gnome.org/viewvc/gtkmm-documentation?rev=31&view=rev
Log:
* configure.in: add new giomm examples
* examples/book/giomm/directory_list/main.cc: change directory to a more
generic '/etc' that almost everybody will have
* examples/book/giomm/read_file/main.cc: changed to more generic filename
that most people will have: /etc/fstab
* examples/book/giomm/getline: add a simple example for implementing
something similar to std::istream::getline() using giomm. I'm not sure how
useful this one is as an example. I'm also not sure how efficient it is to
read a file one character at a time, but I think that's how the standard
library function is implemented as well...
* examples/book/giomm/monitor_directory: add an example for monitoring a
directory using giomm. It should monitor the current directory, then create
a file and print out information on the commannd line when we get a change
notification
Added:
trunk/examples/book/giomm/getline/ (props changed)
trunk/examples/book/giomm/getline/Makefile.am
trunk/examples/book/giomm/getline/getline.cc
trunk/examples/book/giomm/monitor_directory/ (props changed)
trunk/examples/book/giomm/monitor_directory/Makefile.am
trunk/examples/book/giomm/monitor_directory/monitor_directory.cc
Modified:
trunk/ChangeLog
trunk/configure.in
trunk/examples/book/giomm/ (props changed)
trunk/examples/book/giomm/Makefile.am
trunk/examples/book/giomm/directory_list/ (props changed)
trunk/examples/book/giomm/directory_list/main.cc
trunk/examples/book/giomm/read_file/ (props changed)
trunk/examples/book/giomm/read_file/main.cc
trunk/examples/book/giomm/volumes/ (props changed)
Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in (original)
+++ trunk/configure.in Sun Jan 27 21:34:09 2008
@@ -310,6 +310,8 @@
examples/book/giomm/directory_list/Makefile
examples/book/giomm/read_file/Makefile
examples/book/giomm/volumes/Makefile
+ examples/book/giomm/getline/Makefile
+ examples/book/giomm/monitor_directory/Makefile
examples/book/frame/Makefile
examples/book/helloworld/Makefile
examples/book/helloworld2/Makefile
Modified: trunk/examples/book/giomm/Makefile.am
==============================================================================
--- trunk/examples/book/giomm/Makefile.am (original)
+++ trunk/examples/book/giomm/Makefile.am Sun Jan 27 21:34:09 2008
@@ -1 +1 @@
-SUBDIRS = directory_list read_file volumes
+SUBDIRS = directory_list read_file volumes getline monitor_directory
Modified: trunk/examples/book/giomm/directory_list/main.cc
==============================================================================
--- trunk/examples/book/giomm/directory_list/main.cc (original)
+++ trunk/examples/book/giomm/directory_list/main.cc Sun Jan 27 21:34:09 2008
@@ -26,7 +26,7 @@
try
{
- Glib::RefPtr<Gio::File> directory = Gio::File::create_for_path("/home/murrayc/");
+ Glib::RefPtr<Gio::File> directory = Gio::File::create_for_path("/etc");
if(!directory)
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
Added: trunk/examples/book/giomm/getline/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/examples/book/giomm/getline/Makefile.am Sun Jan 27 21:34:09 2008
@@ -0,0 +1,6 @@
+include $(top_srcdir)/examples/Makefile.am_fragment
+
+#Build the executable, but don't install it.
+noinst_PROGRAMS = getline
+getline_SOURCES = getline.cc
+
Added: trunk/examples/book/giomm/getline/getline.cc
==============================================================================
--- (empty file)
+++ trunk/examples/book/giomm/getline/getline.cc Sun Jan 27 21:34:09 2008
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Jonathon Jongsma
+ *
+ * This file is part of gtkmm
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>
+ *
+ *******************************************************************************/
+#include <giomm.h>
+#include <iostream>
+#include <iomanip>
+
+/** A simplistic implementation of a function similar to std::istream::getline()
+ * which reads a 'line' of data from a file. A line is defined as text
+ * separated by the given delimiter character, which defaults to the newline
+ * character. If no delimiter is found before we read size-1 characters, just
+ * return the data read so far, even though it's not a full line.
+ * This function always appends a NULL character to the end of the string
+ *
+ * returns true if any data was read, false if no data was read (e.g. the end of
+ * the stream was reached)
+ */
+bool getline (const Glib::RefPtr<Gio::InputStream>& stream,
+ char* buf,
+ size_t size, char delim = '\n')
+{
+ g_return_val_if_fail (stream, false);
+ char* cur = buf;
+ // read a single character at a time until we read the delimiter
+ while (stream->read (cur, 1) && (cur < (buf + size - 1))) {
+ if (*cur == delim) {
+ *cur++ = 0; // discard the delimiter, add a NULL char
+ break;
+ }
+ ++cur; // advance to next character
+ }
+ *cur = 0; // append a NULL
+ return (cur > buf); // if any data was read, cur will be > buf
+}
+
+int main(int argc, char** argv)
+{
+ Gio::init();
+ Glib::RefPtr<Gio::File> f = Gio::File::create_for_path ("/etc/profile");
+ Glib::RefPtr<Gio::Cancellable> cancellable = Gio::Cancellable::create ();
+ Glib::RefPtr<Gio::FileInputStream> stream = f->read (cancellable);
+ char buf[100];
+ int line_num = 1;
+ while (getline (stream, buf, 100)) {
+ std::cout << std::setw (5) << line_num++ << ": >" << buf << std::endl;
+ }
+ return 0;
+}
Added: trunk/examples/book/giomm/monitor_directory/Makefile.am
==============================================================================
--- (empty file)
+++ trunk/examples/book/giomm/monitor_directory/Makefile.am Sun Jan 27 21:34:09 2008
@@ -0,0 +1,6 @@
+include $(top_srcdir)/examples/Makefile.am_fragment
+
+#Build the executable, but don't install it.
+noinst_PROGRAMS = monitor_directory
+monitor_directory_SOURCES = monitor_directory.cc
+
Added: trunk/examples/book/giomm/monitor_directory/monitor_directory.cc
==============================================================================
--- (empty file)
+++ trunk/examples/book/giomm/monitor_directory/monitor_directory.cc Sun Jan 27 21:34:09 2008
@@ -0,0 +1,122 @@
+/*******************************************************************************
+ *
+ * Copyright (c) 2007 Jonathon Jongsma
+ *
+ * This file is part of gtkmm
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>
+ *
+ *******************************************************************************/
+#include <iostream>
+#include <giomm.h>
+
+Glib::RefPtr<Glib::MainLoop> mainloop;
+const std::string FILENAME = "./temp-file";
+
+void
+on_directory_changed (const Glib::RefPtr<Gio::File>& file,
+ const Glib::RefPtr<Gio::File>& other_file,
+ Gio::FileMonitorEvent event)
+{
+ std::cout << "** Directory changed **" << std::endl;
+ if (file) {
+ std::cout << " File 1: " << file->get_path () << std::endl;
+ }
+ if (other_file) {
+ std::cout << " File 2: " << other_file->get_path () << std::endl;
+ }
+ std::cout << " ";
+
+ switch (event)
+ {
+ case Gio::FILE_MONITOR_EVENT_CHANGED:
+ std::cout << "Event: A file is being changed" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+ std::cout << "Event: File changes are done" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_DELETED:
+ std::cout << "Event: A file was deleted" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_CREATED:
+ std::cout << "Event: A file was created" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
+ std::cout << "Event: A file attribute was changed" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_PRE_UNMOUNT:
+ std::cout << "Event: pre-unmount event" << std::endl;
+ break;
+ case Gio::FILE_MONITOR_EVENT_UNMOUNTED:
+ std::cout << "Event: unmounted" << std::endl;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+bool create_temp_file ()
+{
+ try {
+ Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path (FILENAME);
+ // FIXME: need to add an overload to giomm without a cancellable
+ Glib::RefPtr<Gio::Cancellable> cancellable = Gio::Cancellable::create ();
+ Glib::RefPtr<Gio::FileOutputStream> stream =
+ temp_file->create_file (Gio::FILE_CREATE_NONE, cancellable);
+ stream->write ("this is only a test");
+ stream->close ();
+ }
+ catch (Gio::Error& e) {
+ std::cout << e.what () << std::endl;
+ }
+ // stop future timeouts from repeating
+ return false;
+}
+
+bool quit ()
+{
+ try {
+ Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path (FILENAME);
+ // FIXME: need to add an overload to giomm without a cancellable
+ Glib::RefPtr<Gio::Cancellable> cancellable = Gio::Cancellable::create ();
+ temp_file->trash (cancellable);
+ }
+ catch (Gio::Error& e) {
+ std::cout << e.what () << std::endl;
+ }
+ // stop future timeouts from repeating
+ mainloop->quit ();
+ return false;
+}
+
+int main(int argc, char** argv)
+{
+ Gio::init ();
+ mainloop = Glib::MainLoop::create();
+ std::string current_dir = Glib::get_current_dir ();
+ Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path (current_dir);
+ Glib::RefPtr<Gio::FileMonitor> monitor = dir->monitor_directory ();
+ std::cout << "Monitoring directory '" << current_dir << "'..."
+ << std::endl << std::endl;
+ monitor->signal_changed ().connect (sigc::ptr_fun (on_directory_changed));
+ std::cout << "Creating test file '" << FILENAME << "' to see what happens..."
+ << std::endl << std::endl;
+ // wait a couple seconds and then create a temp file to trigger the
+ // directory monitor.
+ Glib::signal_timeout ().connect_seconds (sigc::ptr_fun (create_temp_file), 2);
+ // then exit a couple seconds later
+ Glib::signal_timeout ().connect_seconds (sigc::ptr_fun (quit), 4);
+ mainloop->run ();
+ return 0;
+}
Modified: trunk/examples/book/giomm/read_file/main.cc
==============================================================================
--- trunk/examples/book/giomm/read_file/main.cc (original)
+++ trunk/examples/book/giomm/read_file/main.cc Sun Jan 27 21:34:09 2008
@@ -26,7 +26,7 @@
try
{
- Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/home/murrayc/test.txt");
+ Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/etc/fstab");
if(!file)
std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]