Files
degvi/Line.c
2013-01-31 07:30:12 +09:00

184 lines
3.4 KiB
C

#include "init.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Line.h"
Line Line_create(void)
{
Line this;
this = malloc(sizeof(struct Line_struct));
if (this == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
this->maxSize = 0;
this->size = 0;
this->characters = malloc(sizeof(Character));
if (this->characters == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
return this;
}
void Line_destroy(Line this)
{
if (this == NULL) {
return;
}
free(this->characters);
free(this);
}
Line Line_clone(Line this)
{
Line line;
int i;
line = Line_create();
for (i = 0; i < Line_size(this); i++) {
Line_addCharacter(line, Line_size(line), Line_getCharacter(this, i));
}
return line;
}
int Line_size(Line this)
{
return this->size;
}
void Line_clear(Line this)
{
this->size = 0;
}
Character Line_getCharacter(Line this, int index)
{
assert(index >= 0 && index < Line_size(this));
return *(this->characters + index);
}
void Line_addCharacter(Line this, int index, Character character)
{
assert(index >= 0 && index <= Line_size(this));
if (this->size + 1 > this->maxSize) {
this->maxSize += LINE_BUFFER_INCREMENT_SIZE;
this->characters = realloc(this->characters, sizeof(Character) * (this->maxSize + 1));
if (this->characters == NULL) {
fprintf(stderr, "realloc failed\n");
exit(EXIT_FAILURE);
}
}
if (index < this->size) {
memmove(this->characters + index + 1, this->characters + index, sizeof(Character) * (this->size - index));
}
*(this->characters + index) = character;
this->size++;
}
Character Line_removeCharacter(Line this, int index)
{
Character character;
assert(index >= 0 && index < Line_size(this));
character = Line_getCharacter(this, index);
memmove(this->characters + index, this->characters + index + 1, sizeof(Character) * (this->size - index - 1));
this->size--;
return character;
}
void Line_addLine(Line this, int index, Line line)
{
int i;
assert(index >= 0 && index <= Line_size(this));
for (i = 0; i < Line_size(line); i++) {
Line_addCharacter(this, index + i, Line_getCharacter(line, i));
}
}
Line Line_subLine(Line this, int index, int size)
{
Line line;
int i;
assert(index >= 0 && index <= Line_size(this));
assert(size >= 0 && index + size <= Line_size(this));
line = Line_create();
for (i = 0; i < size; i++) {
Line_addCharacter(line, Line_size(line), Line_getCharacter(this, index + i));
}
return line;
}
Line Line_removeRange(Line this, int index, int size)
{
Line line;
int i;
assert(index >= 0 && index <= Line_size(this));
assert(size >= 0 && index + size <= Line_size(this));
line = Line_create();
for (i = 0; i < size; i++) {
Line_addCharacter(line, Line_size(line), Line_removeCharacter(this, index));
}
return line;
}
char *Line_toString(Line this)
{
int length;
int i;
char buf[6 + 1];
char *string;
char *s;
length = 0;
for (i = 0; i < this->size; i++) {
Character_toString(Line_getCharacter(this, i), buf);
length += strlen(buf);
}
string = malloc(sizeof(char) * (length + 1));
if (string == NULL) {
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
}
s = string;
for (i = 0; i < this->size; i++) {
Character_toString(Line_getCharacter(this, i), s);
for (; *s != '\0'; s++);
}
return string;
}
Character *Line_toRawString(Line this)
{
*(this->characters + this->size) = 0;
return this->characters;
}