Solved

Automatically Add Comment Header to Source Files

Posted on 2009-04-03
17
619 Views
Last Modified: 2013-12-17
Hello Experts,  I need your help !!
I have a bunch of source files (about 80 files) within several folders without a preamble (heading/copyright) header. Is there a way that I can run a macro or program that will add the title for each source file. The header would have the name of that particular file (i.e header.c)
I would like to see a template that I can modify according to the module name. So every time I need to update the header, I would go in the template and change it accordingly.
There may be a case the source files may have a blank header which will need to be replace with the one in the template so they can all be uniformed.  Please help I hate to copy/paste all this manually. The source files are mainly written in c code.








For instance, I would like to see the following in each file.

/*****************************************************************************
*  test.cpp
*
*  Author:  xxx xxx                     xx/xx/2007
*
*  Description:
*
*  Dependencies:
*
*  Modifications:
*   Date      Programmer      Description
*   --------  --------------  -----------------------------------------------
*
*****************************************************************************/
0
Comment
Question by:nedbacan
  • 8
  • 7
17 Comments
 
LVL 3

Expert Comment

by:ddanonimity
Comment Utility
I would create a piece of code in C# using windows forms. have text boxes for the Values you want entered into  the source file. Then you could either enter the name of the source file in 1 text box or use it to add to the header you want to make or add a file browse buton to select it. You could then open the source file as an IO stream. copy that entire stream to a string then append all the header details you wnat to that string and save it back to the original source file. You would need the using System.IO; declartivite at the top.

string ls_header = "********************************************\r\n";
ls_header += textbox1.Text + \r\n;
ls_header += "\r\nAuthor:  " + textbox2.text + "\r\n;
<etc>
l_header = "****************************************************\r\n";

StreamReader lo_reader = new StreamReader();
lo_reader.Open(<filename>);

string ls_file = "";

while(!lo_reader.EndOfStream)
{
     ls_file += "\r\n" + lo_reader.ReadLine;
}

ls_File = ls_header + ls_file;
lo_reader.close();

StreamWriter lo_Writer = new streamWriter()
lo_writer.open(<filename>);
lo_writer.writeline(ls_File)
lo_writer.close();


There may be a few bugs in this but nothing too hard to debug. Tis just the general gist if how to do it
0
 

Author Comment

by:nedbacan
Comment Utility
Hi
I appreciate your help but is it possible for the program to add the filename automatically, and the rest from a template that the program reads from.  Trying to avoid data entry.


0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
If you repost this to the Perl / Python / Ruby zones you'll most likely get a solution fast.

The one thing I would discourage is repeatedly updating files based on a template after the fact. If you are using source code library / version control, then mass updates falsely clutter the change sets.
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Never mind, it took me 5 minutes, here is a starter, you need Perl installed. I advise testing this in a director with copies of a few files while you tweak it. Also, you might want to add more "template" variables to the header, i just put one in #filename#. It will just start in current dircectory and work down looking for file patters you specify (.c|.cc|.cpp), you can add .h there as well

This script just checks for /******* at the beinning of the file, and wont inser the header if it sees that.
If you want a script to completely reparse and rewrite the header, I would recommend changing the foramt just a little to make it easier to parse, by adding some sort of other tag so that you don't parse other pre-existing comments that start the same way. Something like:

/********************
* @Header
*
*
*
*********************/


#!/usr/bin/perl
 

use strict;

use File::Find;
 

my $header =<<EOF;

/*****************************************************************************

*  #filename#

*

*  Author:  xxx xxx                     xx/xx/2007

*

*  Description:

*

*  Dependencies:

*

*  Modifications:

*   Date      Programmer      Description

*   --------  --------------  -----------------------------------------------

*

*****************************************************************************/

EOF
 

find( \&putHeader, '.' );
 

sub putHeader() {
 

   return unless(/\.(c|cc|cpp)$/);
 

   local $/;

   my $file = $_;

   open(IN, "<$file") or die;

   my $str = <IN>;

   close(IN);

   if($str =~ /^\/\*+/) {

      print "Found header in $file, skipping\n";

   }

   else {

      print "Replacing header in $file\n";

      open(OUT, ">$file") or die "Opening output file";
 

      # Substitute template variables in #foo# format

      my $newheader = $header;

      $newheader =~ s/#filename#/$file/g;
 

      print "HEADER: $newheader";

      print OUT $newheader;

      print OUT $str;

   }

}

Open in new window

0
 

Author Comment

by:nedbacan
Comment Utility
I am sorry but I am not familiar with PERL, can you direct me where I can download a copy of PERL.
Or can this be worked into C++.  I am not a developer just know enough to get by.

Thank you
 

0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Do you run Windows or UNIX? You can install Perl for Windows from:  http://www.activestate.com/activeperl/

After installation, any file with .pl extension is executable just like a bat file or other exe.

The script I provided expects to be run from the directory where your files are (or the top level directory, as it will recursively downward). Don't run it from C:\ or it will recursively traverse your whole C drive for .c, .cc and .cpp files.

Sorry, I don't have a C++ solution. C++ is not quickly productive for operations like this.

0
 

Author Comment

by:nedbacan
Comment Utility
Hi ..I downloaded the perl for windows, I loaded the script on the IDE but how do I get it to run on the folder where all the c files are stored
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Either harccode the folder path into the script by changing the line:

find( \&putHeader, '.' );

to

find( \&putHeader, 'C:\\project\\foo' );  # Change to your project path


2) or copy the script into that directory and run it from there.




0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:nedbacan
Comment Utility
Excellent the program works ..if I was to bump up the points with one more request. Not only I needed the header but is it possible to add comment header for each function in the code ... for instance the folllowing sample ..in the comment I would place the name of the function.

//! A function variable.
    /*!
      Details.
    */
    int (*handler)(int a,int b);
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 250 total points
Comment Utility
Sure, you can do it, but it requires parsing the file and looking at the function signatures. The reason the "$file" variable is available so easily is that we know the filename when we open it. The function name would have to be parsed from the file itself, and you need to look for all possible C signatures:

(void|long|int|float|char|...) *  (foo)

Thats a bit tougher to do with just a simple Perl script, as it actually means parsing using the C grammar for a function, while ignoring variable declarations, ifdefs, other comments, etc.


0
 

Author Comment

by:nedbacan
Comment Utility
understand ...so the task is tougher than I though ....are you willing to give it a try.
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
Well you are now changing the question. :) I provided a solution to the original problem because it was easy.

If you want me to write a script to insert a comment header for each function, that is a different problem, which I might tackle offline from this thread if you email directly, or you could ask another question with that request in its title to get other's attention.

There may also be a tool out there, doxygen comes to mind, but I don't think it actually inserts comments INTO code, it pulls FROM the code. Feel free to contact me offline if security is an issue with your sourcecode. It might be as simple as some research to find a tool.

0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
I see you opened the new question, my recommendations are to add "Parsers" to the zones and to up the points to get more attention. I don't have an answer to it, but I will research and post on the new question if I do find something.
0
 

Author Comment

by:nedbacan
Comment Utility
I appreciate your help. I went into edit question and I don't see where to change the zone. It just allow me to change the description snd the summary.
0
 

Author Closing Comment

by:nedbacan
Comment Utility
Very responsive to my questions.
0
 
LVL 40

Expert Comment

by:mrjoltcola
Comment Utility
You can ask for admin assistance with the new question by clicking "Request attention" at the top.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This is about my first experience with programming Arduino.
If your app took Google’s lash recently, here are the 5 most likely reasons.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now