#ifndef TYPESTRING_H
#define TYPESTRING_H
#include <stdlib.h>
#define STRING_MIN 1
#define STRING_MAX 4096
typedef struct
{
unsigned long Capacity;
unsigned long Length;
char* Data;
} typeString;
long stringCreate(typeString* str,unsigned long capacity);
long stringGetCString(typeString* str,char* buffer,unsigned long buffersize);
long stringSetLength(typeString* str,unsigned long length);
long stringSetCapacity(typeString* str,unsigned long capacity);
long stringReverse(typeString* str);
long stringReverseRange(typeString* str,unsigned long start,unsigned long length);
long stringAppendCharacter(typeString* str,char value);
long stringAppendCString(typeString* str,const char* value);
long stringAppendString(typeString* str,typeString* value);
long stringAppendInt64(typeString* str,unsigned long long value,long base);
long stringDestroy(typeString* str);
long stringCreate(typeString* str,unsigned long capacity)
{
char* buffer;
if (capacity < STRING_MIN)
capacity = STRING_MIN;
buffer = (char*)malloc(capacity);
if (buffer == 0)
return 0;
str->Capacity = capacity;
str->Length = 0;
str->Data = buffer;
return 1;
}
long stringGetCString(typeString* str,char* buffer,unsigned long buffersize)
{
unsigned long I;
I = 0;
while ((I < str->Length) && (I < buffersize-1))
{
buffer[I] = str->Data[I];
buffer[I+1] = 0;
I++;
}
return 1;
}
long stringSetLength(typeString* str,unsigned long length)
{
if (length > str->Capacity)
return 0;
str->Length = length;
return 1;
}
long stringSetCapacity(typeString* str,unsigned long capacity)
{
unsigned long I;
char* buffer;
if (capacity == str->Capacity)
return 1;
if (capacity < STRING_MIN)
capacity = STRING_MIN;
buffer = (char*)malloc(capacity);
if (buffer == 0)
return 0;
I = 0;
while (I < capacity)
{
if (I < str->Length)
buffer[I] = str->Data[I];
else
buffer[I] = 0;
}
free(str->Data);
if (str->Length > capacity)
str->Length = capacity;
str->Capacity = capacity;
str->Data = buffer;
return 1;
}
long stringReverse(typeString* str)
{
unsigned long I,J;
char temp;
I = 0;
J = str->Length - 1;
while (I < J)
{
temp = str->Data[I];
str->Data[I] = str->Data[J];
str->Data[J] = temp;
I++;
J--;
}
return 1;
}
long stringReverseRange(typeString* str,unsigned long start,unsigned long length)
{
unsigned long I,J;
char temp;
I = start;
J = start + length - 1;
if ((I < 0) || (J > str->Length))
return 0;
while (I < J)
{
temp = str->Data[I];
str->Data[I] = str->Data[J];
str->Data[J] = temp;
I++;
J--;
}
return 1;
}
long stringAppendCharacter(typeString* str,char value)
{
if (str->Length >= str->Capacity)
return 0;
str->Data[str->Length] = value;
str->Length++;
return 1;
}
long stringAppendCString(typeString* str,const char* value)
{
unsigned long I,J;
I = str->Length;
J = 0;
while ((value[J] != 0) &&
(J < STRING_MAX) &&
(I < str->Capacity))
{
str->Data[I] = value[J];
I++;
J++;
}
str->Length = I;
return 1;
}
long stringAppendString(typeString* str,typeString* value)
{
unsigned long I,J;
I = str->Length;
J = 0;
while ((I < str->Capacity) && (J < value->Length))
{
str->Data[I] = value->Data[J];
I++;
J++;
}
str->Length = I;
return 1;
}
long stringAppendInt64(typeString* str,unsigned long long value,long base)
{
static const char digits[] = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz-_";
unsigned long start,length;
if ((base < 2) || (base > 64))
return 0;
start = str->Length;
do
{
stringAppendCharacter(str,digits[value % base]);
value = value/base;
}
while (value != 0);
length = str->Length - start;
stringReverseRange(str,start,length);
return 1;
}
long stringDestroy(typeString* str)
{
free(str->Data);
return 1;
}
#endif // TYPESTRING_H
Saturday, June 11, 2016
Source code for a string builder class
This one is a string class. I should really call it string builder, but that name is just too long. At some point I am going to explain how each part of the code works, but I really don't feel like writing right now. I am including the full source code here just so I can refer to it later. None of these code files are really that good, they are just some quick stuff that I made without much thought.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment