@@ -547,10 +547,9 @@ unsigned int CppCheck::check(const std::string &path)
547547 return checkFile (Path::simplifyPath (path), emptyString);
548548}
549549
550- unsigned int CppCheck::check (const std::string &path, const std::string &content )
550+ unsigned int CppCheck::check (const std::string &path, const uint8_t * data, std::size_t size )
551551{
552- std::istringstream iss (content);
553- return checkFile (Path::simplifyPath (path), emptyString, &iss);
552+ return checkBuffer (Path::simplifyPath (path), emptyString, data, size);
554553}
555554
556555unsigned int CppCheck::check (const FileSettings &fs)
@@ -590,15 +589,41 @@ unsigned int CppCheck::check(const FileSettings &fs)
590589 return returnValue;
591590}
592591
593- static simplecpp::TokenList createTokenList (const std::string& filename, std::vector<std:: string>& files, simplecpp::OutputList* outputList , std::istream* fileStream )
592+ unsigned int CppCheck::checkBuffer (const std::string& filename, const std::string &cfgname, const uint8_t * data , std::size_t size )
594593{
595- if (fileStream)
596- return {*fileStream, files, filename, outputList};
594+ return checkInternal (filename, cfgname,
595+ [&filename, data, size](TokenList& list) {
596+ list.createTokens (data, size, filename);
597+ },
598+ [&filename, data, size](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
599+ return simplecpp::TokenList{data, size, files, filename, outputList};
600+ });
601+ }
602+
603+ unsigned int CppCheck::checkStream (const std::string& filename, const std::string &cfgname, std::istream& fileStream)
604+ {
605+ return checkInternal (filename, cfgname,
606+ [&filename, &fileStream](TokenList& list) {
607+ list.createTokens (fileStream, filename);
608+ },
609+ [&filename, &fileStream](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
610+ return simplecpp::TokenList{fileStream, files, filename, outputList};
611+ });
612+ }
597613
598- return {filename, files, outputList};
614+ unsigned int CppCheck::checkFile (const std::string& filename, const std::string &cfgname)
615+ {
616+ return checkInternal (filename, cfgname,
617+ [&filename](TokenList& list) {
618+ std::ifstream in (filename);
619+ list.createTokens (in, filename);
620+ },
621+ [&filename](std::vector<std::string>& files, simplecpp::OutputList* outputList) {
622+ return simplecpp::TokenList{filename, files, outputList};
623+ });
599624}
600625
601- unsigned int CppCheck::checkFile (const std::string& filename, const std::string &cfgname, std::istream* fileStream )
626+ unsigned int CppCheck::checkInternal (const std::string& filename, const std::string &cfgname, const CreateTokensFn& createTokens, const CreateTokenListFn& createTokenList )
602627{
603628 // TODO: move to constructor when CppCheck no longer owns the settings
604629 if (mSettings .checks .isEnabled (Checks::unusedFunction) && !mUnusedFunctionsCheck )
@@ -645,21 +670,15 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
645670 Tokenizer tokenizer (mSettings , *this );
646671 // enforce the language since markup files are special and do not adhere to the enforced language
647672 tokenizer.list .setLang (Standards::Language::C, true );
648- if (fileStream) {
649- tokenizer.list .createTokens (*fileStream, filename);
650- }
651- else {
652- std::ifstream in (filename);
653- tokenizer.list .createTokens (in, filename);
654- }
673+ createTokens (tokenizer.list );
655674 mUnusedFunctionsCheck ->parseTokens (tokenizer, mSettings );
656675 }
657676 return EXIT_SUCCESS;
658677 }
659678
660679 simplecpp::OutputList outputList;
661680 std::vector<std::string> files;
662- simplecpp::TokenList tokens1 = createTokenList (filename, files, &outputList, fileStream );
681+ simplecpp::TokenList tokens1 = createTokenList (files, &outputList);
663682
664683 // If there is a syntax error, report it and stop
665684 const auto output_it = std::find_if (outputList.cbegin (), outputList.cend (), [](const simplecpp::Output &output){
0 commit comments