Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. TB newTB(char *text) {
  2. // Intializing textbuffer
  3. TB new = malloc(sizeof(struct textbuffer));
  4.  
  5. new->head = NULL;
  6. new->tail = NULL;
  7. new->size = 0;
  8.  
  9. if (text == NULL) {
  10. return new;
  11. }
  12.  
  13. struct node *curr = NULL;
  14. struct node *temp = NULL;
  15. int offset = 0;
  16.  
  17. while (text[offset] != '\0') {
  18. struct node *newNode = malloc(sizeof(*newNode));
  19. char *words = split (text, offset);
  20. // For the very first node in the tb
  21. if (new->head == NULL) {
  22. newNode->line = words;
  23. newNode->prev = NULL;
  24. newNode->next = NULL;
  25. new->head = newNode;
  26. new->tail = newNode;
  27. new->size = 1;
  28. curr = new->head;
  29. }
  30. // For all other nodes
  31. else {
  32. newNode->line = words;
  33. newNode->next = NULL;
  34. newNode->prev = curr;
  35. curr->next = newNode;
  36. temp = curr;
  37. curr = curr->next;
  38. curr->prev = temp;
  39. new->tail = curr;
  40. new->size++;
  41. }
  42. offset = offset + strlen(words) + 1;
  43. free(words);
  44. }
  45. return new;
  46. }
  47.  
  48. // Split function that tokenises the string
  49. char *split (char *string, int offset) {
  50. int counter = offset;
  51. int counter1 = 0;
  52. char *splitWords = calloc(strlen(string), sizeof(char));
  53. while (string[counter] != '\0') {
  54. splitWords[counter1] = string[counter];
  55. counter++;
  56. counter1++;
  57. }
  58.  
  59. return splitWords;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement