crap-libs/tools/strbuilder.c

137 lines
3.1 KiB
C
Raw Normal View History

2017-03-07 22:52:11 +00:00
#include "strbuilder.h"
#include "../memory.h"
2017-03-12 00:18:08 +00:00
#include "../try.h"
#include "../exceptions/stdex.h"
2017-03-07 22:52:11 +00:00
#include <string.h>
#include <stdbool.h>
class_t Strbuilder_class;
2017-03-07 22:52:11 +00:00
void method(Strbuilder, clear)(Strbuilder_t* this) {
2017-03-12 00:18:08 +00:00
throws(NullPointerException_t);
if (this == NULL)
2017-04-14 22:12:54 +00:00
throw(new (NullPointerException)());
2017-03-12 00:18:08 +00:00
2017-03-07 22:52:11 +00:00
for (int i = 0; i < this->nrstrings; i++) {
free(this->strings[i]);
}
free(this->string);
this->string = NULL;
this->nrstrings = 0;
}
void method(Strbuilder, destruct)(Strbuilder_t* this) {
2017-03-12 00:18:08 +00:00
throws(NullPointerException_t);
if (this == NULL)
2017-04-14 22:12:54 +00:00
throw(new (NullPointerException)());
2017-03-12 00:18:08 +00:00
2017-03-07 22:52:11 +00:00
this->clear(this);
this->super.destruct((Object_t*) this);
2017-03-07 22:52:11 +00:00
}
void method(Strbuilder, add)(Strbuilder_t* this, const char* string) {
2017-03-12 00:18:08 +00:00
throws(OutOfMemoryException_t, NullPointerException_t);
if (this == NULL || string == NULL)
2017-04-14 22:12:54 +00:00
throw(new (NullPointerException)());
s_(this->strings = reallocate(this->strings, ++this->nrstrings * sizeof(char*)));
s_(this->strings[this->nrstrings - 1] = allocate(strlen(string) + 1));
2017-03-07 22:52:11 +00:00
strcpy(this->strings[this->nrstrings - 1], string);
}
size_t method(Strbuilder, length)(Strbuilder_t* this) {
2017-03-12 00:18:08 +00:00
throws(NullPointerException_t);
if (this == NULL)
2017-04-14 22:12:54 +00:00
throwr(new (NullPointerException)(), 0);
2017-03-12 00:18:08 +00:00
2017-03-07 22:52:11 +00:00
size_t length = 0;
if (this->string != NULL)
length = strlen(this->string);
for (int i = 0; i < this->nrstrings; i++) {
length += strlen(this->strings[i]);
}
return length;
}
void method(Strbuilder, build)(Strbuilder_t* this) {
2017-03-12 00:18:08 +00:00
throws(OutOfMemoryException_t, NullPointerException_t);
if (this == NULL)
2017-04-14 22:12:54 +00:00
throw(new (NullPointerException)());
2017-03-07 22:52:11 +00:00
size_t length = this->length(this);
bool empty = this->string == NULL;
s_(this->string = reallocate(this->string, length + 1));
2017-03-07 22:52:11 +00:00
if (empty)
this->string[0] = '\0';
for (int i = 0; i < this->nrstrings; i++) {
strcat(this->string, this->strings[i]);
free(this->strings[i]);
}
this->nrstrings = 0;
}
const char* method(Strbuilder, get)(Strbuilder_t* this) {
2017-03-12 00:18:08 +00:00
throws(NullPointerException_t);
if (this == NULL)
2017-04-14 22:12:54 +00:00
throwr(new (NullPointerException)(), NULL);
2017-03-12 00:18:08 +00:00
if (this->string == NULL)
return "";
2017-03-07 22:52:11 +00:00
return this->string;
}
2017-04-14 22:12:54 +00:00
Strbuilder_t* method(Strbuilder, construct)(const char* string) {
throws(OutOfMemoryException_t);
sr_(Strbuilder_t* obj = allocate_object(Strbuilder), NULL);
2017-03-09 15:24:26 +00:00
populate(Strbuilder)(obj, Strbuilder_class);
2017-03-09 15:24:26 +00:00
2017-03-12 00:18:08 +00:00
if (string != NULL) {
sr_(obj->string = allocate(strlen(string) + 1), NULL);
strcpy(obj->string, string);
}
2017-03-09 15:24:26 +00:00
return obj;
}
2017-04-14 22:12:54 +00:00
int method(Strbuilder, hashCode)(void* obj) {
throws(NullPointerException_t);
if (obj == NULL)
throwr(new (NullPointerException)(), 0);
Strbuilder_t* builder = (Strbuilder_t*) obj;
UNUSED(builder);
return 0;
}
2017-03-09 15:24:26 +00:00
void method(Strbuilder, populate)(Strbuilder_t* obj, class_t c) {
populate(Object)((Object_t*) obj, c);
2017-03-09 15:24:26 +00:00
obj->string = NULL;
2017-03-07 22:52:11 +00:00
obj->strings = NULL;
obj->nrstrings = 0;
2017-04-14 22:12:54 +00:00
add_hashcode(obj);
override_method(obj, Object, Strbuilder, hashCode);
add_method(obj, Strbuilder, destruct);
add_method(obj, Strbuilder, add);
add_method(obj, Strbuilder, build);
add_method(obj, Strbuilder, get);
add_method(obj, Strbuilder, clear);
add_method(obj, Strbuilder, length);
2017-03-07 22:52:11 +00:00
}