Added testing function for ZString.

This commit is contained in:
Brad Arant 2021-09-22 18:01:54 -07:00
parent 191899fbb7
commit a0129273b3
8 changed files with 99 additions and 70 deletions

View File

@ -57,6 +57,10 @@ namespace coreutils {
return ZString();
}
ZString IMFMessage::getHeaderKeyPairValue(const char *headerKey, const char *key) {
return getHeaderKeyPairValue(ZString(headerKey), ZString(key));
}
ZString IMFMessage::getHeaderKeyPairValue(ZString headerKey, ZString key) {
ZString value;
ZString psource(getHeader(headerKey, false));

View File

@ -19,6 +19,7 @@ namespace coreutils {
void addHeader(const char *key, ZString value);
ZString getHeader(ZString key, bool valueOnly = true);
ZString getHeaderKeyPairValue(const char *headerKey, const char *key);
ZString getHeaderKeyPairValue(ZString headerKey, ZString key);
IMFBody * getBody();

View File

@ -2,46 +2,50 @@
#include "Log.h"
namespace coreutils {
IMFResponse::IMFResponse() {}
IMFResponse::~IMFResponse() {}
IMFResponse::~IMFResponse() {}
std::stringstream IMFResponse::getResponse(Mode mode) {
std::stringstream stream("");
return getResponse(stream, mode);
}
std::stringstream IMFResponse::getResponse(std::stringstream &content, Mode mode) {
std::stringstream response;
response << protocol << " " << code << " " << text << CRLF;
if(mode == LENGTH)
addHeader((char *)"Content-Length", (char *)std::to_string(content.gcount()).c_str());
else
addHeader((char *)"Transfer-Encoding", (char *)"chunked");
addHeader((char *)"Server", (char *)"JETServer v0.0.1");
output(response);
response << content.str();
output(response);
response << content.str();
// core::Log(core::LOG_DEBUG_4) << response.str();
return response;
return response;
}
void IMFResponse::setProtocol(ZString protocol) {
this->protocol = protocol;
}
void IMFResponse::setCode(ZString code) {
this->code = code;
}
void IMFResponse::setText(ZString text) {
this->text = text;
}
void IMFResponse::setCookie(ZString key, ZString data) {
addHeader("Set-Cookie", data);
}
void IMFResponse::setCookie(ZString key, std::string data) {
addHeader("Set-Cookie", ZString(data.c_str()));
}
}

View File

@ -5,32 +5,32 @@
#include "IMFMessage.h"
namespace coreutils {
///
/// IMFResponse
///
/// Use this object to build a response output for a protocol that uses headers
/// and responses as the main communications protocol.
///
class IMFResponse : public IMFMessage {
public:
enum Mode { LENGTH, STREAMING };
///
/// The constructor for the object.
///
IMFResponse();
///
///
/// The destructor for the object.
///
~IMFResponse();
~IMFResponse();
///
/// Returns the response generated from the contained values that
/// do not return a content length. Using this constructor ensures
@ -38,9 +38,9 @@ namespace coreutils {
///
/// @return the complete response string to send to client.
///
std::stringstream getResponse(Mode mode = LENGTH);
///
/// Returns the response plus the content passed as a parameter.
///
@ -52,46 +52,47 @@ namespace coreutils {
///
/// @return the complete response string to send to client.
///
std::stringstream getResponse(std::stringstream &content, Mode mode = LENGTH);
///
/// Sets the protocol value for the response message. The protocol
/// Sets the protocol value for the response message. The protocol
/// should match the header received.
///
/// @param protocol the protocol value to return in response.
/// @param protocol the protocol value to return in response.
///
void setProtocol(ZString protocol);
///
/// Sets the return code value for the response.
///
/// @param code the response code value to return in the response.
///
void setCode(ZString code);
///
/// Sets the return code string value for the response.
///
/// @param text the text value for the response code to return on
/// @param text the text value for the response code to return on
/// the response.
///
void setText(ZString text);
void setCookie(ZString key, ZString data);
void setCookie(ZString key, std::string data);
private:
ZString protocol;
ZString code;
ZString text;
const char *CRLF = "\r\n";
};
}
#endif

View File

@ -36,7 +36,7 @@ namespace coreutils {
data = zstring.data;
length = zstring.length;
cursor = zstring.cursor;
// Log(LOG_DEBUG_2) << "ZSrting Copy Constructor: ";
// Log(LOG_DEBUG_2) << "ZString Copy Constructor: ";
}
std::vector<ZString> &ZString::getList() {
@ -49,93 +49,93 @@ namespace coreutils {
temp >> tempInt;
return tempInt;
}
std::string ZString::str() {
return std::string(data, length);
}
std::string ZString::str(int len) {
return std::string(data, len);
}
std::vector<ZString> &ZString::split(std::string delimiter, size_t maxSize) {
coreutils::ZString zDelimiter((char *)delimiter.c_str(), delimiter.size());
return split(zDelimiter, maxSize);
}
std::vector<ZString> &ZString::split(ZString &delimiter, size_t maxSize) {
list.clear();
if(length == 0) {
list.push_back(ZString((char *)""));
list.push_back(ZString(""));
return list;
}
char *end = data + length;
char *pos = cursor;
while(pos < end) {
if(strncmp(pos, delimiter.getData(), delimiter.getLength()) == 0) {
list.push_back(ZString(cursor, pos - cursor));
cursor = pos + delimiter.getLength();
pos = cursor;
}
else {
++pos;
}
list.push_back(ZString(cursor, pos - cursor));
cursor = pos + delimiter.getLength();
pos = cursor;
}
else {
++pos;
}
}
list.push_back(ZString(cursor, pos - cursor));
cursor = pos;
return list;
}
ZString ZString::getTokenInclude(const char *include) {
char *start = cursor;
int len = strcspn(cursor, include);
cursor += len;
return ZString(start, len);
}
ZString ZString::getTokenExclude(const char *exclude) {
char *start = cursor;
int len = strspn(cursor, exclude);
cursor += len;
return ZString(start, len);
}
ZString ZString::getTokenExclude(std::string exclude) {
return getTokenExclude(exclude.c_str());
}
ZString &ZString::operator[](int index) {
return list[index];
}
bool ZString::eod() {
return cursor >= data + length;
}
bool ZString::equals(const char *value) {
if(strlen(value) != length)
return false;
return strncmp(data, value, length) == 0;
}
bool ZString::equals(char *value) {
if(strlen(value) != length)
return false;
return strncmp(data, value, length) == 0;
}
bool ZString::equals(ZString &zstring) {
if(zstring.getLength() != getLength())
return false;
return false;
return strncmp(data, zstring.getData(), getLength()) == 0;
}
bool ZString::equals(std::string &string) {
return string == std::string(data, length);
}
bool ZString::ifNext(const char *value) {
bool test = (strncmp(cursor, value, strlen(value)) == 0);
if(test)
@ -168,11 +168,11 @@ namespace coreutils {
size_t ZString::getLength() {
return length;
}
void ZString::setZString(ZString zstring) {
data = zstring.getData();
length = zstring.getLength();
cursor = data;
}
}
}

View File

@ -197,9 +197,11 @@ namespace coreutils {
void setZString(ZString zstring);
size_t length;
private:
char *data;
size_t length;
// size_t length;
char *cursor;
std::vector<ZString> list;

BIN
testing/zstring_test Executable file

Binary file not shown.

17
testing/zstring_test.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include "../ZString.h"
int main(int argc, char **argv) {
coreutils::ZString test("character1111:22222:33333");
test.length = 9;
test.split(":");
std::cout << test << std::endl;
std::cout << test.getLength() << std::endl;
std::cout << test[0] << std::endl;
std::cout << test[1] << std::endl;
}