Commit c539cb97 authored by Daniel Elstner's avatar Daniel Elstner
Browse files

Add comment about assertions and reorganize the code a bit.

* src/fileio.cc (save_iochannel): Add comment about assertions and
reorganize the code a bit.
(Regexxer::save_file): Elaborate on why IOChannel::close() is
used explicitely.

* src/filetreeprivate.cc (FileTreePrivate::collatekey_sort_func):
Fix oversight:  Subtracting two unsigned values and converting the
result to a signed type relies on implementation-defined behavior.
Explicitely cast the operands to a signed type before substraction.

* src/main.cc (main): Use g_get_prgname() instead of the string
literal PACKAGE_TARNAME in the error message for Glib::OptionError
exceptions.

svn path=/trunk/; revision=621
parent ec127ebc
......@@ -58,10 +58,9 @@ Glib::RefPtr<FileBuffer> load_iochannel(const Glib::RefPtr<Glib::IOChannel>& inp
void save_iochannel(const Glib::RefPtr<Glib::IOChannel>& output, const Glib::RefPtr<FileBuffer>& buffer)
{
FileBuffer::iterator start = buffer->begin();
FileBuffer::iterator stop = start;
FileBuffer::iterator stop = buffer->begin();
for (; start; start = stop)
for (FileBuffer::iterator start = stop; start; start = stop)
{
stop.forward_chars(BUFSIZE); // inaccurate, but doesn't matter
const Glib::ustring chunk = buffer->get_slice(start, stop);
......@@ -69,6 +68,8 @@ void save_iochannel(const Glib::RefPtr<Glib::IOChannel>& output, const Glib::Ref
gsize bytes_written = 0;
const Glib::IOStatus status = output->write(chunk.data(), chunk.bytes(), bytes_written);
// These conditions really must hold true at this point
// since any error should have caused an exception.
g_assert(status == Glib::IO_STATUS_NORMAL);
g_assert(bytes_written == chunk.bytes());
}
......@@ -163,11 +164,13 @@ void save_file(const FileInfoPtr& fileinfo)
Glib::IOChannel::create_from_file(fileinfo->fullname, "w");
channel->set_buffer_size(BUFSIZE);
channel->set_encoding(fileinfo->encoding);
save_iochannel(channel, fileinfo->buffer);
channel->close(); // might throw
// Explicitely close() the buffer at this point so that
// we get an exception if closing the file fails.
channel->close();
fileinfo->buffer->set_modified(false);
}
......
......@@ -49,6 +49,8 @@ int default_sort_func(const Gtk::TreeModel::iterator& lhs, const Gtk::TreeModel:
int collatekey_sort_func(const Gtk::TreeModel::iterator& lhs, const Gtk::TreeModel::iterator& rhs)
{
typedef std::string::difference_type diff_type;
const FileTreeColumns& columns = FileTreeColumns::instance();
const std::string lhs_key = (*lhs)[columns.collatekey];
......@@ -57,7 +59,7 @@ int collatekey_sort_func(const Gtk::TreeModel::iterator& lhs, const Gtk::TreeMod
if (lhs_key.size() > 1 && rhs_key.size() > 1)
return lhs_key.compare(1, std::string::npos, rhs_key, 1, std::string::npos);
else
return (lhs_key.size() - rhs_key.size());
return diff_type(lhs_key.size()) - diff_type(rhs_key.size());
}
bool next_match_file(Gtk::TreeModel::iterator& iter, Gtk::TreeModel::Path* collapse)
......
......@@ -239,8 +239,8 @@ int main(int argc, char** argv)
std::auto_ptr<RegexxerOptions> options = RegexxerOptions::create();
Gtk::Main main_instance (argc, argv, options->context());
Glib::set_application_name(PACKAGE_NAME);
Glib::set_application_name(PACKAGE_NAME);
register_stock_items();
gtk_window_set_default_icon_name(PACKAGE_TARNAME);
......@@ -251,12 +251,13 @@ int main(int argc, char** argv)
options.reset();
Gtk::Main::run(*window.get_window());
return 0;
}
catch (const Glib::OptionError& error)
{
const Glib::ustring what = error.what();
g_printerr(PACKAGE_TARNAME ": %s\n", what.c_str());
return 1;
g_printerr("%s: %s\n", g_get_prgname(), what.c_str());
}
catch (const Glib::Error& error)
{
......@@ -272,5 +273,5 @@ int main(int argc, char** argv)
g_error("unhandled exception: (type unknown)");
}
return 0;
return 1;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment