Skip to content

Commit

Permalink
add omega_viewport_has_changes function (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
scholarsmate authored Dec 13, 2021
1 parent d0489ab commit 36157f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/omega_edit/include/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ int64_t omega_viewport_get_length(const omega_viewport_t *viewport_ptr);
*/
const omega_byte_t *omega_viewport_get_data(const omega_viewport_t *viewport_ptr);

/**
* Given a viewport, determine if it contains changes since the last omega_viewport_get_data call
* @param viewport_ptr viewport to determine if changes are present
* @return 0 if there are no changes present, and non-zero otherwise
*/
int omega_viewport_has_changes(const omega_viewport_t *viewport_ptr);

/**
* Given a viewport, return the viewport offset
* @param viewport_ptr viewport to get the viewport offset from
Expand Down
10 changes: 7 additions & 3 deletions src/omega_edit/src/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int64_t omega_viewport_get_capacity(const omega_viewport_t *viewport_ptr) {
}

int64_t omega_viewport_get_length(const omega_viewport_t *viewport_ptr) {
if (0 < viewport_ptr->data_segment.capacity) { return viewport_ptr->data_segment.length; }
if (!omega_viewport_has_changes(viewport_ptr)) { return viewport_ptr->data_segment.length; }
auto const capacity = omega_viewport_get_capacity(viewport_ptr);
auto const remaining_file_size =
std::max(omega_session_get_computed_file_size(omega_viewport_get_session(viewport_ptr)) -
Expand Down Expand Up @@ -67,11 +67,15 @@ int omega_viewport_update(omega_viewport_t *viewport_ptr, int64_t offset, int64_

const omega_byte_t *omega_viewport_get_data(const omega_viewport_t *viewport_ptr) {
auto mut_viewport_ptr = const_cast<omega_viewport_t *>(viewport_ptr);
if (viewport_ptr->data_segment.capacity < 0) {
if (omega_viewport_has_changes(viewport_ptr)) {
// Clean the dirty read with a fresh data segment population
mut_viewport_ptr->data_segment.capacity *= -1;
mut_viewport_ptr->data_segment.capacity = std::abs(viewport_ptr->data_segment.capacity);
if (populate_data_segment_(viewport_ptr->session_ptr, &mut_viewport_ptr->data_segment) != 0) { return nullptr; }
assert(omega_viewport_get_length(viewport_ptr) == viewport_ptr->data_segment.length);
}
return get_data_segment_data_(&mut_viewport_ptr->data_segment);
}

int omega_viewport_has_changes(const omega_viewport_t *viewport_ptr) {
return (viewport_ptr->data_segment.capacity < 0) ? 1 : 0;
}

0 comments on commit 36157f5

Please sign in to comment.