1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
typedef char *str;
struct __attribute__ ((__packed__)) strstruct {
int length;
int allocated;
volatile char buff[];
};
using namespace std;
static inline size_t getLength(const str string) {
return (size_t) (((struct strstruct*)(string)) - sizeof(struct strstruct))->length;
}
static inline void setLength(const str string, int newLength) {
(((struct strstruct*)(string)) - sizeof(struct strstruct))->length = newLength;
}
static inline size_t getAllocated(const str string) {
return (size_t) (((struct strstruct*)(string)) - sizeof(struct strstruct))->allocated;
}
static inline void setAllocated(const str string, int newAllocated) {
(((struct strstruct*)(string)) - sizeof(struct strstruct))->allocated = newAllocated;
}
#define STRLENGTH (sizeof(strstruct))
str strNew(char *init) {
return strNewLength(init, strLength(init));
}
str strNewLength(char *init, int length) {
void *pointer = malloc(STRLENGTH + length + 1);
// memset(pointer, 0, STRLENGTH + length + 1);
str string = ((str) pointer) + STRLENGTH;
setLength(string, length);
setAllocated(string, length);
if (init != NULL)
memcpy(string, init, (size_t) length);
string[length] = '\0';
return string;
}
str strSubString(str string, int startIndex, int endIndex) {
if (endIndex == -1 || startIndex == -1)
return NULL;
return strNewLength(string + startIndex, endIndex - startIndex);
}
int strIndexOf(str string, char divisor, int index) {
for (int i = 0; i < getLength(string); i++) {
if (string[i] == divisor) {
if (index == 0) {
return i;
}
--index;
}
}
return -1;
}
int strNumberFound(str string, char divisor) {
int num = 0;
int length = getLength(string);
for (int i = 0; i < length; ++i) {
cout << string[i] << endl;
if (string[i] == divisor)
++num;
}
return num;
}
str strAppend(str original, char *other, int otherLength) {
int length = getLength(original);
int allocated = getAllocated(original);
if (allocated < length + otherLength) {
void *pointer = malloc(STRLENGTH + length + otherLength + 1);
str string = (char *) pointer + STRLENGTH;
memcpy(string, original, (size_t) length);
memcpy(string + length, other, (size_t) otherLength);
string[length + otherLength] = '\0';
setLength(string, length + otherLength);
strFree(original);
return string;
} else {
memcpy(original + length, other, (size_t) otherLength);
setLength(original, length + otherLength);
original[length + otherLength] = '\0';
return original;
}
}
str *strSplit(str string, char divisor, int &length) {
int numFound = strNumberFound(string, divisor);
str* array = new str[numFound + 1];
int previousEnd = 0;
length = 0;
int startIndex;
int endIndex;
for (int i = 0; i < numFound + 1; ++i) {
startIndex = previousEnd + (i == 0 ? 0 : 1);
endIndex = strIndexOf(string, divisor, i);
array[i] = strSubString(string, startIndex, endIndex == -1 ? getLength(string) : (size_t) endIndex);
previousEnd = endIndex;
++length;
}
return array;
}
void strFree(str string) {
if (string == NULL)
return;
//memoryHexDump(string - 2, 10);
// cout << "String length " << getLength(string) << endl;
// for(int i = 0; i < getLength(string) + 1; i++) {
// cout << (int)(string[i]) << "|";
// }
free(string - STRLENGTH);
string = nullptr;
}
|