Solved

Automatically Add Comment Header to Source Files

Posted on 2009-04-03
17
647 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
ID: 24066700
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
ID: 24067166
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
ID: 24068048
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24068121
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
ID: 24078894
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
ID: 24079056
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
ID: 24081495
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
ID: 24082331
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
 

Author Comment

by:nedbacan
ID: 24083403
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
ID: 24083466
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
ID: 24083492
understand ...so the task is tougher than I though ....are you willing to give it a try.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24083646
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
ID: 24083745
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
ID: 24085917
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
ID: 31566465
Very responsive to my questions.
0
 
LVL 40

Expert Comment

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

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
best imaging disk tool 15 55
programming a polycom voip phone 3 52
PDF edit software 2 35
deprecated mysql extensions 1 22
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

679 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