Tools made in assistance of the Metacall Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.1 KiB

  1. #include "tstring.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. tstring* tstring_create(size_t sz) {
  8. tstring* buffer = malloc(sizeof(tstring)+sz);
  9. memset(buffer, 0, sizeof(tstring)+sz);
  10. buffer->size = sz;
  11. return buffer;
  12. }
  13. tstring* tstring_copy(tstring* src) {
  14. tstring* buffer = tstring_create(src->size);
  15. memcpy(buffer->data, src->data, src->size+1);
  16. return buffer;
  17. }
  18. tstring* cstring_to_tstring(char* src) {
  19. size_t sz = strlen(src);
  20. tstring* buffer = tstring_create(sz);
  21. memcpy(buffer->data, src, sz);
  22. buffer->size = sz;
  23. return buffer;
  24. }
  25. tstring* integer_to_tstring(int64_t val) {
  26. char conversion[20];
  27. bool sign = val < 0;
  28. char* it = conversion;
  29. if(sign) {
  30. val *= -1;
  31. }
  32. if(val == 0) {
  33. tstring* ret = tstring_create(1);
  34. ret->data[0] = '0';
  35. return ret;
  36. }
  37. while(val != 0) {
  38. *(it++) = val%10+'0';
  39. val/=10;
  40. }
  41. tstring* ret = tstring_create((it-conversion) + sign);
  42. char* dest_it = ret->data;
  43. if(sign) {
  44. *(dest_it++) = '-';
  45. }
  46. do {
  47. *(dest_it++) = *(it--);
  48. } while(it != conversion);
  49. return ret;
  50. }
  51. tstring* tstring_concatenate(tstring* lhs, tstring* rhs) {
  52. tstring* ret = tstring_create(lhs->size + rhs->size);
  53. memcpy(ret->data, lhs->data, lhs->size);
  54. memcpy(ret->data + lhs->size, rhs->data, rhs->size);
  55. return ret;
  56. }
  57. tstring* tstring_n_concatenate(size_t count, ...) {
  58. va_list strs;
  59. va_start(strs, count);
  60. size_t tot_sz = 0;
  61. size_t idx;
  62. for(idx = 0; idx < count; idx++) {
  63. tot_sz += va_arg(strs, tstring*)->size;
  64. }
  65. tstring* ret = tstring_create(tot_sz);
  66. va_end(strs);
  67. va_start(strs, count);
  68. char* it = ret->data;
  69. for(idx = 0; idx < count; idx++) {
  70. tstring* curr = va_arg(strs, tstring*);
  71. memcpy(it, curr->data, curr->size);
  72. it+=curr->size;
  73. }
  74. va_end(strs);
  75. ret->size = tot_sz;
  76. return ret;
  77. }
  78. int tstring_n_write(FILE* file, size_t count, ...) {
  79. va_list strs;
  80. va_start(strs, count);
  81. size_t idx;
  82. for(idx = 0; idx < count; idx++) {
  83. tstring* curr = va_arg(strs, tstring*);
  84. if(fwrite(curr->data,1, curr->size, file) != curr->size) {
  85. return ferror(file);
  86. }
  87. }
  88. va_end(strs);
  89. return 0;
  90. }
  91. tstring* tstring_n_compose(const char* count, ...) {
  92. va_list strs;
  93. size_t nb = strlen(count);
  94. va_start(strs, count);
  95. size_t tot_sz = 0;
  96. size_t idx;
  97. for(idx = 0; idx < nb; idx++) {
  98. switch(count[idx]){
  99. case 't':
  100. tot_sz += va_arg(strs, tstring*)->size;
  101. break;
  102. case 'c':
  103. tot_sz += strlen(va_arg(strs, char*));
  104. break;
  105. default:
  106. va_end(strs);
  107. exit(EXIT_FAILURE);
  108. break;
  109. }
  110. }
  111. tstring* ret = tstring_create(tot_sz);
  112. char* it = ret->data;
  113. va_end(strs);
  114. va_start(strs, count);
  115. for(idx = 0; idx < nb; idx++) {
  116. switch(count[idx]){
  117. case 't':
  118. {
  119. tstring* curr = va_arg(strs, tstring*);
  120. memcpy(it, curr->data, curr->size);
  121. it+=curr->size;
  122. }
  123. break;
  124. case 'c':
  125. {
  126. char* curr = va_arg(strs, char*);
  127. size_t sz = strlen(curr);
  128. memcpy(it, curr, sz);
  129. it+=sz;
  130. }
  131. break;
  132. default:
  133. perror("Invalid tstring concatenation");
  134. exit(EXIT_FAILURE);
  135. break;
  136. }
  137. }
  138. va_end(strs);
  139. ret->size = tot_sz;
  140. return ret;
  141. }
  142. void tstring_destroy(tstring* del) {
  143. free(del);
  144. }