Skip to content
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
|-|-|
| [![PyPI version](https://badge.fury.io/py/retranslator.svg)](https://badge.fury.io/py/retranslator) | __Python__ |
| [![nimble](https://raw.githubusercontent.com/yglukhov/nimble-tag/master/nimble.png)](https://nimble.directory/pkg/retranslator) | __Nim__ |
| [![CMake](https://img.shields.io/badge/CMake-3.16+-blue.svg)](https://cmake.org/) | __C++__ |

# [RegularExpressions.Transformer](https://github.com/linksplatform/RegularExpressions.Transformer)

Expand All @@ -25,6 +26,8 @@ NuGet package: [Platform.RegularExpressions.Transformer](https://www.nuget.org/p

Python version: [retranslator](https://github.com/linksplatform/RegularExpressions.Transformer/tree/master/python)

C++ version: [cpp](https://github.com/linksplatform/RegularExpressions.Transformer/tree/master/cpp)

## [Documentation](https://linksplatform.github.io/RegularExpressions.Transformer)
[PDF file](https://linksplatform.github.io/RegularExpressions.Transformer/csharp/Platform.RegularExpressions.Transformer.pdf) with code for e-readers.

Expand Down
99 changes: 99 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
cmake_minimum_required(VERSION 3.16)

project(RegularExpressionsTransformer
VERSION 1.0.0
DESCRIPTION "Platform Regular Expressions Transformer C++ Library"
LANGUAGES CXX)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Add compiler-specific options
if(MSVC)
add_compile_options(/W4 /permissive-)
else()
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Define the library target
add_library(RegularExpressionsTransformer
src/TextSteppedTransformer.cpp
src/FileTransformer.cpp
)

# Set target properties
set_target_properties(RegularExpressionsTransformer PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER "include/ISubstitutionRule.h;include/SubstitutionRule.h;include/ITransformer.h;include/ITextTransformer.h;include/TextSteppedTransformer.h;include/TextTransformer.h;include/IFileTransformer.h;include/FileTransformer.h"
)

# Include directories
target_include_directories(RegularExpressionsTransformer
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Find and link required libraries
find_package(Threads REQUIRED)
target_link_libraries(RegularExpressionsTransformer
PUBLIC
Threads::Threads
)

# Enable testing
enable_testing()

# Add tests subdirectory if it exists
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tests)
add_subdirectory(tests)
endif()

# Add examples subdirectory if it exists
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/examples)
add_subdirectory(examples)
endif()

# Install targets
include(GNUInstallDirs)

install(TARGETS RegularExpressionsTransformer
EXPORT RegularExpressionsTransformerTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/Platform/RegularExpressions/Transformer
)

# Install the export set
install(EXPORT RegularExpressionsTransformerTargets
FILE RegularExpressionsTransformerTargets.cmake
NAMESPACE Platform::RegularExpressions::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RegularExpressionsTransformer
)

# Create config files
include(CMakePackageConfigHelpers)

configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/RegularExpressionsTransformerConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/RegularExpressionsTransformerConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RegularExpressionsTransformer
)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/RegularExpressionsTransformerConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/RegularExpressionsTransformerConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/RegularExpressionsTransformerConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RegularExpressionsTransformer
)
139 changes: 139 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# RegularExpressions.Transformer C++

C++ implementation of Platform.RegularExpressions.Transformer library.

## Features

- **Text Transformation**: Apply sequences of regular expression substitutions to text
- **File Transformation**: Transform files and directories using regex rules
- **Rule-based Processing**: Configure multiple substitution rules with repeat counts
- **Cross-platform**: Uses standard C++17 and CMake for portability

## Requirements

- C++17 compatible compiler
- CMake 3.16 or later
- Google Test (automatically downloaded if not found)

## Building

```bash
mkdir build
cd build
cmake ..
make
```

## Running Tests

```bash
# From build directory
ctest
# or
./tests/RegularExpressionsTransformerTests
```

## Running Examples

```bash
# From build directory
./examples/BasicExample
./examples/FileExample
```

## Usage

### Basic Text Transformation

```cpp
#include "TextTransformer.h"
#include "SubstitutionRule.h"

using namespace Platform::RegularExpressions::Transformer;

// Create substitution rules
auto rule1 = std::make_shared<SubstitutionRule>("hello", "hi");
auto rule2 = std::make_shared<SubstitutionRule>("world", "universe");

std::vector<std::shared_ptr<ISubstitutionRule>> rules = {rule1, rule2};

// Create transformer
TextTransformer transformer(rules);

// Transform text
std::string result = transformer.transform("hello world");
// Result: "hi universe"
```

### File Transformation

```cpp
#include "FileTransformer.h"
#include "TextTransformer.h"
#include "SubstitutionRule.h"

using namespace Platform::RegularExpressions::Transformer;

// Create text transformer with rules
auto rule = std::make_shared<SubstitutionRule>(R"(\d+)", "[NUMBER]");
std::vector<std::shared_ptr<ISubstitutionRule>> rules = {rule};
auto textTransformer = std::make_shared<TextTransformer>(rules);

// Create file transformer
FileTransformer fileTransformer(textTransformer, ".txt", ".transformed");

// Transform file or directory
fileTransformer.transform("input.txt", "output.transformed");
fileTransformer.transform("source_folder/", "target_folder/");
```

### Advanced Rules

```cpp
// Rule with maximum repeat count
auto rule = std::make_shared<SubstitutionRule>("a+", "X", 3);

// Rule with custom regex options
auto complexRule = std::make_shared<SubstitutionRule>(
R"((?i)hello)", // Case-insensitive pattern
"Hi",
0, // No repeat limit
std::regex_constants::ECMAScript | std::regex_constants::icase
);

// Rule with path pattern for file filtering
rule->setPathPattern(R"(.*\.cpp$)"); // Only process .cpp files
```

## Architecture

- **ISubstitutionRule**: Interface for substitution rules
- **SubstitutionRule**: Implementation of substitution rules with regex patterns
- **ITransformer**: Base interface for transformers
- **ITextTransformer**: Interface for text transformation
- **TextTransformer**: Main text transformation implementation
- **TextSteppedTransformer**: Step-by-step text transformation for debugging
- **IFileTransformer**: Interface for file transformation
- **FileTransformer**: File and directory transformation implementation

## Installation

### Using CMake

```cmake
find_package(RegularExpressionsTransformer REQUIRED)
target_link_libraries(your_target Platform::RegularExpressions::RegularExpressionsTransformer)
```

### Manual Installation

```bash
mkdir build && cd build
cmake ..
make
sudo make install
```

## License

This project is licensed under the MIT License - see the LICENSE file for details.
8 changes: 8 additions & 0 deletions cpp/cmake/RegularExpressionsTransformerConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(Threads)

include("${CMAKE_CURRENT_LIST_DIR}/RegularExpressionsTransformerTargets.cmake")

check_required_components(RegularExpressionsTransformer)
19 changes: 19 additions & 0 deletions cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.16)

# Basic example
add_executable(BasicExample
basic_example.cpp
)

target_link_libraries(BasicExample
RegularExpressionsTransformer
)

# File transformation example
add_executable(FileExample
file_example.cpp
)

target_link_libraries(FileExample
RegularExpressionsTransformer
)
39 changes: 39 additions & 0 deletions cpp/examples/basic_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../include/TextTransformer.h"
#include "../include/SubstitutionRule.h"
#include <iostream>
#include <memory>

using namespace Platform::RegularExpressions::Transformer;

int main() {
try {
// Create substitution rules
auto rule1 = std::make_shared<SubstitutionRule>("hello", "hi");
auto rule2 = std::make_shared<SubstitutionRule>("world", "universe");
auto rule3 = std::make_shared<SubstitutionRule>(R"(\d+)", "NUMBER");

// Create a vector of rules
std::vector<std::shared_ptr<ISubstitutionRule>> rules = {rule1, rule2, rule3};

// Create the transformer
TextTransformer transformer(rules);

// Test text
std::string sourceText = "hello world! I have 123 apples and 456 oranges.";

std::cout << "Source text: " << sourceText << std::endl;

// Transform the text
std::string result = transformer.transform(sourceText);

std::cout << "Transformed text: " << result << std::endl;

// Expected output: "hi universe! I have NUMBER apples and NUMBER oranges."

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}

return 0;
}
67 changes: 67 additions & 0 deletions cpp/examples/file_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "../include/FileTransformer.h"
#include "../include/TextTransformer.h"
#include "../include/SubstitutionRule.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <filesystem>

using namespace Platform::RegularExpressions::Transformer;

int main() {
try {
// Create a temporary directory for testing
std::string tempDir = "temp_example";
std::filesystem::create_directories(tempDir);

// Create a source file
std::string sourceFile = tempDir + "/source.txt";
std::ofstream file(sourceFile);
file << "Hello World!\n";
file << "This is a test file with some numbers: 123, 456, 789.\n";
file << "We will transform this content.\n";
file.close();

// Create substitution rules
auto rule1 = std::make_shared<SubstitutionRule>("Hello", "Hi");
auto rule2 = std::make_shared<SubstitutionRule>("World", "Universe");
auto rule3 = std::make_shared<SubstitutionRule>(R"(\b\d+\b)", "[NUMBER]");

std::vector<std::shared_ptr<ISubstitutionRule>> rules = {rule1, rule2, rule3};

// Create transformers
auto textTransformer = std::make_shared<TextTransformer>(rules);
FileTransformer fileTransformer(textTransformer, ".txt", ".transformed");

// Transform the file
std::string targetFile = tempDir + "/source.transformed";
fileTransformer.transform(sourceFile, targetFile);

// Read and display the results
std::cout << "Original file content:" << std::endl;
std::ifstream original(sourceFile);
std::string line;
while (std::getline(original, line)) {
std::cout << " " << line << std::endl;
}
original.close();

std::cout << std::endl << "Transformed file content:" << std::endl;
std::ifstream transformed(targetFile);
while (std::getline(transformed, line)) {
std::cout << " " << line << std::endl;
}
transformed.close();

// Clean up
std::filesystem::remove_all(tempDir);

std::cout << std::endl << "Example completed successfully!" << std::endl;

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}

return 0;
}
Loading
Loading