c0
C0DATA — structured data using ASCII C0 control codes (C)
Loading...
Searching...
No Matches
c0_diff.h File Reference
#include "c0.h"
#include "c0_strbuf.h"

Go to the source code of this file.

Data Structures

struct  c0_diff_unit
 One unit of a pattern: an anchor (is_sub 0, new_ NULL) or a substitution old␚␟new. More...
struct  c0_diff_record
 A record: one anchored pattern. More...
struct  c0_diff_file
 A file's records, in order. More...
struct  c0_diff
 A parsed diff. More...
struct  c0_diff_error
struct  c0_diff_file_input
 An input file (borrowed). More...
struct  c0_diff_file_output
 An output file (malloc'd path and content). More...
struct  c0_diff_builder

Enumerations

enum  c0_diff_status {
  C0_DIFF_OK = 0 , C0_DIFF_SYNTAX , C0_DIFF_WRONG_SHAPE , C0_DIFF_CONSTRAINT ,
  C0_DIFF_DUPLICATE_PATH , C0_DIFF_FILE_NOT_FOUND , C0_DIFF_EMPTY_SEARCH , C0_DIFF_PATTERN_NOT_FOUND ,
  C0_DIFF_PATTERN_AMBIGUOUS , C0_DIFF_OOM
}

Functions

c0_diff c0_diff_parse (const uint8_t *buf, size_t len)
 Parse a diff document (with or without its SYN heading), refusing everything DIFF.md lists under "What a diff may not hold".
void c0_diff_free (c0_diff *d)
uint8_t * c0_diff_record_search (const c0_diff_record *r, size_t *out_len)
 A record's search text (its anchors and olds) or replacement (news in place of olds), concatenated into a malloc'd buffer the caller frees.
uint8_t * c0_diff_record_replacement (const c0_diff_record *r, size_t *out_len)
int c0_diff_apply (const uint8_t *diff_buf, size_t diff_len, const c0_diff_file_input *files, size_t nfiles, c0_diff_file_output **out, size_t *out_count, c0_diff_error *err)
 Apply a diff to in-memory files.
void c0_diff_free_outputs (c0_diff_file_output *out, size_t count)
void c0_diff_builder_init (c0_diff_builder *b, int heading)
 Start a diff; with heading the SYN heading shape diff is written first.
void c0_diff_builder_free (c0_diff_builder *b)
void c0_diff_file_begin (c0_diff_builder *b, const uint8_t *path, size_t path_len)
 FS and the path (a name).
void c0_diff_record_begin (c0_diff_builder *b)
 RS: a new record (anchored pattern).
void c0_diff_anchor (c0_diff_builder *b, const uint8_t *text, size_t len)
 An anchor / a substitution old␚␟new as the next unit of the current record.
void c0_diff_sub (c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len)
void c0_diff_replace (c0_diff_builder *b, const uint8_t *before, size_t before_len, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len, const uint8_t *after, size_t after_len)
 One record: anchor, old␚␟new, anchor (empty anchors are omitted).
void c0_diff_insert_after (c0_diff_builder *b, const uint8_t *anchor, size_t anchor_len, const uint8_t *text, size_t len)
 One record: insert text after anchor (␞anchor␟␚␟text).
void c0_diff_delete (c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *before, size_t before_len, const uint8_t *after, size_t after_len)
 One record: remove old, found once after the cursor, between optional anchors.
void c0_diff_assert (c0_diff_builder *b, const uint8_t *text, size_t len)
 One record: assert that text is present once; nothing changes.
void c0_diff_note (c0_diff_builder *b, const uint8_t *text, size_t len)
 BEL and a note.
void c0_diff_eot (c0_diff_builder *b)
 EOT.
c0_bytes c0_diff_builder_bytes (const c0_diff_builder *b)
 Borrow the builder's bytes (valid until it is freed or extended).
void c0_diff_file_begin_str (c0_diff_builder *b, const char *path)
void c0_diff_anchor_str (c0_diff_builder *b, const char *text)
void c0_diff_sub_str (c0_diff_builder *b, const char *old, const char *new_)
void c0_diff_replace_str (c0_diff_builder *b, const char *before, const char *old, const char *new_, const char *after)
void c0_diff_insert_after_str (c0_diff_builder *b, const char *anchor, const char *text)
void c0_diff_delete_str (c0_diff_builder *b, const char *old, const char *before, const char *after)
void c0_diff_assert_str (c0_diff_builder *b, const char *text)
void c0_diff_note_str (c0_diff_builder *b, const char *text)

Enumeration Type Documentation

◆ c0_diff_status

Enumerator
C0_DIFF_OK 
C0_DIFF_SYNTAX 

the diff is not well-formed C0DATA (see msg/pos)

C0_DIFF_WRONG_SHAPE 

the heading's shape is present and not diff

C0_DIFF_CONSTRAINT 

the diff holds something a diff may not (see msg)

C0_DIFF_DUPLICATE_PATH 

the same path twice

C0_DIFF_FILE_NOT_FOUND 

a path with no input file

C0_DIFF_EMPTY_SEARCH 

a record whose search text is empty

C0_DIFF_PATTERN_NOT_FOUND 

a record's search text does not occur

C0_DIFF_PATTERN_AMBIGUOUS 

it occurs more than once after the cursor

C0_DIFF_OOM 

Function Documentation

◆ c0_diff_anchor()

void c0_diff_anchor ( c0_diff_builder * b,
const uint8_t * text,
size_t len )

An anchor / a substitution old␚␟new as the next unit of the current record.

◆ c0_diff_anchor_str()

void c0_diff_anchor_str ( c0_diff_builder * b,
const char * text )

◆ c0_diff_apply()

int c0_diff_apply ( const uint8_t * diff_buf,
size_t diff_len,
const c0_diff_file_input * files,
size_t nfiles,
c0_diff_file_output ** out,
size_t * out_count,
c0_diff_error * err )

Apply a diff to in-memory files.

Atomic: everything is validated before anything is produced. On success returns 1, allocates *out (one entry per input file, in input order, edited or copied unchanged) and sets *out_count; free with c0_diff_free_outputs. On failure returns 0, fills *err (when given) and allocates nothing.

◆ c0_diff_assert()

void c0_diff_assert ( c0_diff_builder * b,
const uint8_t * text,
size_t len )

One record: assert that text is present once; nothing changes.

◆ c0_diff_assert_str()

void c0_diff_assert_str ( c0_diff_builder * b,
const char * text )

◆ c0_diff_builder_bytes()

c0_bytes c0_diff_builder_bytes ( const c0_diff_builder * b)

Borrow the builder's bytes (valid until it is freed or extended).

◆ c0_diff_builder_free()

void c0_diff_builder_free ( c0_diff_builder * b)

◆ c0_diff_builder_init()

void c0_diff_builder_init ( c0_diff_builder * b,
int heading )

Start a diff; with heading the SYN heading shape diff is written first.

◆ c0_diff_delete()

void c0_diff_delete ( c0_diff_builder * b,
const uint8_t * old,
size_t old_len,
const uint8_t * before,
size_t before_len,
const uint8_t * after,
size_t after_len )

One record: remove old, found once after the cursor, between optional anchors.

◆ c0_diff_delete_str()

void c0_diff_delete_str ( c0_diff_builder * b,
const char * old,
const char * before,
const char * after )

◆ c0_diff_eot()

void c0_diff_eot ( c0_diff_builder * b)

EOT.

◆ c0_diff_file_begin()

void c0_diff_file_begin ( c0_diff_builder * b,
const uint8_t * path,
size_t path_len )

FS and the path (a name).

◆ c0_diff_file_begin_str()

void c0_diff_file_begin_str ( c0_diff_builder * b,
const char * path )

◆ c0_diff_free()

void c0_diff_free ( c0_diff * d)

◆ c0_diff_free_outputs()

void c0_diff_free_outputs ( c0_diff_file_output * out,
size_t count )

◆ c0_diff_insert_after()

void c0_diff_insert_after ( c0_diff_builder * b,
const uint8_t * anchor,
size_t anchor_len,
const uint8_t * text,
size_t len )

One record: insert text after anchor (␞anchor␟␚␟text).

◆ c0_diff_insert_after_str()

void c0_diff_insert_after_str ( c0_diff_builder * b,
const char * anchor,
const char * text )

◆ c0_diff_note()

void c0_diff_note ( c0_diff_builder * b,
const uint8_t * text,
size_t len )

BEL and a note.

◆ c0_diff_note_str()

void c0_diff_note_str ( c0_diff_builder * b,
const char * text )

◆ c0_diff_parse()

c0_diff c0_diff_parse ( const uint8_t * buf,
size_t len )

Parse a diff document (with or without its SYN heading), refusing everything DIFF.md lists under "What a diff may not hold".

◆ c0_diff_record_begin()

void c0_diff_record_begin ( c0_diff_builder * b)

RS: a new record (anchored pattern).

◆ c0_diff_record_replacement()

uint8_t * c0_diff_record_replacement ( const c0_diff_record * r,
size_t * out_len )

◆ c0_diff_record_search()

uint8_t * c0_diff_record_search ( const c0_diff_record * r,
size_t * out_len )

A record's search text (its anchors and olds) or replacement (news in place of olds), concatenated into a malloc'd buffer the caller frees.

NULL only on allocation failure; *out_len gets the length.

◆ c0_diff_replace()

void c0_diff_replace ( c0_diff_builder * b,
const uint8_t * before,
size_t before_len,
const uint8_t * old,
size_t old_len,
const uint8_t * new_,
size_t new_len,
const uint8_t * after,
size_t after_len )

One record: anchor, old␚␟new, anchor (empty anchors are omitted).

◆ c0_diff_replace_str()

void c0_diff_replace_str ( c0_diff_builder * b,
const char * before,
const char * old,
const char * new_,
const char * after )

◆ c0_diff_sub()

void c0_diff_sub ( c0_diff_builder * b,
const uint8_t * old,
size_t old_len,
const uint8_t * new_,
size_t new_len )

◆ c0_diff_sub_str()

void c0_diff_sub_str ( c0_diff_builder * b,
const char * old,
const char * new_ )