Advertisement
cwhii

CodeReview - HTML Tidy Sample Program in C++17

Jun 13th, 2017
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. // tidyit.cpp
  2. //
  3. // 11Jun'17 cwhii
  4. //          Improvements based upon Stack Exchange,
  5. //          https://codereview.stackexchange.com/a/165231/2421
  6. //  4May'17 cwhii
  7. //          New, based upon tidylib example,
  8. //          http://api.html-tidy.org/tidy/tidylib_api_5.1.25/tidylib.html
  9. // ---------------------------------------------------------------------------------------------------
  10. #include <sstream>
  11. #include <iostream>
  12. #include <string>
  13. #include <tidy/tidy.h>
  14. #include <tidy/buffio.h>
  15.  
  16. class TidyDocCPP {
  17.    std::string file_name_;
  18.    TidyDoc tdoc_;
  19.    TidyBuffer errbuf_;
  20.    TidyBuffer outbuf_;
  21.    int rc_;
  22. public:
  23.    TidyDocCPP(const std::string file_name_a):file_name_{file_name_a},tdoc_{tidyCreate()},errbuf_{0},outbuf_{0},rc_{-1} {
  24.       tidyOptSetInt(tdoc_, TidyIndentContent, yes);
  25.       tidyOptSetBool(tdoc_, TidyXhtmlOut, yes);
  26.       tidyOptSetBool(tdoc_, TidyForceOutput,yes);
  27.       }
  28.    ~TidyDocCPP() {
  29.       tidyBufFree(&outbuf_);
  30.       tidyBufFree(&errbuf_);
  31.       tidyRelease(tdoc_);
  32.       }
  33.    TidyDocCPP(TidyDocCPP const&)            = delete;   // Rule of 5.
  34.    TidyDocCPP(TidyDocCPP&&)                 = delete;
  35.    TidyDocCPP& operator=(TidyDocCPP const&) = delete;
  36.    TidyDocCPP& operator=(TidyDocCPP&&)      = delete;
  37.    operator TidyDoc() const {return tdoc_;} // Convert object back to C for use in C tidy functions
  38.    char* getErr(){return (char*)((errbuf_.bp)?(char*)(errbuf_.bp):"");}
  39.    char* getOut(){return (char*)((outbuf_.bp)?(char*)(outbuf_.bp):"");}
  40.    int getRc(){return rc_;}
  41.    bool is_severe_error(int code_a){return ((rc_=code_a)<0);}        // Change to auto in cevelop 1.8.
  42.  
  43.    bool tidy(){
  44.       return is_severe_error(tidySetErrorBuffer(tdoc_,&errbuf_))
  45.             || is_severe_error(tidyParseFile(tdoc_,file_name_.c_str()))
  46.             || is_severe_error(tidyCleanAndRepair(tdoc_))
  47.             || is_severe_error(tidyRunDiagnostics(tdoc_))
  48.             || is_severe_error(tidySaveBuffer(tdoc_,&outbuf_));
  49.       }
  50.    };
  51.  
  52. const std::string tidy_file(std::string file_name_a) {               // Change to auto in cevelop 1.8.
  53.    TidyDocCPP tDoc{file_name_a};
  54.    if (tDoc.tidy()){
  55.       std::cerr << "tidyit-A severe error (" << tDoc.getRc() << ") occurred.\n";
  56.       }
  57.    std::cerr << "tidyit-Diagnostics:\n\n" << tDoc.getErr() << "\n";
  58.    const std::string outstr{tDoc.getOut()};
  59.    return outstr;
  60.    }
  61.  
  62. int main(int argc, char **argv) {
  63.    std::cout << "tidyit-version: so-2\n\n";
  64.    for (std::string name : {"poor.html","good.html"}){
  65.       std::cout << "\nInput file name: " + name << std::endl;
  66.       std::cout << tidy_file(name);
  67.       }
  68.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement