Testing Editbox stuffs
Below is a code block test.
/* ********************* //
// ** JOE COADYS LEXA ** //
// ** Copyright 2005 ** //
// ********************* */
#pragma once
#include <stdio.h>
#define LEX_SPACE_TAB " "
enum lex_types_e {
LEX_EMPTY,
LEX_NUMBER,
LEX_STRING,
LEX_FILENAME,
LEX_IDENT,
LEX_NODE
};
class eXLexToken {
public:
eXLexToken();
~eXLexToken();
union {
float i_value; /* The Float or number Value */
char* s_value; /* The String Value */
char* f_value; /* File name value */
class eXLexNode* n_value; /* The Node it is */
};
bool operator == (const char *value);
template<class T>
bool operator == (T value);
int LineNumber;
int type; /* Tell what Data we have */
};
enum lex_comment_e {
LEX_COMMENT_NONE = 0,
LEX_COMMENT_NORMAL,
LEX_COMMENT_ML
};
class eXLexNode {
public:
eXLexNode();
~eXLexNode();
eXLexToken** TokenArray; /* Array of tokens for that node */
int NumTokens; /* Number of tokens */
char opennode; /* OpenNode ie... { or ( or [ */
char closenode; /* OpenNode ie... } or ) or ] */
void ClearTree();
eXLexToken& operator [] ( int index );
eXLexToken* addtoken(eXLexToken *token);
int comment; /* If we are currently in a comment */
};
class eXLexa {
public:
eXLexa() { } /* Set up the data ready for lexing */
~eXLexa() { } /* And get rid of it */
int Analyze (char *unlexeddata);
void PrintTree (char *out);
void PrintNode (eXLexNode &node, FILE *out, int &LineNumber, int &tabbed);
int LexData (char* datastring, int upto, eXLexNode *NewNode, int &LineNumber);
eXLexToken* CreateToken (char *str, int type, int LineNumber);
void ClearTree();
eXLexNode RootNode;
};
#include <winsock.h>
#include <stdio.h>
#include "JS_LinkList.h"
// Socket Status
enum Net_C_States_e {
NC_CLOSED = 0,
NC_CONNECTED,
NC_LISTENING,
};
// Poll read state
enum NET_C_PRS_e {
NC_PRS_NO,
NC_PRS_YES,
NC_PRS_CLOSED,
NC_PRS_DROPPED
};
// Poll Write states
enum NET_C_PWS_e {
NC_PWS_NO,
NC_PWS_YES
};
// My socket Container.
class Net_Connection {
public:
Net_Connection();
// Connection Funcs
void Listen(int s_port);
void Accept(Net_Connection &NewConnection);
bool Connect(int s_port, char* hostname);
void Close();
// Polling Funcs
int PollRead();
int PollWrite();
// I/O Funcs
bool DownloadAvailableData();
void Sendraw(char* data, int size);
void Recvraw(char* dataout, int sizein, int &sizeout);
void Send_prot(char* data, int size);
bool Recv_prot(char* &dataout, int &sizeout);
void Setraw_string(char* strings, ...);
// Buffer Funcs
inline void ResizeBufferSize();
void clearbufftoo(int upto);
void clearbuff();
void msgbuff()
{
char* tmp = new char [buf_upto + 1];
memcpy(tmp, IncommingData, buf_upto);
tmp[buf_upto] = NULL;
MessageBox(0, tmp, tmp, 0);
delete tmp;
}
// SocketState
int state;
private:
int OurSocket;
u_short port;
// Last Error
int Werr;
public:
// Buff Data
int buf_size;
int buf_upto;
int buf_SSize;
char* IncommingData;
};
// Named link list of sockets
typedef named_link<Net_Connection> Net_Connections;
// Net Class
class Net {
public:
static void initWinSock();
};
#include "JS_Socket.h"
#pragma comment(lib, "wsock32")
Net_Connection::Net_Connection()
{
state = NC_CLOSED;
port = 0;
IncommingData = 0;
buf_size = 0;
buf_upto = 0;
buf_SSize = 128;
}
void Net_Connection::Listen(int s_port)
{
port = htons(s_port);
OurSocket = (int)socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
if (OurSocket == INVALID_SOCKET) {
Werr = WSAGetLastError();
WSACleanup();
return;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET; //TCP/IP
serverInfo.sin_addr.s_addr = INADDR_ANY; //ANY ADDRESS
serverInfo.sin_port = port; //Port Number
// Bind the socket to our local server address
//printf("Trying to bind port...");
Werr = bind(OurSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (Werr == SOCKET_ERROR) {
WSACleanup();
//printf("FAILED!!\n");
return;
}
//printf("Success!!\n");
// Set it to listen
Werr = listen(OurSocket, 4);
state = NC_LISTENING;
//printf("Listening...\n");
}
int Net_Connection::PollRead()
{
if (state == NC_CLOSED) return NC_PRS_CLOSED;
fd_set set;
FD_ZERO(&set);
FD_SET(OurSocket, &set);
struct timeval bt = {0, 0};
int num = select(0, &set, NULL, NULL, &bt);
if (num == 0) return NC_PRS_NO;
if (state == NC_CONNECTED) {
char tmp;
int ret = recv(OurSocket, &tmp, 1, MSG_PEEK);
if (ret == 0) { state = NC_CLOSED; return NC_PRS_CLOSED; }
if (ret == SOCKET_ERROR) { state = NC_CLOSED; return NC_PRS_DROPPED; }
}
return NC_PRS_YES;
}
int Net_Connection::PollWrite()
{
if (state == NC_CLOSED) return NC_PWS_NO;
fd_set set;
FD_ZERO(&set);
FD_SET(OurSocket, &set);
struct timeval bt = {0, 0};
int num = select(0, NULL, &set, NULL, &bt);
if (num == 0) return NC_PWS_NO;
return NC_PWS_YES;
}
void Net_Connection::Accept(Net_Connection &NewConnection)
{
if (state == NC_LISTENING) {
//printf("Attempting Connection... ");
sockaddr_in from;
int fromlen = sizeof(from);
int newsock = (int)accept(OurSocket, (struct sockaddr*)&from, &fromlen);
//printf("Accepting... ");
if (newsock == INVALID_SOCKET) {
//printf("ERROR CLIENT NOT THERE OMG WTF\n");
return;
}
NewConnection.OurSocket = newsock;
NewConnection.state = NC_CONNECTED;
//printf("Done...\n");
}
}
bool Net_Connection::Connect(int s_port, char* hostname)
{
state = NC_CLOSED;
port = htons(s_port);
//printf("Resolving Host Name (%s)... ", hostname);
LPHOSTENT hostEntry;
hostEntry = gethostbyname(hostname);
if (!hostEntry) {
//printf("Unable To Resolve Host Name\n");
return false;
}
//printf("Done\n");
//printf("Connecting to: %s:%d... ", hostname, s_port);
OurSocket = (int)socket(AF_INET, // Go over TCP/IP
SOCK_STREAM, // This is a stream-oriented socket
IPPROTO_TCP); // Use TCP rather than UDP
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_port = port;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
int ret = connect(OurSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (ret == SOCKET_ERROR) {
//printf("Unable to connect!\n", hostname, s_port);
return false;
}
//printf("Connected!\n");
state = NC_CONNECTED;
return true;
}
void Net_Connection::Sendraw(char* data, int size)
{
//printf("Sending %d Bytes\n\n", size);
if (state == NC_CONNECTED) {
send(OurSocket, data, size+1, MSG_OOB);
}
}
void Net_Connection::Setraw_string(char* strings, ...)
{
va_list va; // Get the text we wish to render
va_start(va, strings);
char text[65536];
vsprintf(text, strings, va);
va_end(va);
printf("%s\n", text);
int len = (int)strlen(text);
if (state == NC_CONNECTED) send(OurSocket, text, len+1, MSG_OOB);
}
void Net_Connection::Send_prot(char* data, int size)
{
char* tmp = new char[size + 40];
sprintf(tmp, "\\s%d\\s", size);
int len = (int)strlen(tmp);
memcpy(&tmp[len], data, size);
if (state == NC_CONNECTED) {
send(OurSocket, tmp, size+len+1, MSG_OOB);
}
delete tmp;
}
void Net_Connection::Recvraw(char* dataout, int sizein, int &sizeout)
{
sizeout = recv(OurSocket, dataout, sizein, 0);
//printf("Receiving Data: %d Bytes\n", sizeout);
}
inline void Net_Connection::ResizeBufferSize()
{
if (buf_upto == buf_size) {
//printf("Re-sizing Buffer From: %d to: %d\n", buf_size, buf_SSize + buf_size);
int size = buf_size + buf_SSize;
if (IncommingData) {
char* tmp = new char[size];
memcpy(tmp, IncommingData, buf_upto);
delete IncommingData;
IncommingData = tmp;
} else {
IncommingData = new char[size];
}
buf_size = size;
}
}
void Net_Connection::clearbufftoo(int upto)
{
if (IncommingData) {
char* tmp = new char[buf_size - upto];
memcpy(tmp, &IncommingData[upto], buf_size - upto);
delete IncommingData;
IncommingData = tmp;
//printf("----BEFORE----\nBUFF SIZE: %d\nUPTO: %d\n\n", buf_size, buf_upto);
buf_size = buf_size - upto;
buf_upto -= upto;
//printf("----AFTER----\nBUFF SIZE: %d\nUPTO: %d\n\n", buf_size, buf_upto);
}
}
void Net_Connection::clearbuff()
{
if (IncommingData) delete IncommingData;
IncommingData = NULL;
buf_upto = 0;
buf_size = 0;
}
bool Net_Connection::DownloadAvailableData()
{
bool ReadSomeData = false;
int PRS_ = false;
do {
PRS_ = PollRead();
if (PRS_ == NC_PRS_YES)
{
ReadSomeData = true;
ResizeBufferSize();
int sizeleft = buf_size - buf_upto;
buf_upto += recv(OurSocket, &IncommingData[buf_upto], sizeleft, 0);
}
} while (PRS_ == NC_PRS_YES);
return ReadSomeData;
}
bool Net_Connection::Recv_prot(char* &dataout, int &sizeout)
{
DownloadAvailableData();
if (buf_upto) {
int DownloadSize = 0;
bool sizing = false;
bool slash = false;
int s_size = 0;
int e_size = 0;
for (int c = 0; c < buf_upto; c++) {
if (slash) {
if (IncommingData[c] == '\\') {
slash = false;
} else {
slash = false;
if (IncommingData[c] == 's') {
sizing = !sizing;
if (sizing) {
s_size = c+1;
} else {
e_size = c-2;
}
}
}
} else {
slash = (IncommingData[c] == '\\');
}
if (e_size) {
//printf("download size = '");
//printfblock(&IncommingData[s_size], e_size - s_size + 1);
char tmp[10];
ZeroMemory(tmp, 10);
strncpy(tmp, &IncommingData[s_size], e_size - s_size + 1);
DownloadSize = atoi(tmp);
//printf("'.. or INT:%d\n", DownloadSize);
break;
}
}
if (buf_upto >= DownloadSize + e_size + 3) {
//printf("Data Download Finished...\n\nDOWNLOADED: '");
//printfblock(&IncommingData[e_size + 3], DownloadSize);
//printf("'\n\n\n");
dataout = new char[DownloadSize+1];
memcpy(dataout, &IncommingData[e_size + 3], DownloadSize);
dataout[DownloadSize] = NULL;
sizeout = DownloadSize;
clearbufftoo(DownloadSize + e_size + 3);
return true;
}
}
return false;
}
void Net_Connection::Close()
{
closesocket(OurSocket);
}
// Net Class ----------------------------------
void Net::initWinSock() {
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
}