defstr 0.1.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.
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ === 0.1.0 / 2008-12-05
2
+
3
+ * Initial Release
data/Manifest.txt ADDED
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/defstr
6
+ lib/defstr.rb
7
+ test/actual
8
+ test/test_command_line.rb
9
+ test/test_defstr.rb
10
+ test/print_program.c
11
+ test/program.c
data/README.txt ADDED
@@ -0,0 +1,113 @@
1
+ = defstr
2
+
3
+ * http://lab.moekaku.com/code/defstr/
4
+
5
+ == DESCRIPTION:
6
+
7
+ defstr converts text to a C string literal
8
+ that parses (by a C compiler) to the original text, and creates
9
+ a C macro that defines it as a constant. The liternal preserves
10
+ the original text's structure (line breaks, paragraphs, etc).
11
+
12
+ defstr can be used as a tool for C/C++ code generation.
13
+
14
+ == Library Usage:
15
+
16
+ Use Defstr.string_literal to convert a string to a C literal. For example:
17
+
18
+ require 'defstr'
19
+ Defstr.string_literal "abc"
20
+
21
+ gets you a ruby string that represents the text:
22
+
23
+ "\
24
+ abc"
25
+
26
+ User Defstr.define_string to define the string as a constant. For example:
27
+
28
+ require 'defstr'
29
+ Defstr.define_string("CG_VERTEX_PROGRAM", <<EOF
30
+ void main(float3 position : POSITION,
31
+ out float3 clipPosition : POSITION)
32
+ {
33
+ clipPosition = position;
34
+ }
35
+ EOF
36
+ )
37
+
38
+ gets you a ruby string that represents the text:
39
+
40
+ #define CG_VERTEX_PROGRAM "\
41
+ void main(float3 position : POSITION,\n\
42
+ out float3 clipPosition : POSITION)\n\
43
+ {\n\
44
+ clipPosition = position;\n\
45
+ }\n\
46
+ "
47
+
48
+ == Shell Command Usage:
49
+
50
+ Suppose the content of vertex_program.cg is the same as
51
+ the program snipper defined in a string above. After you run
52
+
53
+ $ defstr CG_VERTEX_PROGRAM < vertex_program.cg > vertex_program_string.h
54
+
55
+ the content of vertex_program_string.h will be:
56
+
57
+ #ifndef __DEFSTR_CG_VERTEX_PROGRAM_H__
58
+ #define __DEFSTR_CG_VERTEX_PROGRAM_H__
59
+
60
+ #define CG_VERTEX_PROGRAM "\
61
+ void main(float3 position : POSITION,\n\
62
+ out float3 clipPosition : POSITION)\n\
63
+ {\n\
64
+ clipPosition = position;\n\
65
+ }\n\
66
+ "
67
+
68
+ #endif
69
+
70
+ If you do not want the #ifndef, use the --no-ifndef flag. For example:
71
+
72
+ $ defstr CG_VERTEX_PROGRAM < vertex_program.cg > vertex_program_string.h --no-ifndef
73
+
74
+ will write the following text:
75
+
76
+ #define CG_VERTEX_PROGRAM "\
77
+ void main(float3 position : POSITION,\n\
78
+ out float3 clipPosition : POSITION)\n\
79
+ {\n\
80
+ clipPosition = position;\n\
81
+ }\
82
+ "
83
+
84
+ to vertex_program_string.h
85
+
86
+ == INSTALL:
87
+
88
+ * sudo gem install defstr
89
+
90
+ == LICENSE:
91
+
92
+ (The MIT License)
93
+
94
+ Copyright (c) 2008 Pramook Khungurn
95
+
96
+ Permission is hereby granted, free of charge, to any person obtaining
97
+ a copy of this software and associated documentation files (the
98
+ 'Software'), to deal in the Software without restriction, including
99
+ without limitation the rights to use, copy, modify, merge, publish,
100
+ distribute, sublicense, and/or sell copies of the Software, and to
101
+ permit persons to whom the Software is furnished to do so, subject to
102
+ the following conditions:
103
+
104
+ The above copyright notice and this permission notice shall be
105
+ included in all copies or substantial portions of the Software.
106
+
107
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
108
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
109
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
110
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
111
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
112
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
113
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/defstr.rb'
6
+
7
+ Hoe.new('defstr', Defstr::VERSION) do |p|
8
+ p.developer('dragonmeteor', 'dragonmeteor@gmail.com')
9
+ p.remote_rdoc_dir = '' # Release to root
10
+ p.clean_globs = ["test/actual/*"]
11
+ end
12
+
13
+ # vim: syntax=Ruby
data/bin/defstr ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby -ws
2
+
3
+ require 'optparse'
4
+ require File.dirname(__FILE__) + "/../lib/defstr.rb"
5
+
6
+ ifndef = true
7
+
8
+ parser = OptionParser.new do |parser|
9
+ parser.banner = "Usage: defstr constant_name [options]"
10
+
11
+ parser.on("--[no-]ifndef", "Generate #ifndef (default: YES)") do |value|
12
+ ifndef = value
13
+ end
14
+
15
+ parser.on_tail("-h", "--help", "Show this message") do
16
+ puts parser
17
+ exit
18
+ end
19
+ end
20
+
21
+ parser.parse(ARGV)
22
+ const_name = ARGV.shift
23
+ if not const_name
24
+ puts parser
25
+ exit
26
+ end
27
+
28
+ s = STDIN.read
29
+
30
+ if ifndef
31
+ puts "#ifndef __DEFSTR_#{const_name}_H__"
32
+ puts "#define __DEFSTR_#{const_name}_H__"
33
+ puts
34
+ end
35
+
36
+ puts Defstr.define_string(const_name, s)
37
+
38
+ if ifndef
39
+ puts
40
+ puts "#endif"
41
+ end
data/lib/defstr.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Defstr
2
+ VERSION = '0.1.0'
3
+
4
+ # Turn the given string to
5
+ # a C string literal that
6
+ # parses to the original string.
7
+ def string_literal(s)
8
+ result = "\"\\\n"
9
+ s.each_byte do |b|
10
+ c = b.chr
11
+ if c == "\n"
12
+ result += "\\n\\\n"
13
+ elsif c == "\r"
14
+ next
15
+ elsif c == "\\"
16
+ result += "\\\\"
17
+ elsif c == "\""
18
+ result += "\\\""
19
+ else
20
+ result += c
21
+ end
22
+ end
23
+ result += "\"\n"
24
+ result
25
+ end
26
+ module_function :string_literal
27
+
28
+ # Generate a C macro that
29
+ # defines the given string
30
+ # as a string constant.
31
+ def define_string(name, s)
32
+ result = "#define #{name.upcase} " + string_literal(s)
33
+ end
34
+ module_function :define_string
35
+ end
@@ -0,0 +1,8 @@
1
+ #include <stdio.h>
2
+ #include "actual/program.h"
3
+
4
+ int main()
5
+ {
6
+ printf("%s", PROGRAM);
7
+ return 0;
8
+ }
data/test/program.c ADDED
@@ -0,0 +1,7 @@
1
+ #include <stdio.h>
2
+
3
+ int main()
4
+ {
5
+ printf("\"\n");
6
+ return 0;
7
+ }
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + "/../lib/defstr.rb"
3
+
4
+ class TestCommandLine < Test::Unit::TestCase
5
+ def test_defstr
6
+ dir = File.dirname(__FILE__)
7
+ system "ruby #{dir}/../bin/defstr PROGRAM < #{dir}/program.c > #{dir}/actual/program.h"
8
+ system "gcc #{dir}/print_program.c -o #{dir}/actual/print_program"
9
+ actual = `#{dir}/actual/print_program`
10
+ f = File.open("#{dir}/program.c")
11
+ expected = f.read
12
+ f.close
13
+ assert expected, actual
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + "/../lib/defstr.rb"
3
+
4
+ class TestLibrary < Test::Unit::TestCase
5
+ def test_string_literal
6
+ expected_abc = <<EOF
7
+ "\\
8
+ abc"
9
+ EOF
10
+ assert_equal(expected_abc, Defstr.string_literal("abc"))
11
+
12
+ expected_abcdef = <<EOF
13
+ "\\
14
+ abc\\n\\
15
+ def\\n\\
16
+ "
17
+ EOF
18
+ assert_equal(expected_abcdef, Defstr.string_literal("abc\ndef\n"))
19
+ end
20
+
21
+ def test_define_string
22
+ expected_cg_program = <<EOF
23
+ #define PROGRAM "\\
24
+ void main()\\n\\
25
+ {\\n\\
26
+ return 0;\\n\\
27
+ }\\n\\
28
+ "
29
+ EOF
30
+ assert_equal(expected_cg_program,
31
+ Defstr.define_string("PROGRAM", "void main()\n{\nreturn 0;\n}\n"))
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defstr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dragonmeteor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-07 00:00:00 +07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ description: defstr converts text to a C string literal that parses (by a C compiler) to the original text, and creates a C macro that defines it as a constant. The liternal preserves the original text's structure (line breaks, paragraphs, etc). defstr can be used as a tool for C/C++ code generation.
26
+ email:
27
+ - dragonmeteor@gmail.com
28
+ executables:
29
+ - defstr
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - bin/defstr
42
+ - lib/defstr.rb
43
+ - test/actual
44
+ - test/test_command_line.rb
45
+ - test/test_defstr.rb
46
+ - test/print_program.c
47
+ - test/program.c
48
+ has_rdoc: true
49
+ homepage: http://lab.moekaku.com/code/defstr/
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --main
53
+ - README.txt
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project: defstr
71
+ rubygems_version: 1.3.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: defstr converts text to a C string literal that parses (by a C compiler) to the original text, and creates a C macro that defines it as a constant
75
+ test_files:
76
+ - test/test_command_line.rb
77
+ - test/test_defstr.rb