Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue67 #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# Output and source files.
bin_PROGRAMS = tocc tocc-initialize

tocc_SOURCES = common/exceptions/base_exception.cpp common/exceptions/cmd_usage_exceptions.cpp utilities/errno_translator.cpp utilities/cmd_parser.cpp utilities/string_utils.cpp selectors/selector.cpp selectors/id_selector.cpp selectors/import_file_selector.cpp selectors/query_selector.cpp actions/action.cpp actions/print_action.cpp actions/assign_action.cpp actions/unassign_action.cpp actions/tags_statistics_action.cpp actions/remove_action.cpp actions/set_title_action.cpp engine/cmd_manager.cpp main.cpp
tocc_SOURCES = common/exceptions/base_exception.cpp common/exceptions/cmd_usage_exceptions.cpp utilities/errno_translator.cpp utilities/cmd_parser.cpp utilities/string_utils.cpp utilities/file_system.cpp selectors/selector.cpp selectors/id_selector.cpp selectors/import_file_selector.cpp selectors/query_selector.cpp actions/action.cpp actions/print_action.cpp actions/assign_action.cpp actions/unassign_action.cpp actions/tags_statistics_action.cpp actions/remove_action.cpp actions/set_title_action.cpp engine/singletons.cpp engine/cmd_manager.cpp engine/wild_card_manager.cpp main.cpp wild_cards/wild_card.cpp wild_cards/asterisk_wild_card.cpp

tocc_initialize_SOURCES = utilities/errno_translator.cpp initializer.cpp
44 changes: 44 additions & 0 deletions cli/src/engine/singletons.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of Tocc. (see <http://t-o-c-c.com>)
* Copyright (C) 2013, 2014, Aidin Gharibnavaz <aidin@t-o-c-c.com>
*
* Tocc 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.
*
* Tocc 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 Tocc. If not, see <http://www.gnu.org/licenses/>.
*/

#include "engine/singletons.h"
#include "engine/wild_card_manager.h"

namespace tocccli
{

WildCardManager* Singletons::wildCardMgr = 0;

Singletons::Singletons()
{
wildCardMgr = new WildCardManager();
}

Singletons::~Singletons()
{
if(wildCardMgr != 0)
{
delete wildCardMgr;
}
}

WildCardManager* Singletons::getWildCardManager()
{
return wildCardMgr;
}
}
40 changes: 40 additions & 0 deletions cli/src/engine/singletons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of Tocc. (see <http://t-o-c-c.com>)
* Copyright (C) 2013, 2014, Aidin Gharibnavaz <aidin@t-o-c-c.com>
*
* Tocc 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.
*
* Tocc 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 Tocc. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef TOCCCLI_SINGLETONS_H_INCLUDED
#define TOCCCLI_SINGLETONS_H_INCLUDED

#define WILDCARD_MANAGER Singletons::getWildCardManager()

namespace tocccli
{
class WildCardManager;
class Singletons
{
private:
static WildCardManager* wildCardMgr;
public:
Singletons();
virtual ~Singletons();

static WildCardManager* getWildCardManager();
};
}

#endif
63 changes: 63 additions & 0 deletions cli/src/engine/wild_card_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of Tocc. (see <http://t-o-c-c.com>)
* Copyright (C) 2013, 2014, Aidin Gharibnavaz <aidin@t-o-c-c.com>
*
* Tocc 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.
*
* Tocc 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 Tocc. If not, see <http://www.gnu.org/licenses/>.
*/


#include "engine/wild_card_manager.h"
#include "wild_cards/asterisk_wild_card.h"
#include "utilities/file_system.h"

namespace tocccli
{
WildCardManager::WildCardManager()
{
wild_cards.push_back(new AsteriskWildCard());
}

std::vector<std::string> WildCardManager::process_wild_cards(std::string expression)
{
std::vector<std::string> files;
std::string dir = get_dir(expression);
std::string file_expression = get_file(expression);

for(int i = 0; i < wild_cards.size(); i++)
{
if(file_expression.find(wild_cards[i]->get_expr()) >= 0)
{
files = wild_cards[i]->match(dir, file_expression);
}
}

return files;
}

bool WildCardManager::detect_wild_cards(std::string expression)
{
std::vector<std::string> files;
std::string dir = get_dir(expression);
std::string file_expression = get_file(expression);
for(int i = 0; i < wild_cards.size(); i++)
{
if(file_expression.find(wild_cards[i]->get_expr()) != std::string::npos)
{
return true;
}
}

return false;
}
}
42 changes: 42 additions & 0 deletions cli/src/engine/wild_card_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of Tocc. (see <http://t-o-c-c.com>)
* Copyright (C) 2013, 2014, Aidin Gharibnavaz <aidin@t-o-c-c.com>
*
* Tocc 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.
*
* Tocc 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 Tocc. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef TOCCCLI_WILD_CARD_MANAGER_H_INCLUDED
#define TOCCCLI_WILD_CARD_MANAGER_H_INCLUDED

#include "wild_cards/wild_card.h"
#include <vector>
#include <string>

namespace tocccli
{
class WildCardManager
{
friend Singletons;
public:
std::vector<std::string> process_wild_cards(std::string expression);
bool detect_wild_cards(std::string expression);

private:
std::vector<WildCard*> wild_cards;
WildCardManager();
};
}

#endif
2 changes: 2 additions & 0 deletions cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "utilities/cmd_parser.h"
#include "utilities/errno_translator.h"
#include "engine/cmd_manager.h"
#include "engine/singletons.h"
#include "common/exceptions/cmd_usage_exceptions.h"


Expand Down Expand Up @@ -83,6 +84,7 @@ int main(int argc, char* argv[])

// Executing.
CmdManager cmd_manager(base_path);
Singletons singletons;
try
{
cmd_manager.execute(cmd_parameters);
Expand Down
25 changes: 22 additions & 3 deletions cli/src/selectors/import_file_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "selectors/import_file_selector.h"
#include "common/exceptions/cmd_usage_exceptions.h"

#include "engine/wild_card_manager.h"
#include <cassert>
#include <cstdio>

namespace tocccli
{
Expand Down Expand Up @@ -55,10 +57,27 @@ namespace tocccli
throw InvalidParametersError("-i and --import must have an argument.");
}

libtocc::FileInfo new_file = this->libtocc_manager->import_file(cmd_arguments.front().c_str());
assert(WILDCARD_MANAGER != 0);

std::vector<libtocc::FileInfo> result;
result.push_back(new_file);

if(WILDCARD_MANAGER->detect_wild_cards(cmd_arguments.front()))
{
std::vector<std::string> files = WILDCARD_MANAGER->process_wild_cards(cmd_arguments.front());
printf("here\n");
for(int i = 0; i < files.size(); i++)
{
libtocc::FileInfo new_file = this->libtocc_manager->import_file(files[i].c_str());
printf("%s \n", files[i].c_str());
result.push_back(new_file);
}
}
else
{
libtocc::FileInfo new_file = this->libtocc_manager->import_file(cmd_arguments.front().c_str());
result.push_back(new_file);
}

return result;
}

Expand Down
108 changes: 108 additions & 0 deletions cli/src/utilities/file_system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* This file is part of Tocc. (see <http://t-o-c-c.com>)
* Copyright (C) 2013, 2014, Aidin Gharibnavaz <aidin@t-o-c-c.com>
*
* Tocc 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.
*
* Tocc 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 Tocc. If not, see <http://www.gnu.org/licenses/>.
*/


#include "utilities/file_system.h"
#include <sys/stat.h>
#include <dirent.h>
#include <cstdio>
#include <cstring>


namespace tocccli
{
bool is_directory(std::string dir_path)
{
struct stat dir_info;
if(stat(dir_path.c_str(), &dir_info) == 0 && S_ISDIR(dir_info.st_mode))
{
return true;
}
return false;
}

bool is_file(std::string file_path)
{
struct stat file_info;
if(stat(file_path.c_str(), &file_info) == 0 && S_ISREG(file_info.st_mode))
{
return true;
}
return false;
}

bool is_valid_path(std::string path)
{
struct stat path_info;
if(stat(path.c_str(), &path_info) == 0)
{
return true;
}

return false;
}

std::vector<std::string> get_all_files(std::string dir_path)
{
std::vector<std::string> files;
if(is_valid_path(dir_path))
{
DIR* opened_directory = opendir(dir_path.c_str());
if( opened_directory != 0)
{
struct dirent* file = 0;
while( (file = readdir(opened_directory)) != 0)
{
if( file->d_name[0] != '.' && file->d_name[strlen(file->d_name) - 1 ] != '~')
{
std::string file_full_path = dir_path + "/";
file_full_path += file->d_name;
if(is_file(file_full_path))
{
files.push_back(file_full_path);
}
}
}
closedir(opened_directory);
}
}
return files;
}

std::string get_dir(std::string path)
{
int last_slash_occurence = path.find_last_of("/");
if(last_slash_occurence >= 0)
{
return path.substr(0, last_slash_occurence);
}

return path;
}

std::string get_file(std::string file_path)
{
int last_slash_occurence = file_path.find_last_of("/");
if(last_slash_occurence >= 0)
{
return file_path.substr(last_slash_occurence + 1);
}

return file_path;
}
}
Loading