Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cerrno>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <limits>
- #include <stdexcept>
- float str2f(std::string const &str, bool rethrow, float def) {
- if (str.empty()) {
- return def;
- }
- char *ep;
- errno = 0;
- double rtn = strtod(str.c_str(), &ep);
- if ((rtn > std::numeric_limits<float>::max()) ||
- (rtn < -std::numeric_limits<float>::max()) ||
- ((rtn > 0) && (rtn < std::numeric_limits<float>::min())) ||
- ((rtn < 0) && (rtn > -std::numeric_limits<float>::min()))) {
- if (rethrow) {
- throw std::out_of_range(str + " is too large for a float.");
- }
- } else if (errno || (*ep != '\0')) {
- if (rethrow) {
- if (errno) {
- throw std::invalid_argument(std::string(strerror(errno)));
- }
- throw std::invalid_argument(std::string("Failed to parse: ") + str +
- " as float.");
- }
- return def;
- }
- return rtn;
- }
- void chomp(std::string &str) {
- size_t lnf = str.find_last_not_of("\r\n");
- if(lnf != std::string::npos){
- str = str.substr(0, lnf+1);
- }
- }
- std::vector<std::string> SplitStringByDelim(std::string const &inp,
- char const *delim,
- bool PushEmpty,
- bool chompInput) {
- std::string inpCpy = inp;
- if(chompInput){
- chomp(inpCpy);
- }
- size_t nextOccurence = 0;
- size_t prevOccurence = 0;
- std::vector<std::string> outV;
- bool AtEnd = false;
- while (!AtEnd) {
- nextOccurence = inpCpy.find_first_of(delim, prevOccurence);
- if (nextOccurence == std::string::npos) {
- if (prevOccurence == inpCpy.length()) {
- break;
- }
- AtEnd = true;
- }
- if (PushEmpty || (nextOccurence != prevOccurence)) {
- outV.push_back(
- inpCpy.substr(prevOccurence, (nextOccurence - prevOccurence)));
- }
- prevOccurence = nextOccurence + 1;
- }
- return outV;
- }
- std::vector<double> StringVToDoubleV(std::vector<std::string> const &stringV) {
- std::vector<double> outV;
- size_t end = stringV.size();
- for (size_t it = 0; it < end; ++it) {
- outV.push_back(str2d(stringV[it], true));
- }
- return outV;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement