This siteAll blogsLe 17ClémentCCBCuisineLe 3ContactSe connecterS'inscrire
  • All blogs
  • Le 17
  • Clément
  • CCB
  • Cuisine
  • Le 3
  • Contact

  • S'inscrire

Recettes informatiques

  • Page de garde
  • Contact
  • Se connecter
  • « NetworkManager, Wifi et nmblookup
  • Fichier de ressource Bash »

Extraire des commentaires d'un fichier C/C++

Posted by mazet on 17 Nov 2009 in Programmation, Perl
Extraction de commentaires d'un fichier C/C++ (toute la puissance du perl)

Code

#!/usr/bin/perl
 
use strict;
 
# default value
my $format = "%";
my $pattern = "";
 
# help message
sub usage() {
 
  print <<EOF;
usage: getcomments [-f string] [-h] [-p regex] file...
-f|--format string: format string for output printing [%]
-h|--help: help message
-p|--pattern regex: pattern matching on block []
 
Extract C/C++ block of comments
 
Example: getcomments.pl -p='test:\s' -f='./%' random.c
EOF
 
  exit 1;
}
 
usage() if ($#ARGV < 0);
 
# process argument
foreach my $arg (@ARGV) {
  use vars qw/$caif $caip $naif $naip/;
 
  # analyse format argument
  ($caif, $_) = ($arg =~ /^(-f|--format)=(.*)/);
  ($caif, $_) = (1, $arg) if ($naif);
  next if ($naif = ($arg =~ /^(-f|--format)$/));
  if ($caif) { $format = $_; next }
 
  # check for help message
  usage() if ($arg =~ /^(-h|--help)$/);
 
  # analyse pattern argument
  ($caip, $_) = ($arg =~ /^(-p|--pattern)=(.*)/);
  ($caip, $_) = (1, $arg) if ($naip);
  next if ($naip = ($arg =~ /^(-p|--pattern)$/));
  if ($caip) { $pattern = $_; next }
 
  # no more argument, only file
  my $filename = $arg;
 
  # open file
  if (!open (IN, "<", $filename)) {
    print "Can not open $filename\n";
  }
 
  # init table of comments
  my @comments;
  $#comments = -1;
 
  # read all the file
  while ($_ .= <IN>) {
    my $cmt;
 
    # process c++ comments
    ($cmt, $_) = m{//\s*(.*?)\s*$()} if (m{//} && !m{/\*.*//});
 
    # process standard c comments
    ($cmt, $_) = m{^.*?/\*\s*(.*?)\s*\*/(.*)}s if (m{/\*.*\*/}s);
 
    push(@comments, $cmt) if ($cmt);
 
    # empty buffer if no comment is present
    undef($_) if (!m{/[/*]});
  }
 
  # close file
  close (IN);
 
  # display comment blocks
  foreach my $block (@comments) {
    if (($block) = ($block =~ /$pattern(.*)/s)) {
      ($_ = $format) =~ s/%/$block/gs;
      print "$_\n";
    }
  }
}
This entry was posted by mazet and filed under Programmation, Perl.

Aucun commentaire pour le moment

Catégories

  • Toutes
  • Non catégorisé
  • Programmation
    • Awk
    • Bash
    • C
    • C++
    • Javascript
    • LaTeX
    • Perl
    • Tcl/Tk
  • Systeme
    • Android
    • Debian
    • Ubuntu

Contenu

  • Linux Router
  • Creating a network between 2 virtual machines on Windows with Qemu
  • Build a Debian package for pdmenu
  • Extract informations from epub file
  • Connexion PostgreSql / Java par Socket Unix
  • Example of fork and respawn
  • Start SSH daemon on Git portable distribution
  • Create a git mirror
  • Color diff
  • Create on Debian a Minecraft server
  • GIT/HG/SVN on multiple repos simultaneously
  • Convert Comics into CBZ format
  • Random password generator function
  • Convert to camelCase
  • DKIM and SPF
  • Migration from BackupPC 3 (Debian package) to BackupPC 4 (Standalone Installation)
  • Minimal BC for Mingw
  • Diskless Debian cluster
  • Colorize log file
  • Generate certificates with (or without) a certificate authority
Septembre 2025
Lun Mar Mer Jeu Ven Sam Dim
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          
 << <   > >>
  • Accueil
  • Récemment
  • Archives
  • Catégories
  • Derniers commentaires

Rechercher

Flux XML

  • RSS 2.0: Posts
  • Atom: Posts
What is RSS?

©2025 by Laurent Mazet • Contact • Aide • Photo albums software

Blog software

Cookies are required to enable core site functionality.