Skip to content

C++ class: CodeBuffer

Index · Français

Description

UTF-8 multiline buffer with cursor, dirty state and undo.

Qualified name : gaspardos::desktop::CodeBuffer.

Header : bindings/cpp/code_buffer.hpp. C++98.

When to use it

Edit GaspardEdit text while preserving BOM and line endings.

Simple program example

#include "code_buffer.hpp"
#include <cstdio>
#include <cstring>

int main()
{
    gaspardos::desktop::CodeBuffer buffer;
    std::string                    error;
    if (!buffer.load("hello\r\n", error))
        return 1;
    buffer.place(0, 5);
    if (!buffer.key('!'))
        return 1;
    std::printf("%s", buffer.serialize().c_str());
    return buffer.dirty() && buffer.undo() && !buffer.dirty() ? 0 : 1;
}

Building from Windows

Save the program above as build/api-examples/CodeBuffer.cpp from the repository root. Create the directory if necessary.

PowerShell — from the repository root:

New-Item -ItemType Directory -Force build/api-examples | Out-Null
& .\build-x64-install\bin\x86_64-gaspardos-g++.exe -std=c++98 -O2 -Ibindings/cpp build/api-examples/CodeBuffer.cpp bindings/cpp/file_explorer.cpp bindings/cpp/code_buffer.cpp -o build/api-examples/CodeBuffer-x64.elf
if ($LASTEXITCODE -ne 0) { throw "C++ compilation/link failed" }

Run the ELF on GaspardOS, not Windows. The driver supplies CRT and libraries. Prepare the required volumes/services; examples bound their waits and may exit on backpressure rather than retry. Successful compilation does not validate kernel execution.

The headers, implementation files, native wrappers and complete examples are available under Source files.

Class methods

CodeBuffer()

Create an empty single-line buffer.

bool load(const std::string& raw, std::string& error)

Load UTF-8/ASCII without NUL, at most 1 MiB and 20000 lines; false and message when rejected.

void clear()

Clear text, history and serialization settings.

bool dirty() const

Compare current text with the last saved version.

std::string serialize() const

Bytes to write; restore BOM/CRLF; exact original when unchanged.

const std::string& original() const

Bytes from last load or saved; internal reference.

void saved()

Mark current state as saved; call only after successful storage.

std::size_t lineCount() const

Number of lines (at least one).

std::string line(std::size_t number) const

Zero-based line without terminator; empty if out of bounds.

std::size_t row() const

Zero-based cursor row.

std::size_t column() const

Zero-based UTF-8 character column.

void place(std::size_t row, std::size_t column)

Position the cursor with row/column clamping.

bool key(uint32_t key)

Apply a Unicode/native key; false if insertion rejected; Tab inserts four spaces.

bool undo()

Restore a snapshot; false if history is empty (64 states / 2 MiB).