University of Calgary

pretty printer

Source Program Text Formatter 
source: Katrin Becker, 2002
From the Ellemtel C++ Style Guide: (page 23-24)
Recommendations:
20. Always provide the return type of a function explicitly.

21. When declaring functions, the leading parenthesis and the first argument (if any) are to be written on the same line as the function name. If space permits, other arguments and the closing parenthesis may also be written on the same line as the function name. Otherwise, each additional argument is to be written on a separate line (with the closing parenthesis directly after the last argument).

22. In a function definition, the return type of the function should be written on a separate line directly above the function name.

23. Always write the left parenthesis directly after a function name.

24. Braces ("{ }") which enclose a block are to be placed in the same column, on a separate line directly before and after the block.


You are to write a program that will take two (command-line) arguments:
1. the name of a C++ source program (<name>.cc, <name>.cpp)
2. an output name for the processed version of the program (same suffix assumed if not given)

Your program should process the source file to implement Recommendation #22 from the Style Guide.

Assumptions:
- you may assume the original source compiled without errors or warnings
- you may assume Recommendation #20 has been implemented on all source files given to your program (your program may not fail if this is not the case; it just means you don't have to recognize functions that have no explicit return type)

Specifications:
- your program may not terminate abnormally. (It can give up; claim to have found no functions, etc. but it cannot bomb)
- any messages produced by your program should be written to the monitor.
- the output file must compile and run as the original (i.e. the new program must be semantically identical to the original)
- you do not need to design this as an OO program.
- this 'utility' must not over-write the original source program unless explicitly asked to do so, for example:
Format encrypt.cc encrypt.cc
will cause the original source file to be over-written.

 

- if only one argument is provided, the utility will print an error message
- if no arguments are provided, the utility will print a brief message outlining how it is used. (For a UNIX example, try typing cp with no arguments)


BONUSES:
1. [1 points] Implement Recommendation #23
2. [3 points] Implement Recommendation #21
3. [4 points] Implement Recommendation #24
4. If you're still looking for more then you need to take up a hobby.

Test Suites: (part marks will be allotted for the ability to format part of a suite)
Constructs
Suites:
     I II III IV V VI (bonus)
function return types simple            
   reference            
   pointer            
 ignores prototypes            
 messed up line "orientation"            
 declarations simple            
   struct            
 literal strings              
 comments              
 preprocessor stuff simple            
   ignore macros; #ifndef            
   handles macros; #ifndef            
 classes ignoring insides            
   processing insides            
 template functions              
 template classes              
 inline, virtual, static, const ... functions            
 spaces _::_              
 functions w/o return types            



Updated: August 10, 2005 09:14 AM