2017-03-07 22:52:11 +00:00
|
|
|
#include "strbuilder.h"
|
2017-03-11 17:00:23 +00:00
|
|
|
#include "../memory.h"
|
2017-03-12 00:18:08 +00:00
|
|
|
#include "../try.h"
|
|
|
|
#include "../exceptions/stdex.h"
|
2017-03-11 17:00:23 +00:00
|
|
|
|
2017-03-07 22:52:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
class_t Strbuilder_class;
|
2017-03-07 22:52:11 +00:00
|
|
|
|
2017-03-11 01:00:02 +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;
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
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);
|
2017-03-11 01:00:02 +00:00
|
|
|
this->super.destruct((Object_t*) this);
|
2017-03-07 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 01:00:02 +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)());
|
2017-03-11 17:00:23 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
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-11 17:00:23 +00:00
|
|
|
|
2017-03-07 22:52:11 +00:00
|
|
|
size_t length = this->length(this);
|
|
|
|
bool empty = this->string == NULL;
|
2017-03-11 17:00:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
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
|
|
|
|
2017-03-11 17:00:23 +00:00
|
|
|
Strbuilder_t* method(Strbuilder, construct)(const char* string) {
|
|
|
|
throws(OutOfMemoryException_t);
|
|
|
|
|
2017-03-12 17:58:29 +00:00
|
|
|
sr_(Strbuilder_t* obj = allocate_object(Strbuilder), NULL);
|
2017-03-09 15:24:26 +00:00
|
|
|
|
2017-03-11 01:00:02 +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
|
|
|
|
2017-03-11 01:00:02 +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);
|
|
|
|
|
2017-03-11 01:00:02 +00:00
|
|
|
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
|
|
|
}
|