예제 #1
0
bool grep_callback(const  boost::smatch& what)
{
	// 1、先将what[0]中的内容拷贝到 pUtf8[] 中,再转换成 wstring型的输出

	// 1.1、计算在文件中的位置
	long mLocation=mPageNum*2*4096+what.position();

	// 1.2、取出匹配项
	//fs.seekg(mLocation,fstream::beg);
	//fs.read(pUtf8,(what[0]->second - what[0]->first));

	// 1.3、转换成 wstring 型
	string mUtf8(pUtf8);
	mUtf8.assign(what[0]);
	wstring mUnicode=UTF8ToWide(mUtf8);

	// 试验
	//string strUtf8(what[0].first+7,what[0].second-8);
	//wstring wstrTitle=UTF8ToWide(strUtf8);

	// 1.4、输出-wstring转换成string
	strOut=WstrToStr(mUnicode);

	// 每次匹配,都修改日志文件名
//	pFlogfile[6]=mCount;
//	int nIndex=mCount-'a';
//	strcpy(pLogReturn[nIndex],pFlogfile);
//	mCount++;

	//outFile.seekp(0,ios::end);

	if (bFind==false)
	{
		outFile.open(logFile,ofstream::out);
		outFile<<"结果如下:"<<endl;
		outFile<<"————————————————————————————"<<endl;
		strcpy(pLogReturn[mIndex],logFile.c_str());
		bFind=true;
	}
	// 转换一哈,写入结果


	int size=strOut.size();
	wchar_t *out=new wchar_t[size];
	PreShow(strOut.c_str(),out);

	string show=WstrToStr(out);
	
	outFile<<show<<endl;

	//outFile<<strOut<<endl;
	outFile<<endl;
	outFile<<"————————————————————————————"<<endl;
	outFile<<endl;
	//outFile.open(pFlogfile,ofstream::out);
	//outFile<<strOut;
	//outFile.close();
	delete[] out;
	return TRUE;
} 
예제 #2
0
void settings_controller::get_key(Mongoose::Request &request, boost::smatch &what, Mongoose::StreamResponse &response) {
	if (!session->is_loggedin("settings", request, response))
		return;

	if (!validate_arguments(2, what, response)) {
		return;
	}
	std::string path = what.str(1);
	std::string key = what.str(2);

	if (!session->can("settings.get", request, response))
		return;

	Plugin::SettingsRequestMessage rm;
	Plugin::SettingsRequestMessage::Request *payload = rm.add_payload();
	payload->mutable_query()->mutable_node()->set_path(path);
	payload->mutable_query()->mutable_node()->set_key(key);
	payload->mutable_query()->set_recursive(false);
	payload->set_plugin_id(plugin_id);

	std::string str_response;
	core->settings_query(rm.SerializeAsString(), str_response);
	Plugin::SettingsResponseMessage pb_response;
	pb_response.ParseFromString(str_response);
	json_spirit::Object node;

	BOOST_FOREACH(const Plugin::SettingsResponseMessage::Response r, pb_response.payload()) {
		if (!r.has_query()) {
			response.setCode(HTTP_NOT_FOUND);
			response.append("Key not found: " + path + "/" + key);
			return;
		}
		const Plugin::SettingsResponseMessage::Response::Query &i = r.query();
		node["path"] = i.node().path();
		node["key"] = i.node().key();
		if (i.value().has_string_data()) {
			node["value"] = i.value().string_data();
		} else if (i.value().has_int_data()) {
			node["value"] = i.value().int_data();
		} else if (i.value().has_bool_data()) {
			node["value"] = i.value().bool_data();
		}
	}
	response.append(json_spirit::write(node));
}
예제 #3
0
 void operator()(const std::string& ua,
                 const RegexImpl::smatch& matches,
                 const size_t group,
                 Result& result) const
 {
     if (v_)
     {
         result.*f_ = *v_;
         return;
     }
     if (f_ && matches.size() >= group)
     {
         auto v = matches[group].str();
         result.*f_ = fn_ ? fn_(std::move(v)) : std::move(v);
     }
 }
예제 #4
0
파일: grep.cpp 프로젝트: 0xDEC0DE8/mcsema
void process_stream(std::istream& is)
{
   std::string line;
   int match_found = 0;
   int linenum = 1;
   while(std::getline(is, line))
   {
      bool result = boost::regex_search(line, what, re);
      if(result)
      {
         if(print_non_matching_files)
            return;
         if(files_only)
         {
            std::cout << current_file << std::endl;
            return;
         }
         if(!match_found && !count_only && (file_count > 1))
         {
            std::cout << current_file << ":\n";
         }
         ++match_found;
         if(!count_only)
         {
            if(print_line_numbers)
            {
               std::cout << linenum << ":";
            }
            if(print_byte_offset)
            {
               std::cout << what.position() << ":";
            }
            std::cout << what[0] << std::endl;
         }
      }
      ++linenum;
   }
   if(count_only && match_found)
   {
      std::cout << match_found << " matches found in file " << current_file << std::endl;
   }
   else if(print_non_matching_files && !match_found)
   {
      std::cout << current_file << std::endl;
   }
}
예제 #5
0
파일: regex.hpp 프로젝트: butangero/cfacter
    inline bool re_search_helper(Text const& txt, const boost::smatch &what, size_t depth, Arg arg, Args&&... args)
    {
        if (depth >= what.size()) {
            return false;
        }

        // If the match was optional and unmatched, skip it and leave the variable uninitialized.
        if (what[depth].matched) {
            try {
                using ArgType = typename std::pointer_traits<Arg>::element_type;
                auto val = boost::lexical_cast<ArgType>(what[depth]);
                *arg = val;
            } catch (const boost::bad_lexical_cast &e) {
                return false;
            }
        }

        return re_search_helper(txt, what, depth+1, std::forward<Args>(args)...);
    }
예제 #6
0
bool grep_callback_tianya(const  boost::smatch& what)
{
	// 1、先将what[0]中的内容拷贝到 pUtf8[] 中,再转换成 wstring型的输出

	// 1.1、计算在文件中的位置
	long mLocation=mPageNum*2*4096+what.position();

	// 1.2、取出匹配项
	//fs.seekg(mLocation,fstream::beg);
	//fs.read(pUtf8,(what[0]->second - what[0]->first));

	// 1.3、转换成 wstring 型
	string mUtf8(pUtf8);
	mUtf8.assign(what[0]);

	if (bFind==false)
	{
		outFile.open(logFile,ofstream::out);
		outFile<<"结果如下:"<<endl;
		outFile<<"————————————————————————————"<<endl;
		strcpy(pLogReturn[mIndex],logFile.c_str());
		bFind=true;
	}
	// 转换一哈,写入结果
	CString strUtf8(mUtf8.c_str());
	
	string show=CCharacterSetCode::UTF_8toString(strUtf8);

	outFile<<show<<endl;

	//outFile<<strOut<<endl;
	outFile<<endl;
	outFile<<"————————————————————————————"<<endl;
	outFile<<endl;
	//outFile.open(pFlogfile,ofstream::out);
	//outFile<<strOut;
	//outFile.close();
	//delete[] out;
	return TRUE;
} 
예제 #7
0
// Transcode the contents of a diff while leaving the structure of the diff
// untouched (including filenames and other non-diff content).
// This is implemented using a quick and dirty regex instead of with a proper
// diff parser, so there might be edge cases where stuff gets transcoded that
// should not be.
std::string convertDiff(const std::string& diff,
                        const std::string& fromEncoding,
                        const std::string& toEncoding,
                        bool allowSubst,
                        bool* pSuccess)
{
   if (pSuccess)
      *pSuccess = true;

   if (fromEncoding == toEncoding)
      return diff;

   if ((fromEncoding.empty() || fromEncoding == "UTF-8") &&
       (toEncoding.empty() || toEncoding == "UTF-8"))
   {
      return diff;
   }

   if (pSuccess)
      *pSuccess = false;

   std::string result;

   std::string transcoded;
   Error error;

   std::string::const_iterator lastMatchEnd = diff.begin();

   boost::regex contentLine("^(?:[+\\- ]|(?:@@[+\\-,\\d ]+@@))(.+?)$");
   boost::sregex_iterator iter(diff.begin(), diff.end(), contentLine,
                               boost::regex_constants::match_not_dot_newline);
   boost::sregex_iterator end;
   for (; iter != end; iter++)
   {
      const boost::smatch m = *iter;

      // Copy any lines we skipped over in getting here
      std::copy(m.prefix().first, m.prefix().second,
                std::back_inserter(result));

      std::string line = std::string(m[0].first, m[0].second);
      if (boost::algorithm::starts_with(line, "+++ ") ||
          boost::algorithm::starts_with(line, "--- "))
      {
         // This is a +++ or --- line, leave it alone
         std::copy(m[0].first, m[0].second, std::back_inserter(result));
      }
      else
      {
         // This is a content line, replace it!

         // Copy the leading part of the match verbatim
         std::copy(m[0].first, m[1].first, std::back_inserter(result));

         transcoded.clear();
         error = r::util::iconvstr(std::string(m[1].first, m[1].second),
                                   fromEncoding,
                                   toEncoding,
                                   allowSubst,
                                   &transcoded);
         if (error)
            return diff;

         // Don't allow transcoding to break diff semantics, which would happen if
         // new lines were introduced
         if (transcoded.find('\n') != std::string::npos)
            return diff;

         std::copy(transcoded.begin(), transcoded.end(),
                   std::back_inserter(result));

         // This should never copy any characters with the regex as it is
         // written today, but keeping it for symmetry.
         std::copy(m[1].second, m[0].second, std::back_inserter(result));
      }

      lastMatchEnd = m[0].second;
   }

   // Copy the last set of lines we skipped over (or if there were no matches,
   // then we're actually copying the entire diff)
   std::copy(lastMatchEnd, diff.end(), std::back_inserter(result));

   if (pSuccess)
      *pSuccess = true;

   return result;
}
예제 #8
0
void settings_controller::get_section(Mongoose::Request &request, boost::smatch &what, Mongoose::StreamResponse &response) {
	if (!session->is_loggedin("settings.list", request, response))
		return;

	if (!validate_arguments(1, what, response)) {
		return;
	}

	std::string path = what.str(1);

	Plugin::SettingsRequestMessage rm;
	Plugin::SettingsRequestMessage::Request *payload = rm.add_payload();
	payload->mutable_query()->mutable_node()->set_path(path);
	payload->mutable_query()->set_recursive(false);
	payload->set_plugin_id(plugin_id);
	payload = rm.add_payload();
	payload->mutable_query()->mutable_node()->set_path(path);
	payload->mutable_query()->set_recursive(true);
	payload->set_plugin_id(plugin_id);

	std::string str_response;
	core->settings_query(rm.SerializeAsString(), str_response);
	Plugin::SettingsResponseMessage pb_response;
	pb_response.ParseFromString(str_response);
	json_spirit::Object node;
	node["path"] = path;

	if (pb_response.payload_size() != 2) {
		response.setCode(HTTP_SERVER_ERROR);
		response.append("Failed to fetch keys");
		return;
	}

	const Plugin::SettingsResponseMessage::Response rKeys = pb_response.payload(0);
	if (!rKeys.has_query()) {
		response.setCode(HTTP_NOT_FOUND);
		response.append("Key not found: " + path);
		return;
	}

	json_spirit::Array keys;
	BOOST_FOREACH(const std::string &s, rKeys.query().value().list_data()) {
		keys.push_back(s);
	}
	node["keys"] = keys;

	const Plugin::SettingsResponseMessage::Response rPath = pb_response.payload(1);
	if (!rPath.has_query()) {
		response.setCode(HTTP_NOT_FOUND);
		response.append("Key not found: " + path);
		return;
	}
	json_spirit::Array paths;
	BOOST_FOREACH(const std::string &s, rPath.query().value().list_data()) {
		paths.push_back(s);
	}
	node["paths"] = paths;


	response.append(json_spirit::write(node));
}