Loading...
Searching...
No Matches
buffer.c File Reference
`#include "arc/console/buffer.h"`
`#include "arc/console/view.h"`
`#include "arc/std/string.h"`
`#include "arc/std/vector.h"`
`#include <stdlib.h>`

Go to the source code of this file.

## Data Structures

struct | ARC_ConsoleBuffer

## Functions

void | ARC_ConsoleBuffer_Create (ARC_ConsoleBuffer **buffer)
| creates ARC_ConsoleBuffer type
void | ARC_ConsoleBuffer_Destroy (ARC_ConsoleBuffer *buffer)
| destroys ARC_ConsoleBuffer type
void | ARC_ConsoleBuffer_Clear (ARC_ConsoleBuffer *buffer)
| clears the contents of a ARC_ConsoleBuffer
void | ARC_ConsoleBuffer_Render (ARC_ConsoleBuffer *buffer, ARC_ConsoleView *view)
| renders a buffer to a ARC_ConsoleView
void | ARC_ConsoleBuffer_RenderSection (ARC_ConsoleBuffer *buffer, ARC_ConsoleView *view, uint32_t startIndex, uint32_t lines)
| renders a section of buffer to a ARC_ConsoleView
void | ARC_ConsoleBuffer_AddChar (ARC_ConsoleBuffer *buffer, char character)
| adds a character to the buffer
void | ARC_ConsoleBuffer_AddString (ARC_ConsoleBuffer *buffer, ARC_String *string)
| adds an ARC_String to the buffer
void | ARC_ConsoleBuffer_AddCString (ARC_ConsoleBuffer *buffer, char *cstring, uint64_t length)
| adds a cstring to the buffer
void | ARC_ConsoleBuffer_AddCStringWithStrlen (ARC_ConsoleBuffer *buffer, char *cstring)
| adds a cstring to the buffer with the cstrings string length
uint32_t | ARC_ConsoleBuffer_GetLineNumbers (ARC_ConsoleBuffer *buffer)
| gets the number of lines from a console line buffer

## Function Documentation

## ARC_ConsoleBuffer_AddChar()

void ARC_ConsoleBuffer_AddChar | ( | ARC_ConsoleBuffer * | buffer,
|  | char | character )

adds a character to the buffer

Parameters

  buffer | ARC_ConsoleBuffer to add character to
  character | char to add to ARC_ConsoleBuffer

Definition at line 94 of file buffer.c.

{
if(character == '\n'){
ARC_Vector_Add(buffer->bufferLines, (void *)NULL);
return;
}
//get the last line and add a char to it
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, ARC_Vector_Size(buffer->bufferLines) - 1);
ARC_Vector_RemoveIndex(buffer->bufferLines, ARC_Vector_Size(buffer->bufferLines) - 1);
if(bufferLine == NULL){
ARC_String_Create(&bufferLine, &character, 1);
ARC_Vector_Add(buffer->bufferLines, (void *)bufferLine);
return;
}
//add char to the end of the bufferline
ARC_String *nextChar;
ARC_String_Create(&nextChar, &character, 1);
ARC_String *tempBufferLine = bufferLine;
ARC_String_Merge(&bufferLine, tempBufferLine, nextChar);
ARC_String_Destroy(tempBufferLine);
ARC_String_Destroy(nextChar);
//add buffer line back to the bufferLines
ARC_Vector_Add(buffer->bufferLines, (void *)bufferLine);
}
ARC_String_Merge
void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second)
merges two strings together
Definition string.c:485
ARC_String_Create
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9
ARC_String_Destroy
void ARC_String_Destroy(ARC_String *string)
destroys ARC_String type
Definition string.c:52
ARC_ConsoleBuffer::bufferLines
ARC_Vector * bufferLines
Definition buffer.c:10
ARC_String
substring position within a string
Definition string.h:14
ARC_Vector_Get
void * ARC_Vector_Get(ARC_Vector *vector, uint32_t index)
gets an item from an ARC_Vector at a position index
Definition vector.c:153
ARC_Vector_Add
void ARC_Vector_Add(ARC_Vector *vector, void *data)
adds an item to an ARC_Vector
Definition vector.c:70
ARC_Vector_RemoveIndex
void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t index)
removes an item from an ARC_Vector at an index
Definition vector.c:110

References ARC_String_Create(), ARC_String_Destroy(), ARC_String_Merge(), ARC_Vector_Add(), ARC_Vector_Get(), ARC_Vector_RemoveIndex(), and ARC_ConsoleBuffer::bufferLines.

Referenced by ARC_ConsoleBuffer_AddCString(), ARC_ConsoleBuffer_AddCStringWithStrlen(), and ARC_ConsoleBuffer_AddString().

## ARC_ConsoleBuffer_AddCString()

void ARC_ConsoleBuffer_AddCString | ( | ARC_ConsoleBuffer * | buffer,
|  | char * | cstring,
|  | uint64_t | length )

adds a cstring to the buffer

Parameters

  buffer | ARC_ConsoleBuffer to add character to
  string | cstring to add to ARC_ConsoleBuffer
  length | the length of the c string to add

Definition at line 131 of file buffer.c.

{
//TODO: this in a more efficient way
for(uint64_t i = 0; i < length; i++){
ARC_ConsoleBuffer_AddChar(buffer, cstring[i]);
}
}
ARC_ConsoleBuffer_AddChar
void ARC_ConsoleBuffer_AddChar(ARC_ConsoleBuffer *buffer, char character)
adds a character to the buffer
Definition buffer.c:94

References ARC_ConsoleBuffer_AddChar().

## ARC_ConsoleBuffer_AddCStringWithStrlen()

void ARC_ConsoleBuffer_AddCStringWithStrlen | ( | ARC_ConsoleBuffer * | buffer,
|  | char * | cstring )

adds a cstring to the buffer with the cstrings string length

Parameters

  buffer | ARC_ConsoleBuffer to add character to
  string | cstring to add to ARC_ConsoleBuffer

Definition at line 138 of file buffer.c.

{
//TODO: this in a more efficient way
for(uint64_t i = 0; i < strlen(cstring); i++){
ARC_ConsoleBuffer_AddChar(buffer, cstring[i]);
}
}

References ARC_ConsoleBuffer_AddChar().

## ARC_ConsoleBuffer_AddString()

void ARC_ConsoleBuffer_AddString | ( | ARC_ConsoleBuffer * | buffer,
|  | ARC_String * | string )

adds an ARC_String to the buffer

Parameters

  buffer | ARC_ConsoleBuffer to add character to
  string | ARC_String to add to ARC_ConsoleBuffer

Definition at line 124 of file buffer.c.

{
//TODO: this in a more efficient way
for(uint64_t i = 0; i < string->length; i++){
ARC_ConsoleBuffer_AddChar(buffer, string->data[i]);
}
}
ARC_String::data
char * data
Definition string.h:15

References ARC_ConsoleBuffer_AddChar(), and ARC_String::data.

## ARC_ConsoleBuffer_Clear()

void ARC_ConsoleBuffer_Clear | ( | ARC_ConsoleBuffer * | buffer | ) |

clears the contents of a ARC_ConsoleBuffer

Parameters

  buffer | ARC_ConsoleBuffer to clear

Definition at line 35 of file buffer.c.

{
for(uint32_t i = 0; i < ARC_Vector_Size(buffer->bufferLines); i++){
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i);
if(bufferLine != NULL){
ARC_String_Destroy(bufferLine);
}
}
ARC_Vector_Destroy(buffer->bufferLines);
ARC_Vector_Create(&(buffer->bufferLines));
//add first line to vector
ARC_Vector_Add(buffer->bufferLines, NULL);
}
ARC_Vector_Destroy
void ARC_Vector_Destroy(ARC_Vector *vector)
destroys an ARC_Vector
Definition vector.c:54
ARC_Vector_Create
void ARC_Vector_Create(ARC_Vector **vector, ARC_Vector_CompareDataFn *compareDataFn, ARC_Vector_DestroyDataFn *destroyDataFn)
creates an ARC_Vector which is an "expandable" array
Definition vector.c:31

References ARC_String_Destroy(), ARC_Vector_Add(), ARC_Vector_Create(), ARC_Vector_Destroy(), ARC_Vector_Get(), and ARC_ConsoleBuffer::bufferLines.

## ARC_ConsoleBuffer_Create()

void ARC_ConsoleBuffer_Create | ( | ARC_ConsoleBuffer ** | buffer | ) |

creates ARC_ConsoleBuffer type

Parameters

  buffer | ARC_ConsoleBuffer to create

Definition at line 13 of file buffer.c.

{
*buffer = (ARC_ConsoleBuffer *)malloc(sizeof(ARC_ConsoleBuffer));
ARC_Vector_Create(&((*buffer)->bufferLines));
//add first line to vector
ARC_Vector_Add((*buffer)->bufferLines, NULL);
}
ARC_ConsoleBuffer
Definition buffer.c:9

References ARC_Vector_Add(), and ARC_Vector_Create().

Referenced by ARC_ConsoleShell_Create().

## ARC_ConsoleBuffer_Destroy()

void ARC_ConsoleBuffer_Destroy | ( | ARC_ConsoleBuffer * | buffer | ) |

destroys ARC_ConsoleBuffer type

Parameters

  buffer | ARC_ConsoleBuffer to destroy

Definition at line 22 of file buffer.c.

{
for(uint32_t i = 0; i < ARC_Vector_Size(buffer->bufferLines); i++){
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i);
if(bufferLine != NULL){
ARC_String_Destroy(bufferLine);
}
}
ARC_Vector_Destroy(buffer->bufferLines);
free(buffer);
}

References ARC_String_Destroy(), ARC_Vector_Destroy(), ARC_Vector_Get(), and ARC_ConsoleBuffer::bufferLines.

Referenced by ARC_ConsoleShell_Destroy().

## ARC_ConsoleBuffer_GetLineNumbers()

uint32_t ARC_ConsoleBuffer_GetLineNumbers | ( | ARC_ConsoleBuffer * | buffer | ) |

gets the number of lines from a console line buffer

Parameters

  buffer | ARC_ConsoleBuffer get number of lines from

Returns
  the number of lines within an ARC_ConsoleBuffer

Definition at line 145 of file buffer.c.

{
return ARC_Vector_Size(buffer->bufferLines);
}

References ARC_ConsoleBuffer::bufferLines.

## ARC_ConsoleBuffer_Render()

void ARC_ConsoleBuffer_Render | ( | ARC_ConsoleBuffer * | buffer,
|  | ARC_ConsoleView * | view )

renders a buffer to a ARC_ConsoleView

Parameters

  buffer | ARC_ConsoleBuffer to render
  view | ARC_ConsoleView to render the buffer contents to

Definition at line 51 of file buffer.c.

{
ARC_Rect viewBounds = ARC_ConsoleView_GetBounds(view);
uint32_t bufferStartIndex = 0;
if(ARC_Vector_Size(buffer->bufferLines) > (uint32_t)viewBounds.h){
bufferStartIndex = ARC_Vector_Size(buffer->bufferLines) - viewBounds.h;
}
for(uint32_t i = 0; i < (uint32_t)viewBounds.h; i++){
if(i + bufferStartIndex > ARC_Vector_Size(buffer->bufferLines)){
break;
}
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i + bufferStartIndex);
if(bufferLine == NULL){
continue;
}
ARC_ConsoleView_RenderStringAt(view, bufferLine, (ARC_Point){ 0, i });
}
}
ARC_ConsoleView_RenderStringAt
void ARC_ConsoleView_RenderStringAt(ARC_ConsoleView *view, ARC_String *text, ARC_Point pos)
Definition view.c:109
ARC_ConsoleView_GetBounds
ARC_Rect ARC_ConsoleView_GetBounds(ARC_ConsoleView *view)
gets the bounds of an ARC_ConsoleView
Definition view.c:147
ARC_Point
Definition point.h:10
ARC_Rect
Definition rectangle.h:12
ARC_Rect::h
int32_t h
Definition rectangle.h:16

References ARC_ConsoleView_GetBounds(), ARC_ConsoleView_RenderStringAt(), ARC_Vector_Get(), ARC_ConsoleBuffer::bufferLines, and ARC_Rect::h.

Referenced by ARC_ConsoleShell_Render().

## ARC_ConsoleBuffer_RenderSection()

void ARC_ConsoleBuffer_RenderSection | ( | ARC_ConsoleBuffer * | buffer,
|  | ARC_ConsoleView * | view,
|  | uint32_t | startIndex,
|  | uint32_t | lines )

renders a section of buffer to a ARC_ConsoleView

Parameters

  buffer | ARC_ConsoleBuffer to render
  view | ARC_ConsoleView to render the buffer contents to
  startIndex | start index of buffer to render
  lines | the number of lines of buffer to render

Definition at line 73 of file buffer.c.

{
ARC_Rect viewBounds = ARC_ConsoleView_GetBounds(view);
for(uint32_t i = 0; i < lines; i++){
if(i + startIndex >= ARC_Vector_Size(buffer->bufferLines)){
break;
}
if(i >= (uint32_t)viewBounds.h){
break;
}
ARC_String *bufferLine = (ARC_String *)ARC_Vector_Get(buffer->bufferLines, i + startIndex);
if(bufferLine == NULL){
continue;
}
ARC_ConsoleView_RenderStringAt(view, bufferLine, (ARC_Point){ 0, i });
}
}

References ARC_ConsoleView_GetBounds(), ARC_ConsoleView_RenderStringAt(), ARC_Vector_Get(), ARC_ConsoleBuffer::bufferLines, and ARC_Rect::h.