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

WIP plugin support #709

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ set(OMEGA_EDIT_SOURCE_FILES
"src/include/omega_edit/utility.h" "src/lib/utility.c"
"src/include/omega_edit/encode.h" "src/lib/encode.c"
"src/include/omega_edit/filesystem.h" "src/lib/filesystem.cpp"
"src/include/omega_edit/plugins/replace.h" "src/lib/plugins/replace.c"
"src/lib/impl_/macros.h" "src/lib/impl_/internal_fwd_defs.hpp"
"src/lib/impl_/internal_fun.hpp" "src/lib/impl_/internal_fun.cpp"
"src/lib/impl_/find.h" "src/lib/impl_/find.cpp"
"src/lib/impl_/data_def.hpp" "src/lib/impl_/model_def.hpp" "src/lib/impl_/model_segment_def.hpp")
"src/lib/impl_/data_def.hpp" "src/lib/impl_/model_def.hpp" "src/lib/impl_/model_segment_def.hpp"
)

# Don't add RPATH so we can manipulate the library search path using the environment at runtime
set(CMAKE_MACOSX_RPATH OFF)
Expand Down Expand Up @@ -124,6 +126,9 @@ endforeach ()
# EXAMPLES
#######################################################################################################################
if (BUILD_EXAMPLES)
add_executable(plugin_replace "src/examples/plugin_replace.c")
target_link_libraries(plugin_replace PRIVATE omega_edit::omega_edit ${FILESYSTEM_LIB})

add_executable(replace "src/examples/replace.cpp")
target_link_libraries(replace PRIVATE omega_edit::omega_edit ${FILESYSTEM_LIB})

Expand Down
52 changes: 52 additions & 0 deletions core/src/examples/plugin_replace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**********************************************************************************************************************
* Copyright (c) 2021 Concurrent Technologies Corporation. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software is distributed under the License is *
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
* implied. See the License for the specific language governing permissions and limitations under the License. *
* *
**********************************************************************************************************************/

#include "omega_edit/plugins/replace.h"
#include "omega_edit/check.h"
#include <omega_edit.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

int main(int argc, char **argv) {
if (argc != 6) {
fprintf(stderr, "USAGE: %s <input file> <output file> <search string> <replace string> <case insensitive>\n",
argv[0]);
return -1;
}

omega_session_t *session_ptr = omega_edit_create_session(argv[1], NULL, NULL, NO_EVENTS, NULL);
if (!session_ptr) {
fprintf(stderr, "ERROR: Failed to open input file %s\n", argv[1]);
return -1;
}

// Create the context
omega_edit_transform_replace_context_t context;
context.search = (omega_byte_t *) argv[3];
context.search_length = (int64_t) strlen(argv[3]);
context.replace = (omega_byte_t *) argv[4];
context.replace_length = (int64_t) strlen(argv[4]);
context.case_insensitive = atoi(argv[5]);
context.replacements = 0;

int rc = omega_check_model(session_ptr);
fprintf(stderr, "%d\n", rc);
assert(0 == rc);
omega_edit_apply_transform(session_ptr, 0, 0, omega_edit_transform_replace, &context);
rc = omega_check_model(session_ptr);
fprintf(stderr, "%d\n", rc);
assert(0 == rc);
return omega_edit_save(session_ptr, argv[2], IO_FLG_OVERWRITE, NULL);
}
2 changes: 1 addition & 1 deletion core/src/examples/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char **argv) {
return -1;
}
omega_session_t *session_ptr = omega_edit_create_session(argv[2], NULL, NULL, NO_EVENTS, NULL);
omega_edit_apply_transform(session_ptr, (argv[1][0] == 'l') ? &to_lower : &to_upper, NULL, 0, 0);
omega_edit_apply_transform_old(session_ptr, (argv[1][0] == 'l') ? &to_lower : &to_upper, NULL, 0, 0);
omega_edit_save(session_ptr, argv[3], IO_FLG_OVERWRITE, NULL);
omega_edit_destroy_session(session_ptr);
return 0;
Expand Down
28 changes: 26 additions & 2 deletions core/src/include/omega_edit/edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ extern "C" {

#endif

/**
* Function signature to transform a stream.
* @param in input stream
* @param start_offset offset in input stream to start reading
* @param length number of bytes to read from input stream
* @param out output stream
* @param context plugin context
* @return 0 on success, non-zero on failure
*/
OMEGA_EDIT_EXPORT typedef int (*omega_edit_transform_func_t)(FILE *in, int64_t start_offset, int64_t length, FILE *out,
void *context);

/**
* Create a file editing session from a file path
* @param file_path file path, will be opened for read, to create an editing session with, or nullptr if starting from
Expand Down Expand Up @@ -193,8 +205,20 @@ OMEGA_EDIT_EXPORT int64_t omega_edit_overwrite(omega_session_t *session_ptr, int
* @param length the number of bytes from the given offset to apply the mask to
* @return zero on success, non-zero otherwise
*/
OMEGA_EDIT_EXPORT int omega_edit_apply_transform(omega_session_t *session_ptr, omega_util_byte_transform_t transform,
void *user_data_ptr, int64_t offset, int64_t length);
OMEGA_EDIT_EXPORT int omega_edit_apply_transform_old(omega_session_t *session_ptr, omega_util_byte_transform_t transform,
void *user_data_ptr, int64_t offset, int64_t length);

/**
* Apply a transform to the bytes starting at the given offset up to the given length
* @param session_ptr session to make the change in
* @param offset offset to make the change
* @param length number of bytes to apply the transform to
* @param transform transform function to apply
* @param transform_context context to pass through to the transform function
* @return zero on success, non-zero otherwise
*/
OMEGA_EDIT_EXPORT int omega_edit_apply_transform(omega_session_t *session_ptr, int64_t offset, int64_t length,
omega_edit_transform_func_t transform, void *transform_context);

/**
* Creates a session checkpoint.
Expand Down
53 changes: 53 additions & 0 deletions core/src/include/omega_edit/plugins/replace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**********************************************************************************************************************
* Copyright (c) 2021 Concurrent Technologies Corporation. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software is distributed under the License is *
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
* implied. See the License for the specific language governing permissions and limitations under the License. *
* *
**********************************************************************************************************************/

#ifndef OMEGA_EDIT_PLUGINS_REPLACE_H
#define OMEGA_EDIT_PLUGINS_REPLACE_H

#include <stdio.h>
#include "omega_edit/export.h"
#include "omega_edit/byte.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* Replace context.
*/
OMEGA_EDIT_EXPORT typedef struct {
omega_byte_t *search;
int64_t search_length;
omega_byte_t *replace;
int64_t replace_length;
int case_insensitive;
int64_t replacements;
} omega_edit_transform_replace_context_t;

/**
* Replaces tokens in a stream.
* @param in input stream
* @param start_offset offset in input stream to start reading
* @param length number of bytes to read from input stream
* @param out output stream
* @param context context
* @return 0 on success, non-zero on failure
*/
OMEGA_EDIT_EXPORT int omega_edit_transform_replace(FILE *in, int64_t start_offset, int64_t length, FILE *out, void *context);

#ifdef __cplusplus
}
#endif

#endif //OMEGA_EDIT_PLUGINS_REPLACE_H
2 changes: 1 addition & 1 deletion core/src/lib/change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* *
**********************************************************************************************************************/

#include "../include/omega_edit/change.h"
#include "omega_edit/change.h"
#include "impl_/change_def.hpp"
#include "impl_/macros.h"
#include <cassert>
Expand Down
7 changes: 4 additions & 3 deletions core/src/lib/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
* *
**********************************************************************************************************************/

#include "../include/omega_edit/check.h"
#include "omega_edit/check.h"
#include "impl_/change_def.hpp"
#include "impl_/internal_fun.hpp"
#include "impl_/macros.h"
#include "impl_/model_def.hpp"
#include "impl_/session_def.hpp"
#include <cassert>

Expand All @@ -36,10 +35,12 @@ int omega_check_model(const omega_session_t *session_ptr) {
expected_offset += segment->computed_length;
}
}
CLOG << "Serial: " << session_ptr->models_.front()->model_segments.front()->change_ptr->serial << "\n";
CLOG << "Kind: " << session_ptr->models_.front()->model_segments.front()->change_ptr->kind << "\n";
if (1 != session_ptr->models_.front()->model_segments.front()->change_ptr->serial ||
0 != (session_ptr->models_.front()->model_segments.front()->change_ptr->kind &
OMEGA_CHANGE_TRANSACTION_BIT)) {
return -1;
return -2;
}
}
return 0;
Expand Down
45 changes: 37 additions & 8 deletions core/src/lib/edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* *
**********************************************************************************************************************/

#include "../include/omega_edit/edit.h"
#include "../include/omega_edit/change.h"
#include "../include/omega_edit/search.h"
#include "../include/omega_edit/segment.h"
#include "../include/omega_edit/session.h"
#include "../include/omega_edit/viewport.h"
#include "omega_edit/edit.h"
#include "omega_edit/change.h"
#include "omega_edit/search.h"
#include "omega_edit/segment.h"
#include "omega_edit/session.h"
#include "omega_edit/viewport.h"
#include "impl_/change_def.hpp"
#include "impl_/internal_fun.hpp"
#include "impl_/macros.h"
Expand Down Expand Up @@ -514,8 +514,8 @@ int64_t omega_edit_overwrite(omega_session_t *session_ptr, int64_t offset, const
return omega_edit_overwrite_bytes(session_ptr, offset, (const omega_byte_t *) cstr, length);
}

int omega_edit_apply_transform(omega_session_t *session_ptr, omega_util_byte_transform_t transform, void *user_data_ptr,
int64_t offset, int64_t length) {
int omega_edit_apply_transform_old(omega_session_t *session_ptr, omega_util_byte_transform_t transform, void *user_data_ptr,
int64_t offset, int64_t length) {
if (!omega_session_changes_paused(session_ptr) && 0 == omega_edit_create_checkpoint(session_ptr)) {
const auto in_file = session_ptr->models_.back()->file_path;
const auto out_file = in_file + "_";
Expand Down Expand Up @@ -543,6 +543,35 @@ int omega_edit_apply_transform(omega_session_t *session_ptr, omega_util_byte_tra
return -1;
}

int omega_edit_apply_transform(omega_session_t *session_ptr, int64_t offset, int64_t length,
omega_edit_transform_func_t transform, void *transform_context) {
if (!omega_session_changes_paused(session_ptr) && 0 == omega_edit_create_checkpoint(session_ptr)) {
const auto in_file = session_ptr->models_.back()->file_path;
const auto out_file = in_file + "_";
auto out_file_ptr = fopen(out_file.c_str(), "wb");
if (0 == transform(session_ptr->models_.back()->file_ptr, offset, length, out_file_ptr, transform_context)) {
errno = 0;// reset errno
if (0 == fclose(session_ptr->models_.back()->file_ptr) && 0 == omega_util_remove_file(in_file.c_str()) &&
0 == rename(out_file.c_str(), in_file.c_str()) &&
(session_ptr->models_.back()->file_ptr = fopen(in_file.c_str(), "rb"))) {
for (const auto &viewport_ptr : session_ptr->viewports_) {
viewport_ptr->data_segment.capacity =
-1 * std::abs(viewport_ptr->data_segment.capacity);// indicate dirty read
omega_viewport_notify(viewport_ptr.get(), VIEWPORT_EVT_TRANSFORM, nullptr);
}
omega_session_notify(session_ptr, SESSION_EVT_TRANSFORM, nullptr);
return 0;
}

// In a bad state (I/O failure), so abort
ABORT(LOG_ERRNO(););
}
// The transform failed, but we can recover from this
if (omega_util_file_exists(out_file.c_str())) { omega_util_remove_file(out_file.c_str()); }
}
return -1;
}

int omega_edit_save(omega_session_t *session_ptr, const char *file_path, int io_flags, char *saved_file_path) {
assert(session_ptr);
assert(file_path);
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* *
**********************************************************************************************************************/

#include "../include/omega_edit/encode.h"
#include "omega_edit/encode.h"
#include <assert.h>

size_t omega_encode_bin2hex(const omega_byte_t *src, char *dst, size_t src_length) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* *
**********************************************************************************************************************/

#include "../include/omega_edit/filesystem.h"
#include "../include/omega_edit/utility.h"
#include "omega_edit/filesystem.h"
#include "omega_edit/utility.h"
#include "impl_/macros.h"
#include <cassert>
#include <filesystem>
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/change_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_CHANGE_DEF_HPP
#define OMEGA_EDIT_CHANGE_DEF_HPP

#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/fwd_defs.h"
#include "data_def.hpp"
#include <cstdint>

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/data_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_DATA_DEF_HPP
#define OMEGA_EDIT_DATA_DEF_HPP

#include "../../include/omega_edit/byte.h"
#include "omega_edit/byte.h"
#include <cstdint>

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/impl_/internal_fun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
**********************************************************************************************************************/

#include "internal_fun.hpp"
#include "../../include/omega_edit/change.h"
#include "../../include/omega_edit/segment.h"
#include "omega_edit/change.h"
#include "omega_edit/segment.h"
#include "change_def.hpp"
#include "macros.h"
#include "model_def.hpp"
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/impl_/internal_fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#ifndef OMEGA_EDIT_INTERNAL_FUN_HPP
#define OMEGA_EDIT_INTERNAL_FUN_HPP

#include "../../include/omega_edit/byte.h"
#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/byte.h"
#include "omega_edit/fwd_defs.h"
#include "internal_fwd_defs.hpp"
#include <iosfwd>

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/internal_fwd_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_INTERNAL_FWD_DEFS_HPP
#define OMEGA_EDIT_INTERNAL_FWD_DEFS_HPP

#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/fwd_defs.h"
#include <memory>

using omega_model_t = struct omega_model_struct;
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_MACROS_H
#define OMEGA_EDIT_MACROS_H

#include "../../include/omega_edit/config.h"
#include "omega_edit/config.h"

#ifdef __cplusplus

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/model_segment_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_MODEL_SEGMENT_DEF_HPP
#define OMEGA_EDIT_MODEL_SEGMENT_DEF_HPP

#include "../../include/omega_edit/change.h"
#include "omega_edit/change.h"
#include "internal_fwd_defs.hpp"

enum class model_segment_kind_t { SEGMENT_READ, SEGMENT_INSERT };
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/search_context_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_SEARCH_CONTEXT_DEF_H
#define OMEGA_EDIT_SEARCH_CONTEXT_DEF_H

#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/fwd_defs.h"
#include "data_def.hpp"

struct omega_search_context_struct {
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib/impl_/session_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#ifndef OMEGA_EDIT_SESSION_DEF_HPP
#define OMEGA_EDIT_SESSION_DEF_HPP

#include "../../include/omega_edit/edit.h"
#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/edit.h"
#include "omega_edit/fwd_defs.h"
#include "internal_fwd_defs.hpp"
#include "model_def.hpp"
#include <vector>
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib/impl_/viewport_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef OMEGA_EDIT_VIEWPORT_DEF_HPP
#define OMEGA_EDIT_VIEWPORT_DEF_HPP

#include "../../include/omega_edit/fwd_defs.h"
#include "omega_edit/fwd_defs.h"
#include "internal_fwd_defs.hpp"
#include "segment_def.hpp"

Expand Down
Loading