static inline std::streamsize get_frame(std::istream& is, std::vector<char>& buf) { using boost::lexical_cast; using boost::starts_with; static const std::string b{"--RaNd0m"}; static const std::string cl{"Content-length: "}; std::string line{}; do { std::getline(is, line); } while (is.good() && !starts_with(line, b)); do { std::getline(is, line); } while (is.good() && !starts_with(line, cl)); if (is.eof()) { return false; } else if (is.bad()) { throw std::runtime_error{"Error finding start of frame."}; } std::size_t n; try { auto s = line.substr(cl.length(), line.length() - cl.length() - 1); n = boost::lexical_cast<std::size_t>(std::move(s)); } catch (boost::bad_lexical_cast& e) { throw std::runtime_error{"Error parsing content length."}; } if (n <= 0) { throw std::runtime_error{"Content length not positive."}; } if (buf.size() < n) { buf.resize(n); } std::getline(is, line); if (!(line.length() == 1 && line[0] == '\r')) { throw std::runtime_error{"Expected \"\\r\\n\" before frame contents."}; } is.read(buf.data(), n); if (is.bad()) { throw std::runtime_error{"Error reading frame contents."}; } else { return true; } }
void UploaderImp::TranslateRequest() { std::istream s( &request_ ); std::string line; s >> line; //method if( line == "GET" ) method_ = GET; else if( line == "HEAD" ) method_ = HEAD; else throw Unhandled(400, "Unknown method" ); s >> line; //object if( !starts_with( line, "/uri-res/N2R?" ) ) throw Unhandled(400, "Requested uri type is not supported"); std::string last = urn_; urn_ = line.substr(line.find('?') + 1); fileInfo_ = System::GetShareMgr()->GetByUrn( urn_ ); if(last != urn_) { System::LogBas() << "Host " << endpoint_ << " requested file: " << fileInfo_.Name() << std::endl; fileInfo_.IncreaseRequests(); } while( std::getline( s, line ) && line != "\r" ) { std::string value = boost::trim_copy( line.substr( line.find( ':' ) + 1 ) ); if( istarts_with( line, "Connection:" ) ) keepAlive_ = iequals( value, "keep-alive" ); else if( istarts_with( line, "X-Nick:" ) ) nick_ = value; else if(istarts_with(line, "User-Agent:") && client_ != value) { client_ = value; if(System::GetSecurity()->AgentRestricted(client_)) throw Unhandled(403, "Client software is restricted"); } else if( istarts_with( line, "Range:" ) ) { file_offset_t first = 0; file_offset_t last = 0; int result = sscanf( value.c_str(), "bytes=%llu-%llu", &first, &last ); if( result == 0 ) throw Unhandled(416, "Couldn't parse range"); if( result == 1 ) range_.SetBoundary( first, fileInfo_.Size() - 1); if( result == 2 ) range_.SetBoundary( first, last ); } } if( range_.Empty() ) throw std::range_error( "Range is empty" ); // std::cout << range_.Last() << " " << fileInfo.Size() << std::endl; if( range_.Last() >= fileInfo_.Size() ) throw Unhandled(416, "Range is too large" ); }
void mdb_shell::filter_metaprogram() { assert(mp); using boost::starts_with; using boost::ends_with; using boost::trim_copy; using vertex_descriptor = metaprogram::vertex_descriptor; using edge_descriptor = metaprogram::edge_descriptor; using edge_property = metaprogram::edge_property; using discovered_t = metaprogram::discovered_t; static const std::string wrap_prefix = "metashell::impl::wrap<"; static const std::string wrap_suffix = ">"; // TODO this check could be made more strict, // since we now whats inside wrap<...> (mp->get_evaluation_result) auto is_wrap_type = [](const std::string& type) { return starts_with(type, wrap_prefix) && ends_with(type, wrap_suffix); }; std::string env_buffer = env.get(); int line_number = std::count(env_buffer.begin(), env_buffer.end(), '\n'); // First disable everything for (edge_descriptor edge : mp->get_edges()) { mp->get_edge_property(edge).enabled = false; } // We will traverse the interesting edges later std::stack<edge_descriptor> edge_stack; // Enable the interesting root edges for (edge_descriptor edge : mp->get_out_edges(mp->get_root_vertex())) { edge_property& property = mp->get_edge_property(edge); const std::string& target_name = mp->get_vertex_property(mp->get_target(edge)).name; // Filter out edges, that is not instantiated by the entered type if (property.point_of_instantiation.name == internal_file_name && property.point_of_instantiation.row == line_number + 2 && (property.kind == instantiation_kind::template_instantiation || property.kind == instantiation_kind::memoization) && (!is_wrap_type(target_name) || property.kind != instantiation_kind::memoization)) { property.enabled = true; edge_stack.push(edge); } } discovered_t discovered(mp->get_num_vertices()); // Traverse the graph to enable all edges which are reachable from the // edges enabled above while (!edge_stack.empty()) { edge_descriptor edge = edge_stack.top(); edge_stack.pop(); assert(mp->get_edge_property(edge).enabled); vertex_descriptor vertex = mp->get_target(edge); if (discovered[vertex]) { continue; } for (edge_descriptor out_edge : mp->get_out_edges(vertex)) { edge_property& property = mp->get_edge_property(out_edge); if (property.kind == instantiation_kind::template_instantiation || property.kind == instantiation_kind::memoization) { property.enabled = true; edge_stack.push(out_edge); } } } // Unwrap vertex names for (metaprogram::vertex_descriptor vertex : mp->get_vertices()) { std::string& name = mp->get_vertex_property(vertex).name; if (is_wrap_type(name)) { name = trim_copy(name.substr( wrap_prefix.size(), name.size() - wrap_prefix.size() - wrap_suffix.size())); if (!is_template_type(name)) { for (metaprogram::edge_descriptor in_edge : mp->get_in_edges(vertex)) { mp->get_edge_property(in_edge).kind = instantiation_kind::non_template_type; } } } } // Clang sometimes produces equivalent instantiations events from the same // point. Filter out all but one of each for (metaprogram::vertex_descriptor vertex : mp->get_vertices()) { typedef std::tuple<file_location, instantiation_kind, vertex_descriptor> set_element_t; std::set<set_element_t> similar_edges; for (metaprogram::edge_descriptor edge : mp->get_out_edges(vertex)) { metaprogram::edge_property& edge_property = mp->get_edge_property(edge); set_element_t set_element = std::make_tuple( edge_property.point_of_instantiation, edge_property.kind, mp->get_target(edge)); auto it = similar_edges.find(set_element); if (it != similar_edges.end()) { edge_property.enabled = false; } else { similar_edges.insert(set_element); } } } }
void FortranProgramDeclarationsAndDefinitions::visit (SgNode * node) { using boost::filesystem::path; using boost::filesystem::system_complete; using boost::iequals; using boost::starts_with; using boost::lexical_cast; using std::string; if (isSgSourceFile (node)) { path p = system_complete (path (isSgSourceFile (node)->getFileName ())); currentSourceFile = p.filename (); Debug::getInstance ()->debugMessage ("Source file '" + currentSourceFile + "' detected", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__ ); } else if (Globals::getInstance ()->isInputFile (currentSourceFile)) { /* * ====================================================== * Only process this portion of the AST if we recognise * this source file as one passed on the command line. In * Fortran, .rmod files are sometimes generated whose * traversal should be avoided * ====================================================== */ switch (node->variantT ()) { case V_SgModuleStatement: { SgModuleStatement * moduleStatement = isSgModuleStatement (node); currentModuleName = moduleStatement->get_name ().getString (); fileNameToModuleNames[currentSourceFile].push_back (currentModuleName); moduleNameToFileName[currentModuleName] = currentSourceFile; Debug::getInstance ()->debugMessage ("Module '" + currentModuleName + "' in file '" + currentSourceFile + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__ ); break; } case V_SgProcedureHeaderStatement: { /* * ====================================================== * We need to store all subroutine definitions since we * later have to copy and modify the user kernel subroutine * ====================================================== */ SgProcedureHeaderStatement * procedureHeaderStatement = isSgProcedureHeaderStatement (node); string const subroutineName = procedureHeaderStatement->get_name ().getString (); subroutinesInSourceCode[subroutineName] = procedureHeaderStatement; ROSE_ASSERT (currentModuleName.size() > 0); moduleNameToSubroutines[currentModuleName].push_back (subroutineName); subroutineToFileName[subroutineName] = currentSourceFile; Debug::getInstance ()->debugMessage ( "Found procedure header statement '" + procedureHeaderStatement->get_name ().getString () + "' in file '" + currentSourceFile + "', and module '" + currentModuleName + "'", Debug::FUNCTION_LEVEL, __FILE__, __LINE__); break; } case V_SgFunctionCallExp: { /* * ====================================================== * Function call found in the AST. Get its actual arguments * and the callee name * ====================================================== */ SgFunctionCallExp * functionCallExp = isSgFunctionCallExp (node); SgExprListExp * actualArguments = functionCallExp->get_args (); string const calleeName = functionCallExp->getAssociatedFunctionSymbol ()->get_name ().getString (); Debug::getInstance ()->debugMessage ("Found function call '" + calleeName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); if ( iequals (calleeName, OP2::OP_DECL_SET) || iequals (calleeName, OP2::OP_DECL_SET_HDF5) ) { /* * ====================================================== * An OP_SET variable declared through an OP_DECL_SET call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_SET_HDF5) ) isHDF5Format = true; FortranOpSetDefinition * opSetDeclaration = new FortranOpSetDefinition (actualArguments, isHDF5Format); OpSetDefinitions[opSetDeclaration->getVariableName ()] = opSetDeclaration; } else if ( iequals (calleeName, OP2::OP_DECL_MAP) || iequals (calleeName, OP2::OP_DECL_MAP_HDF5) ) { /* * ====================================================== * An OP_MAP variable declared through an OP_DECL_MAP call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_MAP_HDF5) ) { isHDF5Format = true; Debug::getInstance ()->debugMessage ("This is the HDF5 version: '" + calleeName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); } FortranOpMapDefinition * opMapDeclaration = new FortranOpMapDefinition (actualArguments, isHDF5Format); OpMapDefinitions[opMapDeclaration->getVariableName ()] = opMapDeclaration; } else if ( iequals (calleeName, OP2::OP_DECL_DAT) || iequals (calleeName, OP2::OP_DECL_DAT_HDF5) ) { /* * ====================================================== * An OP_DAT variable declared through an OP_DECL_DAT call * ====================================================== */ bool isHDF5Format = false; if ( iequals (calleeName, OP2::OP_DECL_DAT_HDF5) ) isHDF5Format = true; FortranOpDatDefinition * opDatDeclaration = new FortranOpDatDefinition (actualArguments, isHDF5Format); OpDatDefinitions[opDatDeclaration->getVariableName ()] = opDatDeclaration; } else if (iequals (calleeName, OP2::OP_DECL_CONST)) { /* * ====================================================== * A constant declared through an OP_DECL_CONST call * ====================================================== */ FortranOpConstDefinition * opConstDeclaration = new FortranOpConstDefinition (actualArguments, functionCallExp); OpConstDefinitions[opConstDeclaration->getVariableName ()] = opConstDeclaration; } else if (starts_with (calleeName, OP2::OP_PAR_LOOP)) { /* * ====================================================== * The first argument to an 'OP_PAR_LOOP' call should be * a reference to the kernel function. Cast it and proceed, * otherwise throw an exception * ====================================================== */ SgExprListExp * actualArguments = functionCallExp->get_args (); SgFunctionRefExp * functionRefExpression = isSgFunctionRefExp ( actualArguments->get_expressions ().front ()); ROSE_ASSERT (functionRefExpression != NULL); string const userSubroutineName = functionRefExpression->getAssociatedFunctionDeclaration ()->get_name ().getString (); Debug::getInstance ()->debugMessage ("Found '" + calleeName + "' with (host) user subroutine '" + userSubroutineName + "'", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); if (parallelLoops.find (userSubroutineName) == parallelLoops.end ()) { int numberOfOpArgs = getLoopSuffixNumber ( calleeName ); /* * ====================================================== * If this kernel has not been previously encountered then * build a new parallel loop representation * ====================================================== */ FortranParallelLoop * parallelLoop = new FortranParallelLoop ( functionCallExp); parallelLoop->addFileName (currentSourceFile); parallelLoops[userSubroutineName] = parallelLoop; Debug::getInstance ()->debugMessage ("Parallel loop with '" + lexical_cast <string> (numberOfOpArgs) + "' arguments", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); analyseParallelLoopArguments (parallelLoop, actualArguments, numberOfOpArgs); parallelLoop->checkArguments (); parallelLoop->setIncrementalID (IDCounter); IDCounter++; } else { Debug::getInstance ()->debugMessage ("Parallel loop for '" + userSubroutineName + "' already created", Debug::OUTER_LOOP_LEVEL, __FILE__, __LINE__); ParallelLoop * parallelLoop = parallelLoops[userSubroutineName]; parallelLoop->addFunctionCallExpression (functionCallExp); parallelLoop->addFileName (currentSourceFile); } } break; } default: { break; } } } }
karaoke_match_result auto_match_karaoke(std::vector<std::string> const& source_strings, std::string const& dest_string) { karaoke_match_result result = { 0, 0 }; if (source_strings.empty()) return result; using namespace boost::locale::boundary; using boost::starts_with; result.source_length = 1; ssegment_index destination_characters(character, begin(dest_string), end(dest_string)); auto src = boost::to_lower_copy(source_strings[0]); auto dst = destination_characters.begin(); auto dst_end = destination_characters.end(); // Eat all the whitespace at the beginning of the source and destination // syllables and exit if either ran out. auto eat_whitespace = [&]() -> bool { size_t i = 0, first_non_whitespace = 0; while (is_whitespace(next_codepoint(src.c_str(), &i))) first_non_whitespace = i; if (first_non_whitespace) src = src.substr(first_non_whitespace); while (dst != dst_end && is_whitespace(dst->str())) { ++dst; ++result.destination_length; } // If we ran out of dest then this needs to match the rest of the // source syllables (this probably means the user did something wrong) if (dst == dst_end) { result.source_length = source_strings.size(); return true; } return src.empty(); }; if (eat_whitespace()) return result; // We now have a non-whitespace character at the beginning of both source // and destination. Check if the source starts with a romanized kana, and // if it does then check if the destination also has the appropriate // character. If it does, match them and repeat. while (!src.empty()) { // First check for a basic match of the first character of the source and dest auto first_src_char = ssegment_index(character, begin(src), end(src)).begin()->str(); if (compare(first_src_char, dst->str()) == 0) { ++dst; ++result.destination_length; src.erase(0, first_src_char.size()); if (eat_whitespace()) return result; continue; } auto check = [&](kana_pair const& kp) -> bool { if (!starts_with(&*dst->begin(), kp.kana)) return false; src = src.substr(strlen(kp.romaji)); for (size_t i = 0; kp.kana[i]; ) { i += dst->length(); ++result.destination_length; ++dst; } return true; }; bool matched = false; for (auto const& match : romaji_to_kana(src)) { if (check(match)) { if (eat_whitespace()) return result; matched = true; break; } } if (!matched) break; } // Source and dest are now non-empty and start with non-whitespace. // If there's only one character left in the dest, it obviously needs to // match all of the source syllables left. if (std::distance(dst, dst_end) == 1) { result.source_length = source_strings.size(); ++result.destination_length; return result; } // We couldn't match the current character, but if we can match the *next* // syllable then we know that everything in between must belong to the // current syllable. Do this by looking up to KANA_SEARCH_DISTANCE // characters ahead in destination and seeing if we can match them against // the beginning of a syllable after this syllable. // If a match is found, make a guess at how much source and destination // should be selected based on the distances it was found at. // The longest kanji are 'uketamawa.ru' and 'kokorozashi', each with a // reading consisting of five kana. This means each each character from // the destination can match at most five syllables from the source. static const int max_character_length = 5; // Arbitrarily chosen limit on the number of dest characters to try // skipping. Higher numbers probably increase false-positives. static const int dst_lookahead_max = 3; for (size_t lookahead = 0; lookahead < dst_lookahead_max; ++lookahead) { if (++dst == dst_end) break; // Transliterate this character if it's a known hiragana or katakana character std::vector<const char *> translit; auto next = std::next(dst); if (next != dst_end) boost::copy(kana_to_romaji(dst->str() + next->str()), back_inserter(translit)); boost::copy(kana_to_romaji(dst->str()), back_inserter(translit)); // Search for it and the transliterated version in the source int src_lookahead_max = (lookahead + 1) * max_character_length; int src_lookahead_pos = 0; for (auto const& syl : source_strings) { // Don't count blank syllables in the max search distance if (is_whitespace(syl)) continue; if (++src_lookahead_pos == 1) continue; if (src_lookahead_pos > src_lookahead_max) break; std::string lsyl = boost::to_lower_copy(syl); if (!(starts_with(syl, dst->str()) || util::any_of(translit, [&](const char *str) { return starts_with(lsyl, str); }))) continue; // The syllable immediately after the current one matched, so // everything up to the match must go with the current syllable. if (src_lookahead_pos == 2) { result.destination_length += lookahead + 1; return result; } // The match was multiple syllables ahead, so just divide the // destination characters evenly between the source syllables result.destination_length += 1; result.source_length = static_cast<size_t>((src_lookahead_pos - 1.0) / (lookahead + 1.0) + .5); return result; } } // We wouldn't have gotten here if the dest was empty, so make sure at // least one character is selected result.destination_length = std::max<size_t>(result.destination_length, 1u); return result; }