#include "String.h" #include "Stream.h" #include #include #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 stringReadStreamLine(String* s, Stream* in) { stringRemove(s); int length = streamSeekNewLine(in); s->data = malloc(sizeof(char) * (length + 1)); for(int i = 0; i < length; i++) { streamGetChar(in, &s->data[i]); } char c; streamGetChar(in, &c); // remove \n s->data[length] = '\0'; s->length = length; } void stringRead(String* s) { stringRemove(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; } int stringCompareNoCase(String* s, const char* chars) { return strcasecmp(s->data, chars) == 0; }