C++ 编辑 php 文件以保持其格式
C++ Editing a php file maintaining it's formatting
我有一个识别人的 C++ 脚本,因此它可以识别面孔,还可以识别属于那张面孔的人。我是一个 C++ 新手,所以我很高兴我可以让它工作(原始脚本不是我写的,但需要一些更改才能工作)。
此脚本中有一个函数需要在识别出我时更改 php 文件。
我写了这个函数,但是它完全破坏了php文件的格式,删除了一些我不想删除的代码。
查找 php 文件并对其进行编辑的 C++ 代码:
if(nWho==P_NICK)
{
fstream calendar("/var/www/html/MagicMirror_Old/calendar.php");
string readout;
string search;
search = "$url = 'some_URL_to_some_site'";
string replace;
replace = "$url = 'some_URL_to_some_other_site'"
while(getline(calendar,readout))
{
if(readout == search)
{
calendar << replace;
}
else
{
calendar << readout;
}
}
}
现在正在编辑的原php文件在编辑前的内容如下:
// Set the url of the calendar feed.
$url = 'some_URL_to_some_site';
/*****************************************/
// Run the helper function with the desired URL and echo the contents.
echo get_url($url);
// Define the helper function that retrieved the data and decodes the content.
function get_url($url)
{
//user agent is very necessary, otherwise some websites like google.com wont give zipped content
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",
"ignore_errors" => true //Fix problems getting data
),
//Fixes problems in ssl
"ssl" => array(
"verify_peer"=>false,
"verify_peer_name"=>false
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url ,false,$context);
//If http response header mentions that content is gzipped, then uncompress it
foreach($http_response_header as $c => $h)
{
if(stristr($h, 'content-encoding') and stristr($h, 'gzip'))
{
//Now lets uncompress the compressed data
$content = gzinflate( substr($content,10,-8) );
}
}
return $content;
}
C++编辑文件后变为:
<?php
<?php Set the url of the calendar feed.
Set the url of the calendar feed.= 'https://p01-calendarws.icloud.com/ca/subscribe/1/n6x7Farxpt7m9S8bHg1TGArSj7J6kanm_2KEoJPL5YIAk3y70FpRo4GyWwO-6QfHSY5mXtHcRGVxYZUf7U3HPDOTG5x0qYnno1Zr_VuKH2M';
= 'https://p01-calendarws.icloud.com/ca/subscribe/1/n6x7Farxpt7m9S8bHg1TGArSj7J6kanm_2KEoJPL5YIAk3y70FpRo4GyWwO-6QfHSY5mXtHcRGVxYZUf7U3HPDOTG5x0qYnno1Zr_VuKH2M';***********/
***********/ helper function with the desired URL and echo the contents.
helper function with the desired URL and echo the contents.trieved the data and decodes the content.
trieved the data and decodes the content.ent is very necessary, otherwise some websites like google.com wont give zipped content
ent is very necessary, otherwise some websites like google.com wont give zipped content'header'=>"Accept-Language: en-US,en;q=0.8rn" .
'header'=>"Accept-Language: en-US,en;q=0.8rn" .,deflate,sdchrn" .
,deflate,sdchrn" . "Accept-Charset:UTF-8,*;q=0.5rn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .illa/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",
illa/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",ixes problems in ssl
ixes problems in ssl "verify_peer"=>false,
"verify_peer"=>false,=>false
=>false );
); $context = stream_context_create($opts);
$context = stream_context_create($opts);e,$context);
e,$context); /If http response header mentions that content is gzipped, then uncompress it
/If http response header mentions that content is gzipped, then uncompress it, 'content-encoding') and stristr($h, 'gzip'))
, 'content-encoding') and stristr($h, 'gzip'))the compressed data
the compressed datant = gzinflate( substr($content,10,-8) );
nt = gzinflate( substr($content,10,-8) );tent;
tent;
您可能已经注意到,考虑到文件的原始状态,这不是该文件应有的样子。
基本上只有第二行的 $url 需要替换为不同的 url,php 文件的其余格式应保持不变。
有没有办法在 C++ 中做到这一点?
使用此 SO 答案中的 replace() 和 replaceAll() 函数代码来替换字符串中的某些文本:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
int main()
{
ifstream calendar("calendar.php");
std::stringstream buffer;
// read whole file in a buffer
buffer << calendar.rdbuf();
// use a new file for output
ofstream newcalendar;
newcalendar.open("newcalendar.php");
string search = "$url = 'some_URL_to_some_site'";
string to = "$url = 'some_URL_to_some_other_site'";
string content = buffer.str();
replaceAll(content, search, to);
newcalendar << content;
newcalendar.close();
calendar.close();
remove("calendar.php");
rename("newcalendar.php", "calendar.php");
return 0;
}
注意,搜索到的文字拼写必须准确!
编辑: 添加两行用于重命名文件
我有一个识别人的 C++ 脚本,因此它可以识别面孔,还可以识别属于那张面孔的人。我是一个 C++ 新手,所以我很高兴我可以让它工作(原始脚本不是我写的,但需要一些更改才能工作)。
此脚本中有一个函数需要在识别出我时更改 php 文件。
我写了这个函数,但是它完全破坏了php文件的格式,删除了一些我不想删除的代码。
查找 php 文件并对其进行编辑的 C++ 代码:
if(nWho==P_NICK)
{
fstream calendar("/var/www/html/MagicMirror_Old/calendar.php");
string readout;
string search;
search = "$url = 'some_URL_to_some_site'";
string replace;
replace = "$url = 'some_URL_to_some_other_site'"
while(getline(calendar,readout))
{
if(readout == search)
{
calendar << replace;
}
else
{
calendar << readout;
}
}
}
现在正在编辑的原php文件在编辑前的内容如下:
// Set the url of the calendar feed.
$url = 'some_URL_to_some_site';
/*****************************************/
// Run the helper function with the desired URL and echo the contents.
echo get_url($url);
// Define the helper function that retrieved the data and decodes the content.
function get_url($url)
{
//user agent is very necessary, otherwise some websites like google.com wont give zipped content
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-Language: en-US,en;q=0.8rn" .
"Accept-Encoding: gzip,deflate,sdchrn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",
"ignore_errors" => true //Fix problems getting data
),
//Fixes problems in ssl
"ssl" => array(
"verify_peer"=>false,
"verify_peer_name"=>false
)
);
$context = stream_context_create($opts);
$content = file_get_contents($url ,false,$context);
//If http response header mentions that content is gzipped, then uncompress it
foreach($http_response_header as $c => $h)
{
if(stristr($h, 'content-encoding') and stristr($h, 'gzip'))
{
//Now lets uncompress the compressed data
$content = gzinflate( substr($content,10,-8) );
}
}
return $content;
}
C++编辑文件后变为:
<?php
<?php Set the url of the calendar feed.
Set the url of the calendar feed.= 'https://p01-calendarws.icloud.com/ca/subscribe/1/n6x7Farxpt7m9S8bHg1TGArSj7J6kanm_2KEoJPL5YIAk3y70FpRo4GyWwO-6QfHSY5mXtHcRGVxYZUf7U3HPDOTG5x0qYnno1Zr_VuKH2M';
= 'https://p01-calendarws.icloud.com/ca/subscribe/1/n6x7Farxpt7m9S8bHg1TGArSj7J6kanm_2KEoJPL5YIAk3y70FpRo4GyWwO-6QfHSY5mXtHcRGVxYZUf7U3HPDOTG5x0qYnno1Zr_VuKH2M';***********/
***********/ helper function with the desired URL and echo the contents.
helper function with the desired URL and echo the contents.trieved the data and decodes the content.
trieved the data and decodes the content.ent is very necessary, otherwise some websites like google.com wont give zipped content
ent is very necessary, otherwise some websites like google.com wont give zipped content'header'=>"Accept-Language: en-US,en;q=0.8rn" .
'header'=>"Accept-Language: en-US,en;q=0.8rn" .,deflate,sdchrn" .
,deflate,sdchrn" . "Accept-Charset:UTF-8,*;q=0.5rn" .
"Accept-Charset:UTF-8,*;q=0.5rn" .illa/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",
illa/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn",ixes problems in ssl
ixes problems in ssl "verify_peer"=>false,
"verify_peer"=>false,=>false
=>false );
); $context = stream_context_create($opts);
$context = stream_context_create($opts);e,$context);
e,$context); /If http response header mentions that content is gzipped, then uncompress it
/If http response header mentions that content is gzipped, then uncompress it, 'content-encoding') and stristr($h, 'gzip'))
, 'content-encoding') and stristr($h, 'gzip'))the compressed data
the compressed datant = gzinflate( substr($content,10,-8) );
nt = gzinflate( substr($content,10,-8) );tent;
tent;
您可能已经注意到,考虑到文件的原始状态,这不是该文件应有的样子。
基本上只有第二行的 $url 需要替换为不同的 url,php 文件的其余格式应保持不变。 有没有办法在 C++ 中做到这一点?
使用此 SO 答案中的 replace() 和 replaceAll() 函数代码来替换字符串中的某些文本:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
int main()
{
ifstream calendar("calendar.php");
std::stringstream buffer;
// read whole file in a buffer
buffer << calendar.rdbuf();
// use a new file for output
ofstream newcalendar;
newcalendar.open("newcalendar.php");
string search = "$url = 'some_URL_to_some_site'";
string to = "$url = 'some_URL_to_some_other_site'";
string content = buffer.str();
replaceAll(content, search, to);
newcalendar << content;
newcalendar.close();
calendar.close();
remove("calendar.php");
rename("newcalendar.php", "calendar.php");
return 0;
}
注意,搜索到的文字拼写必须准确!
编辑: 添加两行用于重命名文件