[gnote] Replace boost::filesystem::path with Glib equivalents
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnote] Replace boost::filesystem::path with Glib equivalents
- Date: Tue, 15 Mar 2011 04:32:37 +0000 (UTC)
commit a52e9a93621f4df84c0c93b4b5367165778cc362
Author: Debarshi Ray <debarshir src gnome org>
Date: Tue Mar 8 16:53:12 2011 +0200
Replace boost::filesystem::path with Glib equivalents
Added some tests to reduce chances of breakage and document edge-case
behavior.
Changes from prior behavior:
+ sharp::file_filename("/foo/bar/") == "bar" (not ".")
Ditto for sharp::FileInfo::get_name.
+ sharp::file_basename("/foo/bar/") == "bar" (not "")
Fixes: https://bugzilla.gnome.org/641416
src/Makefile.am | 10 +++++--
src/sharp/directory.cpp | 30 ++++++++++++---------
src/sharp/fileinfo.cpp | 17 ++++++------
src/sharp/files.cpp | 20 ++++++--------
src/test/fileinfotest.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++
src/test/filestest.cpp | 44 +++++++++++++++++++++++++++++--
6 files changed, 145 insertions(+), 38 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 141d923..19c8795 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,9 +28,9 @@ GNOTE_LIBS = libgnote.a $(top_builddir)/libtomboy/libtomboy.la \
noinst_LIBRARIES = libgnote.a
bin_PROGRAMS = gnote
check_PROGRAMS = trietest stringtest notetest dttest uritest filestest \
- xmlreadertest
+ fileinfotest xmlreadertest
TESTS = trietest stringtest notetest dttest uritest filestest \
- xmlreadertest
+ fileinfotest xmlreadertest
trietest_SOURCES = test/trietest.cpp \
@@ -47,7 +47,11 @@ stringtest_LDADD = @PCRE_LIBS@ @LIBGLIBMM_LIBS@
filestest_SOURCES = test/filestest.cpp \
sharp/files.cpp
-filestest_LDADD = @BOOST_FILESYSTEM_LIBS@ -lboost_system-mt
+filestest_LDADD = @BOOST_FILESYSTEM_LIBS@ @LIBGLIBMM_LIBS@ -lboost_system-mt
+
+fileinfotest_SOURCES = test/fileinfotest.cpp \
+ sharp/fileinfo.cpp
+fileinfotest_LDADD = @BOOST_FILESYSTEM_LIBS@ @LIBGLIBMM_LIBS@ -lboost_system-mt
uritest_SOURCES = test/uritest.cpp \
sharp/string.cpp sharp/uri.cpp debug.cpp
diff --git a/src/sharp/directory.cpp b/src/sharp/directory.cpp
index 8a38f43..d9ee931 100644
--- a/src/sharp/directory.cpp
+++ b/src/sharp/directory.cpp
@@ -1,6 +1,7 @@
/*
* gnote
*
+ * Copyright (C) 2011 Debarshi Ray
* Copyright (C) 2009 Hubert Figuiere
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -27,8 +28,10 @@
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
+#include <glibmm.h>
#include "sharp/directory.hpp"
+#include "sharp/fileinfo.hpp"
#include "sharp/string.hpp"
namespace sharp {
@@ -38,20 +41,21 @@ namespace sharp {
const std::string & ext,
std::list<std::string> & list)
{
- boost::filesystem::path p(dir);
-
- if(!exists(p)) {
+ if (!Glib::file_test(dir, Glib::FILE_TEST_EXISTS))
return;
- }
- boost::filesystem::directory_iterator end_itr;
- for ( boost::filesystem::directory_iterator itr( p );
- itr != end_itr;
- ++itr )
- {
- // is_regular() is deprecated but is_regular_file isn't in 1.34.
- if ( is_regular(*itr) && (ext.empty() || (sharp::string_to_lower(extension(*itr)) == ext)) )
- {
- list.push_back(itr->string());
+
+ if (!Glib::file_test(dir, Glib::FILE_TEST_IS_DIR))
+ return;
+
+ Glib::Dir d(dir);
+
+ for (Glib::Dir::iterator itr = d.begin(); itr != d.end(); ++itr) {
+ const sharp::FileInfo file_info(*itr);
+ const std::string & extension = file_info.get_extension();
+
+ if (Glib::file_test(*itr, Glib::FILE_TEST_IS_REGULAR)
+ && (ext.empty() || (sharp::string_to_lower(extension) == ext))) {
+ list.push_back(*itr);
}
}
}
diff --git a/src/sharp/fileinfo.cpp b/src/sharp/fileinfo.cpp
index 52d2760..a53d570 100644
--- a/src/sharp/fileinfo.cpp
+++ b/src/sharp/fileinfo.cpp
@@ -23,8 +23,7 @@
*/
-#include <boost/filesystem/path.hpp>
-#include <boost/filesystem/convenience.hpp>
+#include <glibmm.h>
#include "sharp/fileinfo.hpp"
@@ -42,17 +41,19 @@ namespace sharp {
std::string FileInfo::get_name() const
{
-#if BOOST_VERSION >= 103600
- return boost::filesystem::path(m_path).filename();
-#else
- return boost::filesystem::path(m_path).leaf();
-#endif
+ return Glib::path_get_basename(m_path);
}
std::string FileInfo::get_extension() const
{
- return boost::filesystem::extension(m_path);
+ const std::string & name = get_name();
+
+ if ("." == name || ".." == name)
+ return "";
+
+ const std::string::size_type pos = name.find_last_of('.');
+ return (std::string::npos == pos) ? "" : std::string(name, pos);
}
diff --git a/src/sharp/files.cpp b/src/sharp/files.cpp
index d15cb32..f1a1e4b 100644
--- a/src/sharp/files.cpp
+++ b/src/sharp/files.cpp
@@ -24,7 +24,7 @@
#include <boost/version.hpp>
#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/convenience.hpp>
+#include <glibmm.h>
#include "files.hpp"
@@ -34,30 +34,28 @@ namespace sharp {
bool file_exists(const std::string & file)
{
- boost::filesystem::path p(file);
- // is_regular_file isn't in 1.34. is_regular is deprecated.
- return (exists(p) && is_regular(p));
+ return Glib::file_test(file, Glib::FILE_TEST_EXISTS)
+ && Glib::file_test(file, Glib::FILE_TEST_IS_REGULAR);
}
std::string file_basename(const std::string & p)
{
-#if BOOST_VERSION >= 103600
- return boost::filesystem::path(p).stem();
-#else
- return boost::filesystem::basename(boost::filesystem::path(p));
-#endif
+ const std::string & filename = Glib::path_get_basename(p);
+ const std::string::size_type pos = filename.find_last_of('.');
+
+ return std::string(filename, 0, pos);
}
std::string file_dirname(const std::string & p)
{
- return boost::filesystem::path(p).branch_path().string();
+ return Glib::path_get_dirname(p);
}
std::string file_filename(const std::string & p)
{
- return boost::filesystem::path(p).leaf();
+ return Glib::path_get_basename(p);
}
void file_delete(const std::string & p)
diff --git a/src/test/fileinfotest.cpp b/src/test/fileinfotest.cpp
new file mode 100644
index 0000000..c4c5931
--- /dev/null
+++ b/src/test/fileinfotest.cpp
@@ -0,0 +1,62 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2011 Debarshi Ray
+ *
+ * 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 3 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 <boost/test/minimal.hpp>
+
+#include "sharp/fileinfo.hpp"
+
+int test_main(int /*argc*/, char ** /*argv*/)
+{
+ {
+ sharp::FileInfo file_info("/foo/bar/baz.txt");
+
+ BOOST_CHECK(file_info.get_name() == "baz.txt");
+ BOOST_CHECK(file_info.get_extension() == ".txt");
+ }
+
+ {
+ sharp::FileInfo file_info("/foo/bar/baz.");
+
+ BOOST_CHECK(file_info.get_name() == "baz.");
+ BOOST_CHECK(file_info.get_extension() == ".");
+ }
+
+ {
+ sharp::FileInfo file_info("/foo/bar/baz");
+
+ BOOST_CHECK(file_info.get_name() == "baz");
+ BOOST_CHECK(file_info.get_extension() == "");
+ }
+
+ {
+ sharp::FileInfo file_info("/foo/bar/..");
+
+ BOOST_CHECK(file_info.get_name() == "..");
+ BOOST_CHECK(file_info.get_extension() == "");
+ }
+
+ {
+ sharp::FileInfo file_info("/foo/bar/");
+
+ BOOST_CHECK(file_info.get_name() == "bar");
+ BOOST_CHECK(file_info.get_extension() == "");
+ }
+
+ return 0;
+}
diff --git a/src/test/filestest.cpp b/src/test/filestest.cpp
index d11e54c..7579dba 100644
--- a/src/test/filestest.cpp
+++ b/src/test/filestest.cpp
@@ -1,6 +1,7 @@
/*
* gnote
*
+ * Copyright (C) 2011 Debarshi Ray
* Copyright (C) 2009 Hubert Figuiere
*
* This program is free software: you can redistribute it and/or modify
@@ -20,6 +21,7 @@
#include <iostream>
#include <boost/test/minimal.hpp>
+#include <glibmm.h>
#include "sharp/files.hpp"
@@ -27,10 +29,46 @@ using namespace sharp;
int test_main(int /*argc*/, char ** /*argv*/)
{
- std::string path = "/foo/bar/baz.txt";
+ {
+ std::string path = "/foo/bar/baz.txt";
- BOOST_CHECK(file_basename(path) == "baz");
- BOOST_CHECK(file_dirname(path) == "/foo/bar");
+ BOOST_CHECK(file_basename(path) == "baz");
+ BOOST_CHECK(file_dirname(path) == "/foo/bar");
+ BOOST_CHECK(file_filename(path) == "baz.txt");
+ }
+
+ {
+ std::string path = "/foo/bar/baz";
+
+ BOOST_CHECK(file_basename(path) == "baz");
+ BOOST_CHECK(file_dirname(path) == "/foo/bar");
+ BOOST_CHECK(file_filename(path) == "baz");
+ }
+
+ {
+ std::string path = "/foo/bar/..";
+
+ BOOST_CHECK(file_basename(path) == ".");
+ BOOST_CHECK(file_dirname(path) == "/foo/bar");
+ BOOST_CHECK(file_filename(path) == "..");
+ }
+
+ {
+ std::string path = "/foo/bar/";
+
+ BOOST_CHECK(file_basename(path) == "bar");
+ BOOST_CHECK(file_dirname(path) == "/foo/bar");
+ BOOST_CHECK(file_filename(path) == "bar");
+ }
+
+ {
+ std::string dir = Glib::get_current_dir();
+
+ BOOST_CHECK(file_exists(dir) == false);
+ // Very unlikely to exist.
+ BOOST_CHECK(file_exists(__FILE__ __FILE__) == false);
+ BOOST_CHECK(file_exists(__FILE__) == true);
+ }
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]