diff --git a/Tests/CommonUtilsTest.cpp b/Tests/CommonUtilsTest.cpp index 6d51874..9b24da5 100644 --- a/Tests/CommonUtilsTest.cpp +++ b/Tests/CommonUtilsTest.cpp @@ -1,29 +1,30 @@ -#include "gtest/gtest.h" -#include "common_utils/common_utils.h" -#include #include -#include +#include #include -//#include +#include +#include "common_utils/common_utils.h" +#include "gtest/gtest.h" +// #include using std::string; using std::vector; vector args{"Apple", "Orange", "Banan", "Pineapple", "Kiwi"}; -vector args_big{"Yes",//1 - "No", //2 - "Ok", //3 - "Good",//4 - "Bad",//5 - "Nice",//6 - "Ugly",//7 - "Perfect",//8 - "Strong",//9 - "Davis",//10 - "Cat",//11 - "Plotly"//12 +vector args_big{ + "Yes", // 1 + "No", // 2 + "Ok", // 3 + "Good", // 4 + "Bad", // 5 + "Nice", // 6 + "Ugly", // 7 + "Perfect", // 8 + "Strong", // 9 + "Davis", // 10 + "Cat", // 11 + "Plotly" // 12 }; constexpr char check_test_string_1[] = @@ -47,18 +48,17 @@ constexpr char not_filled_test_string_3[] = // C++ stream interface class TestCout : public std::stringstream { public: - ~TestCout() { - std::cout << str() << std::flush; - } + ~TestCout() { std::cout << str() << std::flush; } }; -#define TEST_COUT TestCout() +#define TEST_COUT TestCout() TEST(CommonUtils, CreateStringFromArgs1) { string out; dvs::make_string(not_filled_test_string_1, args, out); EXPECT_EQ(check_test_string_1, out) << out; - vector neg_args{"Strawbery", "Orange", "Banan", "Pineapple", "Kiwi"}; + vector neg_args{"Strawbery", "Orange", "Banan", "Pineapple", + "Kiwi"}; dvs::make_string(not_filled_test_string_1, neg_args, out); EXPECT_FALSE(check_test_string_1 == out); } @@ -77,12 +77,11 @@ TEST(CommonUtils, CreateStringFromBigArgs) { TEST(CommonUtils, CreateStringFantasyArgs) { string out; - vectorsample = {"Ferrari", "Ford", "BMW", "Tatra"}; + vector sample = {"Ferrari", "Ford", "BMW", "Tatra"}; dvs::make_string("I like %1 100%! I don't like %4.", sample, out); EXPECT_EQ("I like Ferrari 100%! I don't like Tatra.", out) << out; } - TEST(CommonUtils, SplitString) { auto result = dvs::split(not_filled_test_string_1, '%'); for (size_t i = 0; i < result.size(); ++i) { @@ -131,6 +130,8 @@ TEST(CommonUtils, RussianFileC) { }*/ +TEST(CommonUtils, JsDownloadByCurl) { dvs::tryToDownloadJsByCurl(); } + int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); std::ignore = RUN_ALL_TESTS(); diff --git a/common_utils/common_constants.cpp b/common_utils/common_constants.cpp index a73e42e..378f7a4 100644 --- a/common_utils/common_constants.cpp +++ b/common_utils/common_constants.cpp @@ -1,7 +1,7 @@ #include "common_constants.h" -#include "string" + namespace dvs { -//#START_GRAB_TO_DVS_NAMESPACE +// #START_GRAB_TO_DVS_NAMESPACE const char kAppName[] = "davis"; const char kOutFolderName[] = "davis_htmls/"; const char kPlotlyJsWorkPath[] = "./davis_htmls/plotly-2.32.0.min.js"; @@ -10,5 +10,7 @@ const char kPlotlyJsResourcePath[] = "plotly_maker/plotly-2.32.0.min.js"; const char kWarningPagePath[] = "./davis_htmls/warning_js_absent.html"; const char kReportPagePath[] = "./davis_htmls/report.html"; const char kCloudPagePath[] = "./davis_htmls/cloud_of_points.html"; -//#STOP_GRAB_TO_DVS_NAMESPACE -} +const char kJsUrlToDownolad[] = + "https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.32.0/plotly.min.js"; +// #STOP_GRAB_TO_DVS_NAMESPACE +} // namespace dvs diff --git a/common_utils/common_constants.h b/common_utils/common_constants.h index 99df196..cc2eb16 100644 --- a/common_utils/common_constants.h +++ b/common_utils/common_constants.h @@ -2,7 +2,7 @@ #define COMMON_UTILS_COMMON_CONSTANTS_H namespace dvs { -//#START_GRAB_TO_DVS_NAMESPACE +// #START_GRAB_TO_DVS_NAMESPACE extern const char kAppName[]; extern const char kOutFolderName[]; extern const char kPlotlyJsName[]; @@ -11,7 +11,8 @@ extern const char kPlotlyJsWorkPath[]; extern const char kWarningPagePath[]; extern const char kReportPagePath[]; extern const char kCloudPagePath[]; -//#STOP_GRAB_TO_DVS_NAMESPACE -} +extern const char kJsUrlToDownolad[]; +// #STOP_GRAB_TO_DVS_NAMESPACE +} // namespace dvs -#endif // COMMON_UTILS_COMMON_CONSTANTS_H +#endif // COMMON_UTILS_COMMON_CONSTANTS_H diff --git a/common_utils/common_utils.cpp b/common_utils/common_utils.cpp index 065d1b2..09c2f60 100644 --- a/common_utils/common_utils.cpp +++ b/common_utils/common_utils.cpp @@ -1,31 +1,33 @@ #include "common_utils.h" + #include "common_constants.h" -//#START_GRAB_TO_INCLUDES_LIST -#include -#include -#include -#include +// #START_GRAB_TO_INCLUDES_LIST #include #include -#include -#include -#include #include -#include +#include + #include +#include +#include #include +#include #include +#include +#include +#include +#include #include -//#STOP_GRAB_TO_INCLUDES_LIST +// #STOP_GRAB_TO_INCLUDES_LIST namespace dvs { -//#START_GRAB_TO_DVS_NAMESPACE +// #START_GRAB_TO_DVS_NAMESPACE using std::string; #ifdef _WIN32 #include #include - #define getcwd _getcwd // stupid MSFT "deprecation" warning + #define getcwd _getcwd // stupid MSFT "deprecation" warning #elif __linux__ #include #endif @@ -54,7 +56,7 @@ void openFileBySystem(const string& file_name) { } string getCurrentPath() { -#if defined (_WIN32) || (__linux__) +#if defined(_WIN32) || (__linux__) char buffer[1024]; char* answer = getcwd(buffer, sizeof(buffer)); string s_cwd; @@ -63,17 +65,26 @@ string getCurrentPath() { } return s_cwd; #elif __APPLE__ - //TODO macos get current path implementation + // TODO macos get current path implementation return ""; #endif } bool isPlotlyScriptExists() { + bool isJsExists = is_file_exists(kPlotlyJsWorkPath); + if (!isJsExists) + tryToDownloadJsByCurl(); return is_file_exists(kPlotlyJsWorkPath); } -bool saveStringToFile(const string& file_name, - const string& data) { +void tryToDownloadJsByCurl() { + mayBeCreateJsWorkingFolder(); + std::string cmd = "curl -o " + std::string(dvs::kPlotlyJsWorkPath) + " " + + std::string(dvs::kJsUrlToDownolad); + std::system(cmd.c_str()); +} + +bool saveStringToFile(const string& file_name, const string& data) { std::ofstream out(file_name); if (out.is_open()) { out << data.c_str(); @@ -83,10 +94,7 @@ bool saveStringToFile(const string& file_name, return false; } - -void openPlotlyHtml(const string& file_name) { - openFileBySystem(file_name); -} +void openPlotlyHtml(const string& file_name) { openFileBySystem(file_name); } void sleepMicroSec(unsigned long microsec) { #ifdef _WIN32 @@ -111,17 +119,15 @@ void mayBeCreateJsWorkingFolder() { bool deleteFolder(const char* fname) { struct stat sb; if (stat(fname, &sb) == 0) { - //rmdir(fname); + // rmdir(fname); return true; } else { return false; } } -bool get_data_from_file(const string& path, - vector& result) { - - //TODO different scenarious and sanitizing +bool get_data_from_file(const string& path, vector& result) { + // TODO different scenarious and sanitizing std::setlocale(LC_ALL, "ru_RU.UTF-8"); if (!is_file_exists(path)) { return false; @@ -142,7 +148,8 @@ bool get_data_from_file(const string& path, return true; } -bool readMatrix(vector>& outMatrix, const std::string& path, char dlmtr) { +bool readMatrix(vector>& outMatrix, const std::string& path, + char dlmtr) { outMatrix.clear(); std::setlocale(LC_ALL, "ru_RU.UTF-8"); std::ifstream ifs; @@ -151,7 +158,7 @@ bool readMatrix(vector>& outMatrix, const std::string& path, char if (ifs) { while (!ifs.eof()) { std::getline(ifs, str); - if (str.size() == 0) //if exist empty line + if (str.size() == 0) // if exist empty line continue; std::vector parts = split(str, dlmtr); vector doubleLine; @@ -164,14 +171,14 @@ bool readMatrix(vector>& outMatrix, const std::string& path, char ifs.close(); return true; } else { - std:: cout << "Unable to open file to read: " << path << std::endl; + std::cout << "Unable to open file to read: " << path << std::endl; return false; } } vector split(const string& target, char c) { std::string temp; - std::stringstream stringstream { target }; + std::stringstream stringstream{target}; std::vector result; while (std::getline(stringstream, temp, c)) { result.push_back(temp); @@ -180,9 +187,7 @@ vector split(const string& target, char c) { return result; } -bool make_string(const string& src, - const vector& args, - string& out) { +bool make_string(const string& src, const vector& args, string& out) { if (!out.empty()) { out.clear(); } @@ -195,7 +200,7 @@ bool make_string(const string& src, while (pos < src.size()) { size_t new_pos = src.find('%', pos); if (new_pos == string::npos) { - //out.append(src.substr(pos, src.size() - pos)); + // out.append(src.substr(pos, src.size() - pos)); reserve_size += (src.size() - pos); road_map.push_back({pos, src.size() - pos}); break; @@ -206,24 +211,24 @@ bool make_string(const string& src, while (temp_pos < src.size() && isdigit(src[++temp_pos])) { arg_index += src[temp_pos]; } - //string part = src.substr(pos, new_pos - pos); + // string part = src.substr(pos, new_pos - pos); road_map.push_back({pos, new_pos - pos}); reserve_size += (new_pos - pos); if (!arg_index.empty()) { size_t index = std::stol(arg_index); if (index > 0 && index <= args.size()) { - //part.append(args[index - 1]); + // part.append(args[index - 1]); reserve_size += args[index - 1].size(); road_map.push_back({index - 1}); } else { - //TODO return false or continue + // TODO return false or continue } } else { - //part.append("%"); + // part.append("%"); road_map.push_back({UINT_MAX}); ++reserve_size; } - //out.append(part); + // out.append(part); pos = temp_pos; } // create out according on the road map @@ -240,12 +245,11 @@ bool make_string(const string& src, } } } - //std::cout<<"\n\n"<"<"< ignored_chars = {'+', '-', 'e', 'E', '.', '\r', ','}; std::set unique_chars; bool is_service_char = false; @@ -255,7 +259,6 @@ int find_separator(const std::string& src, size_t dot_counter = 0; for (size_t i = 0; i < src.size(); ++i) { - if (isdigit((unsigned char)src[i])) continue; is_service_char = false; @@ -301,19 +304,17 @@ string removeSpecialCharacters(const string& s) { for (int i = 0; i < s.length(); i++) { if (s[i] == ' ') { t += '_'; - } else if ((s[i] >= 'a' && s[i] <= 'z') - || (s[i] >= 'A' && s[i] <= 'Z') - || (s[i] >= '0' && s[i] <= '9') - || (s[i] == '-') || (s[i] == '_')) { + } else if ((s[i] >= 'a' && s[i] <= 'z') || + (s[i] >= 'A' && s[i] <= 'Z') || + (s[i] >= '0' && s[i] <= '9') || (s[i] == '-') || + (s[i] == '_')) { t += s[i]; } } return t; } - bool is_string_convertable_to_digit(const string& sample) { - try { std::ignore = std::stod(sample); } catch (const std::invalid_argument& e) { @@ -350,7 +351,8 @@ std::string makeUniqueDavisHtmlName() { sleepMicroSec(1); using namespace std::chrono; auto now = system_clock::now(); - auto ms_since_epoch = duration_cast(now.time_since_epoch()).count(); + auto ms_since_epoch = + duration_cast(now.time_since_epoch()).count(); int ms = static_cast(ms_since_epoch % 1000); std::time_t in_time_t = system_clock::to_time_t(now); std::tm tm{}; @@ -361,19 +363,20 @@ std::string makeUniqueDavisHtmlName() { tm = *p; #endif std::ostringstream ss; - ss << std::put_time(&tm, "%Y-%m-%d_%H_%M_%S") - << '_' << std::setfill('0') << std::setw(3) << ms; + ss << std::put_time(&tm, "%Y-%m-%d_%H_%M_%S") << '_' << std::setfill('0') + << std::setw(3) << ms; return ss.str(); } std::string makeUniqueDavisHtmlRelativePath() { string name = makeUniqueDavisHtmlName(); - return std::string("./").append(kOutFolderName).append(name).append(".html"); + return std::string("./") + .append(kOutFolderName) + .append(name) + .append(".html"); } - -void transponeMatrix(std::vector >& matrix) { - +void transponeMatrix(std::vector>& matrix) { if (matrix.empty()) return; @@ -390,7 +393,6 @@ void transponeMatrix(std::vector >& matrix) { } vector calculateAverageVector(const vector>& vectors) { - if (vectors.empty()) { throw std::invalid_argument("Input vector of vectors is empty."); } @@ -414,13 +416,10 @@ vector calculateAverageVector(const vector>& vectors) { } return averageVector; - } - vector calculateStandardDeviation(const vector& mean, const vector>& data) { - std::vector stddev(mean.size(), 0.0); int n = data.size(); for (const auto& vec : data) { @@ -436,7 +435,6 @@ vector calculateStandardDeviation(const vector& mean, } std::string reverseString(const std::string& input) { - std::stringstream ss(input); std::string item; std::vector elements; @@ -458,10 +456,8 @@ std::string reverseString(const std::string& input) { return result; } - vector doubleAndReverse(const vector& input, const vector& mean) { - vector result(input.size(), 0); vector minus_result = input; for (size_t i = 0; i < result.size(); ++i) { @@ -473,7 +469,5 @@ vector doubleAndReverse(const vector& input, return result; } - - -//#STOP_GRAB_TO_DVS_NAMESPACE -}; // namespace dvs +// #STOP_GRAB_TO_DVS_NAMESPACE +}; // namespace dvs diff --git a/common_utils/common_utils.h b/common_utils/common_utils.h index 461aa1a..531b5bf 100644 --- a/common_utils/common_utils.h +++ b/common_utils/common_utils.h @@ -1,22 +1,23 @@ #ifndef COMMON_UTILS_COMMON_UTILS_H_ #define COMMON_UTILS_COMMON_UTILS_H_ #include "../array_core/configurator.h" -//#START_GRAB_TO_INCLUDES_LIST -#include -#include +// #START_GRAB_TO_INCLUDES_LIST +#include + +#include +#include +#include #include #include -#include -#include -#include -#include #include #include +#include #include -//#STOP_GRAB_TO_INCLUDES_LIST +#include +// #STOP_GRAB_TO_INCLUDES_LIST namespace dvs { -//#START_GRAB_TO_DVS_NAMESPACE +// #START_GRAB_TO_DVS_NAMESPACE enum SEPARATOR_RESULT { GOOD_SEPARATOR, MORE_THAN_ONE_SEPARATOR, @@ -35,8 +36,9 @@ void openFileBySystem(const string& file_name); bool isPlotlyScriptExists(); -bool saveStringToFile(const string& file_name, - const string& data); +void tryToDownloadJsByCurl(); + +bool saveStringToFile(const string& file_name, const string& data); void mayBeCreateJsWorkingFolder(); @@ -44,25 +46,22 @@ void sleepMicroSec(unsigned long microsec); void openPlotlyHtml(const string& file_name); -bool get_data_from_file(const string& path, - vector& result); +bool get_data_from_file(const string& path, vector& result); vector split(const string& target, char c); -bool readMatrix(vector>& outMatrix, const string& path, char dlmtr); - +bool readMatrix(vector>& outMatrix, const string& path, + char dlmtr); -bool make_string(const string& src, - const vector& args, - string& out); +bool make_string(const string& src, const vector& args, string& out); // Now it doesn't work. bool deleteFolder(const char* fname); -int find_separator(const std::string& src, - char& separator); +int find_separator(const std::string& src, char& separator); -//! remove special characters except letters, numbers and '-', '_'. Spaces -> '_' +//! remove special characters except letters, numbers and '-', '_'. Spaces -> +//! '_' string removeSpecialCharacters(const string& s); //! convert this cases to string "null" for Plotly @@ -87,7 +86,8 @@ string toStringDotSeparator(T data) { //! save to disk vector data template -bool saveVec(const vector& vec, const string& filename, dv::configSaveToDisk config) { +bool saveVec(const vector& vec, const string& filename, + dv::configSaveToDisk config) { if (vec.size() == 0) { return false; } @@ -105,7 +105,8 @@ bool saveVec(const vector& vec, const string& filename, dv::configSaveToDisk //! save to disk vector> data template -bool saveVecVec(const vector>& vecVec, const string& filename, dv::configSaveToDisk config) { +bool saveVecVec(const vector>& vecVec, const string& filename, + dv::configSaveToDisk config) { if (vecVec.size() == 0) { return false; } else if (vecVec.at(0).size() == 0) { @@ -123,7 +124,7 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con for (int j = 0; j < cols; ++j) { double val = vecVec.at(j).at(i); fout << val; - if (j < cols - 1) { // we dont need sep at row end + if (j < cols - 1) { // we dont need sep at row end fout << config.separatorOfCols; } } @@ -136,7 +137,7 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con for (size_t j = 0; j < cols; ++j) { double val = vecVec.at(i).at(j); fout << val; - if (j < cols - 1) { // we dont need sep at row end + if (j < cols - 1) { // we dont need sep at row end fout << config.separatorOfCols; } } @@ -148,13 +149,17 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con } //! convert any container to std::vector -template()))>::type, - typename Enable = typename std::enable_if::value>::type> +template < + typename G, + typename C, // https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599 + typename T = + typename std::decay()))>::type, + typename Enable = + typename std::enable_if::value>::type > std::vector makeVecFromContainer(const C& container) { std::vector vec; - vec.reserve(static_cast(std::distance(std::begin(container), std::end(container)))); + vec.reserve(static_cast( + std::distance(std::begin(container), std::end(container)))); for (auto const& v : container) { vec.push_back(static_cast(v)); } @@ -163,7 +168,9 @@ std::vector makeVecFromContainer(const C& container) { //! convert T** to std::vector template -inline std::vector> makeVecVecFromRowPtr(const T* const* data, uint64_t rows, uint64_t cols) { +inline std::vector> makeVecVecFromRowPtr(const T* const* data, + uint64_t rows, +uint64_t cols) { std::vector> res; res.reserve(rows); for (uint64_t i = 0; i < rows; ++i) { @@ -179,7 +186,9 @@ inline std::vector> makeVecVecFromRowPtr(const T* const* data, ui //! convert pseudo 2D T* to std::vector template -inline std::vector> makeVecVecFromFlat(const T* data, uint64_t rows, uint64_t cols) { +inline std::vector> makeVecVecFromFlat(const T* data, + uint64_t rows, +uint64_t cols) { std::vector> res; res.reserve(rows); for (uint64_t i = 0; i < rows; ++i) { @@ -212,12 +221,11 @@ vector calculateAverageVector(const vector>& vectors); vector calculateStandardDeviation(const vector& mean, const vector>& data); -vector doubleAndReverse(const vector& input, const vector& mean); - +vector doubleAndReverse(const vector& input, + const vector& mean); std::string reverseString(const std::string& input); - template std::vector> readBinaryFile(const std::string& filePath, size_t rowLength) { @@ -229,7 +237,8 @@ size_t rowLength) { std::vector> data; std::vector row(rowLength); - while (file.read(reinterpret_cast(row.data()), rowLength * sizeof(T))) { + while ( + file.read(reinterpret_cast(row.data()), rowLength * sizeof(T))) { data.push_back(row); } @@ -243,7 +252,7 @@ size_t rowLength) { return data; } -//#STOP_GRAB_TO_DVS_NAMESPACE -}; // namespace dvs +// #STOP_GRAB_TO_DVS_NAMESPACE +}; // namespace dvs -#endif // COMMON_UTILS_COMMON_UTILS_H_ +#endif // COMMON_UTILS_COMMON_UTILS_H_ diff --git a/davis_one/davis.cpp b/davis_one/davis.cpp index 56a6740..26f70e6 100644 --- a/davis_one/davis.cpp +++ b/davis_one/davis.cpp @@ -9,6 +9,7 @@ #include "davis.h" + #include #include #include @@ -35,6 +36,8 @@ const char kPlotlyJsResourcePath[] = "plotly_maker/plotly-2.32.0.min.js"; const char kWarningPagePath[] = "./davis_htmls/warning_js_absent.html"; const char kReportPagePath[] = "./davis_htmls/report.html"; const char kCloudPagePath[] = "./davis_htmls/cloud_of_points.html"; +const char kJsUrlToDownolad[] = + "https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.32.0/plotly.min.js"; } // namespace dvs end @@ -779,7 +782,7 @@ using std::string; #ifdef _WIN32 #include #include - #define getcwd _getcwd // stupid MSFT "deprecation" warning + #define getcwd _getcwd // stupid MSFT "deprecation" warning #elif __linux__ #include #endif @@ -808,7 +811,7 @@ void openFileBySystem(const string& file_name) { } string getCurrentPath() { -#if defined (_WIN32) || (__linux__) +#if defined(_WIN32) || (__linux__) char buffer[1024]; char* answer = getcwd(buffer, sizeof(buffer)); string s_cwd; @@ -817,17 +820,26 @@ string getCurrentPath() { } return s_cwd; #elif __APPLE__ - //TODO macos get current path implementation + // TODO macos get current path implementation return ""; #endif } bool isPlotlyScriptExists() { + bool isJsExists = is_file_exists(kPlotlyJsWorkPath); + if (!isJsExists) + tryToDownloadJsByCurl(); return is_file_exists(kPlotlyJsWorkPath); } -bool saveStringToFile(const string& file_name, - const string& data) { +void tryToDownloadJsByCurl() { + mayBeCreateJsWorkingFolder(); + std::string cmd = "curl -o " + std::string(dvs::kPlotlyJsWorkPath) + " " + + std::string(dvs::kJsUrlToDownolad); + std::system(cmd.c_str()); +} + +bool saveStringToFile(const string& file_name, const string& data) { std::ofstream out(file_name); if (out.is_open()) { out << data.c_str(); @@ -837,10 +849,7 @@ bool saveStringToFile(const string& file_name, return false; } - -void openPlotlyHtml(const string& file_name) { - openFileBySystem(file_name); -} +void openPlotlyHtml(const string& file_name) { openFileBySystem(file_name); } void sleepMicroSec(unsigned long microsec) { #ifdef _WIN32 @@ -865,17 +874,15 @@ void mayBeCreateJsWorkingFolder() { bool deleteFolder(const char* fname) { struct stat sb; if (stat(fname, &sb) == 0) { - //rmdir(fname); + // rmdir(fname); return true; } else { return false; } } -bool get_data_from_file(const string& path, - vector& result) { - - //TODO different scenarious and sanitizing +bool get_data_from_file(const string& path, vector& result) { + // TODO different scenarious and sanitizing std::setlocale(LC_ALL, "ru_RU.UTF-8"); if (!is_file_exists(path)) { return false; @@ -896,7 +903,8 @@ bool get_data_from_file(const string& path, return true; } -bool readMatrix(vector>& outMatrix, const std::string& path, char dlmtr) { +bool readMatrix(vector>& outMatrix, const std::string& path, + char dlmtr) { outMatrix.clear(); std::setlocale(LC_ALL, "ru_RU.UTF-8"); std::ifstream ifs; @@ -905,7 +913,7 @@ bool readMatrix(vector>& outMatrix, const std::string& path, char if (ifs) { while (!ifs.eof()) { std::getline(ifs, str); - if (str.size() == 0) //if exist empty line + if (str.size() == 0) // if exist empty line continue; std::vector parts = split(str, dlmtr); vector doubleLine; @@ -918,14 +926,14 @@ bool readMatrix(vector>& outMatrix, const std::string& path, char ifs.close(); return true; } else { - std:: cout << "Unable to open file to read: " << path << std::endl; + std::cout << "Unable to open file to read: " << path << std::endl; return false; } } vector split(const string& target, char c) { std::string temp; - std::stringstream stringstream { target }; + std::stringstream stringstream{target}; std::vector result; while (std::getline(stringstream, temp, c)) { result.push_back(temp); @@ -934,9 +942,7 @@ vector split(const string& target, char c) { return result; } -bool make_string(const string& src, - const vector& args, - string& out) { +bool make_string(const string& src, const vector& args, string& out) { if (!out.empty()) { out.clear(); } @@ -949,7 +955,7 @@ bool make_string(const string& src, while (pos < src.size()) { size_t new_pos = src.find('%', pos); if (new_pos == string::npos) { - //out.append(src.substr(pos, src.size() - pos)); + // out.append(src.substr(pos, src.size() - pos)); reserve_size += (src.size() - pos); road_map.push_back({pos, src.size() - pos}); break; @@ -960,24 +966,24 @@ bool make_string(const string& src, while (temp_pos < src.size() && isdigit(src[++temp_pos])) { arg_index += src[temp_pos]; } - //string part = src.substr(pos, new_pos - pos); + // string part = src.substr(pos, new_pos - pos); road_map.push_back({pos, new_pos - pos}); reserve_size += (new_pos - pos); if (!arg_index.empty()) { size_t index = std::stol(arg_index); if (index > 0 && index <= args.size()) { - //part.append(args[index - 1]); + // part.append(args[index - 1]); reserve_size += args[index - 1].size(); road_map.push_back({index - 1}); } else { - //TODO return false or continue + // TODO return false or continue } } else { - //part.append("%"); + // part.append("%"); road_map.push_back({UINT_MAX}); ++reserve_size; } - //out.append(part); + // out.append(part); pos = temp_pos; } // create out according on the road map @@ -994,12 +1000,11 @@ bool make_string(const string& src, } } } - //std::cout<<"\n\n"<"<"< ignored_chars = {'+', '-', 'e', 'E', '.', '\r', ','}; std::set unique_chars; bool is_service_char = false; @@ -1009,7 +1014,6 @@ int find_separator(const std::string& src, size_t dot_counter = 0; for (size_t i = 0; i < src.size(); ++i) { - if (isdigit((unsigned char)src[i])) continue; is_service_char = false; @@ -1055,19 +1059,17 @@ string removeSpecialCharacters(const string& s) { for (int i = 0; i < s.length(); i++) { if (s[i] == ' ') { t += '_'; - } else if ((s[i] >= 'a' && s[i] <= 'z') - || (s[i] >= 'A' && s[i] <= 'Z') - || (s[i] >= '0' && s[i] <= '9') - || (s[i] == '-') || (s[i] == '_')) { + } else if ((s[i] >= 'a' && s[i] <= 'z') || + (s[i] >= 'A' && s[i] <= 'Z') || + (s[i] >= '0' && s[i] <= '9') || (s[i] == '-') || + (s[i] == '_')) { t += s[i]; } } return t; } - bool is_string_convertable_to_digit(const string& sample) { - try { std::ignore = std::stod(sample); } catch (const std::invalid_argument& e) { @@ -1104,7 +1106,8 @@ std::string makeUniqueDavisHtmlName() { sleepMicroSec(1); using namespace std::chrono; auto now = system_clock::now(); - auto ms_since_epoch = duration_cast(now.time_since_epoch()).count(); + auto ms_since_epoch = + duration_cast(now.time_since_epoch()).count(); int ms = static_cast(ms_since_epoch % 1000); std::time_t in_time_t = system_clock::to_time_t(now); std::tm tm{}; @@ -1115,19 +1118,20 @@ std::string makeUniqueDavisHtmlName() { tm = *p; #endif std::ostringstream ss; - ss << std::put_time(&tm, "%Y-%m-%d_%H_%M_%S") - << '_' << std::setfill('0') << std::setw(3) << ms; + ss << std::put_time(&tm, "%Y-%m-%d_%H_%M_%S") << '_' << std::setfill('0') + << std::setw(3) << ms; return ss.str(); } std::string makeUniqueDavisHtmlRelativePath() { string name = makeUniqueDavisHtmlName(); - return std::string("./").append(kOutFolderName).append(name).append(".html"); + return std::string("./") + .append(kOutFolderName) + .append(name) + .append(".html"); } - -void transponeMatrix(std::vector >& matrix) { - +void transponeMatrix(std::vector>& matrix) { if (matrix.empty()) return; @@ -1144,7 +1148,6 @@ void transponeMatrix(std::vector >& matrix) { } vector calculateAverageVector(const vector>& vectors) { - if (vectors.empty()) { throw std::invalid_argument("Input vector of vectors is empty."); } @@ -1168,13 +1171,10 @@ vector calculateAverageVector(const vector>& vectors) { } return averageVector; - } - vector calculateStandardDeviation(const vector& mean, const vector>& data) { - std::vector stddev(mean.size(), 0.0); int n = data.size(); for (const auto& vec : data) { @@ -1190,7 +1190,6 @@ vector calculateStandardDeviation(const vector& mean, } std::string reverseString(const std::string& input) { - std::stringstream ss(input); std::string item; std::vector elements; @@ -1212,10 +1211,8 @@ std::string reverseString(const std::string& input) { return result; } - vector doubleAndReverse(const vector& input, const vector& mean) { - vector result(input.size(), 0); vector minus_result = input; for (size_t i = 0; i < result.size(); ++i) { @@ -1228,8 +1225,6 @@ vector doubleAndReverse(const vector& input, } - - } // namespace dvs end namespace dvs { diff --git a/davis_one/davis.h b/davis_one/davis.h index e0f21c9..e68e6ae 100644 --- a/davis_one/davis.h +++ b/davis_one/davis.h @@ -10,6 +10,7 @@ #ifndef DAVIS_H_ #define DAVIS_H_ + #include #include #include @@ -31,6 +32,7 @@ extern const char kPlotlyJsWorkPath[]; extern const char kWarningPagePath[]; extern const char kReportPagePath[]; extern const char kCloudPagePath[]; +extern const char kJsUrlToDownolad[]; } // namespace dvs end @@ -251,8 +253,9 @@ void openFileBySystem(const string& file_name); bool isPlotlyScriptExists(); -bool saveStringToFile(const string& file_name, - const string& data); +void tryToDownloadJsByCurl(); + +bool saveStringToFile(const string& file_name, const string& data); void mayBeCreateJsWorkingFolder(); @@ -260,25 +263,22 @@ void sleepMicroSec(unsigned long microsec); void openPlotlyHtml(const string& file_name); -bool get_data_from_file(const string& path, - vector& result); +bool get_data_from_file(const string& path, vector& result); vector split(const string& target, char c); -bool readMatrix(vector>& outMatrix, const string& path, char dlmtr); - +bool readMatrix(vector>& outMatrix, const string& path, + char dlmtr); -bool make_string(const string& src, - const vector& args, - string& out); +bool make_string(const string& src, const vector& args, string& out); // Now it doesn't work. bool deleteFolder(const char* fname); -int find_separator(const std::string& src, - char& separator); +int find_separator(const std::string& src, char& separator); -//! remove special characters except letters, numbers and '-', '_'. Spaces -> '_' +//! remove special characters except letters, numbers and '-', '_'. Spaces -> +//! '_' string removeSpecialCharacters(const string& s); //! convert this cases to string "null" for Plotly @@ -303,7 +303,8 @@ string toStringDotSeparator(T data) { //! save to disk vector data template -bool saveVec(const vector& vec, const string& filename, dv::configSaveToDisk config) { +bool saveVec(const vector& vec, const string& filename, + dv::configSaveToDisk config) { if (vec.size() == 0) { return false; } @@ -321,7 +322,8 @@ bool saveVec(const vector& vec, const string& filename, dv::configSaveToDisk //! save to disk vector> data template -bool saveVecVec(const vector>& vecVec, const string& filename, dv::configSaveToDisk config) { +bool saveVecVec(const vector>& vecVec, const string& filename, + dv::configSaveToDisk config) { if (vecVec.size() == 0) { return false; } else if (vecVec.at(0).size() == 0) { @@ -339,7 +341,7 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con for (int j = 0; j < cols; ++j) { double val = vecVec.at(j).at(i); fout << val; - if (j < cols - 1) { // we dont need sep at row end + if (j < cols - 1) { // we dont need sep at row end fout << config.separatorOfCols; } } @@ -352,7 +354,7 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con for (size_t j = 0; j < cols; ++j) { double val = vecVec.at(i).at(j); fout << val; - if (j < cols - 1) { // we dont need sep at row end + if (j < cols - 1) { // we dont need sep at row end fout << config.separatorOfCols; } } @@ -364,13 +366,17 @@ bool saveVecVec(const vector>& vecVec, const string& filename, dv::con } //! convert any container to std::vector -template()))>::type, - typename Enable = typename std::enable_if::value>::type> +template < + typename G, + typename C, // https://devblogs.microsoft.com/oldnewthing/20190619-00/?p=102599 + typename T = + typename std::decay()))>::type, + typename Enable = + typename std::enable_if::value>::type > std::vector makeVecFromContainer(const C& container) { std::vector vec; - vec.reserve(static_cast(std::distance(std::begin(container), std::end(container)))); + vec.reserve(static_cast( + std::distance(std::begin(container), std::end(container)))); for (auto const& v : container) { vec.push_back(static_cast(v)); } @@ -379,7 +385,9 @@ std::vector makeVecFromContainer(const C& container) { //! convert T** to std::vector template -inline std::vector> makeVecVecFromRowPtr(const T* const* data, uint64_t rows, uint64_t cols) { +inline std::vector> makeVecVecFromRowPtr(const T* const* data, + uint64_t rows, +uint64_t cols) { std::vector> res; res.reserve(rows); for (uint64_t i = 0; i < rows; ++i) { @@ -395,7 +403,9 @@ inline std::vector> makeVecVecFromRowPtr(const T* const* data, ui //! convert pseudo 2D T* to std::vector template -inline std::vector> makeVecVecFromFlat(const T* data, uint64_t rows, uint64_t cols) { +inline std::vector> makeVecVecFromFlat(const T* data, + uint64_t rows, +uint64_t cols) { std::vector> res; res.reserve(rows); for (uint64_t i = 0; i < rows; ++i) { @@ -428,12 +438,11 @@ vector calculateAverageVector(const vector>& vectors); vector calculateStandardDeviation(const vector& mean, const vector>& data); -vector doubleAndReverse(const vector& input, const vector& mean); - +vector doubleAndReverse(const vector& input, + const vector& mean); std::string reverseString(const std::string& input); - template std::vector> readBinaryFile(const std::string& filePath, size_t rowLength) { @@ -445,7 +454,8 @@ size_t rowLength) { std::vector> data; std::vector row(rowLength); - while (file.read(reinterpret_cast(row.data()), rowLength * sizeof(T))) { + while ( + file.read(reinterpret_cast(row.data()), rowLength * sizeof(T))) { data.push_back(row); }