summaryrefslogtreecommitdiff
path: root/comments/dformat.cpp
blob: 7108dbd77a8f9ec61351ba2dee427699afe66254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
 * mangle.cpp - Removes ALL comments and/or formatting from C/C++ code while
 *              keeping what is needed so that the program still operates
 *              the same exact way as before the conversion.
 *
 */

#include "main.h"
#define CLASS_ERROR_PRE "dformat"

dformat::dformat(int a, char** av)
{
  ready=true; // We're ok unless otherwise changed
  current_arg=1; // Start at argument 1
  current_file=0;
  argc=a; argv=av;
  io.regionsCount = 0;
  io.doneOutput = 0;

  if(!load_arguments((char *)"-x")) cerr << "Programmer goofed, you should not see this. Error clearing out arguments." << endl;
  test_args=true; // Lets make sure the arg syntax is good first
  while(next()) ;

#ifdef DEBUG
  cerr << CLASS_ERROR_PRE << "::dformat() argument syntax ok." << endl;
#endif

  current_arg=1;
  io.done(test_args);
  test_args=false;
  // Clear out the settings
  if(!load_arguments((char *)"-x")) cerr << "Programmer goofed, you should not see this. Error clearing out arguments." << endl;
};

dformat::~dformat() {};

bool dformat::next()
{
  char temp[FILE_NAME_LENGTH]={"\0"};
  char temp2[FILE_NAME_LENGTH]={"\0"};

  if(!ready) // Can't work if I'm not ready.
    return false;

#ifdef DEBUG
  if(test_args)
    cerr << CLASS_ERROR_PRE << "::next() testing argument ["
         << argv[current_arg] << "]" << endl;
#endif

  if(current_arg<argc)
  {
    if(argv[current_arg][0]=='-') // we have args waiting
    {
      if((current_arg+1)<argc || argv[current_arg][1]=='v')
      {
        // load args and move to next
        if(!load_arguments(argv[current_arg++]))
        {
          usage();
          return false;
        }
      }
      else
      {
#ifdef DEBUG
	cerr << CLASS_ERROR_PRE << "::next() Argument included without a file." << endl;
#endif
	ready=false;
	usage();
	return false;
      }
    }

    io.done(test_args); // Finish it off if needed.
    
    strcpy(temp,argv[current_arg]);
    strcat(temp,DEFAULT_MANGLED_POSTFIX);
    strcpy(temp2,argv[current_arg]);
    strcat(temp2,DEFAULT_COMMENTS_POSTFIX);
    io.init(argv[current_arg],temp, temp2);
    
    if(!io.ok())
    {
#ifdef DEBUG
      cerr << CLASS_ERROR_PRE << "::next() io object not ok." << endl;
#endif
      ready=false;
      return false;
    }
    current_arg++; // Done messing with this one, move to next
  }
  else if(argc==1) // tisk tisk...you need atleast 2 arguments
  {
    usage();
    ready=false;
    return false;
  }
  else
    return false;

  if(!test_args)
    current_file++;

  return true; // all is good
};

void dformat::done()
{
  if(append_newline)
    io.out('\n');

  if(!tabular_delimited_result && !comma_delimited_result)
  {
    if (!io.output_to_stdout) {
      cerr << "[" << current_file << "] \"" << io.i_name << "\" (" << io.input_bytes() << "b) ";
      
      cerr << ">> \"" << io.o_name << "\" (" << io.output_bytes() << "b) (" << (100.0-(100.0*(io.output_bytes()/io.input_bytes()))) << "% reduced)";
      cerr << endl;
    }
  }
  else if(tabular_delimited_result) // print in tabular form
    cerr << current_file
         << "\t" << io.i_name
         << "\t" << io.o_name
         << "\t" << io.input_bytes()
         << "\t" << io.output_bytes()
         << "\t" << (100.0-(100.0*(io.output_bytes()/io.input_bytes())))
         << endl;
  else if(comma_delimited_result)
    cerr << current_file
         << "," << io.i_name
         << "," << io.o_name
         << "," << io.input_bytes()
         << "," << io.output_bytes()
         << "," << (100.0-(100.0*(io.output_bytes()/io.input_bytes())))
         << endl;

  io.done();
};

void dformat::usage()
{
  cerr << "Usage: " << NAME << " <options> [file1] <options> <file2> <etc>"
       << endl << "       -r            Leave CR/LF's"
       << endl << "       -o            output to STDOUT"
       << endl << "       -n            Append newline"
       << endl << "       -x            Use default options (nulls all previous ones)"
       << endl << "       -d            Leave in preprocessor whitespace"
    //       << endl << "       -w            Write over original"
//       << endl << "       -i            input from STDIN"
       << endl << "       -l            Do no mangling"
       << endl << "       -m            Do not add  markers to output"
       << endl << "       -t            Print summary in tab delimited form"
       << endl << "       -C            Print summary in comma delimited form"
       << endl << "       -v            Print version"
       << endl << "       -c<Number>    Number of comment regions"
       << endl;
}

bool dformat::ok()
{
  return (ready ? true : false);
};

bool dformat::load_arguments(char* str)
{
  if(strlen(str)==0)
    return false;

  for(unsigned int x=1; x<strlen(str); x++)
  {
    switch(str[x])
    {
      case 'x':
        io.input_from_stdin=false;
        io.output_to_stdout=false;
        io.addMarkers=true;
        io.regionsCount = 0;
        append_newline=false;
        comments_only=true;
        keep_preprocessor_whitespace=false;
        tabular_delimited_result=false;
        comma_delimited_result=false;
        leave_newline=false;
        no_modify=false;
        break;

#ifdef asdfasd
      case 'w':
        io.write_over_original=true;
        break;

#endif
      case 'm':
        io.addMarkers=false;
        break;

      case 'n':
        append_newline=true;
        break;
      case 'c':
        // next token should be an integer..
        {
          int i=1;
          char temp[256];
          while ((x+i < strlen(str)) &&
                 str[x+i] >= '0' &&
                 str[x+i] <= '9' &&
                 x+i < 256
                 ) {
            temp[i-1] = str[x+i];
            i++;
          }
          temp[i-1] =0;
          if (i == 1 || i > 255) {
            cerr << "Illegal number of comment regions for -c option" << i
                 << endl;
            exit(1);
          }
          io.regionsCount = atoi(temp);
          //          cerr << "Number of regions [" << io.regionsCount << "]" << endl;
          x+=i-1;
        }
        break;

      case 'r':
        leave_newline=true;
        break;

      case 'd':
        keep_preprocessor_whitespace=true;
        break;

      case 't':
        tabular_delimited_result=true;
        break;

      case 'C':
        comma_delimited_result=true;
        break;

      case 'v':
        version();
        exit(0);
        break;

      case 'l':
        no_modify=true;
        break;

      case 'o':
        io.output_to_stdout=true;
        break;

//      case 'i':
//        io.input_from_stdin=true;
//        break;

      default: // Unknown option
        usage();
        return false;
        break;
    }
  }
  return true;
};

// And now...the meat and potatos
bool dformat::format()
{
  int x=0;
  char c='\0';
  bool tbool=false;

#ifdef DEBUG
  cerr << CLASS_ERROR_PRE << "::format() Now formatting [" << io.i_name << "]"
       << endl;
#endif

  // Reset the variables
  for(x=0; x<FLAG_HISTORY_MAX; x++)
  {
    flag_history[x].in_single_quote=false;
    flag_history[x].in_double_quote=false; 
    flag_history[x].in_line_comment=false;
    flag_history[x].in_star_comment=false;
    flag_history[x].in_preprocessor=false;
    flag_history[x].in_hex=false;
    flag_history[x].num_backslashes=0;
  }

  // keep grabbing data as long as its there
  while(io.in() && io.data_waiting())
  {
    if (io.doneOutput) {
      break;
    }
    if(no_modify)
    {
      io.out();
      continue;
    }

    switch(io.buf[0])
    {
      case '\'':
      case '\"':
        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          if(!flag_history[0].in_single_quote && !flag_history[0].in_double_quote)
          {
            if(io.buf[0]=='\'')
              flag_history[0].in_single_quote=true;
            else if(io.buf[0]=='\"')
              flag_history[0].in_double_quote=true;
          }
          else if(flag_history[0].in_single_quote || flag_history[0].in_double_quote)
          {
            if((flag_history[0].num_backslashes%2)) ; // just an escaped quote, reset number of backslashes
            else
            {
              if(io.buf[0]=='\'' && flag_history[0].in_single_quote)
                flag_history[0].in_single_quote=false;
              if(io.buf[0]=='\"' && flag_history[0].in_double_quote)
                flag_history[0].in_double_quote=false;
            }
          }
          io.out();

          flag_history[0].num_backslashes=0; // null out number of backslashes
          flag_history[0].in_hex=false; // we're not in a hex value anymore
        } else {
          io.commentOut(); //dmg
        }
        
      break;

      case '/':
        if(!flag_history[0].in_single_quote && !flag_history[0].in_double_quote)
        {
          if(io.buf[1]=='/' && !flag_history[0].in_line_comment && !flag_history[0].in_star_comment) {
            flag_history[0].in_line_comment=true;
            io.commentOut(); //dmg
          }
          else if(io.buf[1]=='*' && !flag_history[0].in_star_comment) {
            flag_history[0].in_star_comment=true;
            io.commentOut(); //dmg
          }
          else if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
            io.out();
          else 
            io.commentOut(); //dmg
        }
        else
          io.out();

        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          flag_history[0].in_hex=false; // we're not in a hex value anymore
          flag_history[0].num_backslashes=0; // Null out number of backslashes
        }
      break;

      case '*':
        if(!flag_history[0].in_single_quote && !flag_history[0].in_double_quote)
        {
          if(io.buf[1]=='/' && flag_history[0].in_star_comment &&
             flag_history[2].in_star_comment)
          {
            io.commentOut(); 
            /* We need to write a \n after the comment... otherwise it gets very, very messy */

            flag_history[0].in_star_comment=false;
            io.in(); // Jump ahead one, we dont want the '/' used
            io.commentOut();
            io.commentOut('\n'); 
            continue;
          } else if(!flag_history[0].in_star_comment && !flag_history[0].in_line_comment) {
            io.out();
          } else {
            io.commentOut();
          }
        }
        else
          io.out();

        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          flag_history[0].in_hex=false; // we're not in a hex value anymore
          flag_history[0].num_backslashes=0; // Null out number of backslashes
        }
      break;

      case '#':
        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          if(!flag_history[0].in_single_quote && !flag_history[0].in_double_quote)
          {
            tbool=contain_preprocessor((io.buf+1));
            // Make sure we have the required carriage return before the preprocessor (if it really is a preprocessor)
            if(io.last_written[(LAST_WRITTEN_LENGTH-1)] &&
               (c=last_non_whitespace(io.last_written,(LAST_WRITTEN_LENGTH-1)))!='\r' && c!='\n' &&
               tbool)
              io.out('\n');

            if(tbool)
              flag_history[0].in_preprocessor=true;
          }
          io.out();
          flag_history[0].num_backslashes=0; // null out number of backslashes
          flag_history[0].in_hex=false; // we're not in a hex value anymore
        } else {
          io.commentOut();
        }
      break;

      case '\n':
      case '\r':
        if(!flag_history[0].in_star_comment)
        {
          if((((is_letter(io.last_written[(LAST_WRITTEN_LENGTH-1)]) || is_number(io.last_written[(LAST_WRITTEN_LENGTH-1)]) ||
                io.last_written[(LAST_WRITTEN_LENGTH-1)]=='_') &&
             (is_letter(io.buf[1]) || is_number(io.buf[1]) || io.buf[1]=='_'))
             || flag_history[0].in_preprocessor || io.buf[1]=='#') && !flag_history[0].in_single_quote &&
             !flag_history[0].in_double_quote && !comments_only)
          {
            if(flag_history[0].in_preprocessor)
            {
              if(io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\\') // make sure its not multi-line
              {
                flag_history[0].in_preprocessor=false;
                io.out();
              }
              else if(io.last_written[(LAST_WRITTEN_LENGTH-1)]!=' ' && io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\t' &&
                      !leave_newline)
                io.out(' '); // need atleast one space inbetween preprocessor items
              else
                io.out();
            }
            else if(io.buf[1]=='#')
            {
              if(io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\0' && (io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\r' &&
                 io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\n'))
                io.out();
            }
            else
              io.out(' ');
          }
          else if(comments_only && !flag_history[0].in_line_comment)
            io.out();
          else if(leave_newline && !flag_history[0].in_line_comment)
            io.out();
          else if(flag_history[0].in_hex) // hex values need a space after them, so put a space in place of the crlf
          {
            io.out(' ');
            flag_history[0].in_hex=false; // not in the hex value anymore
          }
          else if(flag_history[0].in_single_quote || flag_history[0].in_double_quote)
            io.out();

          if(flag_history[0].in_line_comment && io.last_read[0]!='\\')
          {
            if(comments_only && (io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\r' ||
                                 io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\n')) {
              io.out();
              io.commentOut(); //dmg print end of line also
            }
            flag_history[0].in_line_comment=false;
          }

          if(!flag_history[0].in_line_comment)
          {
            flag_history[0].in_hex=false; // we're not in a hex value anymore
            flag_history[0].num_backslashes=0; // Null out number of backslashes
          }
        } else { // we are in a start ca
          io.commentOut();
#ifdef DUMPSPACES
          io.out();
#endif
        }
      break;

      case ' ':
      case '\t':
        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          if(flag_history[0].in_single_quote || 
             flag_history[0].in_double_quote || 
             comments_only) { // the only cases where we always output all of them
            io.out(); 
          }
          else if( (flag_history[0].in_preprocessor || flag_history[0].in_hex) &&
                   ((io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\n' && io.last_written[(LAST_WRITTEN_LENGTH-1)]!='\r') || 
                   (keep_preprocessor_whitespace && flag_history[0].in_preprocessor)))
          {
            /* preprocessors require atleast a single whitespace char preserved both in front and behind non-whitespace 
               characters. Hex values require a space afterwards. */
            if((!is_whitespace(io.last_written[(LAST_WRITTEN_LENGTH-1)]) && !is_whitespace(io.buf[1]) && 
                !(io.last_written[(LAST_WRITTEN_LENGTH-1)]=='#' && contain_preprocessor(io.buf)))
               || keep_preprocessor_whitespace) {
              io.out(); 
            } else {
              io.commentOut();
            }

            if(flag_history[0].in_hex) // get out of hex if we are in one
              flag_history[0].in_hex=false;
          }
          else if(((!is_whitespace(io.last_written[(LAST_WRITTEN_LENGTH-1)]) && (is_letter(io.last_written[(LAST_WRITTEN_LENGTH-1)])
                  || io.last_written[(LAST_WRITTEN_LENGTH-1)]=='_')) || is_number(io.last_written[(LAST_WRITTEN_LENGTH-1)]))
                  &&
                  (!is_whitespace(io.buf[1]) && ((is_letter(io.buf[1])
                                                 || io.buf[1]=='_') || is_number(io.buf[1]))) ) 
            io.out();  
          else if(!strncmp((io.buf+1),"...",3) || !strncmp((io.last_written+(LAST_WRITTEN_LENGTH-3)),"...",3))
            io.out(); // need space before (and after) these if already there
          else if(io.last_written[(LAST_WRITTEN_LENGTH-1)]=='/' && io.buf[1]=='*')
            io.out(); // preserve whitespace so that if the file is mangled again it isn't construed as the start of a comment
          else {
            io.commentOut();
          }
        } else {
          io.commentOut();
        }
      break;

      default:
        if(!flag_history[0].in_line_comment && !flag_history[0].in_star_comment)
        {
          // increase num_backslashes if this is a backslash, else set to 0
          flag_history[0].num_backslashes=(io.buf[0]=='\\' ? (flag_history[0].num_backslashes+1) : 0);

          // colons pre-separated by whitespace or a cr/lf still need separation (c++ specific)
          if(io.last_written[(LAST_WRITTEN_LENGTH-1)]==':' &&
             (is_whitespace(io.last_read[0]) || io.last_read[0]=='\r' || io.last_read[0]=='\n')
             && io.buf[0]==':')
            io.out(' ');

          if(io.buf[0]=='\\' && (io.buf[1]=='\n' || io.buf[1]=='\r') && !comments_only && !leave_newline)
          {
            flag_history[x].num_backslashes=0;
            io.in(); // skip over newline
          }
          else
          {
            // check to see if we are getting into a hex value as it needs a space after it
            if((!flag_history[0].in_single_quote && !flag_history[0].in_double_quote) &&
                io.buf[0]=='0' && (io.buf[1]=='x' || io.buf[1]=='X') && (is_letter(io.buf[2]) || is_number(io.buf[2])))
            flag_history[0].in_hex=true;

            io.out();
          }

          if(flag_history[0].in_hex && (!is_letter(io.buf[0]) && !is_number(io.buf[0])))
            flag_history[0].in_hex=false;
        } else {
          io.commentOut();
        }
      break;
    }

    // Remember the flags of previous iterations so we may reference them
    for(x=(FLAG_HISTORY_MAX-1); x; x--)
      flag_history[x]=flag_history[(x-1)];
  }

  return true;
}

void dformat::version()
{
  cerr << NAME << " v" << VERSION
#ifdef BETA
       << "b"
#endif
       << " by Jon Newman, and adapted by Daniel M. German, based on Mangle" << endl;
};