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; }
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 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 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); }
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); }
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 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 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 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! }
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); }
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 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); }
// 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()); }
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 }
std::string engine::send_attachments_impl(std::string server, const std::string& user, const std::string& subject, const std::string& message, const strings& fs, report_level s) { server += "/message"; 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>\ <recipients type=\"array\">\ <recipient>") + user + std::string("</recipient>\ </recipients>\ <subject>") + subject + std::string("</subject>\ <message>") + message + std::string("</message>\ <send_email>true</send_email>\ <authorization>3</authorization>\ <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 user '" << user << "'" << io::endl; } return process_send_responce(perform(), s); }
void HttpInterface::evVariableOrEvent(HttpRequest* req, strings& args) { string nodeName(args[0]); size_t eventPos; if ( ! commonDefinitions.events.contains(UTF8ToWString(args[1]), &eventPos)) { // this is a variable if (req->method.find("POST") == 0 || args.size() >= 3) { // set variable value strings values; if (args.size() >= 3) values.assign(args.begin()+1, args.end()); else { // Parse POST form data values.push_back(args[1]); parse_json_form(req->content, values); } if (values.size() == 0) { finishResponse(req, 404, ""); if (verbose) cerr << req << " evVariableOrEevent 404 can't set variable " << args[0] << ", no values" << endl; return; } sendSetVariable(nodeName, values); finishResponse(req, 200, ""); if (verbose) cerr << req << " evVariableOrEevent 200 set variable " << values[0] << endl; } else { // get variable value strings values; values.assign(args.begin()+1, args.begin()+2); unsigned source, start; if ( ! getNodeAndVarPos(nodeName, values[0], source, start)) { finishResponse(req, 404, ""); if (verbose) cerr << req << " evVariableOrEevent 404 no such variable " << values[0] << endl; return; } sendGetVariables(nodeName, values); pendingVariables[std::make_pair(source,start)].insert(req); if (verbose) cerr << req << " evVariableOrEevent schedule var " << values[0] << "(" << source << "," << start << ") add " << req << " to subscribers" << endl; return; } } else { // this is an event // arguments are args 1..N strings data; data.push_back(args[1]); if (args.size() >= 3) for (size_t i=2; i<args.size(); ++i) data.push_back((args[i].c_str())); else if (req->method.find("POST") == 0) { // Parse POST form data parse_json_form(std::string(req->content, req->content.size()), data); } sendEvent(nodeName, data); finishResponse(req, 200, ""); // or perhaps {"return_value":null,"cmd":"sendEvent","name":nodeName}? return; } }
ConfigLoader::StringMap Garnet::parseCommandLine(const strings& args) { namespace fs = boost::filesystem; ConfigLoader::StringMap ret; #if 1 // int mode = 0; for (auto it = args.begin(); it != args.end(); it++) { auto& arg = *it; if (arg[0] == '/' || arg[0] == '-') { switch (arg[1]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'G': case 'K': case 'S': case 'd': case 'g': case 'i': case 'm': case 'o': case 's': case 'v': case 'r': // report types case 't': // tree case 'a': // alias dictionary case 'c': // CSV case 'f': // Result Image File case 'h': // Summary HTML case 'x': // Size X case 'y': // Size Y mode = arg[1]; break; case 'w': // Window ret["_Window"] = "True"; break; case 'n': // Number ret["_Number"] = "True"; break; case 'I': // Tree Image ret["_TreeImage"] = "True"; break; case 'T': // Trace Garnet::upgradeTraceLevel(); mode = arg[1]; break; } } else { switch (mode) { case 0: ret["_Project"] = arg; break; case '0': ret["_Src0"] = arg; break; case '1': ret["_Src1"] = arg; break; case '2': ret["_Src2"] = arg; break; case '3': ret["_Src3"] = arg; break; case '4': ret["_Src4"] = arg; break; case '5': ret["_Src5"] = arg; break; case '6': ret["_Src6"] = arg; break; case '7': ret["_Src7"] = arg; break; case '8': ret["_Src8"] = arg; break; case '9': ret["_Src9"] = arg; break; case 'G': ret["_MaxGeneration"] = arg; break; case 'K': // Cross Validation ret["_CrossValidation"] = arg; break; case 'S': // Random Cases ret["_RandomCases"] = arg; break; case 'T': // Trace Flags Garnet::setTrace(arg); break; case 'a': // alias dictionary if (!ret["_Dictionaries"].empty()) { ret["_Dictionaries"] += ";"; } ret["_Dictionaries"] += arg; break; case 'c': // CSV ret["_CsvFileName"] = arg; break; case 'd': ret["_Directory"] = arg; break; case 'f': // Result Image File ret["_ResultFileName"] = arg; break; case 'g': ret["_InitialGeneration"] = arg; break; case 'h': // Summary HTML ret["_SummaryFileName"] = arg; break; case 'i': ret["_Input"] = arg; break; case 'm': ret["_Module"] = arg; break; case 'o': ret["_Output"] = arg; break; case 'r': ret["_ReportTypes"] = arg; break; case 's': ret["_RandomSeed"] = arg; break; case 't': // tree if (!ret["_Trees"].empty()) { ret["_Trees"] += ";"; } ret["_Trees"] += arg; break; case 'v': ret["_Verbose"] = arg; break; case 'x': // Size X ret["_SizeX"] = arg; break; case 'y': // Size Y ret["_SizeY"] = arg; break; } mode = 0; } } // Argument resolution. if (ret.find("_Project") == ret.end()) { throw "A project name must be specified."; } // Project name. // A project name is made by removing path and extension from _Project. // Note: All extensions will be removed if multiple extensions are included. fs::path projpath(ret["_Project"]); { fs::path projname = projpath; while (projname.has_extension()) { projname.replace_extension(""); } ret["_ProjectName"] = projname.string(); } // Working directory. // If _Directory is specified, it will be used. // otherwise, use _ProjectName. if (ret.find("_Directory") != ret.end()) { ret["_WorkingDirectory"] = ret["_Directory"]; } else { ret["_WorkingDirectory"] = ret["_ProjectName"]; } // Is Projet? fs::path projfile = projpath / (ret["_ProjectName"] + ".garnet_proj.txt"); if (fs::exists(projfile)) { ret["_IsProject"] = "True"; } // The current directory. ret["_CurrentPath"] = fs::current_path().string(); #else // int mode = 0; for (auto it = args.begin(); it != args.end(); it++) { auto& arg = *it; if (arg[0] == '/' || arg[0] == '-') { switch (arg[1]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'G': case 'd': case 'g': case 'i': case 'm': case 'o': case 's': case 'v': case 't': // tree case 'a': // alias dictionary case 'c': // CSV case 'f': // Result Image File case 'h': // Summary HTML case 'x': // Size X case 'y': // Size Y mode = arg[1]; break; case 'w': // Window ret["_Window"] = "True"; break; case 'n': // Number ret["_Number"] = "True"; break; case 'I': // Tree Image ret["_TreeImage"] = "True"; break; case 'T': // Trace Garnet::upgradeTraceLevel(); mode = arg[1]; break; } } else { switch (mode) { case '0': ret["_Src0"] = arg; break; case '1': ret["_Src1"] = arg; break; case '2': ret["_Src2"] = arg; break; case '3': ret["_Src3"] = arg; break; case '4': ret["_Src4"] = arg; break; case '5': ret["_Src5"] = arg; break; case '6': ret["_Src6"] = arg; break; case '7': ret["_Src7"] = arg; break; case '8': ret["_Src8"] = arg; break; case '9': ret["_Src9"] = arg; break; case 'G': ret["_MaxGeneration"] = arg; break; case 'd': ret["_WorkingDirectory"] = arg; break; case 'g': ret["_InitialGeneration"] = arg; break; case 'i': ret["_Input"] = arg; break; case 'm': ret["_Module"] = arg; break; case 'o': ret["_Output"] = arg; break; case 's': ret["_RandomSeed"] = arg; break; case 'v': ret["_Verbose"] = arg; break; case 't': // tree if (!ret["_Trees"].empty()) { ret["_Trees"] += ";"; } ret["_Trees"] += arg; break; case 'a': // alias dictionary if (!ret["_Dictionaries"].empty()) { ret["_Dictionaries"] += ";"; } ret["_Dictionaries"] += arg; break; case 'c': // CSV ret["_CsvFileName"] = arg; break; case 'f': // Result Image File ret["_ResultFileName"] = arg; break; case 'h': // Summary HTML ret["_SummaryFileName"] = arg; break; case 'x': // Size X ret["_SizeX"] = arg; break; case 'y': // Size Y ret["_SizeY"] = arg; break; case 'T': // Trace Flags Garnet::setTrace(arg); break; case 0: ret["_Project"] = arg; break; } mode = 0; } } // Argument resolution. if (ret.find("_Project") == ret.end()) { throw "A project name must be specified."; } // Project title. // 'title' is made by removing path and extension from _Project. // Note: All extensions will be removed if multiple extensions are included. { std::string::size_type st, ed; st = ret.at("_Project").find_last_of("/\\"); st = (st == std::string::npos ? 0 : st + 1); ed = ret.at("_Project").find_first_of('.', st); ret["_Title"] = ret.at("_Project").substr(st, ed - st); } // Working directory. // If _WorkingDirectory is specified, use it. // otherwise, if _Project includes a path, use "thepath/title". // otherwise, use "./title" if (ret.find("_WorkingDirectory") != ret.end()) { ret["_Directory"] = ret.at("_WorkingDirectory") + "/"; } else { std::string::size_type pos = ret.at("_Project").find_last_of("/\\"); ret["_Directory"] = (boost::format("%s/%s/") % (pos != std::string::npos ? ret.at("_Project").substr(0, pos) : ".") % ret["_Title"]).str(); } // Project filename. { std::string::size_type st, pos; st = ret.at("_Project").find_last_of("/\\"); st = (st == std::string::npos ? 0 : st + 1); if ((pos = ret.at("_Project").find(".", st)) != std::string::npos) { ret["_ProjectName"] = ret.at("_Project"); } else { ret["_ProjectName"] = ret.at("_Project") + ".garnet"; } } // std::string projfile = (boost::format("%s/%s.garnet_proj.txt") % ret["_ProjectName"] % ret["_ProjectName"]).str(); if (boost::filesystem::exists(boost::filesystem::path(projfile))) { ret["_IsProject"] = "True"; } #endif // if (Garnet::isTrace()) { std::cout << boost::format("Trace: Loc:%c,Scr:%c,Sto:%c,Pro:%c") % (Garnet::isLocationTrace() ? "Y" : "N") % (Garnet::isScriptTrace() ? "Y" : "N") % (Garnet::isStorageTrace() ? "Y" : "N") % (Garnet::isProcessTrace() ? "Y" : "N") << std::endl; } return ret; }
void TsubtitlesFile::findPossibleSubtitles(const char_t *aviFlnm, const char_t *sdir, strings &files, subtitleFilesSearchMode searchMode) { if (!aviFlnm || !sdir) { return; } char_t avidir[MAX_PATH]; if (aviFlnm[0]) { char_t dsk[MAX_PATH], dir[MAX_PATH], name[MAX_PATH]; _splitpath_s(aviFlnm, dsk, MAX_PATH, dir, MAX_PATH, name, MAX_PATH, NULL, 0); _makepath_s(avidir, countof(avidir), dsk, dir, NULL, NULL); } else { strcpy(avidir, _l(".")); } strings dirs; strtok(sdir, _l(";"), dirs); fixRelativePaths(dirs, avidir); for (strings::const_iterator sd = dirs.begin(); sd != dirs.end(); sd++) { findPossibleSubtitles(sd->c_str(), files); } // If search mode is "search video file match then heuristic", then test if at least 1 subtitle filename matches with the video filename bool videoFileMatch = false; ffstring videoFlnmNoExt; extractfilenameWOext(aviFlnm, videoFlnmNoExt); videoFlnmNoExt.ConvertToLowerCase(); if (searchMode == SUBFILES_VIDEO_FILE_HEURISTIC) { for (strings::iterator f = files.begin(); f != files.end(); f++) { ffstring flnmNoExt; extractfilenameWOext((*f).c_str(), flnmNoExt); flnmNoExt.ConvertToLowerCase(); if (strncmp(videoFlnmNoExt.c_str(), flnmNoExt.c_str(), videoFlnmNoExt.length()) == 0) { /* Common portion of video and subtitle filenames. Check if lengths are equal (identical names) or if this is a dot "." right after like in videofile.<suffix>.ext */ if (videoFlnmNoExt.length() == flnmNoExt.length() || flnmNoExt[videoFlnmNoExt.length()] == _l('.')) { videoFileMatch = true; break; } } } } if (searchMode != SUBFILES_ALL) { EditDistance dist; for (strings::iterator f = files.begin(); f != files.end();) { ffstring flnmNoExt; extractfilenameWOext((*f).c_str(), flnmNoExt); flnmNoExt.ConvertToLowerCase(); // If search mode is based on video file name removes the non matching entries if (searchMode == SUBFILES_VIDEO_FILE_MATCH || (searchMode == SUBFILES_VIDEO_FILE_HEURISTIC && videoFileMatch)) { if (strncmp(videoFlnmNoExt.c_str(), flnmNoExt.c_str(), videoFlnmNoExt.length()) != 0 || (videoFlnmNoExt.length() != flnmNoExt.length() && flnmNoExt[videoFlnmNoExt.length()] != _l('.'))) { f = files.erase(f); } else { f++; } continue; } // Heuristic search int distance = dist.CalEditDistance(videoFlnmNoExt.c_str(), flnmNoExt.c_str(), 2 * MAX_PATH); //DPRINTF(_l("Subtitle files : %s vs %s match result = %d"), flnmNoExt.c_str(), videoFlnmNoExt.c_str(), distance); if (distance > SUBFILES_HEURISTIC_LIMIT) { f = files.erase(f); } else { f++; } } } }