nice new tool to show version of feisty meow
[feisty_meow.git] / scripts / text / add_cr.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : add CR                                                            #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Turns Unix format text files into DOS format text files.                 #
12 #                                                                             #
13 ###############################################################################
14 # This program is free software; you can redistribute it and/or modify it     #
15 # under the terms of the GNU General Public License as published by the Free  #
16 # Software Foundation; either version 2 of the License, or (at your option)   #
17 # any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a  #
18 # version of the License.  Please send any updates to "fred@gruntose.com".    #
19 ###############################################################################
20
21 require "filename_helper.pl";
22
23 use Env qw(TMP);
24
25 $new_version = `mktemp "$TMP/zz_add_cr_tmp.XXXXXX"`;
26 chop($new_version);
27
28 foreach $filename (&glob_list(@ARGV)) {
29   # go through each file on the command line.
30
31   open(IN_FILE, "<$filename");
32
33   # open our temporary file for appending.
34   open(NEW_FILE, ">$new_version");
35   binmode(NEW_FILE);
36
37   local($changed_file) = 0;  # records whether the file was modified.
38
39   # go through each line in the current file...
40   while ($to_add_cr = <IN_FILE>) {
41     $new_line = "";
42     for ($i = 0; $i < length($to_add_cr); $i++) {
43       $curr_char = substr($to_add_cr, $i, 1);
44       if ($curr_char =~ /[\r\n]/) {
45         if ($curr_char =~ /\n/) {
46           # if LF comes first, we are fixing a unix style file.
47           $changed_file = 1;
48         }
49         last;
50       } else {
51         $new_line .= $curr_char;
52       }
53     }
54
55     # add on dos EOL, a carriage return and a line feed.
56     $new_line .= "\r\n";
57
58     print NEW_FILE "$new_line";  # write out the current line.
59   }
60
61   close(NEW_FILE); 
62   close(IN_FILE); 
63
64   if ($changed_file) {
65 print "filename is $filename\n";
66     open(NEW, "<$new_version");
67     open(CURR, ">$filename");
68     while (<NEW>) { print CURR; }
69     close(NEW);
70     close(CURR);
71   }
72
73   # clean up our temporaries.
74   unlink("$new_version");
75 }
76