TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: JARI LAAKSONEN
date: 1997-03-28 21:57:00
subject: 01/11 Comment utilities for Java

========== CSplit: Version 2.0 ==========
========== CSplit: Begin part 1/11 ==========
========== CSplit: Begin file cmtconvr.txt ==========
This is the new release of my comment utilities, a C version of which was
already in a previous SNIPPETS release. There is not much documentation
-- I hope the code speaks for itself.
The "heart" of the program is implemented as a finite state machine which
parses the source files and also is an abstract base class for all the
utilities.
There are five utilities:
  CMTCONVR - comment converter: converts C++ comments to C comments.
  CMTCOUNT - comment counter: checks the balance of /* */ and counts
             code lines, commented lines and comment ratio.
  CMTREMOV - comment remover: removes comments from a source file
             (output file contains plain code)
  CMTXTRAC - comment extractor: extracts comments from source file
             (output file contains all comments)
  C2HTML   - C/C++ to HTML: creates a HTML file from C/C++ source
             file. Code is formatted as bold, and comments are in
             italics.
The package contains also a test file, CMTTESTS.C which has a bunch of
different styles of commenting -- some of which are quite perverse :-)
As a bonus there are also Java versions of the same utilities. See how
easy it is to translate your programs to Java when your objects are
carefully designed :-)
        // Albert                      email: jla@to.icl.fi
========== CSplit: End file cmtconvr.txt ==========
========== CSplit: Begin file C2Html.java ==========
/**
 * C2Html.java
 * Implementation of C/C++ to HTML converter.
 *
 * ver 1.0, 28 Mar 1997
 *
 * Public domain by:
 *   Jari Laaksonen
 *   Arkkitehdinkatu 30 A 2
 *   FIN-33720 Tampere
 *   FINLAND
 *
 *   Fidonet : 2:221/360.20
 *   Internet: jla@to.icl.fi
 */
class C2HtmlConverter extends CommentParser
{
    private boolean backsl = false;
    private boolean nl = false;
    // This flag is needed when comment starting is continued
    // in next line:
    //   /\
    //   * comment */
    // The BeginComment state is still on after InsideEscape state
    // and without flag the slash would be printed twice.
    private boolean slashPrinted = false;
    //==================================================================
    /**
     * Output chars and convert various HTML-specific characters.
     */
    void checkHtmlChar()
    {
        switch (getReadChar())
        {
        case '");   break;
        case '>' : print ("<");   break;
        case '&' : print ("&");  break;
        case '\"': print ("""); break;
        default  : printChar();
        }
    }
    //==================================================================
    /**
     * Action processor.
     */
    protected void processActions (int theEvent)
    {
        boolean rc = true;
        switch (itsState)
        {
        case CPState.NormalInput:
            if (theEvent == CPEvent.FOUND_SLASH)
            {
                rc = false;               // Don't print it yet...
            }
            break;
        case CPState.InsideEscape:
            if (itsPrevState == CPState.BeginComment
                && theEvent == CPEvent.FOUND_NL)
            {
                rc = false;               // Don't print it yet...
                nl = true;
            }
            break;
        case CPState.BeginComment:
            switch (theEvent)
            {
            case CPEvent.FOUND_SLASH:     // Yes, it's a C++ comment.
            case CPEvent.FOUND_STAR:      // Yes, it's a C comment.
                if (! slashPrinted)
                {
                    print ("/");
                    if (backsl)
                    {
                        // Print the previous backslash.
                        print ('\\');
                        backsl = false;
                    }
                    if (nl)
                    {
                        // Print the previous newline.
                        print ('\n');
                        nl = false;
                    }
                    checkHtmlChar();
                    rc = false;
                    slashPrinted = true;
                }
                else
                    slashPrinted = false;
                break;
            case CPEvent.FOUND_BACKSLASH:   // Escaped character.
                backsl = true;
                rc = false;
                break;
            default:                        // No, just a slash...
                // Print the previous slash.
                print ('/');
                if (backsl)
                {
                    // Print the previous backslash.
                    print ('\\');
                    backsl = false;
                }
                if (nl)
                {
========== CSplit: End part 1/11 crc: 1671 ==========
        // Albert                      email: jla@to.icl.fi
--- GoldED/2 2.50+
---------------
* Origin: Albert's Point/2 in Finland, Europe (2:221/360.20)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.