void config_file_iterator::Impl::substitute_macros( cstring& where ) { m_post_subst_line.clear(); cstring::size_type pos; while( (pos = where.find( m_macro_ref_begin )) != cstring::npos ) { m_post_subst_line.append( where.begin(), pos ); where.trim_left( where.begin() + pos + m_macro_ref_begin.size() ); pos = where.find( m_macro_ref_end ); BOOST_RT_PARAM_VALIDATE_LOGIC( pos != cstring::npos, BOOST_RT_PARAM_LITERAL( "incomplete macro reference" ) ); cstring value = *get_macro_value( where.substr( 0, pos ), false ); m_post_subst_line.append( value.begin(), value.size() ); where.trim_left( where.begin() + pos + m_macro_ref_end.size() ); } if( !m_post_subst_line.empty() ) { m_post_subst_line.append( where.begin(), where.size() ); where = m_post_subst_line; } }
include_level::include_level( cstring file_name, cstring path_separators, include_level* parent_ ) : m_parent( parent_ ) { if( file_name.is_empty() ) return; assign_op( m_curr_location.first, file_name, 0 ); m_curr_location.second = 0; m_stream.open( m_curr_location.first.c_str() ); if( !m_stream.is_open() && !!m_parent.get() ) { cstring parent_path = m_parent->m_curr_location.first; cstring::iterator it = unit_test::find_last_of( parent_path.begin(), parent_path.end(), path_separators.begin(), path_separators.end() ); if( it != parent_path.end() ) { assign_op( m_curr_location.first, cstring( parent_path.begin(), it+1 ), 0 ); m_curr_location.first.append( file_name.begin(), file_name.end() ); m_stream.clear(); m_stream.open( m_curr_location.first.c_str() ); } } BOOST_RT_PARAM_VALIDATE_LOGIC( m_stream.is_open(), BOOST_RT_PARAM_LITERAL( "couldn't open file " ) << file_name ); }
static bool _( cstring source, boost::optional<bool>& res ) { BOOST_RT_PARAM_TRACE( "In interpret_argument_value_impl<bool>" ); static literal_cstring YES( BOOST_RT_PARAM_CSTRING_LITERAL( "YES" ) ); static literal_cstring Y( BOOST_RT_PARAM_CSTRING_LITERAL( "Y" ) ); static literal_cstring NO( BOOST_RT_PARAM_CSTRING_LITERAL( "NO" ) ); static literal_cstring N( BOOST_RT_PARAM_CSTRING_LITERAL( "N" ) ); static literal_cstring one( BOOST_RT_PARAM_CSTRING_LITERAL( "1" ) ); static literal_cstring zero( BOOST_RT_PARAM_CSTRING_LITERAL( "0" ) ); source.trim(); if( case_ins_eq( source, YES ) || case_ins_eq( source, Y ) || case_ins_eq( source, one ) ) { res = true; return true; } else if( case_ins_eq( source, NO ) || case_ins_eq( source, N ) || case_ins_eq( source, zero ) ) { res = false; return true; } else { res = true; return source.is_empty(); } }
inline void putenv_impl( cstring name, cstring value ) { using namespace std; // !! this may actually fail. What should we do? setenv( name.begin(), value.begin(), 1 ); }
bool interpret( cstring param_name, cstring source ) const { static cstring const s_YES( "YES" ); static cstring const s_Y( "Y" ); static cstring const s_NO( "NO" ); static cstring const s_N( "N" ); static cstring const s_TRUE( "TRUE" ); static cstring const s_FALSE( "FALSE" ); static cstring const s_one( "1" ); static cstring const s_zero( "0" ); source.trim(); if( source.is_empty() || case_ins_eq( source, s_YES ) || case_ins_eq( source, s_Y ) || case_ins_eq( source, s_one ) || case_ins_eq( source, s_TRUE ) ) return true; if( case_ins_eq( source, s_NO ) || case_ins_eq( source, s_N ) || case_ins_eq( source, s_zero ) || case_ins_eq( source, s_FALSE ) ) return false; BOOST_TEST_I_THROW( format_error( param_name ) << source << " can't be interpreted as bool value." ); }
bool fromstringhex(cstring s, arrayvieww<byte> val) { if (val.size()*2 != s.length()) return false; bool ok = true; for (size_t i=0;i<val.size();i++) { ok &= fromstringhex(s.substr(i*2, i*2+2), val[i]); } return ok; }
string translateRk86(cstring in) { string out; out.resize(in.size()); const char* inp = in.c_str(); char* o = (char*)out.c_str(); while(*inp) { char c = translateRk86c(*inp++); if(c==0) raise("Имя файла "+in+" содержит неподдерживаемый РК86 символ."); *o++ = c; } return out; }
bmlwriter::mode bmlwriter::type_core(cstring val) { if (val == "") return anon; char first = val[0]; char last = val[val.length()-1]; if (val.contains("\n") || first==' ' || first=='\t' || last==' ' || last=='\t') return multiline; if (val.contains("\"")) return col; if (val.contains(" ") || val.contains("\t")) return quote; return eq; }
string bmlwriter::escape(cstring val) { string esc = "-"; bool needescape = (val.startswith("-")); for (byte c : val.bytes()) { if (isalnum(c) || c=='.') esc+=c; else if (c=='-') esc+="--"; else { esc+="-"+tostringhex<2>((uint8_t)c); needescape=true; } } if (needescape) return esc; else return val; }
bool fromstring(cstring s, double& out) { out = 0; auto tmp_s = s.c_str(); const char * tmp_cp = tmp_s; if (*tmp_cp != '-' && !isdigit(*tmp_cp)) return false; char * tmp_cpo; double ret = strtod(drop0x(tmp_cp), &tmp_cpo); if (tmp_cpo != tmp_cp + s.length()) return false; if (!isdigit(tmp_cpo[-1])) return false; if (ret==HUGE_VAL || ret==-HUGE_VAL) return false; out = ret; return true; }
basic_param( cstring name, bool is_optional, bool is_repeatable, Modifiers const& m ) : p_name( name.begin(), name.end() ) , p_description( nfp::opt_get( m, description, std::string() ) ) , p_help( nfp::opt_get( m, runtime::help, std::string() ) ) , p_env_var( nfp::opt_get( m, env_var, std::string() ) ) , p_value_hint( nfp::opt_get( m, value_hint, std::string() ) ) , p_optional( is_optional ) , p_repeatable( is_repeatable ) , p_has_optional_value( m.has( optional_value ) ) , p_has_default_value( m.has( default_value ) || is_repeatable ) , p_callback( nfp::opt_get( m, callback, callback_type() ) ) { add_cla_id( help_prefix, name, ":" ); }
static size_t linelen(const cstring& input) { //pointers are generally a bad idea, but this is such a hotspot it's worth it const uint8_t * inputraw = input.bytes().ptr(); size_t nlpos = 0; if (input.bytes_hasterm()) { while (!isendl(inputraw[nlpos])) nlpos++; } else { size_t inputlen = input.length(); while (nlpos < inputlen && !isendl(inputraw[nlpos])) nlpos++; } return nlpos; }
//returns size of leading whitespace and comments static size_t bml_size_white(const cstring& data) { int i = 0; while (data[i]==' ' || data[i]=='\t') i++; if (data[i]=='#' || (data[i]=='/' && data[i+1]=='/')) return data.length(); else return i; }
static bool is_valid_identifier( cstring const& source ) { if( source.is_empty() ) return false; cstring::const_iterator it = source.begin(); if( !std::isalpha( *it ) ) return false; while( ++it < source.end() ) { if( !std::isalnum( *it ) && *it != BOOST_RT_PARAM_LITERAL( '_' ) && *it != BOOST_RT_PARAM_LITERAL( '-' ) ) return false; } return true; }
cstring ReferenceMap::newName(cstring base) { // Maybe in the future we'll maintain information with per-scope identifiers, // but today we are content to generate globally-unique identifiers. // If base has a suffix of the form _(\d+), then we discard the suffix. // under the assumption that it is probably a generated suffix. // This will not impact correctness. unsigned len = base.size(); const char digits[] = "0123456789"; const char* s = base.c_str(); while (len > 0 && strchr(digits, s[len-1])) len--; if (len > 0 && base[len - 1] == '_') base = base.substr(0, len - 1); cstring name = cstring::make_unique(usedNames, base, '_'); usedNames.insert(name); return name; }
static string replace(char c1, char c2, cstring s) { StringBuffer buf; for(int i = 0; i < s.length(); i++) { if(s[i] == c1) buf << c2; else buf << s[i]; } return buf.toString(); }
void config_file_iterator::Impl::process_else( cstring line ) { BOOST_RT_PARAM_VALIDATE_LOGIC( m_conditional_states.size() > 0 && m_conditional_states.back(), BOOST_RT_PARAM_LITERAL( "else without matching if" ) ); m_inactive_ifdef_level = m_conditional_states.size() == m_inactive_ifdef_level ? 0 : m_conditional_states.size(); BOOST_RT_PARAM_VALIDATE_LOGIC( line.is_empty(), BOOST_RT_PARAM_LITERAL( "unexpected tokens at the end of else command" ) ); }
messagewin::messagewin(cstring p, cstring opt): targetif(targetif::query_target), toplevel(p.lower(), opt) { sessionid = 0; info = false; serverentry = NULL; build_messagewindow(this); bindings(); }
inline bool interpret_argument_value( cstring source, hexerboost::optional<std::list<T> >& res, int ) { BOOST_RT_PARAM_TRACE( "In interpret_argument_value<std::list<T>>" ); res = std::list<T>(); while( !source.is_empty() ) { // !! should we use token_iterator cstring::iterator single_value_end = std::find( source.begin(), source.end(), BOOST_RT_PARAM_LITERAL( ',' ) ); hexerboost::optional<T> value; interpret_argument_value( cstring( source.begin(), single_value_end ), value, 0 ); res->push_back( *value ); source.trim_left( single_value_end + 1 ); } return true; }
void StructTypeReplacement::flatten(const P4::TypeMap* typeMap, cstring prefix, const IR::Type* type, IR::IndexedVector<IR::StructField> *fields) { if (auto st = type->to<IR::Type_Struct>()) { structFieldMap.emplace(prefix, st); for (auto f : st->fields) flatten(typeMap, prefix + "." + f->name, f->type, fields); return; } cstring fieldName = prefix.replace(".", "_") + cstring::to_cstring(fieldNameRemap.size()); fieldNameRemap.emplace(prefix, fieldName); fields->push_back(new IR::StructField(IR::ID(fieldName), type->getP4Type())); }
void irctext::attrwrite(const int mode, const cstring &msg, const cstring &extratags) { cstring tags = extratags; // colors to use (possibly?) int use_fg = fg, use_bg = bg; if(mode == 0 || msg.length() == 0) { if(msg.length() != 0) text::insert("end", msg, tags); return; } if(mode & MODE_BOLD) tags += " attr_b"; if(mode & MODE_INVERSE) // swap colors if(mode & MODE_COLOR && use_fg != -1 && use_bg != -1) { int t = use_fg; use_fg = use_bg; use_bg = t; } else tags += " attr_i"; // standard tag if(mode & MODE_UNDERLINE) tags += " attr_u"; if(mode & MODE_COLOR) { if(use_fg != -1) tags << " fg" << use_fg; if(use_bg != -1) tags << " bg" << use_bg; } text::insert("end", msg, tags); }
PathName PathName::join(cstring component) const { if (component.isNullOrEmpty()) throw std::logic_error("Empty string for pathname component"); if (str.isNullOrEmpty()) return PathName(component); char last = str[str.size() - 1]; for (char c : pathSeparators) { if (c == last) { auto result = str + component; return PathName(result); } } auto result = str + separator() + component; return PathName(result); }
void config_file_iterator::Impl::process_command_line( cstring line ) { line.trim_left( m_command_delimeter.size() ); unit_test::string_token_iterator tit( line, unit_test::max_tokens = 2 ); command_handler_map::const_iterator it = m_command_handler_map.find( *tit ); BOOST_RT_PARAM_VALIDATE_LOGIC( it != m_command_handler_map.end(), BOOST_RT_PARAM_LITERAL( "Invalid command " ) << *tit ); ++tit; (it->second)( *tit ); }
boost::optional<cstring> get_param_value( param_namespace const& where_from, cstring name_part1, cstring name_part2, cstring name_part3, cstring name_part4, cstring name_part5 ) { if( name_part2.is_empty() ) { boost::optional<cstring> res; BOOST_TEST_FOREACH( parameter const&, p, where_from ) { if( p.p_name == name_part1 ) { res = cstring( p.p_value ); break; } } return res; }
void bmlwriter::node(cstring name, cstring val, mode m, bool enter) { m = type(val, m); bool inlined = (m <= icol && m_caninline && !enter); if (m_data) { if (inlined) m_data += " "; else m_data += "\n"+indent(); } m_indent++; if ((m==anon || m==eq || m==quote) && enter) m_caninline = true; if (m==icol || m==col || m==multiline) m_caninline = false; //for other modes, inlinability isn't affected switch (m) { case ianon: case anon: m_data += name; break; case ieq: case eq: m_data += name+"="+val; break; case iquote: case quote: m_data += name+"=\""+val+"\""; break; case icol: case col: m_data += name+": "+val; break; case multiline: string prefix = "\n"+indent()+":"; m_data += name + prefix + val.replace("\n", prefix); break; } if (!enter) m_indent--; }
/** * Parse a literal string. * @param litt Literal string to parse (first character is ignored). */ void Parser::parseLitt(io::InStream& in, cstring litt) throw(json::Exception) { for(int i = 1; i < litt.length(); i++) if(nextChar(in) != litt[i]) error("unknown identifier"); }
bool config_file_iterator::Impl::get_next_line( cstring& line ) { bool broken_line = false; line.clear(); while( !m_curr_level->m_stream.eof() || !!m_curr_level->m_parent.get() ) { // 10. Switch to upper include level if current one is finished // 20. Read/append next file line // 30. Increment line number // 40. Remove comments // 50. Remove trailing and leading spaces // 60. Skip empty string // 70. Concatenate broken lines if needed. Put the result into line // 80. If line is not completed, try to finish it by reading the next line // 90. Process command line // 100. Substitute macros references with their definitions // 110. Next line found. if( m_curr_level->m_stream.eof() ) { // 10 // m_curr_level = m_curr_level->m_parent; continue; } std::ifstream& input = m_curr_level->m_stream; char_type* buffer_insert_pos = broken_line ? m_buffer.get() + line.size() : m_buffer.get(); input.getline( buffer_insert_pos, (std::streamsize)(m_buffer_size - line.size()), // 20 // m_line_delimeter ); cstring next_line( buffer_insert_pos, input.gcount() > 0 ? buffer_insert_pos + (input.eof() ? input.gcount() : (input.gcount()-1)) : buffer_insert_pos ); m_curr_level->m_curr_location.second++; // 30 // cstring::size_type comment_pos = next_line.find( m_sl_comment_delimeter ); if( comment_pos != cstring::npos ) next_line.trim_right( next_line.begin()+comment_pos ); // 40 // if( m_trim_trailing_spaces ) // 50 // next_line.trim_right(); if( m_trim_leading_spaces && !broken_line ) next_line.trim_left(); if( next_line.is_empty() ) { // 60 // if( m_skip_empty_lines ) continue; else next_line.assign( buffer_insert_pos, buffer_insert_pos ); } line = broken_line ? cstring( line.begin(), next_line.end() ) : next_line; // 70 // broken_line = match_back( line, m_line_beak ); if( broken_line ) { // 80 // line.trim_right( 1 ); continue; } if( match_front( line, m_command_delimeter ) ) { // 90 // process_command_line( line ); continue; } if( !is_active_line() ) continue; substitute_macros( line ); // 100 // return true; // 110 // } BOOST_RT_PARAM_VALIDATE_LOGIC( !broken_line, BOOST_RT_PARAM_LITERAL( "broken line is not completed" ) ); BOOST_RT_PARAM_VALIDATE_LOGIC( m_conditional_states.size() == 0, BOOST_RT_PARAM_LITERAL( "matching endif command is missing" ) ); return false; }
static bool match_back( cstring str, cstring pattern ) { return str.size() >= pattern.size() && str.substr( str.size() - pattern.size() ) == pattern; }
static bool match_front( cstring str, cstring pattern ) { return str.size() >= pattern.size() && str.substr( 0, pattern.size() ) == pattern; }
int wunlink(const wcstring &file_name) { const cstring tmp = wcs2string(file_name); return unlink(tmp.c_str()); }