Link to home
Start Free TrialLog in
Avatar of nedbacan
nedbacanFlag for United States of America

asked on

Automatically Add Comment Header to Source Files

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
*   --------  --------------  -----------------------------------------------
*
*****************************************************************************/
Avatar of ddanonimity
ddanonimity

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
Avatar of nedbacan

ASKER

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.


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.
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

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
 

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.

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
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.




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);
ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
understand ...so the task is tougher than I though ....are you willing to give it a try.
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.

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.
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.
Very responsive to my questions.
You can ask for admin assistance with the new question by clicking "Request attention" at the top.