Originally Posted by
matsp
Your first problem is finding the files in a certain directory. This is covered in the FAQ.
The next problem is to find the extension (.C and .H) in the files in the directory, and copy those to different directories based on the extension. You'll find strrchr() a useful function here.
Copying files can be done either by reading the source file and writing the destination file, or if you are using Windows, you could use the CopyFile() function to do it for you [with the added advantage that any "hidden" information that Windows keeps about will still be copied - unlikely in this particular case, but there are files that have "extended attributes", which will not work right if they are not copied using CopyFile()].
As for replacing the name inside #include, you have to search for #incldue [and remember that it's valid for these to be indented - the only rule is that the first non-white-space should be #, and there can be whitespace (but not newline) after # too]. You'd have to clarify if commented out lines should also be converted - if not, you have to keep track of comments and skip over until the end of the comment. After you've found the #include line, you can quite easily find the quote chars (is this ONLY for double quotes, or also angle-bracket quoted includes? - again clarify with whoever set you the task, or the technical lead or such). Here, you definitely need to copy the file to a new one. I would start by creating a temporary file, and then renaming the original file when the translation is finished, and finally renaming the new file to the original name. [You would want to keep the original file - just in case your program makes a mistake on something, so that someone can compare and fix up the code].
This is not entirely trivial, but with some experience in file handling, it's not too difficult.
--
Mats
hai Mats
i got same code in Perl..i got it from other source..can you do one favour for me..i know u have well knowledge in PERL to please can u convert following code into C programming..i don't know perl i know only basics
Code:
use File::Spec::Functions qw/rel2abs splitpath catfile/;
use strict;
sub mmm_good {
my $d = rel2abs($_[0]);
mkdir $d unless -e $d;
die unless -d $d;
return $d;
}
my(%dirs);
$dirs{c} = mmm_good('source');
$dirs{h} = mmm_good('header');
for my $path (map((glob $_), @ARGV) {
next unless $path =~ /.([chCH])$/ && -r $path;
my $ext = lc $1;
open my $in, "<", $path;
open my $out, ">", catfile($dirs{$ext}, (splitpath($path))[2]);
while(my $line = <$in>) {
$line = lc $line if /^\w*#include/;
print $out $line;
}
close $out;
close $in;
}
I am learning slowly PERL..thanks for help..