@@ -49,37 +49,34 @@ bool PmFromFile::init(const std::string &config, std::string *error) {
4949 std::vector<std::string> tokens = split (m_param, ' ' );
5050
5151 for (const auto & token : tokens) {
52- if (! token.empty ()) {
53-
54- std::istream *iss;
52+ if (token.empty ()) {
53+ continue ;
54+ }
5555
56- if (token.compare (0 , 8 , " https://" ) == 0 ) {
57- Utils::HttpsClient client;
58- bool ret = client.download (token);
59- if (ret == false ) {
60- error->assign (client.error );
61- return false ;
62- }
63- iss = new std::stringstream (client.content );
64- } else {
65- std::string err;
66- std::string resource = utils::find_resource (token, config, &err);
67- iss = new std::ifstream (resource, std::ios::in);
56+ std::unique_ptr<std::istream> iss;
6857
69- if (((std::ifstream *)iss)->is_open () == false ) {
70- error->assign (" Failed to open file: '" + token + " '. " + err);
71- delete iss;
72- return false ;
73- }
58+ if (token.compare (0 , 8 , " https://" ) == 0 ) {
59+ Utils::HttpsClient client;
60+ bool ret = client.download (token);
61+ if (ret == false ) {
62+ error->assign (client.error );
63+ return false ;
7464 }
75-
76- for (std::string line; std::getline (*iss, line); ) {
77- if (isComment (line) == false ) {
78- acmp_add_pattern (m_p, line.c_str (), NULL , NULL , line.length ());
79- }
65+ iss = std::make_unique<std::stringstream>(client.content );
66+ } else {
67+ std::string err;
68+ std::string resource = utils::find_resource (token, config, &err);
69+ auto file = std::make_unique<std::ifstream>(resource, std::ios::in);
70+ if (file->is_open () == false ) {
71+ error->assign (" Failed to open file: '" + token + " '. " + err);
72+ return false ;
73+ }
74+ iss = std::move (file);
75+ }
76+ for (std::string line; std::getline (*iss, line); ) {
77+ if (isComment (line) == false ) {
78+ acmp_add_pattern (m_p, line.c_str (), NULL , NULL , line.length ());
8079 }
81-
82- delete iss;
8380 }
8481 }
8582
0 commit comments