diff --git a/src/Directory.cpp b/src/Directory.cpp new file mode 100644 index 000000000..2f7599996 --- /dev/null +++ b/src/Directory.cpp @@ -0,0 +1,133 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include "Directory.h" + +//////////////////////////////////////////////////////////////////////////////// +Directory::Directory () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory::Directory (const Directory& other) +: File::File (other) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory::Directory (const File& other) +: File::File (other) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory::Directory (const Path& other) +: File::File (other) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory::Directory (const std::string& in) +: File::File (in) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory::~Directory () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Directory& Directory::operator= (const Directory& other) +{ + if (this != &other) + { + File::operator= (other); + } + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Directory::create () +{ + return mkdir (data.c_str (), 0755) == 0 ? true : false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Directory::remove () +{ + return rmdir (data.c_str ()) == 0 ? true : false; +} + +//////////////////////////////////////////////////////////////////////////////// +std::vector Directory::list () +{ + std::vector files; + list (data, files, false); + return files; +} + +//////////////////////////////////////////////////////////////////////////////// +std::vector Directory::listRecursive () +{ + std::vector files; + list (data, files, true); + return files; +} + +//////////////////////////////////////////////////////////////////////////////// +void Directory::list ( + const std::string& base, + std::vector & results, + bool recursive) +{ + DIR* dp = opendir (base.c_str ()); + if (dp != NULL) + { + struct dirent* de; + while ((de = readdir (dp)) != NULL) + { + if (!strcmp (de->d_name, ".") || + !strcmp (de->d_name, "..")) + continue; + + if (recursive && de->d_type == DT_DIR) + list (base + "/" + de->d_name, results, recursive); + else + results.push_back (base + "/" + de->d_name); + } + + closedir (dp); + } +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Directory.h b/src/Directory.h new file mode 100644 index 000000000..5f48107a2 --- /dev/null +++ b/src/Directory.h @@ -0,0 +1,55 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_DIRECTORY +#define INCLUDED_DIRECTORY + +#include "File.h" + +class Directory : public File +{ +public: + Directory (); + Directory (const Directory&); + Directory (const File&); + Directory (const Path&); + Directory (const std::string&); + virtual ~Directory (); + + Directory& operator= (const Directory&); + + virtual bool create (); + virtual bool remove (); + + std::vector list (); + std::vector listRecursive (); + +private: + void list (const std::string&, std::vector &, bool); +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/File.cpp b/src/File.cpp new file mode 100644 index 000000000..740a63215 --- /dev/null +++ b/src/File.cpp @@ -0,0 +1,286 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include "File.h" + +//////////////////////////////////////////////////////////////////////////////// +File::File () +: Path::Path () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +File::File (const Path& other) +: Path::Path (other) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +File::File (const File& other) +: Path::Path (other) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +File::File (const std::string& in) +: Path::Path (in) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +File::~File () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +File& File::operator= (const File& other) +{ + if (this != &other) + { + Path::operator= (other); + } + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::create () +{ + std::ofstream out (data.c_str ()); + if (out.good ()) + { + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::remove () +{ + return unlink (data.c_str ()) == 0 ? true : false; +} + +//////////////////////////////////////////////////////////////////////////////// +// S_IFMT 0170000 type of file +// S_IFIFO 0010000 named pipe (fifo) +// S_IFCHR 0020000 character special +// S_IFDIR 0040000 directory +// S_IFBLK 0060000 block special +// S_IFREG 0100000 regular +// S_IFLNK 0120000 symbolic link +// S_IFSOCK 0140000 socket +// S_IFWHT 0160000 whiteout +// S_ISUID 0004000 set user id on execution +// S_ISGID 0002000 set group id on execution +// S_ISVTX 0001000 save swapped text even after use +// S_IRUSR 0000400 read permission, owner +// S_IWUSR 0000200 write permission, owner +// S_IXUSR 0000100 execute/search permission, owner +mode_t File::mode () +{ + struct stat s; + if (!stat (data.c_str (), &s)) + return s.st_mode; + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +size_t File::size () const +{ + struct stat s; + if (!stat (data.c_str (), &s)) + return s.st_size; + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::create (const std::string& name) +{ + std::ofstream out (expand (name).c_str ()); + if (out.good ()) + { + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string File::read (const std::string& name) +{ + std::string contents = ""; + + std::ifstream in (name.c_str ()); + if (in.good ()) + { + std::string line; + while (getline (in, line)) + contents += line + "\n"; + + in.close (); + } + + return contents; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::read (const std::string& name, std::string& contents) +{ + contents = ""; + + std::ifstream in (name.c_str ()); + if (in.good ()) + { + std::string line; + while (getline (in, line)) + contents += line + "\n"; + + in.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::read (const std::string& name, std::vector & contents) +{ + contents.clear (); + + std::ifstream in (name.c_str ()); + if (in.good ()) + { + std::string line; + while (getline (in, line)) + contents.push_back (line); + + in.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::write (const std::string& name, const std::string& contents) +{ + std::ofstream out (expand (name).c_str (), + std::ios_base::out | std::ios_base::trunc); + if (out.good ()) + { + out << contents; + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::write ( + const std::string& name, + const std::vector & lines, + bool addNewlines /* = true */) +{ + std::ofstream out (expand (name).c_str (), + std::ios_base::out | std::ios_base::trunc); + if (out.good ()) + { + std::vector ::const_iterator it; + for (it = lines.begin (); it != lines.end (); ++it) + { + out << *it; + + if (addNewlines) + out << "\n"; + } + + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::append (const std::string& name, const std::string& contents) +{ + std::ofstream out (expand (name).c_str (), + std::ios_base::out | std::ios_base::app); + if (out.good ()) + { + out << contents; + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::append ( + const std::string& name, + const std::vector & lines, + bool addNewlines /* = true */) +{ + std::ofstream out (expand (name).c_str (), + std::ios_base::out | std::ios_base::app); + if (out.good ()) + { + std::vector ::const_iterator it; + for (it = lines.begin (); it != lines.end (); ++it) + { + out << *it; + + if (addNewlines) + out << "\n"; + } + + out.close (); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool File::remove (const std::string& name) +{ + return unlink (expand (name).c_str ()) == 0 ? true : false; +} + +//////////////////////////////////////////////////////////////////////////////// + diff --git a/src/File.h b/src/File.h new file mode 100644 index 000000000..10e6c6b3f --- /dev/null +++ b/src/File.h @@ -0,0 +1,81 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_FILE +#define INCLUDED_FILE + +#include +#include +#include +#include "Path.h" + +class File : public Path +{ +public: + File (); + File (const Path&); + File (const File&); + File (const std::string&); + virtual ~File (); + + File& operator= (const File&); + + virtual bool create (); + virtual bool remove (); + +// bool open (); +// bool openAndLock (); +// void close (); + +// bool lock (); +// bool lockNoWait (); +// void unlock (); + +// void read (std::string&); +// void read (std::vector &); + +// void write (const std::string&); +// void write (const std::vector &); + + virtual mode_t mode (); + virtual size_t size () const; + + static bool create (const std::string&); + static std::string read (const std::string&); + static bool read (const std::string&, std::string&); + static bool read (const std::string&, std::vector &); + static bool write (const std::string&, const std::string&); + static bool write (const std::string&, const std::vector &, bool addNewlines = true); + static bool append (const std::string&, const std::string&); + static bool append (const std::string&, const std::vector &, bool addNewlines = true); + static bool remove (const std::string&); + +private: +// int handle; +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Makefile.am b/src/Makefile.am index 175f6a414..4bc6c9a73 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,11 +1,14 @@ bin_PROGRAMS = task -task_SOURCES = Att.cpp Cmd.cpp Config.cpp Context.cpp Date.cpp Duration.cpp \ - Filter.cpp Grid.cpp Keymap.cpp Location.cpp Nibbler.cpp \ - Record.cpp Sequence.cpp StringTable.cpp Subst.cpp Task.cpp \ - TDB.cpp Table.cpp Timer.cpp Permission.cpp Color.cpp edit.cpp \ - command.cpp import.cpp interactive.cpp recur.cpp report.cpp \ - custom.cpp rules.cpp main.cpp text.cpp util.cpp \ - Att.h Cmd.h Config.h Context.h Date.h Duration.h Filter.h \ - Grid.h Keymap.h Location.h Nibbler.h Record.h Sequence.h \ - StringTable.h Subst.h Task.h TDB.h Table.h Timer.h \ - Permission.h Color.h i18n.h main.h text.h util.h +task_SOURCES = Att.cpp Cmd.cpp Color.cpp Config.cpp Context.cpp Date.cpp \ + Directory.cpp Duration.cpp File.cpp Filter.cpp Grid.cpp \ + Keymap.cpp Location.cpp Nibbler.cpp Path.cpp Permission.cpp \ + Record.cpp Sequence.cpp StringTable.cpp Subst.cpp TDB.cpp \ + Table.cpp Task.cpp Timer.cpp command.cpp custom.cpp edit.cpp \ + import.cpp interactive.cpp main.cpp recur.cpp report.cpp \ + rules.cpp text.cpp util.cpp \ + Att.h Cmd.h Color.h Config.h Context.h Date.h Directory.h \ + Duration.h File.h Filter.h Grid.h Keymap.h Location.h \ + Nibbler.h Path.h Permission.h Record.h Sequence.h \ + StringTable.h Subst.h TDB.h Table.h Task.h Timer.h i18n.h \ + main.h text.h util.h + diff --git a/src/Path.cpp b/src/Path.cpp new file mode 100644 index 000000000..a6551e423 --- /dev/null +++ b/src/Path.cpp @@ -0,0 +1,197 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include +#include "Path.h" + +//////////////////////////////////////////////////////////////////////////////// +Path::Path () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Path::Path (const Path& other) +{ + if (this != &other) + data = other.data; +} + +//////////////////////////////////////////////////////////////////////////////// +Path::Path (const std::string& in) +{ + data = expand (in); +} + +//////////////////////////////////////////////////////////////////////////////// +Path::~Path () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +Path& Path::operator= (const Path& other) +{ + if (this != &other) + this->data = other.data; + + return *this; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string Path::name () const +{ + if (data.length ()) + { + std::string::size_type slash = data.rfind ('/'); + if (slash != std::string::npos) + return data.substr (slash + 1, std::string::npos); + } + + return data; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string Path::parent () const +{ + if (data.length ()) + { + std::string::size_type slash = data.rfind ('/'); + if (slash != std::string::npos) + return data.substr (0, slash); + } + + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +std::string Path::extension () const +{ + if (data.length ()) + { + std::string::size_type dot = data.rfind ('.'); + if (dot != std::string::npos) + return data.substr (dot + 1, std::string::npos); + } + + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Path::exists () const +{ + return access (data.c_str (), F_OK) ? false : true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Path::is_directory () const +{ + struct stat s = {0}; + if (! stat (data.c_str (), &s) && + s.st_mode & S_IFDIR) + return true; + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Path::readable () const +{ + return access (data.c_str (), R_OK) ? false : true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Path::writable () const +{ + return access (data.c_str (), W_OK) ? false : true; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Path::executable () const +{ + return access (data.c_str (), X_OK) ? false : true; +} + +//////////////////////////////////////////////////////////////////////////////// +// ~ --> /home/user +// ~foo/x --> /home/foo/s +// ~/x --> /home/foo/x +std::string Path::expand (const std::string& in) +{ + std::string copy = in; + + std::string::size_type tilde = copy.find ("~"); + std::string::size_type slash; + + if (tilde != std::string::npos) + { + struct passwd* pw = getpwuid (getuid ()); + + // Convert: ~ --> /home/user + if (copy.length () == 1) + { + copy = pw->pw_dir; + } + + // Convert: ~/x --> /home/user/x + else if (copy.length () > tilde + 1 && + copy[tilde + 1] == '/') + { + copy.replace (tilde, 1, pw->pw_dir); + } + + // Convert: ~foo/x --> /home/foo/x + else if ((slash = copy.find ("/", tilde)) != std::string::npos) + { + std::string name = copy.substr (tilde + 1, slash - tilde - 1); + struct passwd* pw = getpwnam (name.c_str ()); + if (pw) + copy.replace (tilde, slash - tilde, pw->pw_dir); + } + } + + return copy; +} + +//////////////////////////////////////////////////////////////////////////////// +std::vector Path::glob (const std::string& pattern) +{ + std::vector results; + + glob_t g; + if (!::glob (pattern.c_str (), GLOB_ERR | GLOB_BRACE | GLOB_TILDE, NULL, &g)) + for (int i = 0; i < g.gl_matchc; ++i) + results.push_back (g.gl_pathv[i]); + + globfree (&g); + return results; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Path.h b/src/Path.h new file mode 100644 index 000000000..9dc2ad440 --- /dev/null +++ b/src/Path.h @@ -0,0 +1,60 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_PATH +#define INCLUDED_PATH + +#include +#include + +class Path +{ +public: + Path (); + Path (const Path&); + Path (const std::string&); + virtual ~Path (); + + Path& operator= (const Path&); + std::string name () const; + std::string parent () const; + std::string extension () const; + bool exists () const; + bool is_directory () const; + bool readable () const; + bool writable () const; + bool executable () const; + + // Statics + static std::string expand (const std::string&); + static std::vector glob (const std::string&); + +public: + std::string data; +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/.gitignore b/src/tests/.gitignore index 75137c796..3f6ca42f3 100644 --- a/src/tests/.gitignore +++ b/src/tests/.gitignore @@ -17,4 +17,7 @@ config.t util.t color.t list.t +path.t +file.t +directory.t *.log diff --git a/src/tests/Makefile b/src/tests/Makefile index b6fc01d2b..2461b7113 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -1,6 +1,6 @@ PROJECT = t.t tdb.t date.t duration.t t.benchmark.t text.t autocomplete.t \ config.t seq.t att.t stringtable.t record.t nibbler.t subst.t filt.t \ - cmd.t util.t color.t list.t + cmd.t util.t color.t list.t path.t file.t directory.t CFLAGS = -I. -I.. -Wall -pedantic -ggdb3 -fno-rtti LFLAGS = -L/usr/local/lib -lncurses OBJECTS = ../TDB.o ../Task.o ../text.o ../Date.o ../Table.o ../Duration.o \ @@ -8,7 +8,8 @@ OBJECTS = ../TDB.o ../Task.o ../text.o ../Date.o ../Table.o ../Duration.o \ ../StringTable.o ../Subst.o ../Nibbler.o ../Location.o ../Filter.o \ ../Context.o ../Keymap.o ../command.o ../interactive.o ../report.o \ ../Grid.o ../Color.o ../rules.o ../recur.o ../custom.o ../import.o \ - ../edit.o ../Timer.o ../Permission.o + ../edit.o ../Timer.o ../Permission.o ../Path.o ../File.o \ + ../Directory.o all: $(PROJECT) @@ -81,3 +82,12 @@ color.t: color.t.o $(OBJECTS) test.o list.t: list.t.o $(OBJECTS) test.o g++ list.t.o $(OBJECTS) test.o $(LFLAGS) -o list.t +path.t: path.t.o $(OBJECTS) test.o + g++ path.t.o $(OBJECTS) test.o $(LFLAGS) -o path.t + +file.t: file.t.o $(OBJECTS) test.o + g++ file.t.o $(OBJECTS) test.o $(LFLAGS) -o file.t + +directory.t: directory.t.o $(OBJECTS) test.o + g++ directory.t.o $(OBJECTS) test.o $(LFLAGS) -o directory.t + diff --git a/src/tests/directory.t.cpp b/src/tests/directory.t.cpp new file mode 100644 index 000000000..26418b969 --- /dev/null +++ b/src/tests/directory.t.cpp @@ -0,0 +1,93 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +Context context; + +int main (int argc, char** argv) +{ + UnitTest t (20); + + // Directory (const File&); + // Directory (const Path&); + Directory d0 (Path ("/tmp")); + Directory d1 (File ("/tmp")); + Directory d2 (File (Path ("/tmp"))); + t.is (d0.data, d1.data, "Directory(std::string) == Directory (File&)"); + t.is (d0.data, d2.data, "Directory(std::string) == Directory (File (Path &))"); + t.is (d1.data, d2.data, "Directory(File&)) == Directory (File (Path &))"); + + // Directory (const Directory&); + Directory d3 (d2); + t.is (d3.data, "/tmp", "Directory (Directory&)"); + + // Directory (const std::string&); + Directory d4 ("/tmp/test_directory"); + + // Directory& operator= (const Directory&); + Directory d5 = d4; + t.is (d5.data, "/tmp/test_directory", "Directory::operator="); + + // virtual bool create (); + t.ok (d5.create (), "Directory::create /tmp/test_directory"); + t.ok (d5.exists (), "Directory::exists /tmp/test_directory"); + + Directory d6 (d5.data + "/dir"); + t.ok (d6.create (), "Directory::create /tmp/test_directory/dir"); + + File::create (d5.data + "/f0"); + File::create (d6.data + "/f1"); + + // std::vector list (); + std::vector files = d5.list (); + t.is ((int)files.size (), 2, "Directory::list 1 file"); + t.is (files[0], "/tmp/test_directory/dir", "file[0] is /tmp/test_directory/dir"); + t.is (files[1], "/tmp/test_directory/f0", "file[1] is /tmp/test_directory/f0"); + + // std::vector listRecursive (); + files = d5.listRecursive (); + t.is ((int)files.size (), 2, "Directory::list 1 file"); + t.is (files[0], "/tmp/test_directory/dir/f1", "file is /tmp/test_directory/dir/f1"); + t.is (files[1], "/tmp/test_directory/f0", "file is /tmp/test_directory/f0"); + + // virtual bool remove (); + t.ok (File::remove (d5.data + "/f0"), "File::remove /tmp/test_directory/f0"); + t.ok (File::remove (d6.data + "/f1"), "File::remove /tmp/test_directory/dir/f1"); + + t.ok (d6.remove (), "Directory::remove /tmp/test_directory/dir"); + t.notok (d6.exists (), "Directory::exists /tmp/test_directory/dir - no"); + + t.ok (d5.remove (), "Directory::remove /tmp/test_directory"); + t.notok (d5.exists (), "Directory::exists /tmp/test_directory - no"); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/file.t.cpp b/src/tests/file.t.cpp new file mode 100644 index 000000000..905aa265e --- /dev/null +++ b/src/tests/file.t.cpp @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +Context context; + +int main (int argc, char** argv) +{ + UnitTest t (5); + + File::write ("/tmp/file.t.txt", "This is a test\n"); + File f6 ("/tmp/file.t.txt"); + t.ok (f6.size () == 15, "File::size /tmp/file.t.txt good"); + 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"); + + 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"); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/path.t.cpp b/src/tests/path.t.cpp new file mode 100644 index 000000000..4fc679a1e --- /dev/null +++ b/src/tests/path.t.cpp @@ -0,0 +1,107 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// 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 2 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, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +Context context; + +int main (int argc, char** argv) +{ + UnitTest t (26); + + // Path (); + Path p0; + t.ok (p0.data == "", "Path::Path"); + + // Path (const Path&); + Path p1 = Path ("foo"); + t.ok (p1.data == "foo", "Path::operator="); + + // Path (const std::string&); + Path p2 ("~"); + t.ok (p2.data != "~", "~ expanded to " + p2.data); + + Path p3 ("/tmp"); + t.ok (p3.data == "/tmp", "/tmp -> /tmp"); + + // Path& operator= (const Path&); + Path p3_copy (p3); + t.is (p3.data, p3_copy.data, "Path::Path (Path&)"); + + // 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"); + + // std::string parent () const; + t.is (p4.parent (), "/a/b/c", "/a/b/c/file.ext parent is /a/b/c"); + + // std::string extension () const; + t.is (p4.extension (), "ext", "/a/b/c/file.ext extension is ext"); + + // bool exists () const; + t.ok (p2.exists (), "~ exists"); + t.ok (p3.exists (), "/tmp exists"); + + // bool is_directory () const; + t.ok (p2.is_directory (), "~ is_directory"); + t.ok (p3.is_directory (), "/tmp is_directory"); + + // bool readable () const; + t.ok (p2.readable (), "~ readable"); + t.ok (p3.readable (), "/tmp readable"); + + // bool writable () const; + t.ok (p2.writable (), "~ writable"); + t.ok (p3.writable (), "/tmp writable"); + + // bool executable () const; + t.ok (p2.executable (), "~ executable"); + t.ok (p3.executable (), "/tmp executable"); + + // static std::string expand (const std::string&); + t.ok (Path::expand ("~") != "~", "Path::expand ~ != ~"); + t.ok (Path::expand ("~/") != "~/", "Path::expand ~/ != ~/"); + + // static std::vector glob (const std::string&); + std::vector out = Path::glob ("/tmp"); + t.ok (out.size () == 1, "/tmp -> 1 result"); + t.is (out[0], "/tmp", "/tmp -> /tmp"); + + out = Path::glob ("/t?p"); + t.ok (out.size () == 1, "/t?p -> 1 result"); + t.is (out[0], "/tmp", "/t?p -> /tmp"); + + out = Path::glob ("/[s-u]mp"); + t.ok (out.size () == 1, "/[s-u]mp -> 1 result"); + t.is (out[0], "/tmp", "/[s-u]mp -> /tmp"); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/valid.t.cpp b/src/tests/valid.t.cpp deleted file mode 100644 index d88e127a5..000000000 --- a/src/tests/valid.t.cpp +++ /dev/null @@ -1,90 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// Copyright 2008, Paul Beckingham. All rights reserved. -// -// -//////////////////////////////////////////////////////////////////////////////// -#include -#include - -//////////////////////////////////////////////////////////////////////////////// -int main (int argc, char** argv) -{ - UnitTest t (66); - - t.notok (validUnsignedInteger (""), "!validUnsignedInteger ''"); - t.notok (validUnsignedInteger ("F"), "!validUnsignedInteger 'F'"); - t.notok (validUnsignedInteger (" "), "!validUnsignedInteger ' '"); - t.ok (validUnsignedInteger ("0"), "validUnsignedInteger '0'"); - t.notok (validUnsignedInteger ("-1"), "!validUnsignedInteger '-1'"); - t.ok (validUnsignedInteger ("+1"), "validUnsignedInteger '+1'"); - t.ok (validUnsignedInteger ("123"), "validUnsignedInteger '123'"); - - t.notok (validInteger (""), "!validInteger ''"); - t.notok (validInteger ("F"), "!validInteger 'F'"); - t.notok (validInteger (" "), "!validInteger ' '"); - t.ok (validInteger ("0"), "validInteger '0'"); - t.ok (validInteger ("-1"), "validInteger '-1'"); - t.ok (validInteger ("+1"), "validInteger '+1'"); - t.ok (validInteger ("123"), "validInteger '123'"); - - t.notok (validReal (""), "!validReal ''"); - t.notok (validReal ("F"), "!validReal 'F'"); - t.notok (validReal (" "), "!validReal ' '"); - t.ok (validReal ("0"), "validReal '0'"); - t.ok (validReal ("1"), "validReal '1'"); - t.ok (validReal ("-1"), "validReal '-1'"); - t.ok (validReal ("1.23"), "validReal '1.23'"); - t.ok (validReal ("-1.23"), "validReal '-1.23'"); - - // 0/1, T/F, t/f, true/false, True/False, TRUE/FALSE, -/+, yes/no - t.notok (validBoolean (""), "!validBoolean ''"); - t.notok (validBoolean (" "), "!validBoolean ' '"); - t.notok (validBoolean ("x"), "!validBoolean 'x'"); - t.notok (validBoolean ("2"), "!validBoolean '1'"); - t.ok (validBoolean ("0"), "validBoolean '0'"); - t.ok (validBoolean ("1"), "validBoolean '1'"); - t.ok (validBoolean ("T"), "validBoolean 'T'"); - t.ok (validBoolean ("F"), "validBoolean 'F'"); - t.ok (validBoolean ("t"), "validBoolean 't'"); - t.ok (validBoolean ("f"), "validBoolean 'f'"); - t.ok (validBoolean ("true"), "validBoolean 'true'"); - t.ok (validBoolean ("false"), "validBoolean 'false'"); - t.ok (validBoolean ("True"), "validBoolean 'True'"); - t.ok (validBoolean ("False"), "validBoolean 'False'"); - t.ok (validBoolean ("TRUE"), "validBoolean 'TRUE'"); - t.ok (validBoolean ("FALSE"), "validBoolean 'FALSE'"); - t.ok (validBoolean ("-"), "validBoolean '-'"); - t.ok (validBoolean ("+"), "validBoolean '+'"); - t.ok (validBoolean ("YES"), "validBoolean 'YES'"); - t.ok (validBoolean ("NO"), "validBoolean 'NO'"); - t.ok (validBoolean ("Yes"), "validBoolean 'Yes'"); - t.ok (validBoolean ("No"), "validBoolean 'No'"); - t.ok (validBoolean ("yes"), "validBoolean 'yes'"); - t.ok (validBoolean ("no"), "validBoolean 'no'"); - t.ok (validBoolean ("Y"), "validBoolean 'Y'"); - t.ok (validBoolean ("N"), "validBoolean 'N'"); - t.ok (validBoolean ("y"), "validBoolean 'y'"); - t.ok (validBoolean ("n"), "validBoolean 'n'"); - t.ok (validBoolean ("on"), "validBoolean 'on'"); - t.ok (validBoolean ("off"), "validBoolean 'off'"); - t.ok (validBoolean ("On"), "validBoolean 'On'"); - t.ok (validBoolean ("Off"), "validBoolean 'Off'"); - t.ok (validBoolean ("ON"), "validBoolean 'ON'"); - t.ok (validBoolean ("OFF"), "validBoolean 'OFF'"); - - t.ok (validColor ("black"), "validColor black"); - t.ok (validColor ("red"), "validColor red"); - t.ok (validColor ("green"), "validColor green"); - t.ok (validColor ("yellow"), "validColor yellow"); - t.ok (validColor ("blue"), "validColor blue"); - t.ok (validColor ("magenta"), "validColor magenta"); - t.ok (validColor ("cyan"), "validColor cyan"); - t.ok (validColor ("white"), "validColor white"); - t.ok (validColor (""), "validColor nocolor"); - t.notok (validColor ("donkey"), "fail <- validColor donkey"); - - return 0; -} - -//////////////////////////////////////////////////////////////////////////////// -