Class: IndentCode::Application

Inherits:
Object
  • Object
show all
Includes:
IndentCode
Defined in:
lib/indent_code.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.



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

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 *~~)') do
      @@options[:delete] = true;
    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.



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

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          
  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