copybook_utils 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/LICENSE.md +21 -0
- data/README.md +94 -0
- data/Rakefile +5 -0
- data/bin/convert_from_ebcdic +22 -0
- data/bin/convert_from_ebcdic.bat +2 -0
- data/copybook_utils.gemspec +25 -0
- data/ext/convert_methods/convert_methods.c +45 -0
- data/ext/convert_methods/extconf.rb +4 -0
- data/ext/convert_methods/tables.h +86 -0
- data/lib/cb2xml-0.95.1/cb2xml.jar +0 -0
- data/lib/cb2xml-0.95.1/cb2xml.properties +3 -0
- data/lib/copybook_utils.rb +28 -0
- data/lib/copybook_utils/convert_file.rb +64 -0
- data/lib/copybook_utils/version.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 600e6d99c32bd1ecacae6efd948314cf448113e6
|
4
|
+
data.tar.gz: a065ae03f78087f0350e653f5203d1ff0ab984cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9c6097320fed8e54f609ac2979ed25980d14c2145fe0c220f71017d3cc6b34279b9cd081cef7062e60f635f3a21b719a295445b91d33b2560f77f70e2ca44f1e
|
7
|
+
data.tar.gz: 614480e875320e3d9af09057888160d255fbed31c332c0809af87df9cc90cd76ed48b7834f53c1f9eec085258db0c205864631e34ffad4352faaa16e3060858e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Glenn Waters
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# CopybookUtils
|
2
|
+
|
3
|
+
Commands to convert fixed-record files described by a copybook record layout
|
4
|
+
from EBCDIC to ASCII, converting only the alpha-numeric fields.
|
5
|
+
|
6
|
+
The Gem can also be used to create scripts.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'copybook_utils'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install copybook_utils
|
23
|
+
|
24
|
+
## Command-line Usage
|
25
|
+
|
26
|
+
A command ```convert_from_ebcdic``` is in the gem ```bin``` directory. It is
|
27
|
+
recommended to copy ```convert_from_ebcdic``` from the bin directory to an
|
28
|
+
appropriate place on your system (also copy ```convert_from_ebcdic.bat``` to
|
29
|
+
the same place if on a Windows system).
|
30
|
+
|
31
|
+
Run the command from a terminal or command prompt. The command usage is:
|
32
|
+
|
33
|
+
```convert_from_ebcdic <Copybook file> <EBCDIC data file> <ASCII converted
|
34
|
+
file>```
|
35
|
+
|
36
|
+
This command parses the ```copybook file``` that contains the record layout for
|
37
|
+
the ```EBCDIC data file```. The ```EBCDIC data file``` is read record by record
|
38
|
+
converting the alpha-numeric fields from EBCDIC to ASCII. The output is written
|
39
|
+
to ```ASCII converted file```.
|
40
|
+
|
41
|
+
All binary fields (e.g.: comp-3, etc) are copied from the input records to the
|
42
|
+
output records unchanged.
|
43
|
+
|
44
|
+
## Gem Usage
|
45
|
+
|
46
|
+
To use CopybookUtils:
|
47
|
+
|
48
|
+
```require 'copybook_utils'```
|
49
|
+
|
50
|
+
### Converting Copybooks to XML
|
51
|
+
|
52
|
+
To convert a copybook to XML (wrapper around cb2xml Java jar from the copybook
|
53
|
+
to XML project):
|
54
|
+
|
55
|
+
```result = CopybookUtils.copybook_to_xml( copybook_filename )```
|
56
|
+
|
57
|
+
Result contains:
|
58
|
+
|
59
|
+
```
|
60
|
+
result[:xml] # XML string representing copybook
|
61
|
+
result[:error_out] # error info from conversion process
|
62
|
+
result[:process_status] # the Process::Status from running java; non-zero is an error
|
63
|
+
```
|
64
|
+
|
65
|
+
The XML may be parsed with your favorite XML parser (e.g.: Nokogiri).
|
66
|
+
|
67
|
+
### Converting Data Files between ASCII and EBCDIC
|
68
|
+
|
69
|
+
```
|
70
|
+
CopybookUtils.to_ascii( copybook_xml, data_file_input, data_file_output )
|
71
|
+
CopybookUtils.to_ebcdic( copybook_xml, data_file_input, data_file_output )
|
72
|
+
```
|
73
|
+
|
74
|
+
These methods take the XML from ```CopybookUtils.copybook_to_xml``` and convert
|
75
|
+
the ```data_file_input``` creating the ```data_file_output```. The input file
|
76
|
+
must be a fixed-record file with the record layout described by the copybook.
|
77
|
+
Note that fixed-record files do not have newlines at the end of each record.
|
78
|
+
|
79
|
+
## Development
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
82
|
+
|
83
|
+
To release a new version, update the version number in `version.rb`, and then
|
84
|
+
run `bundle exec rake release` to create a git tag for the version, push git
|
85
|
+
commits and tags, and push the `.gem` file to
|
86
|
+
[rubygems.org](https://rubygems.org).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
1. Fork it ( https://github.com/[my-github-username]/copybook_utils/fork )
|
91
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
92
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
93
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
94
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "copybook_utils"
|
5
|
+
|
6
|
+
if ARGV.length != 3
|
7
|
+
puts "Usage: #{File.basename(__FILE__)} <Copybook file> <Input EBCDIC data file> <Output ASCII data file>"
|
8
|
+
exit 99
|
9
|
+
end
|
10
|
+
|
11
|
+
copybook_filename = ARGV[0]
|
12
|
+
input_ebcdic_filename = ARGV[1]
|
13
|
+
output_ascii_filename = ARGV[2]
|
14
|
+
|
15
|
+
converted_copybook = CopybookUtils.copybook_to_xml copybook_filename
|
16
|
+
if converted_copybook[:process_status].exitstatus != 0
|
17
|
+
puts "Failed to properly parse copybook. Output from cb2xml is:"
|
18
|
+
puts "converted_copybook[:error_out]"
|
19
|
+
Process::exit converted_copybook[:process_status].exitstatus
|
20
|
+
end
|
21
|
+
|
22
|
+
CopybookUtils.to_ascii converted_copybook[:xml], input_ebcdic_filename, output_ascii_filename
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'copybook_utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "copybook_utils"
|
8
|
+
spec.version = CopybookUtils::VERSION
|
9
|
+
spec.authors = ["Glenn Waters"]
|
10
|
+
spec.email = ["gwwaters@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Utilities for working with COBOL copybooks and file conversion.}
|
13
|
+
spec.homepage = "https://github.com/gwww/CopybookUtils"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.extensions << "ext/convert_methods/extconf.rb"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rake-compiler", "~> 0.9"
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "tables.h"
|
3
|
+
|
4
|
+
VALUE CONVERT_METHODS = Qnil;
|
5
|
+
VALUE ebcdic2ascii(VALUE self, VALUE payload);
|
6
|
+
VALUE ascii2ebcdic(VALUE self, VALUE payload);
|
7
|
+
|
8
|
+
void Init_convert_methods() {
|
9
|
+
CONVERT_METHODS = rb_define_module("CONVERT_METHODS");
|
10
|
+
rb_define_method(CONVERT_METHODS, "ebcdic2ascii", ebcdic2ascii, 1);
|
11
|
+
rb_define_method(CONVERT_METHODS, "ascii2ebcdic", ascii2ebcdic, 1);
|
12
|
+
}
|
13
|
+
|
14
|
+
static VALUE convert( VALUE self, VALUE ruby_string, const unsigned char conversion_table[] ) {
|
15
|
+
u_char *input_string;
|
16
|
+
u_char *output_string;
|
17
|
+
u_char *dest;
|
18
|
+
long input_string_length;
|
19
|
+
long count;
|
20
|
+
VALUE return_string;
|
21
|
+
|
22
|
+
if (TYPE(ruby_string) != T_STRING)
|
23
|
+
rb_raise(rb_eRuntimeError, "expecting string, received %d", TYPE(ruby_string));
|
24
|
+
|
25
|
+
input_string = (u_char *)RSTRING_PTR(ruby_string);
|
26
|
+
input_string_length = RSTRING_LEN(ruby_string);
|
27
|
+
output_string = malloc(input_string_length);
|
28
|
+
|
29
|
+
count = input_string_length;
|
30
|
+
dest = output_string;
|
31
|
+
while (count-- != 0) {
|
32
|
+
*dest++ = conversion_table[*input_string++];
|
33
|
+
}
|
34
|
+
return_string = rb_str_new((const char *)output_string, input_string_length);
|
35
|
+
free(output_string);
|
36
|
+
return return_string;
|
37
|
+
}
|
38
|
+
|
39
|
+
VALUE ebcdic2ascii( VALUE self, VALUE ruby_string ) {
|
40
|
+
return convert( self, ruby_string, os_toascii );
|
41
|
+
}
|
42
|
+
|
43
|
+
VALUE ascii2ebcdic( VALUE self, VALUE ruby_string ) {
|
44
|
+
return convert( self, ruby_string, os_toebcdic );
|
45
|
+
}
|
@@ -0,0 +1,86 @@
|
|
1
|
+
/* os_toascii and os_toebcdic taken from...
|
2
|
+
* Initial Port for Apache-1.3 by <Martin.Kraemer@Mch.SNI.De>
|
3
|
+
* Adapted for OpenSSL-0.9.4 by <Martin.Kraemer@Mch.SNI.De>
|
4
|
+
*/
|
5
|
+
|
6
|
+
/*
|
7
|
+
This code does basic character mapping for IBM's TPF and OS/390 operating systems.
|
8
|
+
It is a modified version of the BS2000 table.
|
9
|
+
|
10
|
+
Bijective EBCDIC (character set IBM-1047) to US-ASCII table:
|
11
|
+
This table is bijective - there are no ambigous or duplicate characters.
|
12
|
+
*/
|
13
|
+
const unsigned char os_toascii[256] = {
|
14
|
+
0x00, 0x01, 0x02, 0x03, 0x85, 0x09, 0x86, 0x7f, /* 00-0f: */
|
15
|
+
0x87, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
|
16
|
+
0x10, 0x11, 0x12, 0x13, 0x8f, 0x0a, 0x08, 0x97, /* 10-1f: */
|
17
|
+
0x18, 0x19, 0x9c, 0x9d, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
|
18
|
+
0x80, 0x81, 0x82, 0x83, 0x84, 0x92, 0x17, 0x1b, /* 20-2f: */
|
19
|
+
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, /* ................ */
|
20
|
+
0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, /* 30-3f: */
|
21
|
+
0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, /* ................ */
|
22
|
+
0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, /* 40-4f: */
|
23
|
+
0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, /* ...........<(+| */
|
24
|
+
0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, /* 50-5f: */
|
25
|
+
0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, /* &.........!$*);^ */
|
26
|
+
0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, /* 60-6f: */
|
27
|
+
0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, /* -/.........,%_>? */
|
28
|
+
0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, /* 70-7f: */
|
29
|
+
0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, /* .........`:#@'=" */
|
30
|
+
0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, /* 80-8f: */
|
31
|
+
0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, /* .abcdefghi...... */
|
32
|
+
0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, /* 90-9f: */
|
33
|
+
0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4, /* .jklmnopqr...... */
|
34
|
+
0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, /* a0-af: */
|
35
|
+
0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, /* .~stuvwxyz...[.. */
|
36
|
+
0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc, /* b0-bf: */
|
37
|
+
0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, /* .............].. */
|
38
|
+
0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* c0-cf: */
|
39
|
+
0x48, 0x49, 0xad, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5, /* {ABCDEFGHI...... */
|
40
|
+
0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, /* d0-df: */
|
41
|
+
0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, /* }JKLMNOPQR...... */
|
42
|
+
0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, /* e0-ef: */
|
43
|
+
0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, /* \.STUVWXYZ...... */
|
44
|
+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* f0-ff: */
|
45
|
+
0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f /* 0123456789...... */
|
46
|
+
};
|
47
|
+
|
48
|
+
|
49
|
+
/*
|
50
|
+
The US-ASCII to EBCDIC (character set IBM-1047) table:
|
51
|
+
This table is bijective (no ambiguous or duplicate characters)
|
52
|
+
*/
|
53
|
+
const unsigned char os_toebcdic[256] = {
|
54
|
+
0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, /* 00-0f: */
|
55
|
+
0x16, 0x05, 0x15, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, /* ................ */
|
56
|
+
0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, /* 10-1f: */
|
57
|
+
0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, /* ................ */
|
58
|
+
0x40, 0x5a, 0x7f, 0x7b, 0x5b, 0x6c, 0x50, 0x7d, /* 20-2f: */
|
59
|
+
0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, /* !"#$%&'()*+,-./ */
|
60
|
+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, /* 30-3f: */
|
61
|
+
0xf8, 0xf9, 0x7a, 0x5e, 0x4c, 0x7e, 0x6e, 0x6f, /* 0123456789:;<=>? */
|
62
|
+
0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 40-4f: */
|
63
|
+
0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, /* @ABCDEFGHIJKLMNO */
|
64
|
+
0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, /* 50-5f: */
|
65
|
+
0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, /* PQRSTUVWXYZ[\]^_ */
|
66
|
+
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 60-6f: */
|
67
|
+
0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* `abcdefghijklmno */
|
68
|
+
0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, /* 70-7f: */
|
69
|
+
0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, /* pqrstuvwxyz{|}~. */
|
70
|
+
0x20, 0x21, 0x22, 0x23, 0x24, 0x04, 0x06, 0x08, /* 80-8f: */
|
71
|
+
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x14, /* ................ */
|
72
|
+
0x30, 0x31, 0x25, 0x33, 0x34, 0x35, 0x36, 0x17, /* 90-9f: */
|
73
|
+
0x38, 0x39, 0x3a, 0x3b, 0x1a, 0x1b, 0x3e, 0xff, /* ................ */
|
74
|
+
0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, /* a0-af: */
|
75
|
+
0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, /* ................ */
|
76
|
+
0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, /* b0-bf: */
|
77
|
+
0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, /* ................ */
|
78
|
+
0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, /* c0-cf: */
|
79
|
+
0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, /* ................ */
|
80
|
+
0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, /* d0-df: */
|
81
|
+
0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, /* ................ */
|
82
|
+
0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, /* e0-ef: */
|
83
|
+
0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* ................ */
|
84
|
+
0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, /* f0-ff: */
|
85
|
+
0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf /* ................ */
|
86
|
+
};
|
Binary file
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'copybook_utils/version'
|
2
|
+
require 'copybook_utils/convert_file'
|
3
|
+
require 'convert_methods'
|
4
|
+
require 'open3'
|
5
|
+
|
6
|
+
module CopybookUtils
|
7
|
+
# defines native methods ebcdic2ascii and ascii2ebcdic
|
8
|
+
extend CONVERT_METHODS
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def copybook_to_xml copybook_filename
|
12
|
+
path = File.expand_path(File.dirname(__FILE__))
|
13
|
+
cmd = "java -jar #{path}/cb2xml-0.95.1/cb2xml.jar #{copybook_filename}"
|
14
|
+
stdout, stderr, status = Open3.capture3(cmd)
|
15
|
+
{ :xml => stdout, :error_out => stderr, :process_status => status }
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_ascii copybook_xml, from_filename, to_filename
|
19
|
+
converter = CopybookUtils::ConvertFile.new
|
20
|
+
converter.convert_file method(:ebcdic2ascii), copybook_xml, from_filename, to_filename
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_ebcdic copybook_xml, from_filename, to_filename
|
24
|
+
converter = CopybookUtils::ConvertFile.new
|
25
|
+
converter.convert_file method(:ascii2ebcdic), copybook_xml, from_filename, to_filename
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module CopybookUtils
|
4
|
+
class ConvertFile
|
5
|
+
def convert_file conversion_method, copybook_xml, from_filename, to_filename
|
6
|
+
doc = Nokogiri.XML(copybook_xml)
|
7
|
+
|
8
|
+
@conversion_method = conversion_method
|
9
|
+
@record_fields = []
|
10
|
+
flatten_xml 0, 0, doc.root.children
|
11
|
+
record_length = @record_fields[-1][:start] + @record_fields[-1][:length]
|
12
|
+
|
13
|
+
convert record_length, from_filename, to_filename
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def convert_record str
|
19
|
+
@record_fields.each do |field|
|
20
|
+
start = field[:start]
|
21
|
+
len = field[:length]
|
22
|
+
converter = field[:conversion_method]
|
23
|
+
str[start,len] = converter.(str[start,len]) if !converter.nil?
|
24
|
+
end
|
25
|
+
str
|
26
|
+
end
|
27
|
+
|
28
|
+
def convert record_length, from_filename, to_filename
|
29
|
+
puts "Record length #{record_length}"
|
30
|
+
record_count = 0
|
31
|
+
File.open(from_filename) do |input|
|
32
|
+
File.open(to_filename, "w") do |output|
|
33
|
+
while str = input.read(record_length) do
|
34
|
+
puts "Input file not a multiple of #{record_length}" if str.length != record_length
|
35
|
+
len = output.write(convert_record(str))
|
36
|
+
record_count += 1
|
37
|
+
puts "Write failed to output #{record_length} bytes, wrote #{len} bytes." if len != record_length
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
puts "Converted #{record_count} records."
|
42
|
+
end
|
43
|
+
|
44
|
+
def flatten_xml level, offset, tree
|
45
|
+
tree.each do |node|
|
46
|
+
# Note: The first definition is taken for determining how to convert (i.e.: redefines are ignored)
|
47
|
+
next if !node['redefines'].nil?
|
48
|
+
|
49
|
+
occurs = (node['occurs'] || "1").to_i - 1
|
50
|
+
(0..occurs).each do |i|
|
51
|
+
new_offset = node['storage-length'].to_i * i
|
52
|
+
if node.name == 'item'
|
53
|
+
ignoring = node['picture'].nil? && node['numeric'].nil?
|
54
|
+
binary = !((node['usage'] || '') =~ /^comp/).nil?
|
55
|
+
position = node['position'].to_i + offset - 1
|
56
|
+
@record_fields << { :start => position, :length => node['storage-length'].to_i,
|
57
|
+
:conversion_method => binary ? nil : @conversion_method } if !ignoring
|
58
|
+
end
|
59
|
+
flatten_xml level+1, offset+new_offset, node.children
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: copybook_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Glenn Waters
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- gwwaters@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions:
|
60
|
+
- ext/convert_methods/extconf.rb
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.md
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/convert_from_ebcdic
|
71
|
+
- bin/convert_from_ebcdic.bat
|
72
|
+
- copybook_utils.gemspec
|
73
|
+
- ext/convert_methods/convert_methods.c
|
74
|
+
- ext/convert_methods/extconf.rb
|
75
|
+
- ext/convert_methods/tables.h
|
76
|
+
- lib/cb2xml-0.95.1/cb2xml.jar
|
77
|
+
- lib/cb2xml-0.95.1/cb2xml.properties
|
78
|
+
- lib/copybook_utils.rb
|
79
|
+
- lib/copybook_utils/convert_file.rb
|
80
|
+
- lib/copybook_utils/version.rb
|
81
|
+
homepage: https://github.com/gwww/CopybookUtils
|
82
|
+
licenses: []
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Utilities for working with COBOL copybooks and file conversion.
|
104
|
+
test_files: []
|