string unlines2(strings elems) { stringstream ss; std::for_each(elems.begin(), elems.end(), [&ss](const string& s) { ss << s << std::endl; }); return ss.str(); }
std::map<std::string, int> encount(std::istream *in, const strings &searched) { std::map<std::string,int> result; std::vector<s_info> watched; // insert all string from searched with 0 frequency for (s_citer iter = searched.begin(), end = searched.end(); iter != end; ++iter) { result[*iter] = 0; if (iter->begin() != iter->end()) watched.push_back(s_info(&(*iter))); } char ch; while (in->get(ch)) { for (std::vector<s_info>::iterator iter = watched.begin(), end = watched.end(); iter != end; ++iter) { if (ch == iter->curr()) { iter->current++; if (iter->isEnd()) { result[*(iter->pstr)]++; iter->reset(); } } else iter->reset(); } } return result; }
void engine::filedrop_attachments_impl(std::string server, const std::string& key, const std::string& user, const std::string& subject, const std::string& message, const strings& fs, report_level s) { curl_easy_setopt(m_curl, CURLOPT_URL, server.c_str()); curl_header_guard hg(m_curl); std::string data = std::string( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\ <message>\ <api_key>") + key + std::string("</api_key>\ <from>") + user + std::string("</from>\ <subject>") + subject + std::string("</subject>\ <message>") + message + std::string("</message>\ <attachments type='array'>\ "); for (strings::const_iterator i = fs.begin(); i != fs.end(); ++i) { data += " <attachment>"; data += *i; data += "</attachment>\n"; } data += " </attachments>\ </message>\n"; curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, 0); curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, data.c_str()); if (s >= NORMAL) { io::mout << "Sending message to filedrop" << io::endl; } process_filedrop_responce(perform(), s); }
void Tpresets::listRegKeys(strings &l) { l.clear(); HKEY hKey; char_t rkey[MAX_PATH]; tsnprintf_s(rkey, countof(rkey), _TRUNCATE, FFDSHOW_REG_PARENT _l("\\%s"), reg_child); RegOpenKeyEx(HKEY_CURRENT_USER,rkey,0,KEY_READ,&hKey); for (int i=0,retCode=ERROR_SUCCESS; retCode==ERROR_SUCCESS; i++) { char_t keyName[256]; DWORD keyNameSize=255; FILETIME ftLastWriteTime; retCode = RegEnumKeyEx(hKey, i, keyName, &keyNameSize, NULL, NULL, NULL, &ftLastWriteTime ); if (retCode==ERROR_SUCCESS) { l.push_back(ffstring(keyName)); } else { break; } } RegCloseKey(hKey); }
void GUIPlayer::listOggfiles(const string& dirPath, strings& oggFiles) { DIR* dir; if ((dir = opendir(dirPath.data())) == NULL) { return; } struct dirent* entry; while ((entry = readdir(dir)) != NULL) { string name = entry->d_name; if (StringHelper::endsWith(upperCase(name), ".OGG")) { name = "/" + name; name = dirPath + name; oggFiles.push_back(name); } } // hack // we hope startup.ogg to be the first. reverse(oggFiles.begin(), oggFiles.end()); closedir(dir); }
void HttpInterface::evReset(HttpRequest* req, strings& args) { for (NodesMap::iterator descIt = nodes.begin(); descIt != nodes.end(); ++descIt) { bool ok; nodeId = getNodeId(descIt->second.name, 0, &ok); if (!ok) continue; string nodeName = WStringToUTF8(descIt->second.name); Reset(nodeId).serialize(asebaStream); // reset node asebaStream->flush(); Run(nodeId).serialize(asebaStream); // re-run node asebaStream->flush(); if (nodeName.find("thymio-II") == 0) { strings args; args.push_back("motor.left.target"); args.push_back("0"); sendSetVariable(nodeName, args); args[0] = "motor.right.target"; sendSetVariable(nodeName, args); } size_t eventPos; if (commonDefinitions.events.contains(UTF8ToWString("reset"), &eventPos)) { strings data; data.push_back("reset"); sendEvent(nodeName,data); } finishResponse(req, 200, ""); } }
int Twindow::selectFromMenu(const strings &items,int id,bool translate,int columnHeight) { Tstrptrs ptritems; for (strings::const_iterator s=items.begin(); s!=items.end(); s++) { ptritems.push_back(s->c_str()); } ptritems.push_back(NULL); return selectFromMenu(&ptritems[0],id,translate,columnHeight); }
bool nsUtils::runCmd(const string& cmd, const strings& input, strings& output) { int childStdIn[2]; // child read 0, parent write 1 int childStdOut[2]; // parent read 0, child write 1 if (pipe(childStdIn) != 0) { std::cerr << "Failed to create pipe 1\n"; return false; } if (pipe(childStdOut) != 0) { std::cerr << "Failed to create pipe 2\n"; return false; } pid_t child(fork()); if (child == -1) { std::cerr << "Failed to fork\n"; close(childStdIn[0]); close(childStdIn[1]); close(childStdOut[0]); close(childStdOut[1]); return false; } if (child == 0) { // child close(childStdIn[1]); close(childStdOut[0]); dup2(childStdIn[0], 0); dup2(childStdOut[1], 1); execl(cmd.c_str(), cmd.c_str(), NULL); exit(1); } // parent close(childStdIn[0]); close(childStdOut[1]); char lineEnd('\n'); for (size_t i = 0; i < input.size(); ++i) { write(childStdIn[1], input[i].c_str(), input[i].length()); write(childStdIn[1], &lineEnd, 1); } close(childStdIn[1]); ssize_t readCount; const int bufferSize(512); char buffer[bufferSize + 1]; while ((readCount = read(childStdOut[0], buffer, bufferSize)) > 0) { buffer[readCount] = '\0'; if (buffer[readCount - 1] == '\n') { buffer[readCount - 1] = '\0'; } output.push_back(string(buffer)); } close(childStdOut[0]); wait(NULL); return true; }
static void fixRelativePaths(strings &dirs, const char_t *basepath) { for (strings::iterator d = dirs.begin(); d != dirs.end(); d++) if (PathIsRelative(d->c_str())) { char_t absdir[MAX_PATH]; _makepath_s(absdir, countof(absdir), NULL, basepath, d->c_str(), NULL); char_t absdirC[MAX_PATH]; PathCanonicalize(absdirC, absdir); *d = absdirC; } }
void engine::attach(std::string server, const std::string& key, const strings& fs, report_level s, validate_cert v) { init_curl(key, s, v); strings::const_iterator i = fs.begin(); for (; i != fs.end(); ++i) { attach_impl(server, *i, s); } }
void TsubtitlesFile::findPossibleSubtitles(const char_t *dir, strings &files) { char_t autosubmask[MAX_PATH]; _makepath_s(autosubmask, MAX_PATH, NULL, dir,/*name*/_l("*"), _l("*")); strings files0; findFiles(autosubmask, files0); for (strings::iterator f = files0.begin(); f != files0.end(); f++) if (extMatch(f->c_str())) { files.push_back(*f); } std::sort(files.begin(), files.end(), ffstring_iless()); }
void load_volumes(strings& s,grids& vols,int skip,bool verbose) { vols.resize(s.size()-skip); //first column is eigenvalue for(int i=0;i<(s.size()-skip);i++) { /*if(verbose) std::cout<<s[i+skip].c_str()<<" "<<std::flush;*/ minc_1_reader rdr; rdr.open(s[i+skip].c_str()); load_simple_volume(rdr,vols[i]); } }
const std::string join (const strings& ss, const std::string& separator) { std::string s; for (strings::const_iterator ssi = ss.begin (); ssi != ss.end (); ++ssi) { s += *ssi; if ((ssi + 1) != ss.end ()) s += separator; } return s; }
void save_volumes(strings& s,volumes& vols,const char *like,const std::string & append_history) { for(int i=0;i<s.size();i++) { save_volume(vols[i],s[i].c_str(),like,append_history); } }
int getDirectoryContent(const std::string& dir, strings &files, bool recursive) { DIR *dp; struct dirent *dirp; strings todo; if((dp = opendir(dir.c_str())) == NULL) { return errno; } while ((dirp = readdir(dp)) != NULL) { if (dirp->d_type == DT_REG) { files.push_back(dir + "/" + std::string(dirp->d_name)); } else if (recursive && dirp->d_type == DT_DIR) { if (dirp->d_name[0] != '.') { todo.push_back(dir + "/" + dirp->d_name); } } } closedir(dp); for (size_t u = 0; u < todo.size(); u++) { getDirectoryContent(todo[u], files, recursive); } return 0; }
void load_volumes(strings& s,volumes& averages,int skip,bool verbose,bool ignore_missing) { averages.resize(s.size()-skip); //first column is eigenvalue for(int i=0;i<(s.size()-skip);i++) { minc_1_reader rdr; try { rdr.open(s[i+skip].c_str()); load_simple_volume<float>(rdr,averages[i]); } catch(const minc::generic_error & err) { if(!ignore_missing) throw; } } }
RevKBCCTrainer::reverbdpoint RevKBCCTrainer::ReverberateNetwork( strings subView /*= strings(0)*/ ) { //changing the clean reverb to only threshold the outputs. //if the numcontexts is set above 0 then we'll get them from the dataset. int inSize = revNet->GetNumIn(); if (revparams.numContexts > 0 || subView.size() > 0) { //assert(revparams.numContexts > 0); if(revparams.numContexts == 0) { revparams.numContexts = subView.size(); } inSize -= revparams.numContexts; } vecDouble tmpIn(inSize); for (uint i = 0; i < tmpIn.size(); i++) { tmpIn[i] = revRand(); } if (revparams.numContexts > 0) { vecDouble randContext; if (subView.size() > 0) { randContext = dynamic_pointer_cast<CSMTLDataset>(data)->GetRandContext(subView); } else { randContext = dynamic_pointer_cast<CSMTLDataset>(data)->GetRandContext(); } tmpIn = ConcatVec(tmpIn,randContext); } revNet->Activate(tmpIn); for (uint i = 1; i < revparams.numRev; i++) { tmpIn = revNet->GetAutoAssocLayer()->GetOutput(); revNet->Activate(tmpIn); } reverbdpoint retVal; retVal.inPoint = tmpIn; retVal.outPoint = revNet->GetNormOutLayer()->GetOutput(); if(revparams.cleanReverb) { //This shouldn't be used when the outputs are real. retVal.outPoint = ApplyThreshold(retVal.outPoint); } #ifdef _DEBUG cout << "in: " << retVal.inPoint << "out" << retVal.outPoint << endl; #endif return retVal; }
void HttpInterface::evSubscribe(HttpRequest* req, strings& args) { // eventSubscriptions[conn] is an unordered set of strings if (args.size() == 1) eventSubscriptions[req].insert("*"); else for (strings::iterator i = args.begin()+1; i != args.end(); ++i) eventSubscriptions[req].insert(*i); strings headers; headers.push_back("Content-Type: text/event-stream"); headers.push_back("Cache-Control: no-cache"); headers.push_back("Connection: keep-alive"); addHeaders(req, headers); appendResponse(req,200,true,""); // connection must stay open! }
string join(strings strs, string glue) { string result; for (auto it : strs) { if (it != *strs.begin()) result += glue; result += it; } return result; }
std::string engine::send(std::string server, const std::string& key, const std::string& user, const std::string& subject, const std::string& message, const strings& fs, report_level s, validate_cert v) { init_curl(key, s, v); std::set<std::string> attachments; strings::const_iterator i = fs.begin(); for (; i != fs.end(); ++i) { std::string a = attach_impl(server, *i, s); attachments.insert(a); } return send_attachments_impl(server, user, subject, message, attachments, s); }
void str2argv(const string src, strings &argv) { string arg; enum state { SYMBOL, SKIP, SKIP_UNTIL } s = SYMBOL; for (string::const_iterator i = src.begin(); i != src.end(); ++i) { switch (*i) { case '\\': s = SKIP; break; case '"': if (s == SKIP_UNTIL) s = SYMBOL; else s = SKIP_UNTIL; break; case ' ': switch (s) { case SKIP: s = SYMBOL; arg += *i; break; case SKIP_UNTIL: arg += *i; break; default: argv.push_back(arg); arg.clear(); break; } break; default: if (s == SKIP) s = SYMBOL; arg += *i; break; } } if (!arg.empty()) argv.push_back(arg); }
void HttpInterface::sendSetVariable(const std::string nodeName, const strings& args) { // get node id, variable position and length if (verbose) cerr << "setVariables " << nodeName << " " << args[0]; unsigned nodePos, varPos; const bool exists(getNodeAndVarPos(nodeName, args[0], nodePos, varPos)); if (!exists) return; if (verbose) cerr << " (" << nodePos << "," << varPos << "):" << args.size()-1 << endl; // send the message SetVariables::VariablesVector data; for (size_t i=1; i<args.size(); ++i) data.push_back(atoi(args[i].c_str())); SetVariables setVariables(nodePos, varPos, data); setVariables.serialize(asebaStream); asebaStream->flush(); }
void engine::filedrop(std::string server, const std::string& user, const std::string& subject, const std::string& message, const strings& fs, report_level s, validate_cert v) { std::string key = get_filedrop_api_key(server, s, v); init_curl(key, s, v); strings::const_iterator i = fs.begin(); strings rs; std::string url = get_server_from_filedrop(server); for (; i != fs.end(); ++i) { std::string id = attach_impl(url, *i, s); rs.insert(id); } curl_easy_setopt(m_curl, CURLOPT_USERPWD, ""); filedrop_attachments_impl(server, key, user, subject, message, rs, s); }
strings findAnagrams(const strings &words, const string &word) { unordered_multimap<string, string> hashed; string tmpWord; for (auto iter = words.begin(); iter != words.end(); iter++) { tmpWord = (*iter); sort(tmpWord.begin(), tmpWord.end()); hashed.insert(make_pair(tmpWord, *iter)); } tmpWord = word; sort(tmpWord.begin(), tmpWord.end()); auto range = hashed.equal_range(tmpWord); strings result; while (range.first != range.second) { result.push_back(range.first->second); range.first++; } return result; }
void PlaygroundViewer::notifyAsebaEnvironment(const EnvironmentNotificationType type, const std::string& description, const strings& arguments) { if (type == EnvironmentNotificationType::DISPLAY_INFO) { if (description == "missing Thymio2 feature") { addInfoMessage(tr("You are using a feature not available in the simulator, click here to buy a real Thymio."), 5.0, Qt::blue, QUrl(tr("https://www.thymio.org/en:thymiobuy"))); } else qDebug() << "Unknown description for notifying DISPLAY_INFO" << QString::fromStdString(description); } else if ((type == EnvironmentNotificationType::LOG_INFO) || (type == EnvironmentNotificationType::LOG_WARNING) || (type == EnvironmentNotificationType::LOG_ERROR)) { if (description == "cannot read from socket") { log(tr("Target %0, cannot read from socket: %1").arg(QString::fromStdString(arguments.at(0))).arg(QString::fromStdString(arguments.at(1))), notificationLogTypeToColor.at(type)); } else if (description == "new client connected") { log(tr("New client connected from %0").arg(QString::fromStdString(arguments.at(0))), notificationLogTypeToColor.at(type)); } else if (description == "cannot read from socket") { log(tr("Target %0, cannot read from socket: %1").arg(QString::fromStdString(arguments.at(0))).arg(QString::fromStdString(arguments.at(1))), notificationLogTypeToColor.at(type)); } else if (description == "client disconnected properly") { log(tr("Client disconnected properly from %0").arg(QString::fromStdString(arguments.at(0))), notificationLogTypeToColor.at(type)); } else if (description == "client disconnected abnormally") { log(tr("Client disconnected abnormally from %0").arg(QString::fromStdString(arguments.at(0))), notificationLogTypeToColor.at(type)); } else if (description == "old client disconnected") { log(tr("Old client disconnected from %0").arg(QString::fromStdString(arguments.at(0))), notificationLogTypeToColor.at(type)); } else { QString fullDescription(QString::fromStdString(description)); for (const auto& argument: arguments) fullDescription += " " + QString::fromStdString(argument); log(fullDescription, notificationLogTypeToColor.at(type)); } } else if (type == EnvironmentNotificationType::FATAL_ERROR) { if (description == "cannot create listening port") { QMessageBox::critical(0, "Aseba Playground", tr("Cannot create listening port %0: %1").arg(QString::fromStdString(arguments.at(0))).arg(QString::fromStdString(arguments.at(1)))); abort(); } else qDebug() << "Unknown description for notifying FATAL_ERROR" << QString::fromStdString(description); } }
void Shell::setVariable(const strings& args) { // check that there are enough arguments if (args.size() < 4) { wcerr << "missing argument, usage: set NODE_NAME VAR_NAME VAR_DATA+" << endl; return; } // get node id, variable position and length unsigned nodeId, pos; const bool exists(getNodeAndVarPos(args[1], args[2], nodeId, pos)); if (!exists) return; // send the message VariablesDataVector data; for (size_t i=3; i<args.size(); ++i) data.push_back(atoi(args[i].c_str())); SetVariables setVariables(nodeId, pos, data); setVariables.serialize(targetStream); targetStream->flush(); }
void filterOnlyImages(strings& files) { strings out; // O(N) for (size_t u = 0; u < files.size(); u++) { if (isSupportedImageType(files[u])) { out.push_back(files[u]); } } files = out; }
// Utility: extract argument values from JSON request body void HttpInterface::parse_json_form(std::string content, strings& values) { std::string buffer = content; buffer.erase(std::remove_if(buffer.begin(), buffer.end(), ::isspace), buffer.end()); std::stringstream ss(buffer); if (ss.get() == '[') { int i; while (ss >> i) { // values.push_back(std::to_string(short(i))); std::stringstream valss; valss << short(i); values.push_back(valss.str()); if (ss.peek() == ']') break; else if (ss.peek() == ',') ss.ignore(); } if (ss.get() != ']') values.erase(values.begin(),values.end()); }
void Shell::emit(const strings& args) { // check that there are enough arguments if (args.size() < 2) { wcerr << "missing argument, usage: emit EVENT_NAME EVENT_DATA*" << endl; return; } size_t pos; if (!commonDefinitions.events.contains(UTF8ToWString(args[1]), &pos)) { wcerr << "event " << UTF8ToWString(args[1]) << " is unknown" << endl; return; } // build event and emit VariablesDataVector data; for (size_t i=2; i<args.size(); ++i) data.push_back(atoi(args[i].c_str())); UserMessage userMessage(pos, data); userMessage.serialize(targetStream); targetStream->flush(); }
std::pair<unsigned,unsigned> HttpInterface::sendGetVariables(const std::string nodeName, const strings& args) { unsigned nodePos, varPos; for (strings::const_iterator it(args.begin()); it != args.end(); ++it) { // get node id, variable position and length if (verbose) cerr << "getVariables " << nodeName << " " << *it; const bool exists(getNodeAndVarPos(nodeName, *it, nodePos, varPos)); if (!exists) continue; VariablesMap vm = allVariables[nodeName]; const unsigned length(vm[UTF8ToWString(*it)].second); if (verbose) cerr << " (" << nodePos << "," << varPos << "):" << length << "\n"; // send the message GetVariables getVariables(nodePos, varPos, length); getVariables.serialize(asebaStream); } asebaStream->flush(); return std::pair<unsigned,unsigned>(nodePos,varPos); // just last one }