diff --git a/README b/README index 5a6dbb8..c88f5ea 100644 --- a/README +++ b/README @@ -10,7 +10,6 @@ This program is in early stage but is fully functional without any major errors Known issues: - Doesn't support multiple identical devices -- Error reporting on wrong config is sub-par See 'example.mim' for an example config file diff --git a/include/stringTools.hpp b/include/stringTools.hpp deleted file mode 100644 index 34ad46f..0000000 --- a/include/stringTools.hpp +++ /dev/null @@ -1,131 +0,0 @@ -#ifndef READ_H -#define READ_H - -#include -#include -#include -#include -#include - -//NOTE: all functions here are probably not the most efficient - -/*Translates string of 1 to 2 hex char into a byte -returns 0 if an error occured -*/ -uint8_t hexStringToByte(std::string const& in); - -/*Translates an hex digit to its corresponding value -*/ -int8_t hexDigitToNum(char car); - -/*Translates a byte to a two char hex digit string -*/ -std::string byteToHexString(uint8_t const& byte); - -/*Translates a number into its hex char -only answers correctly if 0<=num<16 -otherwise answers \0 -*/ -char toHexDigit(uint8_t const& num); - -/*Reapeats n times string a -*/ -std::string repeatString(std::string const& a, unsigned int n); - -/*Repeats n times char a -*/ -std::string repeatChar(char a, unsigned int n); - -/*Returns true if it an alpha char -*/ -bool isAlpha(char a); - -/*Returns true if it a number char -*/ -bool isNum(char a); - -/*Returns true if it an hexadecimal char -*/ -bool isHexdec(char a); - -/*Checks if two strings are insensitively case equal -*/ -bool equalNoCase(std::string const& st1, std::string const& st2); - -/*Returns true if str1 and str2 have a common char -*/ -bool hasCommonChar(std::string const& str1, std::string const& str2); - -/*Fuses elements of a string vector, from [first] to [last] adding between each -If separator = \0 , adds nothing -if last < 0 , goes until the end -*/ -std::string fuse(std::vector const& vec, int first, int last=-1, char separator=' '); - -/*Finds rank of char in string -returns -1 if not found -*/ -int findInString(std::string const& in, char const find); - -/*Returns the number of times char has appeared in string -*/ -unsigned int charCount(std::string const& in, char const find); - -/*************************************/ - -bool isRead(char in); //Read characters (ascii-7bit) for following functions - -/*Decapsulates the content of string -If first read char is encapsulator, reads until decapsulator -If not, just gets rid of unread char at beginning and end -Encapsulators: "",{},[],<> -*/ -std::string decapsulate(std::string const& in); - -/*Removes unread chars at beggining and end of the string -*/ -std::string ridUnread(std::string const& in); - -/*Reads a variable of type string in a string. -It's important to place the container characters right after the variable name -ex: "variable={ text }" -Smart reading, if another { is detected, it will wait for another } before ending -Considers \n as the separator if there arent {} -Reads only ascii-7bit -*/ -std::string readInString(std::string const& in, std::string const& name, unsigned int occurence=0); - -/*Same as above, but returns pair of value name and value itself of occurence given -instead of value name -puts beginning of value in rank if rank != nullptr -*/ -std::pair readValue(std::string const& in, unsigned int occurence=0, int* rank=nullptr); - -/*Reads list in format [ , , ] and splits into vector -*/ -std::vector readList(std::string const& in); - -/*Splits string using given delimiter. Uses space if not specified -Between encapsulator and decapsulator: delims added -INVALID DELIM: \ -If delim is invalid, returns empty vector -Does not include encapsulators in output -\ adds \ and char, even if it is an encapsulator -*/ -std::vector splitString(std::string const& in, char const delim=' ', char encapsulator='\"', char decapsulator=0); - -/*Replaces char f by r -Returns number of char replaced -*/ -int replaceChar(std::string& in, char const f,char const r); - -/*Returns true if char c is in str -*/ -bool isIn(char const c, std::string const& str); - -/*Compacts suites of any chars from string chars, into char into -note: equivalent to tr -s in bash -*/ -std::string encompact(std::string const& in, std::string const& chars=" \n\r\t\b", char const into=' '); - -#endif // READ_H diff --git a/src/device.cpp b/src/device.cpp index 26280d0..990b634 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -1,7 +1,5 @@ #include "device.hpp" -#include "stringTools.hpp" - #include #include #include @@ -18,6 +16,11 @@ static void sh(std::string const& string) system(string.c_str()); } +static bool _isNum(char a) +{ + return (a>='0' && a<='9'); +} + Device::Device() { busy=false; @@ -312,7 +315,7 @@ void Device::run_signal(char* buff) pos++; pos++; t=1; - while ( isNum(*(pos+t)) ) + while ( _isNum(*(pos+t)) ) t++; ctid = std::stoi( std::string(pos, t) ); pos+=t+2; @@ -325,7 +328,7 @@ void Device::run_signal(char* buff) pos++; pos++; t=1; - while ( isNum(*(pos+t)) ) + while ( _isNum(*(pos+t)) ) t++; value = std::stoi( std::string(pos, t)); } diff --git a/src/stringTools.cpp b/src/stringTools.cpp deleted file mode 100644 index fffdf53..0000000 --- a/src/stringTools.cpp +++ /dev/null @@ -1,734 +0,0 @@ -#include "stringTools.hpp" - -uint8_t hexStringToByte(std::string const& in) -{ - if(in.size() == 2) - { - return hexDigitToNum(in[0])*16 + hexDigitToNum(in[1]); - } - if(in.size() == 1) - { - return hexDigitToNum(in[0]); - } - else - return 0; -} - -int8_t hexDigitToNum(char car) -{ - if(isNum(car)) - return car - '0'; - else if(car>='a' && car<='f') - return car - 'a' + 10; - else if(car>='A' && car<='F') - return car - 'A' + 10; - else - return -1; -} - -std::string byteToHexString(uint8_t const& byte) -{ - std::string ret; - ret += toHexDigit(byte/16); - ret += toHexDigit(byte%16); - return ret; -} - -char toHexDigit(uint8_t const& num) -{ - if(num>=0 && num<=9) - { - return '0' + num; - } - else if(num < 16) - { - return 'A' + num - 10; - } - else - return 0; -} - -std::string repeatString(std::string const& a, unsigned int n) -{ - std::string ret; - while(n>0) - { - ret += a; - n--; - } - return ret; -} - -std::string repeatChar(char a, unsigned int n) -{ - std::string ret; - while(n>0) - { - ret.push_back(a); - n--; - } - return ret; -} - -bool isAlpha(char a) -{ - return (a>='a' && a<='z') || (a>='A' && a<='Z'); -} - -bool isNum(char a) -{ - return (a>='0' && a<='9'); -} - -bool isHexdec(char a) -{ - return isNum(a) || (a>='a' && a<='f') || (a>='A' && a<='F'); -} - -bool equalNoCase(char a, char b) -{ - if(a==b) - return true; - if(!isAlpha(a) || !isAlpha(b)) - return false; - return (a-b == 'a' - 'A') || (a-b == 'A' - 'a'); -} - -bool equalNoCase(std::string const& st1, std::string const& st2) -{ - if(st1.size()!=st2.size()) - return false; - for(unsigned int i=0; i const& vec, int first, int last, char separator) -{ - if(last<0 || last >= (int) vec.size()) - last=vec.size()-1; - if(first<0) - first=0; - std::string ret; - if(first <= last) - { - for(int i=first; i<=last-1; i++) - { - ret+=vec[i]; - if(separator != 0) - ret+=separator; - } - ret += vec[last]; - } - return ret; -} - -int findInString(std::string const& in, char const find) -{ - for(unsigned int i=0; i=33 && in<=126) - out=true; - return out; -} - -std::string decapsulate(std::string const& in) -{ - unsigned int i=0,j; - while(i' && counter==0)) - { - if(in[i]=='\\') - i++; - if(in[i]=='<') - counter++; - if(in[i]=='>') - counter--; - i++; - } - } - else - { - i=in.size()-1; - while(i0 && !isRead(in[i])) - { - i--; - } - i++; - } - return in.substr(j,i-j); -} - -std::string ridUnread(std::string const& in) -{ - int i=0,j=in.size()-1; - while( ((unsigned int) i)=0 && !isRead(in[j])) - j--; - if(i<=j) - return in.substr(i, j-i+1); - else - return ""; -} - -std::string readInString(std::string const& in, std::string const& name, unsigned int occurence) -{ - bool found=false; - std::string out=""; - unsigned int i=0,j=0,reading=0; - while(i readValue(std::string const& in, unsigned int occurence, int* rank) -{ - std::string name=""; - unsigned int i=0,j=0,reading=0; - while(i=in.size()) - { - if(rank != nullptr) - *rank=-1; - return std::make_pair("",""); - } - if(i+1 < in.size() && in[i]=='/' && in[i+1]=='/') //ignore // comment - { - i+=2; - while(iin.size()) - i=in.size(); - if(j>in.size()) - j=in.size(); - return std::make_pair(name, in.substr(j,i-j)); - } - } - else /*On veut une autre occurence: passer cette valeur là*/ - { - reading++; - i+=name.size()+1; - unsigned int counter=0; - if(i readList(std::string const& in) -{ - unsigned int i=0,j=0,counter=0; - std::vector out; - while(i(); - return out; -} - -std::vector splitString(std::string const& in, char const delim, char encapsulator, char decapsulator) -{ - std::vector out; - if(delim!='\\' && delim!=encapsulator && delim!=decapsulator) - { - if(decapsulator==0) - decapsulator=encapsulator; - unsigned int i=0; - while(i