cstruct 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +81 -0
- data/cstruct.gemspec +10 -0
- data/doc/examples/anonymous_struct.rb.html +94 -94
- data/doc/examples/anonymous_union.rb.html +93 -93
- data/doc/examples/array_member.rb.html +79 -79
- data/doc/examples/file_io.rb.html +87 -87
- data/doc/examples/first_example.rb.html +104 -104
- data/doc/examples/get_system_info.rb.html +114 -114
- data/doc/examples/get_versionex.rb.html +97 -97
- data/doc/examples/global_memory.rb.html +102 -102
- data/doc/examples/inner_struct.rb.html +79 -79
- data/doc/examples/inner_union.rb.html +77 -77
- data/doc/examples/namespace.rb.html +80 -80
- data/doc/examples/show_processes.rb.html +95 -95
- data/doc/examples/struct_member.rb.html +128 -128
- data/doc/stylesheets/ie.css +8 -8
- data/examples/anonymous_struct.rb +6 -6
- data/examples/anonymous_union.rb +5 -5
- data/examples/array_member.rb +1 -1
- data/examples/file_io.rb +22 -23
- data/examples/first_example.rb +4 -4
- data/examples/inner_struct.rb +5 -5
- data/examples/inner_union.rb +4 -4
- data/examples/namespace.rb +15 -15
- data/examples/win32/get_system_info.rb +31 -31
- data/examples/win32/get_versionex.rb +12 -12
- data/examples/win32/global_memory.rb +9 -9
- data/examples/win32/show_processes.rb +28 -28
- data/lib/cstruct.rb +1 -523
- data/lib/cstruct/cstruct.rb +457 -0
- data/lib/cstruct/field.rb +45 -0
- data/lib/cstruct/utils.rb +53 -0
- data/lib/cstruct/win32struct.rb +84 -0
- data/lib/cstruct/win64struct.rb +5 -0
- data/lib/win32struct.rb +3 -90
- data/lib/win64struct.rb +3 -13
- data/spec/cstruct_spec.rb +240 -0
- metadata +62 -73
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
##Synopsis
|
2
|
+
CStruct is a simulation of the C language's struct.Its main purpose is to manipulate binary-data conveniently in Ruby. It can be used in:
|
3
|
+
|
4
|
+
* Binary file IO like C.
|
5
|
+
* The parameter of the OS's API.(e.g. Win32)
|
6
|
+
* Other...
|
7
|
+
|
8
|
+
Lean more: [http://cstruct.rubyforge.org/][1]
|
9
|
+
|
10
|
+
##Getting Started
|
11
|
+
###Install CStruct
|
12
|
+
Install CStruct is easy.
|
13
|
+
|
14
|
+
gem install cstruct
|
15
|
+
|
16
|
+
###Using CStruct
|
17
|
+
Let's see an example,struct Point in C language(32-bit platform) like this:
|
18
|
+
|
19
|
+
struct Point
|
20
|
+
{
|
21
|
+
int x;
|
22
|
+
int y;
|
23
|
+
};
|
24
|
+
How to represent struct Point in Ruby? You can use CStruct to do it.
|
25
|
+
|
26
|
+
class Point < CStruct
|
27
|
+
int32:x
|
28
|
+
int32:y
|
29
|
+
end
|
30
|
+
Example:
|
31
|
+
|
32
|
+
require 'cstruct'
|
33
|
+
|
34
|
+
# struct Point in Ruby:
|
35
|
+
class Point < CStruct
|
36
|
+
int32:x
|
37
|
+
int32:y
|
38
|
+
end
|
39
|
+
|
40
|
+
# create a Point's instance
|
41
|
+
point = Point.new
|
42
|
+
|
43
|
+
# assign like as C language
|
44
|
+
point.x = 10
|
45
|
+
point.y = 20
|
46
|
+
puts "point.x = #{point.x},point.y = #{point.y}"
|
47
|
+
|
48
|
+
###Using Win32Struct
|
49
|
+
like this:
|
50
|
+
|
51
|
+
typedef struct _OSVERSIONINFOEXA {
|
52
|
+
DWORD dwOSVersionInfoSize;
|
53
|
+
DWORD dwMajorVersion;
|
54
|
+
DWORD dwMinorVersion;
|
55
|
+
DWORD dwBuildNumber;
|
56
|
+
DWORD dwPlatformId;
|
57
|
+
CHAR szCSDVersion[ 128 ];
|
58
|
+
WORD wServicePackMajor;
|
59
|
+
WORD wReserved[2];
|
60
|
+
} OSVERSIONINFOEXA;
|
61
|
+
in Ruby:
|
62
|
+
|
63
|
+
class OSVERSIONINFOEXA < Win32Struct
|
64
|
+
DWORD :dwOSVersionInfoSize
|
65
|
+
DWORD :dwMajorVersion
|
66
|
+
DWORD :dwMinorVersion
|
67
|
+
DWORD :dwBuildNumber
|
68
|
+
DWORD :dwPlatformId
|
69
|
+
CHAR :szCSDVersion,[ 128 ]
|
70
|
+
WORD :wServicePackMajor
|
71
|
+
WORD :wServicePackMinor
|
72
|
+
WORD :wReserved,[2]
|
73
|
+
end
|
74
|
+
##Lean More
|
75
|
+
Please see also [Documents][2] and [Examples][3].
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
[1]:http://cstruct.rubyforge.org/
|
80
|
+
[2]:http://cstruct.rubyforge.org/documents.html
|
81
|
+
[3]:http://cstruct.rubyforge.org/examples.html
|
data/cstruct.gemspec
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'cstruct'
|
3
|
+
s.version = '1.0.1'
|
4
|
+
s.author = ['skandhas']
|
5
|
+
s.email = 'skandhas@163.com'
|
6
|
+
s.homepage = "http://github.com/skandhas/cstruct"
|
7
|
+
s.summary = "CStruct is a simulation of the C language's struct."
|
8
|
+
s.files = Dir['{lib/**/*,spec/**/*,examples/**/*,doc/**/*}'] +
|
9
|
+
%w(README.md cstruct.gemspec)
|
10
|
+
end
|
@@ -1,94 +1,94 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
2
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
-
<head profile="http://gmpg.org/xfn/11" >
|
4
|
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
-
<title>CStruct:Examples:Anonymous struct</title>
|
6
|
-
<style type="text/css">
|
7
|
-
|
8
|
-
.CodeRay {
|
9
|
-
background-color: #232323;
|
10
|
-
border: 1px solid black;
|
11
|
-
font-family: 'Courier New', 'Terminal', monospace;
|
12
|
-
color: #E6E0DB;
|
13
|
-
padding: 3px 5px;
|
14
|
-
overflow: auto;
|
15
|
-
font-size: 12px;
|
16
|
-
margin: 12px 0;
|
17
|
-
}
|
18
|
-
.CodeRay pre {
|
19
|
-
margin: 0px;
|
20
|
-
padding: 0px;
|
21
|
-
}
|
22
|
-
|
23
|
-
.CodeRay .an { color:#E7BE69 }
|
24
|
-
.CodeRay .c { color:#BC9358; font-style: italic; }
|
25
|
-
.CodeRay .ch { color:#509E4F }
|
26
|
-
.CodeRay .cl { color:#FFF }
|
27
|
-
.CodeRay .co { color:#FFF }
|
28
|
-
.CodeRay .fl { color:#A4C260 }
|
29
|
-
.CodeRay .fu { color:#FFC56D }
|
30
|
-
.CodeRay .gv { color:#D0CFFE }
|
31
|
-
.CodeRay .i { color:#A4C260 }
|
32
|
-
.CodeRay .il { background:#151515 }
|
33
|
-
.CodeRay .iv { color:#D0CFFE }
|
34
|
-
.CodeRay .pp { color:#E7BE69 }
|
35
|
-
.CodeRay .r { color:#CB7832 }
|
36
|
-
.CodeRay .rx { color:#A4C260 }
|
37
|
-
.CodeRay .s { color:#A4C260 }
|
38
|
-
.CodeRay .sy { color:#6C9CBD }
|
39
|
-
.CodeRay .ta { color:#E7BE69 }
|
40
|
-
.CodeRay .pc { color:#6C9CBD }
|
41
|
-
|
42
|
-
</style>
|
43
|
-
</head>
|
44
|
-
<body>
|
45
|
-
<div style="margin-left:auto;margin-right:auto;width:800px;">
|
46
|
-
<div class="CodeRay">
|
47
|
-
<div class="code"><pre><span class="c"># CStruct Examples</span>
|
48
|
-
require <span class="s"><span class="dl">'</span><span class="k">cstruct</span><span class="dl">'</span></span>
|
49
|
-
|
50
|
-
<span class="c"># example:</span>
|
51
|
-
<span class="c"># struct Window in C\C++ (32-bit platform): </span>
|
52
|
-
<span class="c"># </span>
|
53
|
-
<span class="c"># struct Window</span>
|
54
|
-
<span class="c"># {</span>
|
55
|
-
<span class="c"># int style;</span>
|
56
|
-
<span class="c"># struct{</span>
|
57
|
-
<span class="c"># int x;</span>
|
58
|
-
<span class="c"># int y;</span>
|
59
|
-
<span class="c"># }position; /* position is anonymous struct's instance */</span>
|
60
|
-
<span class="c"># }; </span>
|
61
|
-
|
62
|
-
|
63
|
-
<span class="c"># struct Window in Ruby: </span>
|
64
|
-
<span class="r">class</span> <span class="cl">Window</span> < <span class="co">CStruct</span>
|
65
|
-
int32<span class="sy">:style</span>
|
66
|
-
struct <span class="sy">:position</span> <span class="r">do</span>
|
67
|
-
int32<span class="sy">:x</span>
|
68
|
-
int32<span class="sy">:y</span>
|
69
|
-
<span class="r">end</span>
|
70
|
-
<span class="r">end</span>
|
71
|
-
<span class="c"># or like this (use brace):</span>
|
72
|
-
<span class="c"># class Window < CStruct</span>
|
73
|
-
<span class="c"># int32:style</span>
|
74
|
-
<span class="c"># struct (:position) {</span>
|
75
|
-
<span class="c"># int32:x</span>
|
76
|
-
<span class="c"># int32:y </span>
|
77
|
-
<span class="c"># }</span>
|
78
|
-
<span class="c"># end</span>
|
79
|
-
|
80
|
-
<span class="c"># create a Window's instance</span>
|
81
|
-
window = <span class="co">Window</span>.new
|
82
|
-
|
83
|
-
<span class="c"># assign like as C language</span>
|
84
|
-
window.style = <span class="i">1</span>
|
85
|
-
window.position.x = <span class="i">10</span>
|
86
|
-
window.position.y = <span class="i">10</span>
|
87
|
-
|
88
|
-
puts <span class="s"><span class="dl">"</span><span class="k">sizeof(Window) = </span><span class="il"><span class="idl">#{</span><span class="co">Window</span>.__size__<span class="idl">}</span></span><span class="dl">"</span></span> <span class="c"># "__size__" is alias of "size"</span>
|
89
|
-
puts <span class="s"><span class="dl">"</span><span class="k">window.style = </span><span class="il"><span class="idl">#{</span>window.style<span class="idl">}</span></span><span class="k">,window.position.x = </span><span class="il"><span class="idl">#{</span>window.position.x<span class="idl">}</span></span><span class="k">,window.position.y = </span><span class="il"><span class="idl">#{</span>window.position.y<span class="idl">}</span></span><span class="dl">"</span></span>
|
90
|
-
</pre></div>
|
91
|
-
</div>
|
92
|
-
|
93
|
-
</div>
|
94
|
-
</body>
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
+
<head profile="http://gmpg.org/xfn/11" >
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<title>CStruct:Examples:Anonymous struct</title>
|
6
|
+
<style type="text/css">
|
7
|
+
|
8
|
+
.CodeRay {
|
9
|
+
background-color: #232323;
|
10
|
+
border: 1px solid black;
|
11
|
+
font-family: 'Courier New', 'Terminal', monospace;
|
12
|
+
color: #E6E0DB;
|
13
|
+
padding: 3px 5px;
|
14
|
+
overflow: auto;
|
15
|
+
font-size: 12px;
|
16
|
+
margin: 12px 0;
|
17
|
+
}
|
18
|
+
.CodeRay pre {
|
19
|
+
margin: 0px;
|
20
|
+
padding: 0px;
|
21
|
+
}
|
22
|
+
|
23
|
+
.CodeRay .an { color:#E7BE69 }
|
24
|
+
.CodeRay .c { color:#BC9358; font-style: italic; }
|
25
|
+
.CodeRay .ch { color:#509E4F }
|
26
|
+
.CodeRay .cl { color:#FFF }
|
27
|
+
.CodeRay .co { color:#FFF }
|
28
|
+
.CodeRay .fl { color:#A4C260 }
|
29
|
+
.CodeRay .fu { color:#FFC56D }
|
30
|
+
.CodeRay .gv { color:#D0CFFE }
|
31
|
+
.CodeRay .i { color:#A4C260 }
|
32
|
+
.CodeRay .il { background:#151515 }
|
33
|
+
.CodeRay .iv { color:#D0CFFE }
|
34
|
+
.CodeRay .pp { color:#E7BE69 }
|
35
|
+
.CodeRay .r { color:#CB7832 }
|
36
|
+
.CodeRay .rx { color:#A4C260 }
|
37
|
+
.CodeRay .s { color:#A4C260 }
|
38
|
+
.CodeRay .sy { color:#6C9CBD }
|
39
|
+
.CodeRay .ta { color:#E7BE69 }
|
40
|
+
.CodeRay .pc { color:#6C9CBD }
|
41
|
+
|
42
|
+
</style>
|
43
|
+
</head>
|
44
|
+
<body>
|
45
|
+
<div style="margin-left:auto;margin-right:auto;width:800px;">
|
46
|
+
<div class="CodeRay">
|
47
|
+
<div class="code"><pre><span class="c"># CStruct Examples</span>
|
48
|
+
require <span class="s"><span class="dl">'</span><span class="k">cstruct</span><span class="dl">'</span></span>
|
49
|
+
|
50
|
+
<span class="c"># example:</span>
|
51
|
+
<span class="c"># struct Window in C\C++ (32-bit platform): </span>
|
52
|
+
<span class="c"># </span>
|
53
|
+
<span class="c"># struct Window</span>
|
54
|
+
<span class="c"># {</span>
|
55
|
+
<span class="c"># int style;</span>
|
56
|
+
<span class="c"># struct{</span>
|
57
|
+
<span class="c"># int x;</span>
|
58
|
+
<span class="c"># int y;</span>
|
59
|
+
<span class="c"># }position; /* position is anonymous struct's instance */</span>
|
60
|
+
<span class="c"># }; </span>
|
61
|
+
|
62
|
+
|
63
|
+
<span class="c"># struct Window in Ruby: </span>
|
64
|
+
<span class="r">class</span> <span class="cl">Window</span> < <span class="co">CStruct</span>
|
65
|
+
int32<span class="sy">:style</span>
|
66
|
+
struct <span class="sy">:position</span> <span class="r">do</span>
|
67
|
+
int32<span class="sy">:x</span>
|
68
|
+
int32<span class="sy">:y</span>
|
69
|
+
<span class="r">end</span>
|
70
|
+
<span class="r">end</span>
|
71
|
+
<span class="c"># or like this (use brace):</span>
|
72
|
+
<span class="c"># class Window < CStruct</span>
|
73
|
+
<span class="c"># int32:style</span>
|
74
|
+
<span class="c"># struct (:position) {</span>
|
75
|
+
<span class="c"># int32:x</span>
|
76
|
+
<span class="c"># int32:y </span>
|
77
|
+
<span class="c"># }</span>
|
78
|
+
<span class="c"># end</span>
|
79
|
+
|
80
|
+
<span class="c"># create a Window's instance</span>
|
81
|
+
window = <span class="co">Window</span>.new
|
82
|
+
|
83
|
+
<span class="c"># assign like as C language</span>
|
84
|
+
window.style = <span class="i">1</span>
|
85
|
+
window.position.x = <span class="i">10</span>
|
86
|
+
window.position.y = <span class="i">10</span>
|
87
|
+
|
88
|
+
puts <span class="s"><span class="dl">"</span><span class="k">sizeof(Window) = </span><span class="il"><span class="idl">#{</span><span class="co">Window</span>.__size__<span class="idl">}</span></span><span class="dl">"</span></span> <span class="c"># "__size__" is alias of "size"</span>
|
89
|
+
puts <span class="s"><span class="dl">"</span><span class="k">window.style = </span><span class="il"><span class="idl">#{</span>window.style<span class="idl">}</span></span><span class="k">,window.position.x = </span><span class="il"><span class="idl">#{</span>window.position.x<span class="idl">}</span></span><span class="k">,window.position.y = </span><span class="il"><span class="idl">#{</span>window.position.y<span class="idl">}</span></span><span class="dl">"</span></span>
|
90
|
+
</pre></div>
|
91
|
+
</div>
|
92
|
+
|
93
|
+
</div>
|
94
|
+
</body>
|
@@ -1,93 +1,93 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
2
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
-
<head profile="http://gmpg.org/xfn/11" >
|
4
|
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
-
<title>CStruct:Examples:Anonymous union</title>
|
6
|
-
<style type="text/css">
|
7
|
-
|
8
|
-
.CodeRay {
|
9
|
-
background-color: #232323;
|
10
|
-
border: 1px solid black;
|
11
|
-
font-family: 'Courier New', 'Terminal', monospace;
|
12
|
-
color: #E6E0DB;
|
13
|
-
padding: 3px 5px;
|
14
|
-
overflow: auto;
|
15
|
-
font-size: 12px;
|
16
|
-
margin: 12px 0;
|
17
|
-
}
|
18
|
-
.CodeRay pre {
|
19
|
-
margin: 0px;
|
20
|
-
padding: 0px;
|
21
|
-
}
|
22
|
-
|
23
|
-
.CodeRay .an { color:#E7BE69 }
|
24
|
-
.CodeRay .c { color:#BC9358; font-style: italic; }
|
25
|
-
.CodeRay .ch { color:#509E4F }
|
26
|
-
.CodeRay .cl { color:#FFF }
|
27
|
-
.CodeRay .co { color:#FFF }
|
28
|
-
.CodeRay .fl { color:#A4C260 }
|
29
|
-
.CodeRay .fu { color:#FFC56D }
|
30
|
-
.CodeRay .gv { color:#D0CFFE }
|
31
|
-
.CodeRay .i { color:#A4C260 }
|
32
|
-
.CodeRay .il { background:#151515 }
|
33
|
-
.CodeRay .iv { color:#D0CFFE }
|
34
|
-
.CodeRay .pp { color:#E7BE69 }
|
35
|
-
.CodeRay .r { color:#CB7832 }
|
36
|
-
.CodeRay .rx { color:#A4C260 }
|
37
|
-
.CodeRay .s { color:#A4C260 }
|
38
|
-
.CodeRay .sy { color:#6C9CBD }
|
39
|
-
.CodeRay .ta { color:#E7BE69 }
|
40
|
-
.CodeRay .pc { color:#6C9CBD }
|
41
|
-
|
42
|
-
</style>
|
43
|
-
</head>
|
44
|
-
<body>
|
45
|
-
<div style="margin-left:auto;margin-right:auto;width:800px;">
|
46
|
-
<div class="CodeRay">
|
47
|
-
<div class="code"><pre><span class="c"># CStruct Examples</span>
|
48
|
-
require <span class="s"><span class="dl">'</span><span class="k">cstruct</span><span class="dl">'</span></span>
|
49
|
-
|
50
|
-
<span class="c"># example:</span>
|
51
|
-
<span class="c"># struct U in C\C++ (32-bit platform): </span>
|
52
|
-
<span class="c"># </span>
|
53
|
-
<span class="c"># struct U</span>
|
54
|
-
<span class="c"># {</span>
|
55
|
-
<span class="c"># union{</span>
|
56
|
-
<span class="c"># int x;</span>
|
57
|
-
<span class="c"># int y;</span>
|
58
|
-
<span class="c"># }value; /* values is anonymous union's instance */</span>
|
59
|
-
<span class="c"># }; </span>
|
60
|
-
|
61
|
-
<span class="c"># struct U in Ruby: </span>
|
62
|
-
<span class="r">class</span> <span class="cl">U</span> < <span class="co">CStruct</span>
|
63
|
-
union<span class="sy">:value</span> <span class="r">do</span>
|
64
|
-
int32<span class="sy">:x</span>
|
65
|
-
int32<span class="sy">:y</span>
|
66
|
-
<span class="r">end</span>
|
67
|
-
<span class="r">end</span>
|
68
|
-
|
69
|
-
<span class="c"># or like this (use brace):</span>
|
70
|
-
<span class="c"># class U < CStruct</span>
|
71
|
-
<span class="c"># union (:position) {</span>
|
72
|
-
<span class="c"># int32:x</span>
|
73
|
-
<span class="c"># int32:y </span>
|
74
|
-
<span class="c"># }</span>
|
75
|
-
<span class="c"># end</span>
|
76
|
-
|
77
|
-
|
78
|
-
<span class="c"># create a U's instance</span>
|
79
|
-
u = <span class="co">U</span>.new
|
80
|
-
|
81
|
-
<span class="c"># Notice! Assign is different from C language.</span>
|
82
|
-
<span class="c"># 'value' can not be omitted !</span>
|
83
|
-
u.value.x = <span class="i">10</span>
|
84
|
-
|
85
|
-
puts <span class="s"><span class="dl">"</span><span class="k">sizeof(U) = </span><span class="il"><span class="idl">#{</span><span class="co">U</span>.__size__<span class="idl">}</span></span><span class="dl">"</span></span> <span class="c"># "__size__" is alias of "size" . You may use 'size'.</span>
|
86
|
-
puts <span class="s"><span class="dl">"</span><span class="k">u.value.x = </span><span class="il"><span class="idl">#{</span>u.value.x <span class="idl">}</span></span><span class="k">, u.value.y = </span><span class="il"><span class="idl">#{</span>u.value.y <span class="idl">}</span></span><span class="dl">"</span></span>
|
87
|
-
|
88
|
-
|
89
|
-
</pre></div>
|
90
|
-
</div>
|
91
|
-
|
92
|
-
</div>
|
93
|
-
</body>
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
2
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
3
|
+
<head profile="http://gmpg.org/xfn/11" >
|
4
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
5
|
+
<title>CStruct:Examples:Anonymous union</title>
|
6
|
+
<style type="text/css">
|
7
|
+
|
8
|
+
.CodeRay {
|
9
|
+
background-color: #232323;
|
10
|
+
border: 1px solid black;
|
11
|
+
font-family: 'Courier New', 'Terminal', monospace;
|
12
|
+
color: #E6E0DB;
|
13
|
+
padding: 3px 5px;
|
14
|
+
overflow: auto;
|
15
|
+
font-size: 12px;
|
16
|
+
margin: 12px 0;
|
17
|
+
}
|
18
|
+
.CodeRay pre {
|
19
|
+
margin: 0px;
|
20
|
+
padding: 0px;
|
21
|
+
}
|
22
|
+
|
23
|
+
.CodeRay .an { color:#E7BE69 }
|
24
|
+
.CodeRay .c { color:#BC9358; font-style: italic; }
|
25
|
+
.CodeRay .ch { color:#509E4F }
|
26
|
+
.CodeRay .cl { color:#FFF }
|
27
|
+
.CodeRay .co { color:#FFF }
|
28
|
+
.CodeRay .fl { color:#A4C260 }
|
29
|
+
.CodeRay .fu { color:#FFC56D }
|
30
|
+
.CodeRay .gv { color:#D0CFFE }
|
31
|
+
.CodeRay .i { color:#A4C260 }
|
32
|
+
.CodeRay .il { background:#151515 }
|
33
|
+
.CodeRay .iv { color:#D0CFFE }
|
34
|
+
.CodeRay .pp { color:#E7BE69 }
|
35
|
+
.CodeRay .r { color:#CB7832 }
|
36
|
+
.CodeRay .rx { color:#A4C260 }
|
37
|
+
.CodeRay .s { color:#A4C260 }
|
38
|
+
.CodeRay .sy { color:#6C9CBD }
|
39
|
+
.CodeRay .ta { color:#E7BE69 }
|
40
|
+
.CodeRay .pc { color:#6C9CBD }
|
41
|
+
|
42
|
+
</style>
|
43
|
+
</head>
|
44
|
+
<body>
|
45
|
+
<div style="margin-left:auto;margin-right:auto;width:800px;">
|
46
|
+
<div class="CodeRay">
|
47
|
+
<div class="code"><pre><span class="c"># CStruct Examples</span>
|
48
|
+
require <span class="s"><span class="dl">'</span><span class="k">cstruct</span><span class="dl">'</span></span>
|
49
|
+
|
50
|
+
<span class="c"># example:</span>
|
51
|
+
<span class="c"># struct U in C\C++ (32-bit platform): </span>
|
52
|
+
<span class="c"># </span>
|
53
|
+
<span class="c"># struct U</span>
|
54
|
+
<span class="c"># {</span>
|
55
|
+
<span class="c"># union{</span>
|
56
|
+
<span class="c"># int x;</span>
|
57
|
+
<span class="c"># int y;</span>
|
58
|
+
<span class="c"># }value; /* values is anonymous union's instance */</span>
|
59
|
+
<span class="c"># }; </span>
|
60
|
+
|
61
|
+
<span class="c"># struct U in Ruby: </span>
|
62
|
+
<span class="r">class</span> <span class="cl">U</span> < <span class="co">CStruct</span>
|
63
|
+
union<span class="sy">:value</span> <span class="r">do</span>
|
64
|
+
int32<span class="sy">:x</span>
|
65
|
+
int32<span class="sy">:y</span>
|
66
|
+
<span class="r">end</span>
|
67
|
+
<span class="r">end</span>
|
68
|
+
|
69
|
+
<span class="c"># or like this (use brace):</span>
|
70
|
+
<span class="c"># class U < CStruct</span>
|
71
|
+
<span class="c"># union (:position) {</span>
|
72
|
+
<span class="c"># int32:x</span>
|
73
|
+
<span class="c"># int32:y </span>
|
74
|
+
<span class="c"># }</span>
|
75
|
+
<span class="c"># end</span>
|
76
|
+
|
77
|
+
|
78
|
+
<span class="c"># create a U's instance</span>
|
79
|
+
u = <span class="co">U</span>.new
|
80
|
+
|
81
|
+
<span class="c"># Notice! Assign is different from C language.</span>
|
82
|
+
<span class="c"># 'value' can not be omitted !</span>
|
83
|
+
u.value.x = <span class="i">10</span>
|
84
|
+
|
85
|
+
puts <span class="s"><span class="dl">"</span><span class="k">sizeof(U) = </span><span class="il"><span class="idl">#{</span><span class="co">U</span>.__size__<span class="idl">}</span></span><span class="dl">"</span></span> <span class="c"># "__size__" is alias of "size" . You may use 'size'.</span>
|
86
|
+
puts <span class="s"><span class="dl">"</span><span class="k">u.value.x = </span><span class="il"><span class="idl">#{</span>u.value.x <span class="idl">}</span></span><span class="k">, u.value.y = </span><span class="il"><span class="idl">#{</span>u.value.y <span class="idl">}</span></span><span class="dl">"</span></span>
|
87
|
+
|
88
|
+
|
89
|
+
</pre></div>
|
90
|
+
</div>
|
91
|
+
|
92
|
+
</div>
|
93
|
+
</body>
|