⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpptohtm.cpp

📁 把C++源代码转换成HTML
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    cout << ">" << endl;
  }

  // Output a heading for the file, java script to print out the date when the
  // file was last modified and a horizontal rule.
  cout << "<CENTER><H1>" << fileName << "</H1></CENTER>" << endl;
  cout << "<SCRIPT LANGUAGE=\"JavaScript\">" << endl;
  cout << "<!--  Hide script contents from old browsers" << endl;
  cout << "document.write(\"Last Modified: \" + document.lastModified)" << endl;
  cout << "// End hiding of contents from old browsers  -->" << endl;
  cout << "</SCRIPT>" << endl;
  cout << "<HR SIZE=5>" << endl;

  // Indicate that the output text will be preformatted fixed-width text.
  cout << "<PRE>" << endl;

  // Define boolean flags for parsing the input string.
  bool inString     = false;
  bool inComment    = false;
  bool inEOLComment = false;
  bool inNumber     = false;
  bool escapeChar   = false;
  bool startString  = false;
  bool tokenFound   = false;

  // Define variables for the input line and tokens.  
  CString inputLine;
  CString token;

  // Process all lines in the file until end-of-file.
  while ( ! cfile.eof() )
  {
    // Read a line out of the input file.
    inputLine.readLine(cfile, FALSE);

    // Define the HTML output line and a flag indicating whether this line has
    // a preprocessor directive in it.
    CString htmlLine;

    bool preprocessor = false;

    // Loop over every character in the input line.
    for ( int i = 0; i < inputLine.length(); i++ )
    {
      // Extract the desired character.
      char chr = inputLine(i);

      // If a number is being processed and it is not part of a token, then
      // determine if the number has been completed.      
      if ( inNumber && ! tokenFound)
      {
        // If the current character is not a hexadecimal digit and it is not
        // an 'x' or 'X', then the number has been completed and its color
        // formatting will be turned off.
        if ( ! isxdigit(chr) && chr != 'x' && chr != 'X' )
        {
          htmlLine += endColor();

          inNumber = false;
        }
      }

      // Process the current character.
      switch ( chr )
      {
        // Catch all alphabetic characters and the underscore.  The underscore
        // is a legal character in variable names and keywords.
        case 'A': case 'a':
        case 'B': case 'b':
        case 'C': case 'c':
        case 'D': case 'd':
        case 'E': case 'e':
        case 'F': case 'f':
        case 'G': case 'g':
        case 'H': case 'h':
        case 'I': case 'i':
        case 'J': case 'j':
        case 'K': case 'k':
        case 'L': case 'l':
        case 'M': case 'm':
        case 'N': case 'n':
        case 'O': case 'o':
        case 'P': case 'p':
        case 'Q': case 'q':
        case 'R': case 'r':
        case 'S': case 's':
        case 'T': case 't':
        case 'U': case 'u':
        case 'V': case 'v':
        case 'W': case 'w':
        case 'X': case 'x':
        case 'Y': case 'y':
        case 'Z': case 'z':
        case '_':
          // If this character is not in a comment or a string, then add it to
          // the token string and indicate that a token has been found.
          if ( ! inComment && ! inString )
          {
            token += chr;
          
            tokenFound = true;
          }

          // Otherwise, simply add the character to the HTML output line.
          else
            htmlLine += chr;

          break;

        // Catch all numeric characters.           
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
          // If this character is not in a number, comment, string or token,
          // then add font coloring for a number to the HTML output line and
          // indicate that the start of a number has been found.
          if ( ! inNumber && ! inComment && ! inString && ! tokenFound)
          {
            htmlLine += startColor(darkMagenta);

            inNumber = true;
          }

          // If this is part of a token, then add it it to the token.  Otherwise
          // simply add it to the HTML output line.
          if ( tokenFound )          
            token += chr;
          else  
            htmlLine += chr;
          
          break;

        // Catch most of the C and C++ operators.
        case '&':
        case '<':
        case '>':
        case '(':
        case ')':
        case '{':
        case '}':
        case '[':
        case ']':
        case ';':
        case ':':
        case '!':
        case '%':
        case '^':
        case '*':
        case '-':
        case '+':
        case '=':
        case '|':
        case ',':
          // If a token was being built, process it and add it to the HTML
          // output line.
          if ( tokenFound )
            htmlLine += processToken(token, tokenFound, keywordList);

          // If the operator is not in a comment or a string, then turn the
          // coloring to dark red.          
          if ( ! inComment && ! inString )
             htmlLine += startColor(darkRed);

          // The '&', '<' and '>' are special symbols in HTML and must be
          // handled differently.  For all other operators, add them directly
          // to the HTML output line.          
          switch ( chr )
          {
            case '&':
              htmlLine += "&amp;";

              break;

            case '<':
              htmlLine += "&lt;";

              break;

            case '>':
              htmlLine += "&gt;";

              break;

            default:
              htmlLine += chr;
          }

          // If the operator is not in a comment or a string, then turn off
          // the coloring.          
          if ( ! inComment && ! inString )
            htmlLine += endColor();
         
          break;

        // Process a '/' which can be a division operator or the start or end
        // of a comment string.
        case '/':
          // If a token was being built, process it and add it to the HTML
          // output line.
          if ( tokenFound )
            htmlLine += processToken(token, tokenFound, keywordList);

          // Ensure that the character is not in a comment or a string.
          if ( ! inComment && ! inString )
          {
            // If the current character is not the last character in the line.
            if ( i < inputLine.length()-1 )
            {
              // If the next character is also a slash, then indicate that
              // this is a C++ style end-of-line comment.
              if ( inputLine(i+1) == '/' )
              {
                inComment    = true;
                inEOLComment = true;
              }

              // Otherwise, if the next character is an asterisk, then indicate
              // that this is a C style comment.
              else if ( inputLine(i+1) == '*' )
                inComment = true;
            }

            // If this is a comment, then set the color to dark green for
            // comments and italicize the text.
            if ( inComment )
              htmlLine += startColor(darkGreen) + "<I>";

            // Otherwise, color the division operator dark red.
            else
              htmlLine += startColor(darkRed);
          }

          // Add the character to the HTML output line.          
          htmlLine += chr;

          // If this is not in a string, then determine if this is the end of
          // a comment string.
          if ( ! inString )
          {
            // If the character is part of a comment.
            if ( inComment )
            {
              // If it is not in a C++ style end-of-line comment.
              if ( ! inEOLComment )
              {
                // If this is not the start of the line and the previous
                // character was an asterisk, then turn off the italicized
                // text, end the comment coloring and turn off the comment flag.
                if ( i > 0 && inputLine(i-1) == '*' )
                {
                  htmlLine += "</I>" + endColor();

                  inComment    = false;
                }
              }
            }

            // Otherwise this is a division operator, end operator coloring.
            else
              htmlLine += endColor();
          }

          break;

        // Process a quote or tick mark.
        case '\"':
        case '\'':
          // If a token was being built, process it and add it to the HTML
          // output line.
          if ( tokenFound )
            htmlLine += processToken(token, tokenFound, keywordList);

          // Determine if the character is escaped by checking to see if the
          // previous character was a backslash.
          if ( i > 0 && inputLine(i-1) == '\\' )
            escapeChar = true;
          else
            escapeChar = false;

          // If the previous character was an escape character, ensure that
          // the character prior to that was not a back slash also.  A double
          // backslash indicates a backslash inside a string.  If this is the
          // case, then the quote or tick are not escaped.
          if ( escapeChar && i > 1 && inputLine(i-2) == '\\' )
            escapeChar = false;

          // If the character is not in a comment or a string and it is not
          // escaped, then color the text read and indicate that a string has
          // been started.  Note that startString is used instead of inString.
          // This is done to avoid turning the string coloring off once the
          // character has been added to the HTML output string.
          if ( ! inComment && ! inString && ! escapeChar )
          {
            htmlLine += startColor(red);

            startString = true;
          }

          // Since the quote is a special HTML character and must be handled
          // differently.  Simply add the tick to the output line.
          if ( chr == '\"' )
            htmlLine += "&quot;";
          else
            htmlLine += "\'";

          // If this is not a comment and the character is part of a string
          // and it is not escaped, then turn off the quoted coloring.
          if ( ! inComment && inString && ! escapeChar )
          {
            htmlLine += endColor();

            inString = false;
          }

          // If a string has been started, then turn on the in string indicator.
          if ( startString )
          {
            startString = false;
            inString    = true;
          }
         
          break;

        case '#':
          // If a token was being built, process it and add it to the HTML
          // output line.
          if ( tokenFound )
            htmlLine += processToken(token, tokenFound, keywordList);

          // If the character is not in a comment or a string, then add the
          // pound sign with a blue color and bold it.
          if ( ! inComment && ! inString )
          {
            htmlLine += startColor(blue) + "<B>#</B>" + endColor();

            preprocessor = true;
          }

          // Otherwise, add the character to the HTML output line.
          else
            htmlLine += "#";

          break;

        // Catch all other characters.         
        default:
          // If a token was being built, process it and add it to the HTML
          // output line.
          if ( tokenFound )
            htmlLine += processToken(token, tokenFound, keywordList);

          // Add the character unedited to the HTML output line.
          htmlLine += chr;
      }
    }

    // If a number is being processed and it is not part of a token, then
    // terminate coloring.      
    if ( inNumber && ! tokenFound)
    {
      htmlLine += endColor();

      inNumber = false;
    }

    // If there are any unprocessed tokens, process them now.
    if ( tokenFound )
      htmlLine += processToken(token, tokenFound, keywordList);

    // If a C++ style end of line comment was started, then turn off the coloring
    // and italicizing in the HTML output line and indicate that no comments
    // are in effect.
    if ( inEOLComment )
    {
      htmlLine += "</I>" + endColor();

      inComment    = false;
      inEOLComment = false;
    }

    // If this line has an include preprocessor directive, then determine if
    // the system style includes should be colored as strings.
    if ( preprocessor && htmlLine.contains("include") )
      colorInclude(htmlLine, headerNamesList);

    // Output the HTML output line to standard out.
    cout << htmlLine << endl;
  }

  // End the HTML page by turning of preformatting and ending the page.
  cout << "</PRE>"  << endl;
  cout << "</BODY>" << endl;
  cout << "</HTML>" << endl;

  // Return to the system.
  return( 0 );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -