File: videolist.rb.old

package info (click to toggle)
libxml-parser-ruby 0.6.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 676 kB
  • ctags: 800
  • sloc: ruby: 5,723; ansic: 1,734; xml: 574; makefile: 152
file content (144 lines) | stat: -rwxr-xr-x 2,952 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/local/bin/ruby

## Video List Processer
## 1998 by yoshidam
##

require 'parsearg'
require 'xmlparser'
require 'uconv'
include Uconv
require 'kconv'
include Kconv

def volumelist(file = nil, date = nil)

  ## file open
  if !file
    f = $stdin
  else
    begin
      f = open(file, "r")
    rescue
      $stderr.print "#{$0}: #{$!}\n";
      return
    end
  end


  ## empty file
  if ((xml = f.gets).nil?); return; end

  ## rewrite encoding in XML decl.
  if xml =~ /^<\?xml\sversion=.+\sencoding=.EUC-JP./i
    xml.sub!(/EUC-JP/i, "UTF-8")
    encoding = 'EUC-JP'
  elsif xml =~ /^<\?xml\sversion=.+\sencoding=.Shift_JIS./i
    xml.sub!(/Shift_JIS/i, "UTF-8")
    encoding = "Shift_JIS"
  elsif xml =~ /^<\?xml\sversion=.+\sencoding=.ISO-2022-JP./i
    xml.sub!(/ISO-2022-JP/i, "UTF-8")
    encoding = "ISO-2022-JP"
  end

  ## read body
  xml += String(f.read)
  f.close

  ## convert body encoding
  if encoding == "EUC-JP"
    xml = euctou8(xml)
  elsif encoding == "Shift_JIS"
    xml = euctou8(kconv(xml, EUC, SJIS))
  elsif encoding == "ISO-2022-JP"
    xml = euctou8(kconv(xml, EUC, JIS))
  end

  ## dummy default handler
  parser = XMLParser.new
  def parser.default
  end

  ## start to parse
  volume = 0
  element = ''
  pdate = ''
  pname = ''
  ptitle = ''
  visible = if date.nil?; true else false; end
  vlist = []
  plines = 0
  begin
    parser.parse(xml) do |type, name, data|
      case type
      when XMLParser::START_ELEM
        element = name
        case name
        when 'volume'
          volume += 1
          vlist = []
        when 'program'
          pdate = ''
          pname = ''
          ptitle = ''
        when 'date'
        else
        end
      when XMLParser::END_ELEM
        case name
        when 'volume'
          if visible
            if plines + vlist.length + 2 > 66
              print "\f"
              plines = 0
            end
            plines += vlist.length + 2
            vlist.each do |volume|
              print format("%-8s%-32s%-32s\n", volume[0], volume[1], volume[2])
            end
            print "\n\n"
          end
        when 'program'
          if !visible && !date.nil? && pdate =~ /^\d{6}$/ &&
              pdate >= date
            visible = true
          end
          vlist.push([pdate, pname, ptitle])
        end
      when XMLParser::CDATA
        next if data =~ /^\s*$/;
        data = u8toeuc(data)
        case element
        when 'date'
          pdate << data
        when 'name'
          pname << data
        when 'title'
          ptitle << data
        end
      when XMLParser::PI
      else
      end
    end
  rescue XMLParserError
    line = parser.line
    print "#{$0}: #{$!} (#{file}:#{line})\n"
  end
end

def usage
  $stderr.print "Usage: #{$0} [-d <date>] <file>\n"
  exit 1
end

parseArgs(0, nil, nil, "d:")
if (ARGV.length == 0)
  usage
end

volumelist(ARGV[0], $OPT_d)

__END__
## Local variables:
## mode: ruby
## End: