Commit 933c04eb authored by Murray Cumming's avatar Murray Cumming Committed by Murray Cumming
Browse files

set_fallback_encoding_from_string(): Ignore empty value from GConf, so

2003-12-07  Murray Cumming  <murrayc@usa.net>

        * src/configdata.cc: set_fallback_encoding_from_string(): Ignore
        empty value from GConf, so that the default fallback encoding is
        used. That is ISO-8859-15, also known as Latin 9, which seems to
        be commonly-used on Windows.
        * src/fileio.[h|cc]: Use gnome-vfsmm instead of Glib::IOChannel
        to read and write file contents, so we can use any URI.
        * src/filetree.[h|cc]: Likewise, use gnome-vfsmm instead of
        Glib::Dir to list the files.
        * configure.ac: Added pkg-config check for gnome-vfsmm.
parent 7895d37f
0.6.0:
2003-12-07 Murray Cumming <murrayc@usa.net>
* src/configdata.cc: set_fallback_encoding_from_string(): Ignore
empty value from GConf, so that the default fallback encoding is
used. That is ISO-8859-15, also known as Latin 9, which seems to
be commonly-used on Windows.
* src/fileio.[h|cc]: Use gnome-vfsmm instead of Glib::IOChannel
to read and write file contents, so we can use any URI.
* src/filetree.[h|cc]: Likewise, use gnome-vfsmm instead of
Glib::Dir to list the files.
* configure.ac: Added pkg-config check for gnome-vfsmm.
2003-11-29 Murray Cumming <murrayc@usa.net>
* configure.ac: regexxer now depends on gconfmm. Removed the check
for 2.2-specific features.
* Remoevd all gtkmm 2.2-specific #ifdefs because 2.2 is everywhere
* Removed all gtkmm 2.2-specific #ifdefs because 2.2 is everywhere
now..
* src/configdata.[h|cc]: Changed the implemention to use GConfmm
instead of the text file.
0.5.0:
2003-10-31 Murray Cumming <murrayc@usa.net>
* src/mainwindow.cc: Used larger HIG-like borders and spacings to
......
regexxer 0.6 (2003-12-07)
* Use GConf for preferences, instead
of a text file.
* Use gnome-vfs to allow search/replace
of files at any URI, not just on
local file system.
(Murray Cumming)
regexxer 0.5 (2003-10-31)
* Undo feature (Daniel Elstner)
......
......@@ -18,7 +18,7 @@
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
AC_INIT([regexxer], [0.5], [daniel.elstner@gmx.net], [regexxer])
AC_INIT([regexxer], [0.6], [daniel.elstner@gmx.net], [regexxer])
AC_PREREQ([2.53])
AC_REVISION([$Revision$])
......@@ -34,7 +34,7 @@ AC_CHECK_HEADERS([sys/types.h sys/stat.h])
AC_CHECK_FUNCS([umask])
AC_CHECK_TYPES([mode_t])
PKG_CHECK_MODULES([GTKMM], [gtkmm-2.0 >= 2.2.8 gconfmm-2.0 >= 2.0.0])
PKG_CHECK_MODULES([GTKMM], [gtkmm-2.0 >= 2.2.8 gconfmm-2.0 >= 2.0.0 gnome-vfsmm-2.0 >= 1.3.5])
AC_SUBST([GTKMM_CFLAGS])
AC_SUBST([GTKMM_LIBS])
......
......@@ -191,16 +191,19 @@ void ConfigData::save()
void ConfigData::set_fallback_encoding_from_string(const Glib::ustring& value)
{
std::string encoding = value.raw();
if(Util::validate_encoding(encoding))
{
std::transform(encoding.begin(), encoding.end(), encoding.begin(), &Glib::Ascii::toupper);
fallback_encoding = encoding;
}
else
if(!value.empty()) //Use the default fallback encoding if there is nothing in the config settings.
{
//print_invalid_value_warning(value, "fallback_encoding");
std::string encoding = value.raw();
if(Util::validate_encoding(encoding))
{
std::transform(encoding.begin(), encoding.end(), encoding.begin(), &Glib::Ascii::toupper);
fallback_encoding = encoding;
}
else
{
//print_invalid_value_warning(value, "fallback_encoding");
}
}
}
......
......@@ -25,7 +25,9 @@
#include <glib.h>
#include <glibmm.h>
#include <libgnomevfsmm.h>
#include <cstring>
#include <iostream> //For std::cerr.
namespace
......@@ -35,62 +37,81 @@ using Regexxer::FileBuffer;
enum { BUFSIZE = 4096 };
Glib::RefPtr<FileBuffer> load_iochannel(const Glib::RefPtr<Glib::IOChannel>& input)
Glib::RefPtr<FileBuffer> load_try_encoding(const std::string& filename, const std::string& encoding)
{
const Glib::RefPtr<FileBuffer> text_buffer = FileBuffer::create();
FileBuffer::iterator text_end (text_buffer->end());
const Util::ScopedArray<char> inbuf (new char[BUFSIZE]);
gsize bytes_read = 0;
//open the input file for read access:
Gnome::Vfs::Handle read_handle;
while(input->read(inbuf.get(), BUFSIZE, bytes_read) == Glib::IO_STATUS_NORMAL)
try
{
if(std::memchr(inbuf.get(), '\0', bytes_read)) // binary file?
return Glib::RefPtr<FileBuffer>();
text_end = text_buffer->insert(text_end, inbuf.get(), inbuf.get() + bytes_read);
read_handle.open(filename, Gnome::Vfs::OPEN_READ);
}
g_assert(bytes_read == 0);
return text_buffer;
}
void save_iochannel(const Glib::RefPtr<Glib::IOChannel>& output, const Glib::RefPtr<FileBuffer>& buffer)
{
FileBuffer::iterator start = buffer->begin();
FileBuffer::iterator stop = start;
for(; start; start = stop)
catch(const Gnome::Vfs::exception& ex)
{
stop.forward_chars(BUFSIZE); // inaccurate, but doesn't matter
const Glib::ustring chunk (buffer->get_text(start, stop));
gsize bytes_written = 0;
const Glib::IOStatus status = output->write(chunk.data(), chunk.bytes(), bytes_written);
g_assert(status == Glib::IO_STATUS_NORMAL);
g_assert(bytes_written == chunk.bytes());
//TODO: Give the user some meaningful information?
return Glib::RefPtr<FileBuffer>();
}
}
Glib::RefPtr<FileBuffer> load_try_encoding(const std::string& filename, const std::string& encoding)
{
//TODO: Use gnome-vfsmm: murrayc.
const Glib::RefPtr<Glib::IOChannel> channel = Glib::IOChannel::create_from_file(filename, "r");
channel->set_buffer_size(BUFSIZE);
// The whole thing:
//Use std::string because it might not be UTF8,
//and because Gnome::Vfs::Handle::read() reads bytes, not necessarily whole characters.
std::string text;
try
{
channel->set_encoding(encoding);
return load_iochannel(channel);
// read data from the input URI:
// Buffer for each chunk:
const Util::ScopedArray<char> inbuf (new char[BUFSIZE]);
bool bContinue = true;
while(bContinue)
{
Gnome::Vfs::FileSize bytes_read = read_handle.read(inbuf.get(), BUFSIZE);
if(bytes_read == 0)
bContinue = false; //stop because we reached the end.
else
{
//Check that it's not a binary file:
if(std::memchr(inbuf.get(), '\0', bytes_read))
return Glib::RefPtr<FileBuffer>();
//Add the text to the string:
text += std::string(inbuf.get(), bytes_read);
}
}
}
catch(const Gnome::Vfs::exception& ex)
{
std::cerr << "Gnome::Vfs::Handle::read() failed: " << ex.what() << std::endl;
//TODO: Give the user some meaningful information?
return Glib::RefPtr<FileBuffer>();
}
catch(const Glib::ConvertError&)
{}
return Glib::RefPtr<FileBuffer>();
try
{
Glib::RefPtr<FileBuffer> text_buffer = FileBuffer::create();
//Try to convert it to UTF8, guessing the encoding:
//(most of the time, it will already be UTF8
//TODO_performance: This copying of huge strings is probably very slow:
//encoding is a parameter to this method.
//if it fails then this method will be called with a different encoding,
//until the calling method finally gives up.
std::string text_utf8 = Glib::convert_with_fallback(text, "UTF-8", encoding);
//Add the string to a TextBuffer:
text_buffer->insert(text_buffer->end(), text_utf8);
return text_buffer;
}
catch(const Glib::ConvertError& ex)
{
//The conversion failed - we probably guessed the encoding wrongly.
return Glib::RefPtr<FileBuffer>();
}
}
} // anonymous namespace
......@@ -161,16 +182,35 @@ void load_file(const FileInfoPtr& fileinfo, const std::string& fallback_encoding
void save_file(const FileInfoPtr& fileinfo)
{
const Glib::RefPtr<Glib::IOChannel> channel =
Glib::IOChannel::create_from_file(fileinfo->fullname, "w");
channel->set_buffer_size(BUFSIZE);
//open the input file for write access:
Gnome::Vfs::Handle write_handle;
channel->set_encoding(fileinfo->encoding);
save_iochannel(channel, fileinfo->buffer);
channel->close(); // might throw
try
{
write_handle.open(fileinfo->fullname, Gnome::Vfs::OPEN_WRITE);
}
catch(const Gnome::Vfs::exception& ex)
{
std::cerr << "Gnome::Vfs::Handle::open() failed: " << ex.what() << std::endl;
//TODO: Give the user some meaningful information?
return;
}
try
{
// Write the data:
//TODO_performance: It might be faster if we don't use the temporary copy:
const Glib::ustring chunk = fileinfo->buffer->get_text();
/* GnomeVFSFileSize bytes_written = */
write_handle.write(chunk.c_str(), chunk.bytes());
}
catch(const Gnome::Vfs::exception& ex)
{
std::cerr << "Gnome::Vfs::Handle::write() failed: " << ex.what() << std::endl;
//TODO: Give the user some meaningful information?
return;
}
fileinfo->buffer->set_modified(false);
}
......
......@@ -25,6 +25,8 @@
#include "stringutils.h"
#include <gtkmm/stock.h>
#include <libgnomevfsmm.h> //For discovering the list of files.
//#include <iostream> //Just for debug stuff. murrayc
#include <config.h>
......@@ -347,41 +349,67 @@ void FileTree::find_recursively(const std::string& dirname, FindData& find_data)
using namespace Glib;
int file_count = 0;
Dir dir (dirname);
for(Dir::iterator pos = dir.begin(); pos != dir.end(); ++pos)
//Get the filenames in the directory:
typedef std::list< std::string > type_listStrings;
type_listStrings listFilenames;
try
{
if(signal_pulse()) // emit
break;
Gnome::Vfs::DirectoryHandle handle;
handle.open(dirname, Gnome::Vfs::FILE_INFO_DEFAULT |
Gnome::Vfs::FILE_INFO_GET_MIME_TYPE |
Gnome::Vfs::FILE_INFO_FORCE_SLOW_MIME_TYPE);
const std::string basename (*pos);
bool file_exists = true;
while(file_exists) //read_next() returns false when there are no more files to read.
{
if(signal_pulse.emit())
break;
Glib::RefPtr<Gnome::Vfs::FileInfo> refFileInfo = handle.read_next(file_exists);
Glib::ustring basename = refFileInfo->get_name();
//Ignore any non-file filenames:
if( (basename != "..") && (basename != ".") && !basename.empty() )
{
//Avoid hidden files, if that is wanted:
if(!find_data.hidden && *basename.begin() == '.')
continue;
if(!find_data.hidden && *basename.begin() == '.')
continue;
const std::string fullname (build_filename(dirname, basename));
const std::string fullname (build_filename(dirname, basename));
//Check the file:
try
{
if(find_check_file(basename, fullname, find_data)) // file added?
++file_count;
}
catch(const Glib::FileError& error)
{
// Collect errors but don't interrupt the search.
find_data.error_list->push_back(error.what());
}
catch(const Glib::ConvertError& error) // unlikely due to use of our own fallback conversion
{
// Don't use Glib::locale_to_utf8() because we already
// tried that in Util::filename_to_utf8_fallback().
//
const Glib::ustring name = Util::convert_to_ascii(fullname);
const Glib::ustring what = error.what();
try
{
if(find_check_file(basename, fullname, find_data)) // file added?
++file_count;
}
catch(const Glib::FileError& error)
{
// Collect errors but don't interrupt the search.
find_data.error_list->push_back(error.what());
}
catch(const Glib::ConvertError& error) // unlikely due to use of our own fallback conversion
{
// Don't use Glib::locale_to_utf8() because we already
// tried that in Util::filename_to_utf8_fallback().
//
const Glib::ustring name = Util::convert_to_ascii(fullname);
const Glib::ustring what = error.what();
g_warning("Eeeek, can't convert filename `%s' to UTF-8: %s", name.c_str(), what.c_str());
}
g_warning("Eeeek, can't convert filename `%s' to UTF-8: %s", name.c_str(), what.c_str());
}
}
}
catch(const Gnome::Vfs::exception& ex)
{
//TODO: Do something?
//Gtk::MessageDialog dialog(*this, "Regexxer failed while obtaining the list of files.");
//dialog.run();
//return false; //Stop trying.
}
find_increment_file_count(find_data, file_count);
}
......
......@@ -23,6 +23,7 @@
#include <glib.h>
#include <gconfmm.h>
#include <libgnomevfsmm.h>
#include <gtkmm/iconfactory.h>
#include <gtkmm/iconset.h>
#include <gtkmm/iconsource.h>
......@@ -136,6 +137,7 @@ int main(int argc, char** argv)
try
{
Gnome::Conf::init();
Gnome::Vfs::init();
Gtk::Main main_instance (&argc, &argv);
Glib::set_application_name("regexxer");
......
Supports Markdown
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