#include "global.h"
u8 emptyLine(HWND tArea, u16 index) {
u16 lnNumber = 1 + (u16) SendMessage( // Position of next line
(HWND) tArea,
(UINT) EM_LINEFROMCHAR,
(WPARAM) index,
(LPARAM) 0
);
u16 lnIndex = (u16) SendMessage(
(HWND) tArea,
(UINT) EM_LINEINDEX, // Gets 1st char of next line
(WPARAM) lnNumber,
(LPARAM) 0
);
u16 lnLength = (u16) SendMessage( // Length of next line
(HWND) tArea,
(UINT) EM_LINELENGTH,
(WPARAM) lnIndex,
(LPARAM) 0
);
char* line = new char[lnLength];
SendMessage( // Get the line itself
(HWND) tArea,
(UINT) EM_GETLINE,
(WPARAM) lnIndex,
(LPARAM) line
);
// if (lnIndex>strlen(inputC)) return TRUE;
for (u16 index3 = 0; index3<lnLength; index3++) {
if (!isspace(line[index3])) {
return FALSE; // Go through and check for nulls.
}
}
return TRUE;
}
long hexByte(HWND tArea) {
Code* currentCode = firstCode;
u8 currentByte = 0, firstC = TRUE;
u16 index = 0, bArrIndex = 0, thisLine = 0, codeSize = 0, codePos = 0;
withoutNulls = 0;
codeCount = 0;
while (isspace(inputC[index])) {index++;}
for (index; index<(strlen(inputC)); index++) {
if ((strlen(inputC) > 0) && (isalnum(inputC[index])) &&
(firstC)) { // Is there at least 1 char?
withoutNulls += 16; // Allocate a line
firstC = FALSE; // 1st char found
}
if ((strlen(inputC) > 1) && (index<(strlen(inputC))-1) &&
(inputC[index] == '\n') && (isalnum(inputC[index+1]))) {
// Is there a new line of Code?
// currentCode->size += 16;
withoutNulls += 16; // Allocate a new line
}
}
byteArray = new u8[withoutNulls>>1];
index=0;
while (isspace(inputC[index])) {index++;}
for (bArrIndex=0; bArrIndex<withoutNulls>>1 && index<strlen(inputC);) {
while (isspace(inputC[index]) && (inputC[index] != '\r')) {index++;}
if ((thisLine == 0) && (emptyLine(tArea, index))) {
codePos = bArrIndex - codeSize;
currentCode->theCode = new u8[codeSize];
memcpy(currentCode->theCode, &byteArray[codePos],
codeSize);
currentCode->size = codeSize;
currentCode->next = new Code();
currentCode = currentCode->next;
codeCount++;
codeSize = 0;
// while (isspace(inputC[index])) {index++;}
}
// Fill with zeros
if ((inputC[index] == '\r') & (thisLine > 0)) {
currentByte = 0;
}
else {
while (isspace(inputC[index])) {index++;}
if ((inputC[index] >= 'a') & (inputC[index] <= 'f')) {
currentByte = ((inputC[index] - 'a' + 10)<<4);
}
else if ((inputC[index] >= 'A') & (inputC[index] <= 'F')) {
currentByte = ((inputC[index] - 'A' + 10)<<4);
}
else if ((inputC[index] >= '0') & (inputC[index] <= '9')) {
currentByte = ((inputC[index] - '0')<<4);
}
else {
currentByte = 0;
}
index++;
}
// Fill with zeros
while (isspace(inputC[index]) && (inputC[index] != '\r')) {index++;}
if ((inputC[index] == '\r')) {
currentByte += 0;
}
else {
while (isspace(inputC[index])) {index++;}
if ((inputC[index] >= 'a') & (inputC[index] <= 'f')) {
currentByte += (inputC[index] - 'a' + 10);
}
else if ((inputC[index] >= 'A') & (inputC[index] <= 'F')) {
currentByte += (inputC[index] - 'A' + 10);
}
else if ((inputC[index] >= '0') & (inputC[index] <= '9')) {
currentByte += (inputC[index] - '0');
}
else {
currentByte += 0;
}
index++;
}
byteArray[bArrIndex] = currentByte;
if (thisLine < 7) {
thisLine++;
}
else {
thisLine = 0;
}
bArrIndex++;
codeSize++;
}
while (isspace(inputC[index]) && (inputC[index] != '\r')) {index++;}
codePos = bArrIndex - codeSize;
currentCode->theCode = new u8[codeSize];
memcpy(currentCode->theCode, &byteArray[codePos],
codeSize);
currentCode->size = codeSize;
currentCode->next = new Code();
currentCode = currentCode->next;
codeSize = 0;
/*byteArray = new u8[8];
byteArray[0] = 0xBB;
byteArray[1] = 0xAA;
byteArray[2] = 0xAA;
byteArray[3] = 0xAA;
byteArray[4] = 0xFF;
byteArray[5] = 0xFF;
byteArray[6] = 0xFF;
byteArray[7] = 0xFF;
byteArray[8] = 0xDD;
byteArray[9] = 0xCC;
byteArray[10] = 0xCC;
byteArray[11] = 0xCC;
byteArray[12] = 0x22;
byteArray[13] = 0x22;
byteArray[14] = 0x22;
byteArray[15] = 0x22;
withoutNulls = 32;*/
return codeCount; // bArrIndex - codeSize;
}
long byteHex() {
Code* currentCode = firstCode;
outputC = new char[(19 * (withoutNulls>>4)) + ((2 + codeCount)<<1)];
u8 char0 = 0, char1 = 0, char2 = 0; // char1 and 2 are first and last 4 bits
u32 index2 = 0, noOfCodes = 0;
/*for (; currentCode->next; currentCode = currentCode->next) {
for (u32 index = 0; index < (currentCode->size); index += lineLength) {
// currentCode->size was withoutNulls>>1
for (u32 charNo = 0; charNo < lineLength; charNo++) {
char0 = currentCode->theCode[index + charNo];
char1 = char0 >> 4;
char2 = char0 &= 0x0F;
outputC[index2] = hexValues[char1];
index2++;
outputC[index2] = hexValues[char2];
index2++;
if (charNo == 3) {
outputC[index2] = ' ';
index2++;
}
if (charNo == 7) {
outputC[index2] = '\r';
index2++;
outputC[index2] = '\n';
index2++;
}
} // End of this line
} // End of this code
if (noOfCodes<codeCount) { // Seems to reduce number of crashes
// Still crashes it occasionally
outputC[index2] = '\r';
index2++;
outputC[index2] = '\n';
index2++;
}
noOfCodes++;
}*/
outputC[(19 * (withoutNulls>>4)) + ((2 + codeCount)<<1)] = '\0';
return noOfCodes;
}