<commit_before>#include <stdlib.h>
#include <curses.h>

#include "zalloc.h"
#include "error.h"
#include "buffer.h"

void init_buffers(void)
{
	TAILQ_INIT(&buffers_head);
}

struct te_buffer *alloc_and_insert_buffer(void)
{
	struct te_buffer *b = zalloc(sizeof(struct te_buffer));
	
	if (b == NULL)
		fail("Unable to allocate memory for te_buffer struct");

	TAILQ_INSERT_TAIL(&buffers_head, b, buffers);

	b->x = 0;
	b->y = 0;
	b->point = 0;

	return b;
}

void free_buffer(struct te_buffer *b)
{
	if (b == NULL)
		fail("The parameter to free_buffer() is NULL");

	TAILQ_REMOVE(&buffers_head, b, buffers);

	free(b);
}

struct te_buffer* load_buffer(char *filename)
{
	FILE *fp = fopen(filename, "r");

	if (fp == NULL)
		fail("Unable to open %s", filename);

	struct te_buffer *b = alloc_and_insert_buffer();

	b->contents = bfromcstr("");
	if (b->contents == NULL)
		fail("Unable to allocate memory for buffer contents");

	if (breada(b->contents, (bNread) &fread, fp) != 0)
		fail("Unable to read file contents");
	
	statusprintf("%s", filename);

	return b;
}

/* Compute the length of a string between two '\n' */
int line_length(bstring b, int point)
{
	if (b == NULL)
		return -1;

	int c_offset = bstrchrp(b, '\n', point);
	if (c_offset == BSTR_ERR)
		c_offset = blength(b);

	int p_offset = bstrrchrp(b, '\n', c_offset - 1);
	if (p_offset == BSTR_ERR)
		p_offset = 0;

	return c_offset - p_offset;
}

/*
  The length of a line as displayed on the screen.
 */
int screen_line_length(bstring b, int point)
{
	if (b == NULL)
		return -1;

	int len = line_length(b, point);
	int i = 0;
	int count = 0;

	for (i = 0; i < len; i++) {
		if (bchar(b, point - i) == '\t')
			count += TAB_LEN;
		else
			count += 1;
	}

}

/*
  accessor and mutators for the contents of the buffer.

 */
int prev_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point - 1);
}

int curr_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point);
}

int next_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point + 1);
}

int move_left(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	if (buf->point > 0)
		buf->point--;
	else
		return ERR;

	return OK;
}


int move_right(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	if (buf->point < blength(buf->contents))
		buf->point++;
	else
		return ERR;

	return OK;
}


<commit_msg>added a function to insert a char in a buffer<commit_after>#include <stdlib.h>
#include <curses.h>

#include "zalloc.h"
#include "error.h"
#include "buffer.h"

void init_buffers(void)
{
	TAILQ_INIT(&buffers_head);
}

struct te_buffer *alloc_and_insert_buffer(void)
{
	struct te_buffer *b = zalloc(sizeof(struct te_buffer));
	
	if (b == NULL)
		fail("Unable to allocate memory for te_buffer struct");

	TAILQ_INSERT_TAIL(&buffers_head, b, buffers);

	b->x = 0;
	b->y = 0;
	b->point = 0;

	return b;
}

void free_buffer(struct te_buffer *b)
{
	if (b == NULL)
		fail("The parameter to free_buffer() is NULL");

	TAILQ_REMOVE(&buffers_head, b, buffers);

	free(b);
}

struct te_buffer* load_buffer(char *filename)
{
	FILE *fp = fopen(filename, "r");
	
	if (fp == NULL)
		fail("Unable to open %s", filename);

	struct te_buffer *b = alloc_and_insert_buffer();

	b->name = strdup(filename);
	b->contents = bfromcstr("");
	if (b->contents == NULL)
		fail("Unable to allocate memory for buffer contents");

	if (breada(b->contents, (bNread) &fread, fp) != 0)
		fail("Unable to read file contents");
	
	statusprintf("%s", filename);

	return b;
}

/* Compute the length of a string between two '\n' */
int line_length(bstring b, int point)
{
	if (b == NULL)
		return -1;

	int c_offset = bstrchrp(b, '\n', point);
	if (c_offset == BSTR_ERR)
		c_offset = blength(b);

	int p_offset = bstrrchrp(b, '\n', c_offset - 1);
	if (p_offset == BSTR_ERR)
		p_offset = 0;

	return c_offset - p_offset;
}

/*
  The length of a line as displayed on the screen.
 */
int screen_line_length(bstring b, int point)
{
	if (b == NULL)
		return -1;

	int len = line_length(b, point);
	int i = 0;
	int count = 0;

	for (i = 0; i < len; i++) {
		if (bchar(b, point - i) == '\t')
			count += TAB_LEN;
		else
			count += 1;
	}

}

/*
  accessor and mutators for the contents of the buffer.

 */
int prev_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point - 1);
}

int curr_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point);
}

int next_char(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	return bchar(buf->contents, buf->point + 1);
}

int move_left(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	if (buf->point > 0)
		buf->point--;
	else
		return ERR;

	return OK;
}


int move_right(struct te_buffer *buf)
{
	if (buf == NULL)
		return;

	if (buf->point < blength(buf->contents))
		buf->point++;
	else
		return ERR;

	return OK;
}

void insert_char(struct te_buffer *buf, char c)
{
	if (buf == NULL)
		return;

	binsertch(buf->contents, buf->point, 1, c);

	return;
}



{'fraction_non_alphanumeric': 0.15643991374240346, 'fraction_numerical': 0.00607723975691041, 'mean_word_length': 3.6636197440585008, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '150122', 'n_tokens_mistral': 2137, 'n_tokens_neox': 1996, 'n_words': 572}