String.c (3103B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include "String.h" 6 7 size_t STRINGSPLITCOUNT = 0; 8 9 void __stringfree(string *self){ 10 free(self->_str); 11 free(self); 12 } 13 14 void __stringprint(string *self){ 15 printf("%s\n", self->_str); 16 } 17 18 void __stringinput(string *self, size_t len){ 19 char *tmp = calloc(1, len); 20 fgets(tmp, len, stdin); 21 self->fromcstring(self, tmp); 22 } 23 24 void __stringappendchar(string *self, char c){ 25 self->_len++; 26 self->_str = realloc(self->_str, self->_len); 27 self->_str[self->_len-1] = c; 28 } 29 30 int __stringinsert(string *self, string *substring, size_t point){ 31 if (point > self->_len) return 1; 32 33 size_t len = self->_len + substring->_len; 34 35 char *start = malloc(point); 36 char *end = malloc(self->_len - point); 37 38 int i; 39 for (i = 0; i < point; i++){ 40 start[i] = self->_str[i]; 41 } 42 43 int j = 0; 44 for (; i < self->_len; i++){ 45 end[j] = self->_str[i]; 46 j++; 47 } 48 49 self->_str = realloc(self->_str, len); 50 51 for (i = 0; i < strlen(start); i++){ 52 self->_str[i] = start[i]; 53 } 54 j = 0; 55 for (; j < substring->_len; i++){ 56 self->_str[i] = substring->_str[j]; 57 j++; 58 } 59 j = 0; 60 for (; i < len; i++){ 61 self->_str[i] = end[j]; 62 j++; 63 } 64 65 self->_len = len; 66 free(start); 67 free(end); 68 69 return 0; 70 } 71 72 void __stringreplacechar(string *self, char orig, char new){ 73 for (int i = 0; i < self->_len; i++) 74 if (self->_str[i] == orig) self->_str[i] = new; 75 } 76 77 int __stringcountchar(string *self, char c){ 78 int count = 0; 79 for (int i = 0; i < self->_len; i++) 80 if (self->_str[i] == c) count++; 81 82 return count; 83 } 84 85 int __stringcmp(string *str1, string *str2){ 86 return strcmp(str1->_str, str2->_str); 87 } 88 89 void __stringfromcstring(string *self, char *cstring){ 90 size_t len = strlen(cstring); 91 92 self->_str = realloc(self->_str, len); 93 94 memcpy(self->_str, cstring, len); 95 96 self->_len = len; 97 } 98 99 char *__stringtocstring(string *self){ 100 char *cstring = malloc(self->_len + 1); 101 memcpy(cstring, self->_str, self->_len); 102 cstring[self->_len + 1] = '\0'; 103 return cstring; 104 } 105 106 string **__stringsplit(string *self, char delim){ 107 int splitcount = self->countchar(self, delim) + 1; 108 string **strs = malloc(sizeof(string **)); 109 110 int j = 0; 111 for (int i = 0; i < splitcount; i++){ 112 char *str = calloc(1, self->_len); 113 int charcount = 0; 114 for (; self->_str[j] != delim && j < self->_len; j++){ 115 str[charcount] = self->_str[j]; 116 charcount++; 117 } 118 j++; 119 str[charcount+1] = '\0'; 120 strs[i] = String(str); 121 free(str); 122 } 123 124 STRINGSPLITCOUNT = splitcount; 125 return strs; 126 } 127 128 string *String(char *cstring){ // returns an allocated String from a C string input 129 size_t len = strlen(cstring); 130 string *str = malloc(sizeof(string)); 131 132 str->_str = calloc(1, len); 133 memcpy(str->_str, cstring, len); 134 str->_len = len; 135 136 str->free = &__stringfree; 137 str->appendchar = &__stringappendchar; 138 str->insert = &__stringinsert; 139 str->replacechar = &__stringreplacechar; 140 str->countchar = &__stringcountchar; 141 str->cmp = &__stringcmp; 142 str->fromcstring = &__stringfromcstring; 143 str->tocstring = &__stringtocstring; 144 str->split = &__stringsplit; 145 str->print = &__stringprint; 146 str->input = &__stringinput; 147 148 return str; 149 }