Commit a8704d8b authored by Igor Danielewicz's avatar Igor Danielewicz

Added format function and nullptr check to worker::process

parent 432c6850
......@@ -29,7 +29,7 @@ set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
find_package(PwrUtils REQUIRED)
set(LIBS ${LIBS} pwrutils)
add_executable(ediText ediText.cpp ConfigParser.cpp functions/Replace.cpp)
add_executable(ediText ediText.cpp ConfigParser.cpp functions/Replace.cpp functions/Format.cpp)
target_link_libraries(ediText ${LIBS})
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)
......@@ -36,6 +36,8 @@ Function* ConfigParser::str2function(std::string name, ptree& config)
{
if(name == "replace")
return new Replace(config);
if(name == "format")
return new Format(config);
else
return nullptr;
}
......
#pragma once
#include "functions/Function.hpp"
#include "functions/Replace.hpp"
#include "functions/Format.hpp"
#include "textIterators/TextIterator.hpp"
#include "textIterators/LineIterator.hpp"
#include "textIterators/TokenIterator.hpp"
......
## ediText
# ediText
Highly configurable tool for processing text. Input is processed by chain of functions defined by the user as an argument in JSON format.
# Usage
## Usage
Exeplary JSON:
{
"0":
......
......@@ -46,8 +46,18 @@ void ediText_Worker::process(string task_path, ptree &config, string output_path
do
{
auto fun = conf.getFunction();
auto text = conf.getTextIterator(buffer1);
if(text == nullptr)
{
LOG(WARNING) << "Iterator does not exists";
continue;
}
auto fun = conf.getFunction();
if(fun == nullptr)
{
LOG(WARNING) << "Function does not exists";
continue;
}
while(text->has_more())
{
buffer2 << fun->process(text->get());
......
#include "functions/Format.hpp"
#include <iostream>
using ptree = boost::property_tree::ptree;
std::string remove_line_breaks(std::string str)
{
static std::regex reg = std::regex("[-–]\n");
std::string to("");
return std::regex_replace(str, reg, to);
}
std::string remove_page_numbers(std::string str)// TODO
{
// static std::regex reg = std::regex("[0-9]");
// std::string to("[|]");
// return std::regex_replace(str, reg, to);
return str;
}
Format::Format(ptree& config)// TODO
{
tasks.emplace_back(remove_line_breaks);
// if(config.get<std::string>("function") == "all")
// {
// tasks.emplace_back(remove_line_breaks);
// tasks.emplace_back(remove_page_numbers);
// }
// else
// {
// ;// config.get_child("functions");
// }
// std::string from = config.get<std::string>("from");
// auto flags = std::regex_constants::optimize;
// if(!config.get<bool>("case_sensitive", true))
// flags = flags | std::regex_constants::icase;
// reg = std::regex(from, flags);
}
std::string Format::process(std::string str)
{
for(auto& task : tasks)
{
str = task(str);
}
return str;
}
#pragma once
#include <boost/property_tree/ptree.hpp>
#include <queue>
#include <functional>
#include <regex>
#include "functions/Function.hpp"
using ptree = boost::property_tree::ptree;
class Format : public Function
{
public:
Format(ptree& config);
std::string process(std::string str);
private:
std::deque<std::function<std::string(std::string)>> tasks;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment