module CopyExpander

Work with individual source lines to perform token replacement declared in COBOL COPY REPLACING statements.

Public Instance Methods

blank?(line) click to toggle source

Is this line logically blank?

# File lib/copy_expander.rb, line 82
def blank? line
  if ! line.valid_encoding?
    line = line.encode("UTF-16be", :invalid=>:replace, :replace=>" ").encode('UTF-8')
  end
  line == nil || line.gsub(/\s/, '').length == 0
end
break_up_source_line(line) click to toggle source

Save the first six and last eight characters of the 80-character line so that we won’t shift characters into the interpreted area of the line when we make text substitutions.

# File lib/copy_expander.rb, line 68
def break_up_source_line line
  if line[70] == ' ' && line[71..72] != '  ' 
    line[72..79] = line[71..78]
    line[71] = ' '
  end  

  line.length >=  6 ? @first_six_characters  = line[0..5]   : nil
  line.length >= 80 ? @last_eight_characters = line[72..79] : nil
  line.length >= 72 ? @work_area             = line[6..71]  : nil
end
comment?(line) click to toggle source

Is this line a comment? Source comments in COBOL are identified by an asterisk in position 7.

# File lib/copy_expander.rb, line 93
def comment? line
  line[6] == '*'
end
commentize(line) click to toggle source

Make the current source line a comment line.

# File lib/copy_expander.rb, line 114
def commentize line
  line[6] = '*'
  line
end
copy_statement_count() click to toggle source
# File lib/copy_expander.rb, line 17
def copy_statement_count
  @copy_statement_count
end
expand_copybook(copy_statement) click to toggle source

Recursively expand copybooks and replace tokens

# File lib/copy_expander.rb, line 38
def expand_copybook copy_statement
  @depth += 1
  if @depth > 8
    puts "recursion depth is #{@depth}"
    exit(1)
  end  
  copybook = File.open("#{copy_dir}/#{copy_statement.copybook_name}", 'r')
  begin
    line = ('%-80.80s' % copybook.readline.chomp)
    if blank?(line) || comment?(line)
      write_from line
    else
      break_up_source_line line
      if has_copy_statement?
        @copy_statement_count += 1
        expand_copybook CopyStatement.new(@work_area)
      else
        @work_area = replace_tokens(@work_area, copy_statement)
        write_from reconstruct_line 
      end  
    end  
  end while copybook.eof? == false  
  @depth -= 1
end
has_copy_statement?() click to toggle source

Does the working area of the source line contain a COPY statement?

# File lib/copy_expander.rb, line 100
def has_copy_statement?
  @work_area.match(/ COPY (?=([^\"\']*\"[^\"\']*\"\')*[^\"\']*$)/i) ? true : false
end
init() click to toggle source
# File lib/copy_expander.rb, line 12
def init
  @copy_statement_count = 0
  @copy_statement_depth = 0
end
process(line) click to toggle source

Expand COPY statement found on a single line of COBOL source code.

# File lib/copy_expander.rb, line 24
def process line
  return line if blank? line
  return line if comment? line
  break_up_source_line line
  return line unless has_copy_statement?
  @copy_statement_count += 1
  @depth = 0
  expand_copybook CopyStatement.new(@work_area)
  nil
end
reconstruct_line() click to toggle source

Reconstruct the original source line.

# File lib/copy_expander.rb, line 107
def reconstruct_line
  @first_six_characters + ('%-66.66s' % @work_area) + @last_eight_characters
end
replace_tokens(line, copy_statement) click to toggle source

Carry out token replacement in a source line per the REPLACING option

# File lib/copy_expander.rb, line 122
def replace_tokens line, copy_statement
  if copy_statement.old_value == nil || copy_statement.new_value == nil
    line
  else
    line.gsub(copy_statement.old_value, copy_statement.new_value) unless copy_statement.old_value == nil
  end
end