Skip to content

Commit

Permalink
omega_simple.js example with viewport (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
scholarsmate authored Dec 13, 2021
1 parent a0b9fc7 commit 4a28e80
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 18 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/bindings/omega_edit.i
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
%{
/* Includes the header in the wrapper code */
#include "../omega_edit/omega_edit.h"
#include "../omega_edit/include/string.h"
%}
%include <stdint.i>
%include <std_string.i>

/* 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;
Expand Down
244 changes: 243 additions & 1 deletion src/bindings/omega_edit_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h> // Use the C99 official header


#include <string>


SWIGINTERNINLINE
v8::Handle<v8::Value> SWIG_From_long (long value)
{
Expand Down Expand Up @@ -1842,6 +1846,44 @@ SWIG_From_size_t (size_t value)
}


SWIGINTERNINLINE v8::Handle<v8::Value>
SWIG_From_std_string (const std::string& s)
{
return SWIG_FromCharPtrAndSize(s.data(), s.size());
}


SWIGINTERN int
SWIG_AsPtr_std_string (v8::Handle<v8::Value> 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
*/
Expand Down Expand Up @@ -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 ;
Expand Down Expand Up @@ -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<v8::Value> 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<v8::Value> 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<v8::Value> 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<v8::Value> 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<v8::Value> 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();

Expand Down Expand Up @@ -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);
Expand Down
28 changes: 25 additions & 3 deletions src/examples/omega_simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
3 changes: 2 additions & 1 deletion src/omega_edit/include/edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 4a28e80

Please sign in to comment.