Created
April 3, 2010 21:20
-
-
Save sms420/354861 to your computer and use it in GitHub Desktop.
lisr.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby1.9 -w | |
#list.rb | |
#Sean Stephenson | |
#2009-09-24 | |
#CS 177 | |
#takes as input a file and options to display | |
#specific lines and [correspding line number] | |
### usage ### | |
def usage | |
abort "usage: [-n] [ [0-9],[-],[$] file_name" | |
end | |
### getMaxLine ### | |
def getMaxLine | |
file = File.open($file) | |
fileArray = Array.new | |
i = 0 | |
while(file.eof == false) | |
fileArray[i] = file.readline | |
i += 1 | |
end | |
return fileArray | |
end | |
### calcMaxLine ### | |
def calcMaxLine(range) | |
lastLine = 0 | |
val = range.split "," | |
val.each do |i| | |
case | |
when i =~ /([0-9])(\-)([0-9])/ #(1) (num) dash (num) | |
value = i.split "-" | |
lastLine = value[0].to_i if value[0].to_i > lastLine | |
lastLine = value[1].to_i if value[1].to_i > lastLine | |
when i =~ /([0-9])(\-)/ #(2) (num) dash | |
return getMaxLine | |
when i =~ /(\-)([0-9])/ #(3) dash (num) | |
value = i.split "-" | |
lastLine = value[1].to_i if value[1].to_i > lastLine | |
when i =~ /([0-9])/ #(4) number | |
lastLine = i.to_i if i.to_i > lastLine | |
else #(5) dash | |
return getMaxLine | |
end | |
end | |
file = File.open($file) | |
fileArray = Array.new | |
i = 0 | |
while(i < lastLine) | |
if file.eof == true | |
abort "there are only " + (i + 1).to_s + " lines in the file." | |
end | |
fileArray[i] = file.readline | |
i += 1 | |
end | |
return fileArray | |
end | |
### parseNum ### | |
def parseNum(range) | |
abort usage if range =~ /[^\d,^\$,^\-,^\,]/ | |
if range =~ /\$/ | |
fileLines = getMaxLine | |
maxLine = fileLines.size | |
else | |
fileLines = calcMaxLine(range) | |
maxLine = fileLines.size + 1 | |
end | |
val = range.split "," | |
val.each do |i| | |
case | |
when i =~ /([0-9]|\$)(\-)([0-9]|\$)/ ### 1. (num|$) dash (num|$) | |
value = i.split "-" | |
if value[0] =~ /\$/ | |
line_a = maxLine | |
else | |
line_a = value[0].to_i | |
end | |
if value[1] =~ /\$/ | |
line_b = maxLine | |
else | |
line_b = value[1].to_i | |
end | |
line_a -= 1 | |
if line_a < line_b | |
while (line_a < line_b) | |
if $printLineNums == false | |
puts fileLines[line_a].to_s | |
else | |
line_no = line_a + 1 | |
print line_no.to_s + ": " ; print fileLines[line_a] | |
end | |
line_a += 1 | |
end | |
else | |
while (line_b-1 <= line_a) | |
if $printLineNums == false | |
puts fileLines[line_a].to_s | |
else | |
line_no = line_a + 1 | |
print line_no.to_s + ": " ; print fileLines[line_a] | |
end | |
line_a -= 1 | |
end | |
end | |
when i =~ /([0-9]|\$)(\-)/ ### 2. (num|$) dash | |
value = i.split "-" | |
line_a = value[0].to_i - 1 | |
while (line_a < maxLine) | |
if $printLineNums == false | |
puts fileLines[line_a] | |
else | |
line_no = line_a + 1 | |
print line_no.to_s + ": " ; print fileLines[line_a] | |
end | |
line_a += 1 | |
end | |
when i=~ /(\-)([0-9]|\$)/ ### 3. dash (num|$) | |
value = i.split "-" | |
if value[1] =~ /\$/ | |
line_a = maxLine - 1 | |
else | |
line_a = value[1].to_i - 1 | |
end | |
counter = 0 | |
if $printLineNums == false | |
while (counter <= line_a) | |
puts fileLines[counter] | |
counter += 1 | |
end | |
else | |
while (counter <= line_a) | |
line_no = counter + 1 | |
puts line_no.to_s + ": " + fileLines[counter].to_s | |
counter += 1 | |
end | |
end | |
when i =~ /[0-9]/ ; ### 4. number | |
if $printLineNums == false | |
puts fileLines[(i.to_i - 1)] | |
else | |
puts i + ": " + fileLines[(i.to_i - 1)] | |
end | |
else ### 5. dollar or dash | |
if $printLineNums == false | |
puts fileLines | |
else | |
counter = 1 | |
while (counter <= fileLines.size) | |
puts counter.to_s + ": " + fileLines[counter-1] | |
counter += 1 | |
end | |
end | |
end | |
end | |
end | |
#take args and rip it | |
if ARGV.length > 3 | |
abort usage | |
end | |
if ARGV. length == 0 | |
abort usage | |
end | |
# one ARG | |
if ARGV.length == 1 | |
if File.exist?(ARGV[0]) | |
File.open(ARGV[0]) do |f| | |
while line = f.gets | |
puts line | |
end | |
end | |
else | |
abort usage | |
end | |
end | |
# two ARGS | |
if ARGV.length == 2 | |
if ARGV[0] == "-n" | |
if File.exist?(ARGV[1]) | |
File.open(ARGV[1]) do |f| | |
counter = 1 | |
while line = f.gets | |
puts counter.to_s + ": " + line | |
counter += 1 | |
end | |
end | |
else | |
abort usage | |
end | |
else | |
$file = File.open(ARGV[1]) | |
$printLineNums = false | |
parseNum(ARGV[0]) | |
end | |
end | |
# three ARGS | |
if ARGV.length == 3 | |
abort usage if File.exist?(ARGV[2]) == false | |
$file = File.open(ARGV[2]) | |
$printLineNums = true | |
parseNum(ARGV[1]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment