File: ohcount.rb

package info (click to toggle)
ohcount 3.0.0-6.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,828 kB
  • sloc: ansic: 6,534; ruby: 2,560; perl: 2,041; erlang: 350; lisp: 272; sh: 244; pascal: 196; vhdl: 150; haskell: 149; asm: 127; cs: 124; awk: 98; java: 92; php: 73; tcl: 58; xml: 57; fortran: 54; makefile: 32; python: 31; ada: 30; objc: 30; jsp: 28; sql: 18; cobol: 13; ml: 9; cpp: 3
file content (107 lines) | stat: -rw-r--r-- 2,261 bytes parent folder | download | duplicates (4)
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
# ohcount.rb written by Mitchell Foral. mitchell<att>caladbolg.net.
# See COPYING for license information.
# Ohcount module tweaked for use by Ohloh.

$: << File.expand_path(File.dirname(__FILE__))

begin
	require 'ohcount.so'
rescue LoadError
	require 'rbconfig'
	include Config
	require "#{Config::CONFIG['arch']}/ohcount.so"
end

module Ohcount
  class SourceFile
    def file_location=(value) set_diskpath(value) end
    def file_location() diskpath() end
    def filenames=(value) set_filenames(value) end
    def contents() get_contents() end
    def polyglot() get_language() end

    def language_breakdowns
      list = get_parsed_language_list()
      return array_from_list(list, :pl)
    end

    def language_breakdown(language)
      return language_breakdowns().find { |lb| lb.name == language.to_s }
    end

    def licenses
      list = get_license_list()
      array = array_from_list(list, :lic)
      return array.map! { |l| l.name }
    end

    def languages
      return language_breakdowns().collect { |lb| lb.name }
    end

    def loc_list
      list = get_loc_list()
      return array_from_list(list, :loc)
    end

    def diff(to)
      list = _diff(to)
      ret = array_from_list(list, :delta)
      class << ret
        def loc_deltas() self end
      end
      return ret
    end

    def each
      filenames.each { |f| yield f }
    end

    private

    def array_from_list(list, method)
      array = Array.new
      iter = list.head
      while (iter)
        array << iter.send(method)
        iter = iter.next
      end
      return array
    end
  end

  class SourceFileList
    def each_source_file
      iter = self.head
      while (iter)
        yield iter.sf if iter.sf.polyglot
        iter = iter.next
      end
    end

		# this should yield each filename, not an sf object
		def each
      iter = self.head
      while (iter)
        yield iter.sf.filename if iter.sf
        iter = iter.next
      end
		end

    def size
      count = 0
      iter = self.head
      while (iter)
        count += 1
        iter = iter.next
      end
      return count
    end
  end

  class Detector
    def self.binary_filename?(filename)
      return Ohcount.ohcount_is_binary_filename(filename) == 1
    end
  end
end