This commit is contained in:
Anthony J. Bentley
2016-01-28 23:05:25 -07:00
parent 2ea2e47231
commit e241137508

View File

@@ -34,32 +34,19 @@ readUTF8Char(char *destination, char *source)
UBYTE first; UBYTE first;
first = source[0]; first = source[0];
if(first >= 0xFC) if (first >= 0xFC) {
{
size = 6; size = 6;
} } else if (first >= 0xF8) {
else if(first >= 0xF8)
{
size = 5; size = 5;
} } else if (first >= 0xF0) {
else if(first >= 0xF0)
{
size = 4; size = 4;
} } else if (first >= 0xE0) {
else if(first >= 0xE0)
{
size = 3; size = 3;
} } else if (first >= 0xC0) {
else if(first >= 0xC0)
{
size = 2; size = 2;
} } else if (first != '\0') {
else if(first != '\0')
{
size = 1; size = 1;
} } else {
else
{
size = 0; size = 0;
} }
strncpy(destination, source, size); strncpy(destination, source, size);
@@ -71,76 +58,65 @@ int
charmap_Add(char *input, UBYTE output) charmap_Add(char *input, UBYTE output)
{ {
int i, input_length; int i, input_length;
char temp1i[CHARMAPLENGTH + 1], temp2i[CHARMAPLENGTH + 1], temp1o = 0, temp2o = 0; char temp1i[CHARMAPLENGTH + 1], temp2i[CHARMAPLENGTH + 1], temp1o = 0,
temp2o = 0;
struct Charmap *charmap; struct Charmap *charmap;
if(pCurrentSection) if (pCurrentSection) {
{ if (pCurrentSection->charmap) {
if(pCurrentSection -> charmap) charmap = pCurrentSection->charmap;
{ } else {
charmap = pCurrentSection -> charmap; if ((charmap = calloc(1, sizeof(struct Charmap))) ==
} NULL) {
else
{
if((charmap = (struct Charmap *) calloc(1, sizeof(struct Charmap))) == NULL)
{
fatalerror("Not enough memory for charmap"); fatalerror("Not enough memory for charmap");
} }
pCurrentSection -> charmap = charmap; pCurrentSection->charmap = charmap;
} }
} } else {
else
{
charmap = &globalCharmap; charmap = &globalCharmap;
} }
if(nPass == 2) if (nPass == 2) {
{ return charmap->count;
return charmap -> count;
} }
if(charmap -> count > MAXCHARMAPS || strlen(input) > CHARMAPLENGTH) if (charmap->count > MAXCHARMAPS || strlen(input) > CHARMAPLENGTH) {
{
return -1; return -1;
} }
input_length = strlen(input); input_length = strlen(input);
if(input_length > 1) if (input_length > 1) {
{
i = 0; i = 0;
while(i < charmap -> count + 1) while (i < charmap->count + 1) {
{ if (input_length > strlen(charmap->input[i])) {
if(input_length > strlen(charmap -> input[i])) memcpy(temp1i, charmap->input[i],
{ CHARMAPLENGTH + 1);
memcpy(temp1i, charmap -> input[i], CHARMAPLENGTH + 1); memcpy(charmap->input[i], input, input_length);
memcpy(charmap -> input[i], input, input_length); temp1o = charmap->output[i];
temp1o = charmap -> output[i]; charmap->output[i] = output;
charmap -> output[i] = output;
i++; i++;
break; break;
} }
i++; i++;
} }
while(i < charmap -> count + 1) while (i < charmap->count + 1) {
{ memcpy(temp2i, charmap->input[i], CHARMAPLENGTH + 1);
memcpy(temp2i, charmap -> input[i], CHARMAPLENGTH + 1); memcpy(charmap->input[i], temp1i, CHARMAPLENGTH + 1);
memcpy(charmap -> input[i], temp1i, CHARMAPLENGTH + 1);
memcpy(temp1i, temp2i, CHARMAPLENGTH + 1); memcpy(temp1i, temp2i, CHARMAPLENGTH + 1);
temp2o = charmap -> output[i]; temp2o = charmap->output[i];
charmap -> output[i] = temp1o; charmap->output[i] = temp1o;
temp1o = temp2o; temp1o = temp2o;
i++; i++;
} }
memcpy(charmap -> input[charmap -> count + 1], temp1i, CHARMAPLENGTH + 1); memcpy(charmap->input[charmap->count + 1], temp1i,
charmap -> output[charmap -> count + 1] = temp1o; CHARMAPLENGTH + 1);
charmap->output[charmap->count + 1] = temp1o;
} else {
memcpy(charmap->input[charmap->count], input, input_length);
charmap->output[charmap->count] = output;
} }
else return ++charmap->count;
{
memcpy(charmap -> input[charmap -> count], input, input_length);
charmap -> output[charmap -> count] = output;
}
return ++charmap -> count;
} }
int int
@@ -152,47 +128,35 @@ charmap_Convert(char **input)
char *buffer; char *buffer;
int i, j, length; int i, j, length;
if(pCurrentSection && pCurrentSection -> charmap) if (pCurrentSection && pCurrentSection->charmap) {
{ charmap = pCurrentSection->charmap;
charmap = pCurrentSection -> charmap; } else {
}
else
{
charmap = &globalCharmap; charmap = &globalCharmap;
} }
if((buffer = (char *) malloc(strlen(*input))) == NULL) if ((buffer = malloc(strlen(*input))) == NULL) {
{
fatalerror("Not enough memory for buffer"); fatalerror("Not enough memory for buffer");
} }
length = 0; length = 0;
while(**input) while (**input) {
{
j = 0; j = 0;
for(i = 0; i < charmap -> count; i++) for (i = 0; i < charmap->count; i++) {
{ j = strlen(charmap->input[i]);
j = strlen(charmap -> input[i]); if (memcmp(*input, charmap->input[i], j) == 0) {
if(memcmp(*input, charmap -> input[i], j) == 0) outchar[0] = charmap->output[i];
{
outchar[0] = charmap -> output[i];
outchar[1] = 0; outchar[1] = 0;
break; break;
} }
j = 0; j = 0;
} }
if(!j) if (!j) {
{
j = readUTF8Char(outchar, *input); j = readUTF8Char(outchar, *input);
} }
if(!outchar[0]) if (!outchar[0]) {
{
buffer[length++] = 0; buffer[length++] = 0;
} } else {
else for (i = 0; outchar[i]; i++) {
{
for(i = 0; outchar[i]; i++)
{
buffer[length++] = outchar[i]; buffer[length++] = outchar[i];
} }
} }
@@ -201,4 +165,3 @@ charmap_Convert(char **input)
*input = buffer; *input = buffer;
return length; return length;
} }