Cleaned up some code and set up some TString functions.
This commit is contained in:
@@ -1,32 +1,33 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "tstring.h"
|
||||||
|
|
||||||
#define DEFAULT_STRING_SIZE 32
|
// #define DEFAULT_STRING_SIZE 32
|
||||||
|
|
||||||
typedef struct __string {
|
// typedef struct __string {
|
||||||
char* Characters;
|
// char* Characters;
|
||||||
int Length;
|
// int Length;
|
||||||
int ByteCount;
|
// int ByteCount;
|
||||||
int Capacity;
|
// int Capacity;
|
||||||
} TString;
|
// } TString;
|
||||||
|
|
||||||
int GetGlyphByteWidth(const char* glyph);
|
// int GetGlyphByteWidth(const char* glyph);
|
||||||
void AppendGlyph(const char* glyph, TString* string);
|
// void AppendGlyph(const char* glyph, TString* string);
|
||||||
void GetCharByIndex(int index, char characters[8], const TString* string);
|
// void GetCharByIndex(int index, char characters[8], const TString* string);
|
||||||
TString* CreateString(void);
|
// TString* CreateString(void);
|
||||||
|
|
||||||
const unsigned char First = 0x70;
|
// const unsigned char First = 0x70;
|
||||||
const unsigned char Teo = 0xE0;
|
// const unsigned char Teo = 0xE0;
|
||||||
const unsigned char Three = 0xF0;
|
// const unsigned char Three = 0xF0;
|
||||||
const unsigned char Four = 0xF7;
|
// const unsigned char Four = 0xF7;
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
char glyph[10] = { 0 };
|
char glyph[10] = { 0 };
|
||||||
char characters[8] = { 0 };
|
char characters[8] = { 0 };
|
||||||
|
|
||||||
int index = 2;
|
int index = 2;
|
||||||
TString* string = CreateString();
|
TString* string = TStringCreate();
|
||||||
//0xF0 0xA0 0xBA 0x8C
|
//0xF0 0xA0 0xBA 0x8C
|
||||||
char* test = u8"\xE5\x87\x84";
|
char* test = u8"\xE5\x87\x84";
|
||||||
char* test2 = u8"\xE3\x81\x84";
|
char* test2 = u8"\xE3\x81\x84";
|
||||||
@@ -34,17 +35,17 @@ int main(int argc, char** argv) {
|
|||||||
char* test5 = "B";
|
char* test5 = "B";
|
||||||
char* test3 = " Just some\0 extra text";
|
char* test3 = " Just some\0 extra text";
|
||||||
|
|
||||||
AppendGlyph(test, string);
|
TStringAppendText(test, string);
|
||||||
AppendGlyph(test2, string);
|
TStringAppendText(test2, string);
|
||||||
AppendGlyph(test4, string);
|
TStringAppendText(test4, string);
|
||||||
AppendGlyph(test5, string);
|
TStringAppendText(test5, string);
|
||||||
|
|
||||||
GetCharByIndex(index, characters, string);
|
//GetCharByIndex(index, characters, string);
|
||||||
GetCharByIndex(3, glyph, string);
|
//GetCharByIndex(3, glyph, string);
|
||||||
|
|
||||||
printf("From string index %d: '%s' (string length is %d)\n", index, characters, string->Length);
|
// printf("From string index %d: '%s' (string length is %d)\n", index, characters, string->Length);
|
||||||
printf("And one English character: '%s'\n", glyph);
|
// printf("And one English character: '%s'\n", glyph);
|
||||||
printf("Full string: '%s'\n", string->Characters);
|
printf("Full string: '%s'\n", TStringGetString(string));
|
||||||
|
|
||||||
//fgets(glyph, 10, stdin);
|
//fgets(glyph, 10, stdin);
|
||||||
|
|
||||||
@@ -53,65 +54,65 @@ int main(int argc, char** argv) {
|
|||||||
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
|
//printf("Width %d byte(s).\n", GetGlyphByteWidth(glyph));
|
||||||
}
|
}
|
||||||
|
|
||||||
TString* CreateString(void) {
|
// TString* CreateString(void) {
|
||||||
TString* string = malloc(sizeof(TString));
|
// TString* string = malloc(sizeof(TString));
|
||||||
|
|
||||||
string->Characters = calloc(DEFAULT_STRING_SIZE, sizeof(char));
|
// string->Characters = calloc(DEFAULT_STRING_SIZE, sizeof(char));
|
||||||
|
|
||||||
string->Capacity = DEFAULT_STRING_SIZE;
|
// string->Capacity = DEFAULT_STRING_SIZE;
|
||||||
string->ByteCount = 0;
|
// string->ByteCount = 0;
|
||||||
string->Length = 0;
|
// string->Length = 0;
|
||||||
|
|
||||||
return string;
|
// return string;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void AppendGlyph(const char* glyph, TString* string) {
|
// void AppendGlyph(const char* glyph, TString* string) {
|
||||||
int length = strlen(glyph);
|
// int length = strlen(glyph);
|
||||||
|
|
||||||
memcpy(&string->Characters[string->ByteCount], glyph, length);
|
// memcpy(&string->Characters[string->ByteCount], glyph, length);
|
||||||
|
|
||||||
string->ByteCount += length;
|
// string->ByteCount += length;
|
||||||
string->Length++;
|
// string->Length++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
void GetCharByIndex(int index, char characters[8], const TString* string) {
|
// void GetCharByIndex(int index, char characters[8], const TString* string) {
|
||||||
int glyphCount = 0;
|
// int glyphCount = 0;
|
||||||
|
|
||||||
for(int i = 0; i < string->ByteCount;) {
|
// for(int i = 0; i < string->ByteCount;) {
|
||||||
if (glyphCount == index) {
|
// if (glyphCount == index) {
|
||||||
memcpy(characters, &string->Characters[i], GetGlyphByteWidth(&string->Characters[i]));
|
// memcpy(characters, &string->Characters[i], GetGlyphByteWidth(&string->Characters[i]));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
glyphCount++;
|
// glyphCount++;
|
||||||
i += GetGlyphByteWidth(&string->Characters[i]);
|
// i += GetGlyphByteWidth(&string->Characters[i]);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
int GetGlyphByteWidth(const char* glyph) {
|
// int GetGlyphByteWidth(const char* glyph) {
|
||||||
printf("Bytes for '%s': ", glyph);
|
// printf("Bytes for '%s': ", glyph);
|
||||||
|
|
||||||
for(size_t i = 0; i < strlen(glyph); i++) {
|
// for(size_t i = 0; i < strlen(glyph); i++) {
|
||||||
printf("%02X ", 0xFF & glyph[i]);
|
// printf("%02X ", 0xFF & glyph[i]);
|
||||||
}
|
// }
|
||||||
printf("\n");
|
// printf("\n");
|
||||||
|
|
||||||
unsigned char result = *glyph & 0xFF;
|
// unsigned char result = *glyph & 0xFF;
|
||||||
|
|
||||||
if ((*glyph & 0x80) == 0) {
|
// if ((*glyph & 0x80) == 0) {
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
else if ((*glyph & 0xE0) == 0xC0) {
|
// else if ((*glyph & 0xE0) == 0xC0) {
|
||||||
return 2;
|
// return 2;
|
||||||
}
|
// }
|
||||||
else if ((*glyph & 0xF0) == 0xE0) {
|
// else if ((*glyph & 0xF0) == 0xE0) {
|
||||||
return 3;
|
// return 3;
|
||||||
}
|
// }
|
||||||
else if ((*glyph & 0xF8) == 0xF0) {
|
// else if ((*glyph & 0xF8) == 0xF0) {
|
||||||
return 4;
|
// return 4;
|
||||||
}
|
// }
|
||||||
|
|
||||||
fprintf(stderr, "Failed to get glyph %02X\n", 0xFF & *glyph);
|
// fprintf(stderr, "Failed to get glyph %02X\n", 0xFF & *glyph);
|
||||||
|
|
||||||
exit(-1);
|
// exit(-1);
|
||||||
}
|
// }
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
#include "tstring.h"
|
#include "tstring.h"
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int GetGlyphByteWidth(const char* glyph);
|
||||||
|
void TStringCopyGlyph(const char* glyph, int length, TString* string);
|
||||||
|
|
||||||
struct __tstring {
|
struct __tstring {
|
||||||
char* Characters;
|
char* Characters;
|
||||||
@@ -20,11 +26,32 @@ TString* TStringCreate(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TStringAppendText(const char* text, TString* string) {
|
void TStringAppendText(const char* text, TString* string) {
|
||||||
|
int totalLength = strlen(text);
|
||||||
|
int glyphLength = GetGlyphByteWidth(text);
|
||||||
|
|
||||||
|
//If the two lengths are the same then that means we have a single character.
|
||||||
|
//Simply append it to the list and return.
|
||||||
|
if (totalLength == glyphLength) {
|
||||||
|
TStringCopyGlyph(text, glyphLength, string);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char* TStringGetString(const TString* string) {
|
char* TStringGetString(const TString* string) {
|
||||||
|
if (!string) return NULL;
|
||||||
|
|
||||||
|
char* text = calloc(string->ByteCount + 1, sizeof(char));
|
||||||
|
|
||||||
|
if (!text) {
|
||||||
|
fprintf(stderr, "Failed to calloc memory for TString text. %s.\n", strerror(errno));
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(text, string->Characters, string->ByteCount);
|
||||||
|
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TStringGetGlyph(int index, char buffer[8], const TString* string) {
|
void TStringGetGlyph(int index, char buffer[8], const TString* string) {
|
||||||
@@ -38,3 +65,47 @@ void TStringFree(TString* string) {
|
|||||||
|
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int GetGlyphByteWidth(const char* glyph) {
|
||||||
|
// printf("Bytes for '%s': ", glyph);
|
||||||
|
|
||||||
|
// for(size_t i = 0; i < strlen(glyph); i++) {
|
||||||
|
// printf("%02X ", 0xFF & glyph[i]);
|
||||||
|
// }
|
||||||
|
// printf("\n");
|
||||||
|
|
||||||
|
if ((*glyph & 0x80) == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if ((*glyph & 0xE0) == 0xC0) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else if ((*glyph & 0xF0) == 0xE0) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
else if ((*glyph & 0xF8) == 0xF0) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TStringCopyGlyph(const char* glyph, int length, TString* string) {
|
||||||
|
if (string->ByteCount + length > string->Capacity) {
|
||||||
|
char* buffer = realloc(string->Characters, string->Capacity * 2);
|
||||||
|
|
||||||
|
if (!buffer) {
|
||||||
|
fprintf(stderr, "Failed to realloc memory TString. %s.\n", strerror(errno));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string->Characters = buffer;
|
||||||
|
string->Capacity *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(&string->Characters[string->ByteCount], glyph, length);
|
||||||
|
|
||||||
|
string->ByteCount += length;
|
||||||
|
string->Length++;
|
||||||
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
#define TSTRING_H
|
#define TSTRING_H
|
||||||
|
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#ifndef DEFAULT_TSTRING_BYTE_BUFFER
|
#ifndef DEFAULT_TSTRING_BYTE_BUFFER
|
||||||
#define DEFAULT_TSTRING_BYTE_BUFFER 32
|
#define DEFAULT_TSTRING_BYTE_BUFFER 32
|
||||||
|
|||||||
Reference in New Issue
Block a user