Add initial code

This commit is contained in:
sharkdp 2017-05-09 23:28:16 +02:00
parent 21459731ee
commit aab0a2dcfb
3 changed files with 82 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
CMakeCache.txt
CMakeFiles
Makefile
cmake_install.cmake
build
fnd

14
CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.1.0)
project(fnd CXX)
find_package(Boost 1.56 REQUIRED COMPONENTS filesystem)
include_directories( ${Boost_INCLUDE_DIR} )
file(GLOB SOURCES "src/*.cpp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
add_executable(fnd ${SOURCES})
target_link_libraries(fnd ${Boost_LIBRARIES})

62
src/fnd.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <iostream>
#include <regex>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
static const std::string ANSI_PURPLE = "\x1b[35;06m";
static const std::string ANSI_CYAN = "\x1b[36;01m";
static const std::string ANSI_RESET = "\x1b[0m";
void printPath(const fs::path& path) {
if (fs::is_symlink(path)) {
std::cout << ANSI_PURPLE;
} else if (fs::is_directory(path)) {
std::cout << ANSI_CYAN;
}
std::cout << path.string();
std::cout << ANSI_RESET << std::endl;
}
void findFiles(const std::regex& pattern) {
const fs::path& currentPath = fs::current_path();
for (auto& entry: fs::recursive_directory_iterator(currentPath)) {
const fs::path& path = entry.path().lexically_relative(currentPath);
if (std::regex_search(path.string(), pattern)) {
printPath(path);
}
}
}
int main(int argc, char* argv[]) {
std::string argument;
if (argc == 1) {
argument = "";
} else if (argc == 2) {
argument = argv[1];
}
if (argc > 2 || argument == "-h" || argument == "--help") {
std::cerr << "Usage: fnd [PATTERN]" << std::endl;
return 1;
}
// try to parse the argument as a regex
try {
std::regex re(argument);
findFiles(re);
}
catch (const std::regex_error& e) {
std::cerr << "Regex error: " << e.what() << std::endl;
return 1;
}
return 0;
}