Class: IndentCode::Application

Inherits:
Object
  • Object
show all
Includes:
IndentCode
Defined in:
lib/indent_code/application.rb

Overview

Class for application control methods.

Constant Summary

Constants included from IndentCode

VERSION

Instance Method Summary collapse

Methods included from IndentCode

error

Constructor Details

#initializeApplication

Returns a new instance of Application.



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
# File 'lib/indent_code/application.rb', line 9

def initialize
  # parse command line options.
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: indent file [options]"

    opts.on('-i', '--indent size', 'Set indent size (TAB size): --indent 4.') do |size|
      @@options[:tab] = size.to_i;
    end
    opts.on('-c', '--clean', 'Delete code blocks from source code is framed as: #if 0 ... #endif.') do
      @@options[:clean] = true;
    end
	opts.on('-d', '--delete', 'Delete temporary files after work: ...~ and ...~~ files.') do
      @@options[:delete] = true;
    end
	opts.on('-V', '--version', 'Display the program version.') do
      puts "indent, version #{IndentCode::VERSION}"
      exit
    end
    opts.on('-h', '--help', 'Displays Help') do
      puts opts
      exit
    end
  end
  parser.parse!

  # check command line arguments.
  if ARGV.size == 0 then
    IndentCode.error 'no input file, run with option -h or --help'
  end
  if !File.file?( ARGV[0] ) then
    IndentCode.error 'unable to open file ' + ARGV[0]
  end
  exit if File.size( ARGV[0] ) == 0
end

Instance Method Details

#runObject

start indent handling.



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
# File 'lib/indent_code/application.rb', line 46

def run
  # create source file copy for backup.
  FileUtils.copy( ARGV[0], ARGV[0] + ?~ )

  parser = nil
  # create indent parser.
  case @@options[:lang]
  when 'cpp'
    parser = IndentCPP.new
  else
	IndentCode.error 'unknown options - ' + @@options[:lang]
  end

  f = File.new( ARGV[0], 'w' )
  # parse input file.
  File.open( ARGV[0] + ?~, 'r' ).each do |line|
    @@context[:linetxt] = line.strip
    indent = @@context[:indent]

    # parse each char from string.
    line.each_char do |c|
      IndentCode.error() if !parser.indent( c )
    end

    indent = @@context[:indent] if @@context[:indent] < indent
    spaces = ' ' * indent

    f.puts spaces + @@context[:linetxt]
    @@context[:linenum] = @@context[:linenum] + 1
  end
  f.close

  # clean source code.
  if @@options[:clean] then
	# create source file copy for backup.
    FileUtils.copy( ARGV[0], ARGV[0] + '~~' )
	
	cleaner = IndentCode::Cleaner.new	
	@@context[:indent] = ''
	
	f = File.new( ARGV[0], 'w' )
	# parse input file.
	File.open( ARGV[0] + '~~', 'r' ).each do |line|
	  @@context[:linetxt] = line
	  
	  # parse each char from string.
	  line.each_char do |c|
 IndentCode.error() if !cleaner.clean( c )
	  end
	  
	  # check result of paring.
	  case @@context[:indent]
	  when ''
 f.print @@context[:linetxt]
	  when 'lock'
	  when 'unlock'
 @@context[:indent] = ''
	  end
	  
	  @@context[:linenum] = @@context[:linenum] + 1
	end
	f.close
  end
  
  
  # delete temporary files.
  if @@options[:delete] then
	FileUtils.remove( ARGV[0] + '~' )
	FileUtils.remove( ARGV[0] + '~~' )
  end
end