#include "String.h" #include #include #include void stringInit(String* s) { s->length = 0; s->data = NULL; } void stringRemove(String* s) { s->length = 0; if(s->data != NULL) { free(s->data); s->data = NULL; } } int stringGetLength(String* s) { return s->length; } void stringRead(String* s) { int buffer = 16; int index = 0; char* chars = malloc(sizeof(char) * buffer); while(1) { int c = fgetc(stdin); if(c == EOF || c == '\n') { break; } if(index == buffer) { char* newBuffer = malloc(sizeof(char) * buffer * 2); memcpy(newBuffer, chars, buffer); free(chars); chars = newBuffer; buffer *= 2; } chars[index] = c; index++; } s->length = index; s->data = malloc(sizeof(char) * (index + 1)); memcpy(s->data, chars, index); free(chars); s->data[index] = '\0'; } int stringCompare(String* s, const char* chars) { return strcmp(s->data, chars) == 0; }