Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Need help converting unix text to windows text

Reply
Thread Tools

Need help converting unix text to windows text

 
 
sking623@aol.com
Guest
Posts: n/a
 
      03-22-2005
I have been working on this for weeks. Whatever my FTP client does to
translate the unix text file with each line ending in a LF character
back into a LF CR I need some code in Perl to this.

The task is, save data from an html form into a flatfile database. I
have a Linux server. The program I have allows the user to search for a
text file (unix text) and download the file through their browser. It
has to convert the little LF's to CRLF. I have tried using some
people's suggestion on how to do this. But it doesn't work yet. Right
now it serves up the file because I have everything commented out! A
little messy, but the solution might be hiding in there! I just need to
know the right way to tweak it...

Any assistance is greatly appreciated.

Here's my code:

#!/usr/bin/perl
# This file is the one working
# This is called form the search page from option 1 in menu. You have
to login and search for a record.
# use a 1 to search for 1001.txt
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

my $file_location;
my $ID;
my @fileholder;

$subdirectory=param('subdirectory');
$ID = param('id');
$file_location = "../" . $subdirectory. "/" . $ID . "du.txt";
if ($ID eq '') {
print "Content-type: text/html\n\n";
print "You must specify a file to download.";
} else {
#open(FILE, "$file_location") || Error('open', 'file');
#binmode (FILE); # will read file as is (binary) it need to send
images
#@data=<FILE>;
#close FILE;
#print "Content-type: text/html\n\n";
#print $file_location;
#print @data;
#exit;
open(DLFILE, "<$file_location") || Error('open', 'file');
binmode -f(DLFILE);
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');

#foreach $fileholder (@fileholder) {
#$fileholder =~ s|\012|\m\n|g;
#$fileholder =~ s|\012|$/|g;
#$fileholder = split(/\012/,$fileholder);
#}
#print "Content-type: text/html\n\n";
#foreach $fileholder (@fileholder) {
#print $fileholder;
#}
#exit;
open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
Error('open', 'file');
print LOG "$ID\n";
close (LOG);

#print "Content-type:application/x-download\n"; #paste your type
#binmode STDOUT; # i not shure than need it
#print join(//, @data); # send client

print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}

sub Error {
print "Content-type: text/html\n\n";
print "The server can't $_[0] the $_[1]: $! \n";
exit;
}

 
Reply With Quote
 
 
 
 
Reinhard Pagitsch
Guest
Posts: n/a
 
      03-22-2005
Hello,

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have been working on this for weeks. Whatever my FTP client does to
> translate the unix text file with each line ending in a LF character
> back into a LF CR I need some code in Perl to this.
>

A simple substitute of CR (Unix) to CRLF (DOS) shall do this.
Look at: http://www.cpan.org/authors/id/G/GO/...ool-0.2pre1.pl

(No, the script is not written by me)

regards,
Reinhard
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      03-22-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

> Whatever my FTP client does to
> translate the unix text file with each line ending in a LF character
> back into a LF CR



No, you need it back into CR-LF, the order matters.


> I need some code in Perl to this.



s/\012/\015\012/;


> I have tried using some
> people's suggestion on how to do this. But it doesn't work yet.



Then you should followup to the article containing the non-working
suggestion so that we could help you fix it.


> #!/usr/bin/perl



You should ask for all the help you can get!

Especially before asking hundreds of people around the world to
look at it for you.

use warnings;
use strict;


> use CGI ':standard';



> $subdirectory=param('subdirectory');
> $ID = param('id');



Who will be filling out your web form?

Any-old-body on the Internet?

If so, you better have taint checking in your program before you
put it on a publicly available web server:

perldoc perlsec


> $file_location = "../" . $subdirectory. "/" . $ID . "du.txt";



You should declare your variables in the smallest possible scope.

Variable interpolation _is_ concatenation, only easier to read:

my $file_location = "../$subdirectory/${ID}du.txt";


> print "Content-type: text/html\n\n";



Here you tell the browser that you will be giving it HTML.


> print "You must specify a file to download.";



That doesn't look like HTML.

You shouldn't lie to the browser like that.


> open(DLFILE, "<$file_location") || Error('open', 'file');
> binmode -f(DLFILE);



It can't be *both* text and binary.

You must treat it as one or the other.

Which is it?



That isn't how you use binmode() anyway, have you read the docs for it?

perldoc -f binmode


> @fileholder = <DLFILE>;


> #foreach $fileholder (@fileholder) {



Why do you need a "file holder" if you are going to process it
a line-at-a-time anyway?


> #$fileholder =~ s|\012|\m\n|g;



What character does the \m escape give you?

Why the "g" option when you only have a single line anyway?

Where did you get this "suggestion" from?


> #$fileholder =~ s|\012|$/|g;



If you are on a unixlike system, then that is a no-op.

What were you hoping that it would accomplish for you?


> open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
> Error('open', 'file');
> print LOG "$ID\n";
> close (LOG);



You had better implement file-locking in your CGI program if
you don't want the log file to become corrupted.


> #print join(//, @data); # send client



That is a mighty strange looking 1st argument to join() ...


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
setup.py on unix/linux and converting to windows AIM Python 0 04-14-2006 01:58 AM
Converting Java batch file from windows to unix format arun.hallan@gmail.com Java 7 04-12-2005 03:47 PM
compile C programs with UNIX system calls (= Unix Programs??) jrefactors@hotmail.com C++ 12 01-10-2005 03:35 AM
Re: converting c++ programme from windows to solaris unix Peter Koch Larsen C++ 2 08-06-2003 08:38 AM
Re: converting c++ programme from windows to solaris unix Karl Heinz Buchegger C++ 1 08-06-2003 08:21 AM



Advertisments