"Fossies" - the Fresh Open Source Software Archive

Member "cppcheck-1.89/democlient/democlient.cpp" (1 Sep 2019, 2887 Bytes) of package /windows/misc/cppcheck-1.89.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "democlient.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.87_vs_1.88.

    1 #include <ctime>
    2 #include <cstdio>
    3 #include <cstdlib>
    4 #include <cstring>
    5 #include <iostream>
    6 
    7 #include "cppcheck.h"
    8 #include "version.h"
    9 
   10 static void unencode(const char *src, char *dest)
   11 {
   12     for (; *src; src++, dest++) {
   13         if (*src == '+')
   14             *dest = ' ';
   15         else if (*src == '%') {
   16             unsigned int code;
   17             if (std::sscanf(src+1, "%2x", &code) != 1)
   18                 code = '?';
   19             *dest = code;
   20             src += 2;
   21         } else
   22             *dest = *src;
   23     }
   24     *dest = '\0';
   25 }
   26 
   27 static FILE *logfile = nullptr;
   28 
   29 class CppcheckExecutor : public ErrorLogger {
   30 private:
   31     const std::time_t stoptime;
   32     CppCheck cppcheck;
   33 
   34 public:
   35     CppcheckExecutor()
   36         : ErrorLogger()
   37         , stoptime(std::time(nullptr)+2U)
   38         , cppcheck(*this, false) {
   39         cppcheck.settings().addEnabled("all");
   40         cppcheck.settings().inconclusive = true;
   41     }
   42 
   43     void run(const char code[]) {
   44         cppcheck.check("test.cpp", code);
   45     }
   46 
   47     void reportOut(const std::string &outmsg) override { }
   48     void reportErr(const ErrorLogger::ErrorMessage &msg) override {
   49         const std::string s = msg.toString(true);
   50 
   51         std::cout << s << std::endl;
   52 
   53         if (logfile != nullptr)
   54             std::fprintf(logfile, "%s\n", s.c_str());
   55     }
   56 
   57     void reportProgress(const std::string& filename,
   58                         const char stage[],
   59                         const std::size_t value) override {
   60         if (std::time(nullptr) >= stoptime) {
   61             std::cout << "Time to analyse the code exceeded 2 seconds. Terminating.\n\n";
   62             cppcheck.terminate();
   63         }
   64     }
   65 };
   66 
   67 
   68 int main()
   69 {
   70     std::cout << "Content-type: text/html\r\n\r\n"
   71               << "<!DOCTYPE html>\n";
   72 
   73     char data[4096] = {0};
   74 
   75     const char *query_string = std::getenv("QUERY_STRING");
   76     if (query_string)
   77         std::strncpy(data, query_string, sizeof(data)-2);
   78 
   79     const char *lenstr = std::getenv("CONTENT_LENGTH");
   80     if (lenstr) {
   81         int len = std::min(1 + std::atoi(lenstr), (int)(sizeof(data) - 2));
   82         std::fgets(data, len, stdin);
   83     }
   84 
   85     if (data[4000] != '\0') {
   86         std::cout << "<html><body>For performance reasons the code must be shorter than 1000 chars.</body></html>";
   87         return EXIT_SUCCESS;
   88     }
   89 
   90     const char *pdata = data;
   91     if (std::strncmp(pdata, "code=", 5)==0)
   92         pdata += 5;
   93 
   94     char code[4096] = {0};
   95     unencode(pdata, code);
   96 
   97     logfile = std::fopen("democlient.log", "at");
   98     if (logfile != nullptr)
   99         std::fprintf(logfile, "===========================================================\n%s\n", code);
  100 
  101     std::cout << "<html><body>Cppcheck " CPPCHECK_VERSION_STRING "<pre>";
  102 
  103     CppcheckExecutor cppcheckExecutor;
  104     cppcheckExecutor.run(code);
  105 
  106     std::fclose(logfile);
  107 
  108     std::cout << "</pre>Done!</body></html>";
  109 
  110     return EXIT_SUCCESS;
  111 }