From 4a28e80be4b823ec414034330cf797d75b12336f Mon Sep 17 00:00:00 2001 From: Davin Shearer <2205472+scholarsmate@users.noreply.github.com> Date: Mon, 13 Dec 2021 14:21:41 -0500 Subject: [PATCH] omega_simple.js example with viewport (#63) --- CMakeLists.txt | 9 +- binding.gyp | 1 + src/bindings/omega_edit.i | 3 + src/bindings/omega_edit_wrap.cxx | 244 ++++++++++++++++++++++++++++++- src/examples/omega_simple.js | 28 +++- src/omega_edit/include/edit.h | 3 +- src/omega_edit/include/session.h | 7 + src/omega_edit/include/string.h | 47 ++++++ src/omega_edit/src/session.cpp | 5 + src/tests/omega_test.cpp | 31 +++- 10 files changed, 360 insertions(+), 18 deletions(-) create mode 100644 src/omega_edit/include/string.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 747b40c32..2cf149a87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCE_FILES src/omega_edit/include/config.h src/omega_edit/include/fwd_defs.h src/omega_edit/include/byte.h + src/omega_edit/include/string.h src/omega_edit/include/change.h src/omega_edit/src/change.cpp src/omega_edit/src/impl_/change_def.h src/omega_edit/include/edit.h src/omega_edit/src/edit.cpp src/omega_edit/include/session.h src/omega_edit/src/session.cpp src/omega_edit/src/impl_/session_def.h @@ -27,12 +28,10 @@ set(SOURCE_FILES src/omega_edit/include/encodings.h src/omega_edit/src/encodings.c src/omega_edit/src/impl_/internal_fun.h src/omega_edit/src/impl_/internal_fun.cpp src/omega_edit/src/impl_/search.h src/omega_edit/src/impl_/search.cpp + src/omega_edit/src/impl_/data_def.h src/omega_edit/src/impl_/data_segment_def.h + src/omega_edit/src/impl_/model_def.h src/omega_edit/src/impl_/model_segment_def.h src/omega_edit/src/impl_/macros.h - src/omega_edit/src/impl_/internal_fwd_defs.h - src/omega_edit/src/impl_/data_def.h - src/omega_edit/src/impl_/data_segment_def.h - src/omega_edit/src/impl_/model_segment_def.h - src/omega_edit/src/impl_/model_def.h) + src/omega_edit/src/impl_/internal_fwd_defs.h) # Create the library archive for static linking add_library(omega_edit STATIC ${SOURCE_FILES}) diff --git a/binding.gyp b/binding.gyp index cf9964a75..d5675a3f6 100644 --- a/binding.gyp +++ b/binding.gyp @@ -4,6 +4,7 @@ "target_name": "omega_edit", "sources": [ "src/omega_edit/src/edit.cpp", + "src/omega_edit/src/impl_/search.cpp", "src/omega_edit/src/impl_/internal_fun.cpp", "src/omega_edit/src/session.cpp", "src/omega_edit/src/change.cpp", diff --git a/src/bindings/omega_edit.i b/src/bindings/omega_edit.i index 2577cfd46..98c431589 100644 --- a/src/bindings/omega_edit.i +++ b/src/bindings/omega_edit.i @@ -21,14 +21,17 @@ %{ /* Includes the header in the wrapper code */ #include "../omega_edit/omega_edit.h" +#include "../omega_edit/include/string.h" %} %include +%include /* Parse the header file to generate wrappers */ %include "../omega_edit/include/change.h" %include "../omega_edit/include/edit.h" %include "../omega_edit/include/license.h" %include "../omega_edit/include/session.h" +%include "../omega_edit/include/string.h" %include "../omega_edit/include/viewport.h" %feature("director") SessionOnChangeDirector; diff --git a/src/bindings/omega_edit_wrap.cxx b/src/bindings/omega_edit_wrap.cxx index 66d142eaa..959d2a839 100644 --- a/src/bindings/omega_edit_wrap.cxx +++ b/src/bindings/omega_edit_wrap.cxx @@ -1607,11 +1607,15 @@ static swig_module_info swig_module = {swig_types, 21, 0, 0, 0, 0}; /* Includes the header in the wrapper code */ #include "../omega_edit/omega_edit.h" +#include "../omega_edit/include/string.h" #include // Use the C99 official header +#include + + SWIGINTERNINLINE v8::Handle SWIG_From_long (long value) { @@ -1842,6 +1846,44 @@ SWIG_From_size_t (size_t value) } +SWIGINTERNINLINE v8::Handle +SWIG_From_std_string (const std::string& s) +{ + return SWIG_FromCharPtrAndSize(s.data(), s.size()); +} + + +SWIGINTERN int +SWIG_AsPtr_std_string (v8::Handle obj, std::string **val) +{ + char* buf = 0 ; size_t size = 0; int alloc = SWIG_OLDOBJ; + if (SWIG_IsOK((SWIG_AsCharPtrAndSize(obj, &buf, &size, &alloc)))) { + if (buf) { + if (val) *val = new std::string(buf, size - 1); + if (alloc == SWIG_NEWOBJ) delete[] buf; + return SWIG_NEWOBJ; + } else { + if (val) *val = 0; + return SWIG_OLDOBJ; + } + } else { + static int init = 0; + static swig_type_info* descriptor = 0; + if (!init) { + descriptor = SWIG_TypeQuery("std::string" " *"); + init = 1; + } + if (descriptor) { + std::string *vptr; + int res = SWIG_ConvertPtr(obj, (void**)&vptr, descriptor, 0); + if (SWIG_IsOK(res) && val) *val = vptr; + return res; + } + } + return SWIG_ERROR; +} + + /* * Session On Change Director */ @@ -2140,7 +2182,7 @@ static SwigV8ReturnValue _wrap_omega_edit_create_viewport(const SwigV8Arguments omega_session_t *arg1 = (omega_session_t *) 0 ; int64_t arg2 ; int64_t arg3 ; - omega_viewport_on_change_cbk_t arg4 = (omega_viewport_on_change_cbk_t) 0 ; + omega_viewport_on_change_cbk_t arg4 = (omega_viewport_on_change_cbk_t) nullptr ; void *arg5 = (void *) nullptr ; void *argp1 = 0 ; int res1 = 0 ; @@ -3100,6 +3142,201 @@ static SwigV8ReturnValue _wrap_omega_session_visit_changes_reverse(const SwigV8A } +static SwigV8ReturnValue _wrap_omega_session_get_change(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + + v8::Handle jsresult; + omega_session_t *arg1 = (omega_session_t *) 0 ; + int64_t arg2 ; + void *argp1 = 0 ; + int res1 = 0 ; + long long val2 ; + int ecode2 = 0 ; + omega_change_t *result = 0 ; + + if(args.Length() != 2) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_session_get_change."); + + res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_session_t, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_session_get_change" "', argument " "1"" of type '" "omega_session_t const *""'"); + } + arg1 = reinterpret_cast< omega_session_t * >(argp1); + ecode2 = SWIG_AsVal_long_SS_long(args[1], &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "omega_session_get_change" "', argument " "2"" of type '" "int64_t""'"); + } + arg2 = static_cast< int64_t >(val2); + result = (omega_change_t *)omega_session_get_change((omega_session_t const *)arg1,arg2); + jsresult = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_omega_change_t, 0 | 0 ); + + + + SWIGV8_RETURN(jsresult); + + goto fail; +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + + +static SwigV8ReturnValue _wrap_omega_change_get_string(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + + v8::Handle jsresult; + omega_change_t *arg1 = (omega_change_t *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + std::string result; + + if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_change_get_string."); + + res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_change_t, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_change_get_string" "', argument " "1"" of type '" "omega_change_t const *""'"); + } + arg1 = reinterpret_cast< omega_change_t * >(argp1); + result = omega_change_get_string((omega_change_t const *)arg1); + jsresult = SWIG_From_std_string(static_cast< std::string >(result)); + + + SWIGV8_RETURN(jsresult); + + goto fail; +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + + +static SwigV8ReturnValue _wrap_omega_viewport_get_string(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + + v8::Handle jsresult; + omega_viewport_t *arg1 = (omega_viewport_t *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + std::string result; + + if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_viewport_get_string."); + + res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_viewport_t, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_viewport_get_string" "', argument " "1"" of type '" "omega_viewport_t const *""'"); + } + arg1 = reinterpret_cast< omega_viewport_t * >(argp1); + result = omega_viewport_get_string((omega_viewport_t const *)arg1); + jsresult = SWIG_From_std_string(static_cast< std::string >(result)); + + + SWIGV8_RETURN(jsresult); + + goto fail; +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + + +static SwigV8ReturnValue _wrap_omega_edit_insert_string(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + + v8::Handle jsresult; + omega_session_t *arg1 = (omega_session_t *) 0 ; + int64_t arg2 ; + std::string *arg3 = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + long long val2 ; + int ecode2 = 0 ; + int res3 = SWIG_OLDOBJ ; + int64_t result; + + if(args.Length() != 3) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_edit_insert_string."); + + res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_session_t, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_edit_insert_string" "', argument " "1"" of type '" "omega_session_t *""'"); + } + arg1 = reinterpret_cast< omega_session_t * >(argp1); + ecode2 = SWIG_AsVal_long_SS_long(args[1], &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "omega_edit_insert_string" "', argument " "2"" of type '" "int64_t""'"); + } + arg2 = static_cast< int64_t >(val2); + { + std::string *ptr = (std::string *)0; + res3 = SWIG_AsPtr_std_string(args[2], &ptr); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "omega_edit_insert_string" "', argument " "3"" of type '" "std::string const &""'"); + } + if (!ptr) { + SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "omega_edit_insert_string" "', argument " "3"" of type '" "std::string const &""'"); + } + arg3 = ptr; + } + result = (int64_t)omega_edit_insert_string(arg1,arg2,(std::string const &)*arg3); + jsresult = SWIG_From_long_SS_long(static_cast< long long >(result)); + + + if (SWIG_IsNewObj(res3)) delete arg3; + + SWIGV8_RETURN(jsresult); + + goto fail; +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + + +static SwigV8ReturnValue _wrap_omega_edit_overwrite_string(const SwigV8Arguments &args) { + SWIGV8_HANDLESCOPE(); + + v8::Handle jsresult; + omega_session_t *arg1 = (omega_session_t *) 0 ; + int64_t arg2 ; + std::string *arg3 = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + long long val2 ; + int ecode2 = 0 ; + int res3 = SWIG_OLDOBJ ; + int64_t result; + + if(args.Length() != 3) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_edit_overwrite_string."); + + res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_session_t, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_edit_overwrite_string" "', argument " "1"" of type '" "omega_session_t *""'"); + } + arg1 = reinterpret_cast< omega_session_t * >(argp1); + ecode2 = SWIG_AsVal_long_SS_long(args[1], &val2); + if (!SWIG_IsOK(ecode2)) { + SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "omega_edit_overwrite_string" "', argument " "2"" of type '" "int64_t""'"); + } + arg2 = static_cast< int64_t >(val2); + { + std::string *ptr = (std::string *)0; + res3 = SWIG_AsPtr_std_string(args[2], &ptr); + if (!SWIG_IsOK(res3)) { + SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "omega_edit_overwrite_string" "', argument " "3"" of type '" "std::string const &""'"); + } + if (!ptr) { + SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "omega_edit_overwrite_string" "', argument " "3"" of type '" "std::string const &""'"); + } + arg3 = ptr; + } + result = (int64_t)omega_edit_overwrite_string(arg1,arg2,(std::string const &)*arg3); + jsresult = SWIG_From_long_SS_long(static_cast< long long >(result)); + + + if (SWIG_IsNewObj(res3)) delete arg3; + + SWIGV8_RETURN(jsresult); + + goto fail; +fail: + SWIGV8_RETURN(SWIGV8_UNDEFINED()); +} + + static SwigV8ReturnValue _wrap_omega_viewport_get_session(const SwigV8Arguments &args) { SWIGV8_HANDLESCOPE(); @@ -4883,6 +5120,11 @@ SWIGV8_AddStaticFunction(exports_obj, "omega_session_get_last_change", _wrap_ome SWIGV8_AddStaticFunction(exports_obj, "omega_session_get_last_undo", _wrap_omega_session_get_last_undo); SWIGV8_AddStaticFunction(exports_obj, "omega_session_visit_changes", _wrap_omega_session_visit_changes); SWIGV8_AddStaticFunction(exports_obj, "omega_session_visit_changes_reverse", _wrap_omega_session_visit_changes_reverse); +SWIGV8_AddStaticFunction(exports_obj, "omega_session_get_change", _wrap_omega_session_get_change); +SWIGV8_AddStaticFunction(exports_obj, "omega_change_get_string", _wrap_omega_change_get_string); +SWIGV8_AddStaticFunction(exports_obj, "omega_viewport_get_string", _wrap_omega_viewport_get_string); +SWIGV8_AddStaticFunction(exports_obj, "omega_edit_insert_string", _wrap_omega_edit_insert_string); +SWIGV8_AddStaticFunction(exports_obj, "omega_edit_overwrite_string", _wrap_omega_edit_overwrite_string); SWIGV8_AddStaticFunction(exports_obj, "omega_viewport_get_session", _wrap_omega_viewport_get_session); SWIGV8_AddStaticFunction(exports_obj, "omega_viewport_get_capacity", _wrap_omega_viewport_get_capacity); SWIGV8_AddStaticFunction(exports_obj, "omega_viewport_get_length", _wrap_omega_viewport_get_length); diff --git a/src/examples/omega_simple.js b/src/examples/omega_simple.js index f07d5090c..1354079e6 100644 --- a/src/examples/omega_simple.js +++ b/src/examples/omega_simple.js @@ -17,21 +17,43 @@ /* * Uses Omega Edit to extract and save a segment from a file. Example from src/examples:: * node ./omega_simple.js + * R: [] + * I: [Hello Weird!!!!] + * O: [Hello World!!!!] + * D: [Hello World!] * finished! * Verify the results: * cat hello-js.txt * Hello World! */ +function on_viewport_change(viewport, change) { + let change_kind = (change) ? omega_edit.omega_change_get_kind_as_char(change) : 'R' + console.log(change_kind + ": [" + omega_edit.omega_viewport_get_string(viewport) + "]") +} + omega_edit = require('../../build/Release/omega_edit') session = omega_edit.omega_edit_create_session("", null, null) console.assert(session != null, {errorMsg: "session creation failed"}) +viewport = omega_edit.omega_edit_create_viewport(session, 0, 100, null, null) +if (omega_edit.omega_viewport_has_changes(viewport)) { + on_viewport_change(viewport) +} rc = omega_edit.omega_edit_insert(session, 0, "Hello Weird!!!!", 15); console.assert(rc > 0, {rc: rc, errorMsg: "insert failed"}) -rc = omega_edit.omega_edit_overwrite(session, 7, "orl", 3); +if (omega_edit.omega_viewport_has_changes(viewport)) { + on_viewport_change(viewport, omega_edit.omega_session_get_change(session, rc)) +} +rc = omega_edit.omega_edit_overwrite(session, 7, "orl", 3) console.assert(rc > 0, {rc: rc, errorMsg: "overwrite failed"}) -rc = omega_edit.omega_edit_delete(session, 11, 3); +if (omega_edit.omega_viewport_has_changes(viewport)) { + on_viewport_change(viewport, omega_edit.omega_session_get_change(session, rc)) +} +rc = omega_edit.omega_edit_delete(session, 11, 3) console.assert(rc > 0, {rc: rc, errorMsg: "delete failed"}) +if (omega_edit.omega_viewport_has_changes(viewport)) { + on_viewport_change(viewport, omega_edit.omega_session_get_change(session, rc)) +} rc = omega_edit.omega_edit_save(session, "hello-js.txt") console.assert(rc === 0, {rc: rc, errorMsg: "save failed"}) -omega_edit.omega_edit_destroy_session(session) +omega_edit.omega_edit_destroy_session(session); console.log("finished!") diff --git a/src/omega_edit/include/edit.h b/src/omega_edit/include/edit.h index 6058a867f..583beae8b 100644 --- a/src/omega_edit/include/edit.h +++ b/src/omega_edit/include/edit.h @@ -67,7 +67,8 @@ void omega_edit_destroy_session(omega_session_t *session_ptr); * @return pointer to the new viewport, nullptr on failure */ omega_viewport_t *omega_edit_create_viewport(omega_session_t *session_ptr, int64_t offset, int64_t capacity, - omega_viewport_on_change_cbk_t cbk, void *user_data_ptr = nullptr); + omega_viewport_on_change_cbk_t cbk = nullptr, + void *user_data_ptr = nullptr); /** * Destroy a given viewport diff --git a/src/omega_edit/include/session.h b/src/omega_edit/include/session.h index 160722915..574d8c940 100644 --- a/src/omega_edit/include/session.h +++ b/src/omega_edit/include/session.h @@ -111,6 +111,13 @@ int omega_session_visit_changes(const omega_session_t *session_ptr, omega_sessio int omega_session_visit_changes_reverse(const omega_session_t *session_ptr, omega_session_change_visitor_cbk_t cbk, void *user_data); +/** + * Given a change serial, get the change + * @param change_serial change serial of the change to get + * @return change with the matching serial, or nullptr on failure + */ +const omega_change_t *omega_session_get_change(const omega_session_t *session_ptr, int64_t change_serial); + #ifdef __cplusplus } #endif diff --git a/src/omega_edit/include/string.h b/src/omega_edit/include/string.h new file mode 100644 index 000000000..19b58c4af --- /dev/null +++ b/src/omega_edit/include/string.h @@ -0,0 +1,47 @@ +/********************************************************************************************************************** + * 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 * + * 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_STRING_H +#define OMEGA_EDIT_STRING_H + +#ifdef __cplusplus + +#include "change.h" +#include "edit.h" +#include "viewport.h" +#include + +inline std::string omega_change_get_string(const omega_change_t *change_ptr) { + return {reinterpret_cast(omega_change_get_bytes(change_ptr)), + static_cast(omega_change_get_length(change_ptr))}; +} + +inline std::string omega_viewport_get_string(const omega_viewport_t *viewport_ptr) { + return {reinterpret_cast(omega_viewport_get_data(viewport_ptr)), + static_cast(omega_viewport_get_length(viewport_ptr))}; +} + +inline int64_t omega_edit_insert_string(omega_session_t *session_ptr, int64_t offset, const std::string &str) { + return omega_edit_insert(session_ptr, offset, str.c_str(), static_cast(str.length())); +} + +inline int64_t omega_edit_overwrite_string(omega_session_t *session_ptr, int64_t offset, const std::string &str) { + return omega_edit_overwrite(session_ptr, offset, str.c_str(), static_cast(str.length())); +} + +#endif//__cplusplus + +#endif//OMEGA_EDIT_STRING_H diff --git a/src/omega_edit/src/session.cpp b/src/omega_edit/src/session.cpp index ce5c00f86..0bfe62f82 100644 --- a/src/omega_edit/src/session.cpp +++ b/src/omega_edit/src/session.cpp @@ -72,3 +72,8 @@ int omega_session_visit_changes_reverse(const omega_session_t *session_ptr, omeg } return rc; } + +const omega_change_t *omega_session_get_change(const omega_session_t *session_ptr, int64_t change_serial) { + return (0 < change_serial && change_serial <= static_cast(omega_session_get_num_changes(session_ptr))) ? + session_ptr->model_ptr_->changes[change_serial - 1].get() : nullptr; +} diff --git a/src/tests/omega_test.cpp b/src/tests/omega_test.cpp index 49bb5488e..d1dd7a109 100644 --- a/src/tests/omega_test.cpp +++ b/src/tests/omega_test.cpp @@ -18,6 +18,7 @@ #include "../omega_edit/include/utility.h" #include "../omega_edit/omega_edit.h" +#include "../omega_edit/include/string.h" #include "catch.hpp" #include "test_util.h" @@ -156,7 +157,7 @@ TEST_CASE("Model Test", "[ModelTests]") { REQUIRE(0 == omega_edit_save(session_ptr, "data/model-test.actual.4.dat")); REQUIRE(compare_files("data/model-test.expected.4.dat", "data/model-test.actual.4.dat") == 0); REQUIRE(omega_session_get_num_undone_changes(session_ptr) == 1); - REQUIRE(0 < omega_edit_overwrite_bytes(session_ptr, 0, reinterpret_cast("-"))); + REQUIRE(0 < omega_edit_overwrite_string(session_ptr, 0, "-")); REQUIRE(omega_session_get_num_undone_changes(session_ptr) == 0); REQUIRE(0 < omega_edit_overwrite_bytes(session_ptr, file_size - 1, reinterpret_cast("+"), 1)); REQUIRE(0 < omega_edit_insert_bytes(session_ptr, 5, reinterpret_cast("XxXxXxX"), 7)); @@ -196,7 +197,14 @@ TEST_CASE("Hanoi insert", "[ModelTests]") { REQUIRE(session_ptr); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 0); // Hanoi test - REQUIRE(0 < omega_edit_insert_bytes(session_ptr, 0, reinterpret_cast("00"))); + int64_t change_serial; + REQUIRE(0 < (change_serial = omega_edit_insert_bytes(session_ptr, 0, reinterpret_cast("00")))); + auto change_ptr = omega_session_get_change(session_ptr, change_serial); + REQUIRE(change_ptr); + REQUIRE('I' == omega_change_get_kind_as_char(change_ptr)); + REQUIRE(0 == omega_change_get_offset(change_ptr)); + REQUIRE(2 == omega_change_get_length(change_ptr)); + REQUIRE(omega_change_get_string(change_ptr) == "00"); REQUIRE(1 == omega_session_get_num_changes(session_ptr)); REQUIRE(1 == omega_change_get_serial(omega_session_get_last_change(session_ptr))); REQUIRE(2 == omega_session_get_computed_file_size(session_ptr)); @@ -236,7 +244,7 @@ TEST_CASE("Hanoi insert", "[ModelTests]") { REQUIRE(10 == omega_session_get_num_changes(session_ptr)); REQUIRE(10 == omega_change_get_serial(omega_session_get_last_change(session_ptr))); REQUIRE(20 == omega_session_get_computed_file_size(session_ptr)); - REQUIRE(0 < omega_edit_insert_bytes(session_ptr, 10, reinterpret_cast("*****+*****"))); + REQUIRE(0 < omega_edit_insert_string(session_ptr, 10, "*****+*****")); REQUIRE(11 == omega_session_get_num_changes(session_ptr)); REQUIRE(11 == omega_change_get_serial(omega_session_get_last_change(session_ptr))); REQUIRE(31 == omega_session_get_computed_file_size(session_ptr)); @@ -262,11 +270,19 @@ TEST_CASE("Check initialization", "[InitTests]") { session_ptr = omega_edit_create_session(in_filename, session_change_cbk, &file_info); REQUIRE(session_ptr); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 63); - REQUIRE(0 < omega_edit_insert_bytes(session_ptr, 10, reinterpret_cast("++++"), 4)); + REQUIRE(nullptr == omega_session_get_change(session_ptr, 0)); + int64_t rc; + REQUIRE(0 < (rc = omega_edit_insert_bytes(session_ptr, 10, reinterpret_cast("++++"), 4))); + auto change_ptr = omega_session_get_change(session_ptr, rc); + REQUIRE(change_ptr); + REQUIRE('I' == omega_change_get_kind_as_char(change_ptr)); + REQUIRE(10 == omega_change_get_offset(change_ptr)); + REQUIRE(4 == omega_change_get_length(change_ptr)); + REQUIRE(nullptr == omega_session_get_change(session_ptr, rc + 1)); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 67); - REQUIRE(0 < omega_edit_overwrite_bytes(session_ptr, 12, reinterpret_cast("."), 1)); + REQUIRE(0 < (rc = omega_edit_overwrite_bytes(session_ptr, 12, reinterpret_cast("."), 1))); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 67); - REQUIRE(0 < omega_edit_insert(session_ptr, 0, "+++")); + REQUIRE(0 < (rc = omega_edit_insert(session_ptr, 0, "+++"))); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 70); REQUIRE(0 < omega_edit_overwrite_bytes(session_ptr, 1, reinterpret_cast("."))); REQUIRE(omega_session_get_computed_file_size(session_ptr) == 70); @@ -307,8 +323,7 @@ void vpt_change_cbk(const omega_viewport_t *viewport_ptr, const omega_change_t * break; case CHAR_MODE: clog << "CHAR MODE ["; - clog << string((const char *) omega_viewport_get_data(viewport_ptr), - omega_viewport_get_length(viewport_ptr)); + clog << omega_viewport_get_string(viewport_ptr); clog << "]\n"; break; default:// flow through