Utility library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems
Dependents: mtsas mtsas thermostat_fan_demo--fan mtsas ... more
NOTE: MTS-Utils has moved to GitHub. This version will not be updated. For updates, go to the GitHub version.
Revision 2:7779ede60c3d, committed 2014-05-19
- Comitter:
- Mike Fiore
- Date:
- Mon May 19 10:26:34 2014 -0500
- Parent:
- 1:92c0b062d84d
- Child:
- 3:08a693917d8c
- Commit message:
- add MTSText code and add test files for MTSLog and MTSText
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MTSText.cpp Mon May 19 10:26:34 2014 -0500 @@ -0,0 +1,48 @@ +#include "MTSText.h" + +using namespace mts; + +std::string Text::getLine(const std::string& source, const size_t& start, size_t& cursor) { + char delimiters[2]; + delimiters[0] = '\n'; + delimiters[1] = '\r'; + size_t end = source.find_first_of(delimiters, start, 2); + std::string line(source.substr(start, end - start)); + if (end < source.size()) { + if (end < source.size() - 1) + if ((source[end] == '\n' && source[end + 1] == '\r') || (source[end] == '\r' && source[end + 1] == '\n')) { + //Advance an additional character in scenarios where lines end in \r\n or \n\r + end++; + } + end++; + } + cursor = end; + return line; +} + +std::vector<std::string> Text::split(const std::string& str, char delimiter, int limit) { + return split(str, std::string(1, delimiter), limit); +} + +std::vector<std::string> Text::split(const std::string& str, const std::string& delimiter, int limit) { + std::vector<std::string> result; + if(str.size() == 0) { + return result; + } + size_t start = 0; + size_t end = str.find(delimiter, start); + for (int i = 1; i < limit || (limit <= 0 && (end != std::string::npos)); ++i) { + result.push_back(str.substr(start, end - start)); + start = end + delimiter.length(); + end = str.find(delimiter, start); + } + result.push_back(str.substr(start)); + return result; +} + +std::string Text::readString(char* index, int length) +{ + std::string result = std::string(index, length); + index += length; + return result; +} \ No newline at end of file
--- a/MTSText.h Mon May 19 08:22:30 2014 -0500 +++ b/MTSText.h Mon May 19 10:26:34 2014 -0500 @@ -1,4 +1,60 @@ #ifndef MTSTEXT_H #define MTSTEXT_H -#endif \ No newline at end of file +#include <string> +#include <vector> +#include <stddef.h> + +namespace mts +{ + +/** This class contains a number of static methods for manipulating strings and other +* text data. +*/ +class Text +{ +public: + /** This static method can be used to pull out a string at the next line break. A + * break can either be a newline '\n', carriage return '\r' or both. + * + * @param source the source string to look for the line break on. + * @param start the start postion within the string to begin looking for the line + * break. + * @param cursor this value will be updated with the index for the next available character + * after the line break. If a line break is not found returns -1. + * @returns the string beginning with the start index up to including the line breaks. + */ + static std::string getLine(const std::string& source, const size_t& start, size_t& cursor); + + /** This is a static method for splitting strings using a delimeter value. + * + * @param str the string to try and split. + * @param delimiter the delimeter value to split on as a character. + * @param limit the maximum number of splits. If equal to 0 it splits as amny times as possible. + * The default is 0. + * @returns an ordered vector of strings conatining the splits of the original string. + */ + static std::vector<std::string> split(const std::string& str, char delimiter, int limit = 0); + + /** This is a static method for splitting strings using a delimeter value. + * + * @param str the string to try and split. + * @param delimiter the delimeter value to split on as a string. + * @param limit the maximum number of splits. If equal to 0 it splits as amny times as possible. + * The default is 0. + * @returns an ordered vector of strings conatining the splits of the original string. + */ + static std::vector<std::string> split(const std::string& str, const std::string& delimiter, int limit = 0); + + static std::string readString(char* index, int length); + +private: + // Saftey for class with only static methods + Text(); + Text(const Text& other); + Text& operator=(const Text& other); +}; + +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/TestMTSLog.h Mon May 19 10:26:34 2014 -0500 @@ -0,0 +1,107 @@ +#ifndef TESTMTSLOG_H +#define TESTMTSLOG_H + +#include "MTSLog.h" + +using namespace mts; + +class TestMTSLog : public TestCollection +{ +public: + TestMTSLog(); + ~TestMTSLog(); + + virtual void run(); +}; + +TestMTSLog::TestMTSLog() : TestCollection("MTSLog") {} + +TestMTSLog::~TestMTSLog() {} + +void TestMTSLog::run() { + Test::start("Setting log level to TRACE: should see messages from all levels"); + MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::TRACE_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::TRACE_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to DEBUG: should see all messages above TRACE"); + MTSLog::setLogLevel(MTSLog::DEBUG_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::DEBUG_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::DEBUG_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to INFO: should see all messages above DEBUG"); + MTSLog::setLogLevel(MTSLog::INFO_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::INFO_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::INFO_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to WARNING: should see all messages above INFO"); + MTSLog::setLogLevel(MTSLog::WARNING_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::WARNING_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::WARNING_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to ERROR: should see all messages above WARNING"); + MTSLog::setLogLevel(MTSLog::ERROR_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::ERROR_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::ERROR_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to FATAL: should see all messages above ERROR"); + MTSLog::setLogLevel(MTSLog::FATAL_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::FATAL_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::FATAL_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); + + Test::start("Setting log level to NONE: should see no messages"); + MTSLog::setLogLevel(MTSLog::NONE_LEVEL); + Test::assertTrue(strcmp(MTSLog::getLogLevelString(), MTSLog::NONE_LABEL) == 0); + Test::assertTrue(MTSLog::getLogLevel() == MTSLog::NONE_LEVEL); + logFatal(); + logError(); + logWarning(); + logInfo(); + logDebug(); + logTrace(); + Test::end(); +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/TestMTSText.h Mon May 19 10:26:34 2014 -0500 @@ -0,0 +1,36 @@ +#ifndef TESTMTSTEXT_H +#define TESTMTSTEXT_H + +#include <string> +#include <vector> + +#include "MTSText.h" + +using namespace mts; + +class TestMTSText : public TestCollection +{ +public: + TestMTSText(); + ~TestMTSText(); + + virtual void run(); +}; + +TestMTSText::TestMTSText() : TestCollection("MTSText") {} + +TestMTSText::~TestMTSText() {} + +void TestMTSText::run() { + //Testing split method (char delimeter) + Test::start("split method (char delimeter)"); + std::string source = "ABC,,456"; + std::vector<std::string> split = Text::split(source, ','); + Test::assertTrue(split.size() == 3); + Test::assertTrue(split[0].compare("ABC") == 0); + Test::assertTrue(split[1].compare("") == 0); + Test::assertTrue(split[2].compare("456") == 0); + Test::end(); +} + +#endif /* TESTMTSTEXT_H */