gtkmm-documentation r33 - in trunk: . examples/book/giomm/getline examples/book/giomm/monitor_directory



Author: jjongsma
Date: Thu Feb 14 02:15:08 2008
New Revision: 33
URL: http://svn.gnome.org/viewvc/gtkmm-documentation?rev=33&view=rev

Log:
	* examples/book/giomm/getline/getline.cc: fix this example to use the
	correct buffered data input stream instead of doing it manually (and very
	inefficiently).
	* examples/book/giomm/monitor_directory/monitor_directory.cc: fix build
	failure



Modified:
   trunk/ChangeLog
   trunk/examples/book/giomm/getline/getline.cc
   trunk/examples/book/giomm/monitor_directory/monitor_directory.cc

Modified: trunk/examples/book/giomm/getline/getline.cc
==============================================================================
--- trunk/examples/book/giomm/getline/getline.cc	(original)
+++ trunk/examples/book/giomm/getline/getline.cc	Thu Feb 14 02:15:08 2008
@@ -20,45 +20,24 @@
 #include <giomm.h>
 #include <iostream>
 #include <iomanip>
+#include <string>
 
-/** 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)
+/* This is just a simple example to show you how to read a line of text at a
+ * time, similar to using std::istream::getline()
  */
-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];
+    Glib::RefPtr<Gio::FileInputStream> file_stream = f->read ();
+    Glib::RefPtr<Gio::DataInputStream> data_stream =
+        Gio::DataInputStream::create (file_stream);
+    std::string line;
     int line_num = 1;
-    while (getline (stream, buf, 100)) {
-        std::cout << std::setw (5) << line_num++ << ": >" << buf << std::endl;
+    while (data_stream->read_line(line))
+    {
+        // print each line out prefixed with the line number
+        std::cout << std::setw (5) << line_num++ << ": >" << line << std::endl;
     }
     return 0;
 }

Modified: trunk/examples/book/giomm/monitor_directory/monitor_directory.cc
==============================================================================
--- trunk/examples/book/giomm/monitor_directory/monitor_directory.cc	(original)
+++ trunk/examples/book/giomm/monitor_directory/monitor_directory.cc	Thu Feb 14 02:15:08 2008
@@ -73,7 +73,7 @@
         // 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);
+            temp_file->create_file ();
         stream->write ("this is only a test");
         stream->close ();
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]