001: 
002: #include <iostream.h>
003: #include <stdlib.h>
004: #include <ctype.h>
005: 
006: //  C to HTML converter.
007: //  Adds line numbers e.g.  001:
008: //  Boldfaces the code, leaves comments in regular type.
009: //  converts < to &lt;
010: //           > to &gt;
011: //           & to &amp;
012: 
013: //  Bug:    Doesn't handle /* or // in quotes correctly.
014: 
015: 
016: void put_htmlchar(char);
017: void handle_comment(void);
018: void print_ln(void);
019: 
020: main()
021: {
022:     int bold;
023:     int current;
024: 
025:     cout << "<PRE>\n";
026:     while ((current = cin.get()) != EOF)
027:     {   
028:         bold = 0;           // begin lines wo/boldface
029:         print_ln();         // print line number
030:         cin.putback(current);       // otherwise we would miss a character
031:         while ((current = cin.get()) != '\n')
032:         {
033:             if (current == '/')
034:             {   
035:                 if ((current = cin.get()) == '/')
036:                 {   if (bold)
037:                     {   cout << "</B>";     // end boldface for comments
038:                         bold = 0;   }
039:                     cout << '/' << '/';
040:                     while ((current = cin.get()) != '\n')
041:                         put_htmlchar(current);
042:                     break;
043:                 }
044:                 else if (current == '*')    //  '/*' style comment.
045:                 {   if (bold)
046:                     {   cout << "</B>"; 
047:                         bold = 0;   }
048:                     cout << '/' << '*';
049:                     handle_comment();
050:                     continue;
051:                 }
052:                 else 
053:                 {   cout << '/';
054:                     if (current == '\n')    break;
055:                 }
056:             }
057:             if (!bold && !isspace(current))
058:             {   cout << "<B>";      // begin boldface
059:                 bold = 1;       }
060:             put_htmlchar(current);
061:         }
062:         if (bold)
063:             cout << "</B>";
064:         cout << '\n';
065:     }
066:     cout << "</PRE>\n";
067: }
068: 
069: void handle_comment(void)       // deal with /* style comments
070: {   
071:     char current = cin.get();
072:     for (;;)
073:     {
074:         put_htmlchar(current);
075:         if (current == '\n')
076:             print_ln();
077:         else if (current == '*')
078:             if ((current = cin.get()) == '/')
079:                 break;
080:             else continue;
081:         current = cin.get();
082:     }
083:     cout << '/';
084: }
085: 
086: void put_htmlchar(char current)
087: {
088:     if (current == '<')
089:         cout << "&lt;";
090:     else if (current == '>')
091:         cout << "&gt;";
092:     else if (current == '&')
093:         cout << "&amp;";
094:     else cout << char(current);
095: }
096: 
097: void print_ln(void)         // prints line number
098: {
099:     static int linenumber;
100: 
101:         linenumber ++;      
102:         if (linenumber < 10)
103:             cout << "00" << linenumber << ": ";
104:         else if (linenumber < 100)
105:             cout << '0' << linenumber << ": ";
106:         else
107:             cout << linenumber << ": ";
108: }