1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #include "String.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- 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;
- }
|