From e53ba8110b960005dd03d8460fff28d463bf864f Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 16 Jan 2010 14:42:36 -0500 Subject: [PATCH] Enhancement - Path integration - Implemented Path::operator (std::string) const, to provide an automatic cast to std::string for any Path, File or Directory. - Made use of new cast in various code. - Changed use of spaces in atoi () calls. - Switched from std::string::data () to std::string::c_str () calls. --- src/Config.cpp | 2 +- src/Context.cpp | 12 ++++++------ src/Path.cpp | 6 ++++++ src/Path.h | 2 ++ src/TDB.cpp | 2 +- src/report.cpp | 12 ++++++------ src/tests/directory.t.cpp | 5 ++++- src/tests/file.t.cpp | 5 ++++- src/tests/path.t.cpp | 7 +++++-- 9 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 0648d56d3..eec6b148f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -321,7 +321,7 @@ void Config::parse (const std::string& input, int nest /* = 1 */) if (included.is_absolute ()) { if (included.readable ()) - this->load (included.data, nest + 1); + this->load (included, nest + 1); else throw std::string ("Could not read include file '") + included.data + "'"; } diff --git a/src/Context.cpp b/src/Context.cpp index 3d3d83494..15ac70e39 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -125,7 +125,7 @@ void Context::initialize () // init TDB. tdb.clear (); std::vector all; - split (all, location.data, ','); + split (all, location, ','); foreach (path, all) tdb.location (*path); } @@ -372,7 +372,7 @@ void Context::loadCorrectConfigFile () file_override = *arg; rc = File (arg->substr (3)); - home = rc.data; + home = rc; std::string::size_type last_slash = rc.data.rfind ("/"); if (last_slash != std::string::npos) home = rc.data.substr (0, last_slash); @@ -388,7 +388,7 @@ void Context::loadCorrectConfigFile () // Load rc file. config.clear (); // Dump current values. config.setDefaults (); // Add in the custom reports. - config.load (rc.data); // Load new file. + config.load (rc); // Load new file. if (config.get ("data.location") != "") data = Directory (config.get ("data.location")); @@ -417,19 +417,19 @@ void Context::loadCorrectConfigFile () + rc.data + " created, so task can proceed?")) { - config.createDefaultRC (rc.data, data.data); + config.createDefaultRC (rc, data); } else throw std::string ("Cannot proceed without rc file."); } // Create data location, if necessary. - config.createDefaultData (data.data); + config.createDefaultData (data); // Load rc file. config.clear (); // Dump current values. config.setDefaults (); // Add in the custom reports. - config.load (rc.data); // Load new file. + config.load (rc); // Load new file. // Apply overrides of type: "rc.name:value", or "rc.name=value". std::vector filtered; diff --git a/src/Path.cpp b/src/Path.cpp index 8bcd41342..b6613a0b8 100644 --- a/src/Path.cpp +++ b/src/Path.cpp @@ -65,6 +65,12 @@ Path& Path::operator= (const Path& other) return *this; } +//////////////////////////////////////////////////////////////////////////////// +Path::operator std::string () const +{ + return data; +} + //////////////////////////////////////////////////////////////////////////////// std::string Path::name () const { diff --git a/src/Path.h b/src/Path.h index 5f1ba2c1e..b934d0ba3 100644 --- a/src/Path.h +++ b/src/Path.h @@ -39,6 +39,8 @@ public: virtual ~Path (); Path& operator= (const Path&); + operator std::string () const; + std::string name () const; std::string parent () const; std::string extension () const; diff --git a/src/TDB.cpp b/src/TDB.cpp index c231d86ec..c2b9cabb7 100644 --- a/src/TDB.cpp +++ b/src/TDB.cpp @@ -114,7 +114,7 @@ void TDB::location (const std::string& path) path + "' does not exist, or is not readable and writable."; - mLocations.push_back (Location (d.data)); + mLocations.push_back (Location (d)); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/report.cpp b/src/report.cpp index aaa719df4..f13018295 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -1535,7 +1535,7 @@ int handleReportCalendar (std::string &outs) // task cal 2010 monthsToDisplay = 12; mFrom = 1; - yFrom = atoi( context.args[1].data()); + yFrom = atoi (context.args[1].c_str ()); } } else if (numberOfArgs == 3) { @@ -1547,15 +1547,15 @@ int handleReportCalendar (std::string &outs) else { // task cal 8 2010 monthsToDisplay = monthsPerLine; - mFrom = atoi( context.args[1].data()); - yFrom = atoi( context.args[2].data()); + mFrom = atoi (context.args[1].c_str ()); + yFrom = atoi (context.args[2].c_str ()); } } else if (numberOfArgs == 4) { // task cal 8 2010 y monthsToDisplay = 12; - mFrom = atoi( context.args[1].data()); - yFrom = atoi( context.args[2].data()); + mFrom = atoi (context.args[1].c_str ()); + yFrom = atoi (context.args[2].c_str ()); } int countDueDates = 0; @@ -1728,7 +1728,7 @@ int handleReportStats (std::string &outs) dataSize += undo.size (); std::vector undoTxns; - slurp (undo.data, undoTxns, false); + slurp (undo, undoTxns, false); int undoCount = 0; foreach (tx, undoTxns) if (tx->substr (0, 3) == "---") diff --git a/src/tests/directory.t.cpp b/src/tests/directory.t.cpp index 26418b969..d6e5cde56 100644 --- a/src/tests/directory.t.cpp +++ b/src/tests/directory.t.cpp @@ -33,7 +33,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (20); + UnitTest t (21); // Directory (const File&); // Directory (const Path&); @@ -55,6 +55,9 @@ int main (int argc, char** argv) Directory d5 = d4; t.is (d5.data, "/tmp/test_directory", "Directory::operator="); + // operator (std::string) const; + t.is ((std::string) d3, "/tmp", "Directory::operator (std::string) const"); + // virtual bool create (); t.ok (d5.create (), "Directory::create /tmp/test_directory"); t.ok (d5.exists (), "Directory::exists /tmp/test_directory"); diff --git a/src/tests/file.t.cpp b/src/tests/file.t.cpp index 905aa265e..af0b3b9a5 100644 --- a/src/tests/file.t.cpp +++ b/src/tests/file.t.cpp @@ -33,7 +33,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (5); + UnitTest t (6); File::write ("/tmp/file.t.txt", "This is a test\n"); File f6 ("/tmp/file.t.txt"); @@ -41,6 +41,9 @@ int main (int argc, char** argv) t.ok (f6.mode () & S_IRUSR, "File::mode /tmp/file.t.txt good"); t.ok (File::remove ("/tmp/file.t.txt"), "File::remove /tmp/file.t.txt good"); + // operator (std::string) const; + t.is ((std::string) f6, "/tmp/file.t.txt", "File::operator (std::string) const"); + t.ok (File::create ("/tmp/file.t.create"), "File::create /tmp/file.t.create good"); t.ok (File::remove ("/tmp/file.t.create"), "File::remove /tmp/file.t.create good"); diff --git a/src/tests/path.t.cpp b/src/tests/path.t.cpp index aaaed861b..462598fac 100644 --- a/src/tests/path.t.cpp +++ b/src/tests/path.t.cpp @@ -1,7 +1,7 @@ //////////////////////////////////////////////////////////////////////////////// // task - a command line task list manager. // -// Copyright 2006 - 2009, Paul Beckingham. +// Copyright 2006 - 2010, Paul Beckingham. // All rights reserved. // // This program is free software; you can redistribute it and/or modify it under @@ -33,7 +33,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (31); + UnitTest t (32); // Path (); Path p0; @@ -54,6 +54,9 @@ int main (int argc, char** argv) Path p3_copy (p3); t.is (p3.data, p3_copy.data, "Path::Path (Path&)"); + // operator (std::string) const; + t.is ((std::string) p3, "/tmp", "Path::operator (std::string) const"); + // std::string name () const; Path p4 ("/a/b/c/file.ext"); t.is (p4.name (), "file.ext", "/a/b/c/file.ext name is file.ext");