first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / text / strip_cr.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : strip CR                                                          #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    Turns DOS format text files into Unix 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 require "importenv.pl";
23
24 $new_version = `mktemp "$TMP/zz_strip_cr_tmp.XXXXXX"`;
25 chop($new_version);
26
27 foreach $filename (&glob_list(@ARGV)) {
28   # go through each file on the command line.
29
30   open(IN_FILE, "<$filename");
31
32   open(NEW_FILE, ">$new_version");  # open our temporary file for appending.
33   binmode(NEW_FILE);
34
35   local($changed_file) = 0;  # record if we made any changes.
36
37   # go through each line in the current file...
38   while ($to_strip_cr = <IN_FILE>) {
39
40     $new_line = "";
41     for ($i = 0; $i < length($to_strip_cr); $i++) {
42       $curr_char = substr($to_strip_cr, $i, 1);
43       if ($curr_char =~ /[\r\n]/) {
44         if ($curr_char =~ /\r/) {
45           # if CR came first, this is a dos style file.
46           $changed_file = 1;
47         }
48         last;
49       } else { 
50         $new_line .= $curr_char;
51       }
52     }
53
54     # add on unix EOL, just a line feed.
55     $new_line .= "\n";
56
57     print NEW_FILE "$new_line";  # write out the current line.
58   }
59
60   close(NEW_FILE); 
61   close(IN_FILE); 
62
63   if ($changed_file) {
64 print "$filename\n";
65     open(NEW, "<$new_version");
66     open(CURR, ">$filename");
67     while (<NEW>) { print CURR; }
68     close(NEW);
69     close(CURR);
70   }
71
72   # clean up our temporaries.
73   unlink("$new_version");
74 }
75