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(); 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 IMFMessage::getHeaderKeyPairValue(ZString headerKey, ZString key) {
ZString value; ZString value;
ZString psource(getHeader(headerKey, false)); ZString psource(getHeader(headerKey, false));

View File

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

View File

@ -2,46 +2,50 @@
#include "Log.h" #include "Log.h"
namespace coreutils { namespace coreutils {
IMFResponse::IMFResponse() {} IMFResponse::IMFResponse() {}
IMFResponse::~IMFResponse() {} IMFResponse::~IMFResponse() {}
std::stringstream IMFResponse::getResponse(Mode mode) { std::stringstream IMFResponse::getResponse(Mode mode) {
std::stringstream stream(""); std::stringstream stream("");
return getResponse(stream, mode); return getResponse(stream, mode);
} }
std::stringstream IMFResponse::getResponse(std::stringstream &content, Mode mode) { std::stringstream IMFResponse::getResponse(std::stringstream &content, Mode mode) {
std::stringstream response; std::stringstream response;
response << protocol << " " << code << " " << text << CRLF; response << protocol << " " << code << " " << text << CRLF;
if(mode == LENGTH) if(mode == LENGTH)
addHeader((char *)"Content-Length", (char *)std::to_string(content.gcount()).c_str()); addHeader((char *)"Content-Length", (char *)std::to_string(content.gcount()).c_str());
else else
addHeader((char *)"Transfer-Encoding", (char *)"chunked"); addHeader((char *)"Transfer-Encoding", (char *)"chunked");
addHeader((char *)"Server", (char *)"JETServer v0.0.1"); addHeader((char *)"Server", (char *)"JETServer v0.0.1");
output(response); output(response);
response << content.str(); response << content.str();
// core::Log(core::LOG_DEBUG_4) << response.str(); // core::Log(core::LOG_DEBUG_4) << response.str();
return response; return response;
} }
void IMFResponse::setProtocol(ZString protocol) { void IMFResponse::setProtocol(ZString protocol) {
this->protocol = protocol; this->protocol = protocol;
} }
void IMFResponse::setCode(ZString code) { void IMFResponse::setCode(ZString code) {
this->code = code; this->code = code;
} }
void IMFResponse::setText(ZString text) { void IMFResponse::setText(ZString text) {
this->text = text; this->text = text;
} }
void IMFResponse::setCookie(ZString key, ZString data) { void IMFResponse::setCookie(ZString key, ZString data) {
addHeader("Set-Cookie", 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" #include "IMFMessage.h"
namespace coreutils { namespace coreutils {
/// ///
/// IMFResponse /// IMFResponse
/// ///
/// Use this object to build a response output for a protocol that uses headers /// Use this object to build a response output for a protocol that uses headers
/// and responses as the main communications protocol. /// and responses as the main communications protocol.
/// ///
class IMFResponse : public IMFMessage { class IMFResponse : public IMFMessage {
public: public:
enum Mode { LENGTH, STREAMING }; enum Mode { LENGTH, STREAMING };
/// ///
/// The constructor for the object. /// The constructor for the object.
/// ///
IMFResponse(); IMFResponse();
/// ///
/// The destructor for the object. /// The destructor for the object.
/// ///
~IMFResponse(); ~IMFResponse();
/// ///
/// Returns the response generated from the contained values that /// Returns the response generated from the contained values that
/// do not return a content length. Using this constructor ensures /// 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. /// @return the complete response string to send to client.
/// ///
std::stringstream getResponse(Mode mode = LENGTH); std::stringstream getResponse(Mode mode = LENGTH);
/// ///
/// Returns the response plus the content passed as a parameter. /// 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. /// @return the complete response string to send to client.
/// ///
std::stringstream getResponse(std::stringstream &content, Mode mode = LENGTH); 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. /// 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); void setProtocol(ZString protocol);
/// ///
/// Sets the return code value for the response. /// Sets the return code value for the response.
/// ///
/// @param code the response code value to return in the response. /// @param code the response code value to return in the response.
/// ///
void setCode(ZString code); void setCode(ZString code);
/// ///
/// Sets the return code string value for the response. /// 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. /// the response.
/// ///
void setText(ZString text); void setText(ZString text);
void setCookie(ZString key, ZString data); void setCookie(ZString key, ZString data);
void setCookie(ZString key, std::string data);
private: private:
ZString protocol; ZString protocol;
ZString code; ZString code;
ZString text; ZString text;
const char *CRLF = "\r\n"; const char *CRLF = "\r\n";
}; };
} }
#endif #endif

View File

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

View File

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