#include <unistd.h> #include <string> #include "descriptor.h" #include <iostream.h> #include <stdlib.h> #include <stdio.h> Descriptor::Descriptor() : socket(0) { status = STATUS_LOGIN; setReadyBuf(false); } Descriptor::Descriptor(int sock) :socket(sock) { status = STATUS_LOGIN; setReadyBuf(false); } Descriptor::~Descriptor() { } void Descriptor::closeD() { close(socket); } void Descriptor::writeTo(string str) { if (str == "") return; char *temp; temp = strdup(str.c_str()); write(socket, (void *) temp, strlen(temp)); free(temp); } void Descriptor::setSocket(int sock) { socket = sock; } int Descriptor::getSocket() { return socket; } void Descriptor::addReadBuf(string str) { readbuf += str; } void Descriptor::addWriteBuf(string str) { writebuf += str; } void Descriptor::writeBuf() { if (writebuf == "") return; writeTo(writebuf); writebuf = ""; } string Descriptor::getReadBuf() { return readbuf; } string Descriptor::getWriteBuf() { return writebuf; } void Descriptor::clearReadBuf() { setReadyBuf(false); readbuf = ""; } bool Descriptor::bufEnd() { if (readbuf[readbuf.length() - 1] == '\n') { return true; } return false; } void Descriptor::setReadyBuf(bool ready) { bready = ready; } bool Descriptor::isReadyBuf() { return bready; } string Descriptor::argument(int n) { uint pos = 0; int count = 1; if (n == 1) { if (readbuf.find(" ", 0) != string::npos) { return readbuf.substr(0, readbuf.find(" ", 0)); } else { return readbuf; } } while (pos != string::npos) { if (count == n) { return readbuf.substr(pos + 1, readbuf.find(" ", pos + 1)); } pos = readbuf.find(" ", pos + 1); count++; } return ""; } string Descriptor::argumentBut(int n) { uint pos = 0; int count = 0; string temp; if (n == 1) return readbuf.substr(readbuf.find(" ", 0) + 1, readbuf.length() - (readbuf.find(" ", 0) + 1)); while (pos != string::npos) { if (count != n) { temp += readbuf.substr(pos, readbuf.find(" ", pos + 1) - pos); } pos = readbuf.find(" ", pos + 1); count++; } return temp; } void Descriptor::setStatus(int stat) { status = stat; } int Descriptor::getStatus() { return status; }