Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TB newTB(char *text) {
- // Intializing textbuffer
- TB new = malloc(sizeof(struct textbuffer));
- new->size = 0;
- new->head = NULL;
- new->tail = NULL;
- // Checking for error cases
- if (text == NULL) {
- printf("Error! Empty text provided.\n");
- abort();
- }
- if (text[0] == '\0') {
- return new;
- }
- // Defining pointers
- struct node *curr = NULL;
- struct node *temp = NULL;
- int offset = 0;
- // While the text hasnt reached null terminator
- while (text[offset] != '\0') {
- // Allocate space for a new node
- struct node *newNode = malloc(sizeof(*newNode));
- char *words = split(text, offset);
- offset = offset + strlen(words) + 1;
- // For the very first node in the tb
- if (new->head == NULL) {
- // Localises string
- newNode->line = malloc(sizeof(char) * strlen(words) + 1);
- strcpy(newNode->line, words);
- newNode->prev = NULL;
- newNode->next = NULL;
- new->head = newNode;
- new->tail = newNode;
- new->size = 1;
- curr = new->head;
- }
- // For all other nodes
- else {
- newNode->line = malloc(sizeof(char) * strlen(words) + 1);
- strcpy(newNode->line, words);
- newNode->next = NULL;
- newNode->prev = curr;
- curr->next = newNode;
- temp = curr;
- curr = curr->next;
- curr->prev = temp;
- new->tail = curr;
- new->size++;
- }
- free(words);
- }
- return new;
- }
- // Split function that tokenises the string
- static char *split(char *string, int offset) {
- int counter = offset;
- int counter1 = 0;
- char *splitWords = calloc(strlen(string), sizeof(char));
- while (string[counter] != '\n') {
- splitWords[counter1] = string[counter];
- counter++;
- counter1++;
- }
- return splitWords;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement