jadtfmt 0.0.4-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ext/jadtfmt.c +215 -0
  2. data/lib/i386-mswin32/jadtfmt.so +0 -0
  3. metadata +55 -0
data/ext/jadtfmt.c ADDED
@@ -0,0 +1,215 @@
1
+ /* */
2
+ #ifdef _WIN32
3
+ __declspec(dllexport) void Init_jadtfmt();
4
+ #endif
5
+
6
+ #include <string.h>
7
+
8
+ #ifndef _WIN32
9
+ #include <alloca.h>
10
+ #else
11
+ #include <malloc.h>
12
+ #endif
13
+
14
+ #include "ruby.h"
15
+ #include "jadtfmt.h"
16
+ #include "jadtfmt_icu.h"
17
+
18
+ #define VERSION "0.0.4"
19
+
20
+ #define DEFAULT_INT(n) (NIL_P(n) ? 0 : NUM2INT(n))
21
+
22
+ static VALUE JapaneseDateFormat;
23
+ static VALUE pph_G, pph_N, pph_n, pph_E, pph_G_pattern, pph_N_pattern, pph_n_pattern, pph_E_pattern;
24
+
25
+ /* */
26
+ static VALUE jadtfmt_initialize(int argc, VALUE *argv, VALUE self) {
27
+ struct jadtfmt *p;
28
+ VALUE pattern, kcode;
29
+ char *s_pattern = NULL;
30
+ int i_pattern_len = 0;
31
+ const char *s_kcode;
32
+ char *s_codepage;
33
+
34
+ rb_scan_args(argc, argv, "02", &pattern, &kcode);
35
+
36
+ if (!NIL_P(pattern)) {
37
+ Check_Type(pattern, T_STRING);
38
+ s_pattern = StringValuePtr(pattern);
39
+ i_pattern_len = RSTRING(pattern)->len;
40
+ }
41
+
42
+ if (!NIL_P(kcode)) {
43
+ Check_Type(kcode, T_STRING);
44
+ s_kcode = (RSTRING(kcode)->len > 0) ? StringValuePtr(kcode) : "UTF8";
45
+ } else {
46
+ s_kcode = rb_get_kcode();
47
+ }
48
+
49
+ switch (s_kcode[0]) {
50
+ case 'S': case 's': s_codepage = "Shift_JIS"; break;
51
+ case 'E': case 'e': s_codepage = "EUC-JP"; break;
52
+ case 'U': case 'u': s_codepage = "UTF-8"; break;
53
+ default: s_codepage = "UTF-8"; break;
54
+ }
55
+
56
+ Data_Get_Struct(self, struct jadtfmt, p);
57
+ p->fmt = jadtfmt_icu_new(s_pattern, s_codepage);
58
+ p->pattern_len = i_pattern_len;
59
+ p->codepage = s_codepage;
60
+
61
+ return Qnil;
62
+ }
63
+
64
+ /* */
65
+ static VALUE jadtfmt_format(VALUE self, VALUE src) {
66
+ struct jadtfmt *p;
67
+ VALUE year, month, day, hour, min, sec;
68
+ char *buf;
69
+
70
+ if (!rb_obj_is_instance_of(src, rb_cTime)) {
71
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Time)", rb_class2name(CLASS_OF(src)));
72
+ }
73
+
74
+ year = rb_funcall(src, rb_intern("year"), 0);
75
+ month = rb_funcall(src, rb_intern("month"), 0);
76
+ day = rb_funcall(src, rb_intern("day"), 0);
77
+ hour = rb_funcall(src, rb_intern("hour"), 0);
78
+ min = rb_funcall(src, rb_intern("min"), 0);
79
+ sec = rb_funcall(src, rb_intern("sec"), 0);
80
+
81
+ Data_Get_Struct(self, struct jadtfmt, p);
82
+
83
+ if (p->pattern_len > 0) {
84
+ int len = p->pattern_len * 3 + 16;
85
+ buf = alloca(len);
86
+ memset(buf, 0, len);
87
+ } else {
88
+ rb_raise(rb_eRuntimeError, "pattern is not set");
89
+ }
90
+
91
+ jadtfmt_icu_format(p->fmt, NUM2INT(year), NUM2INT(month), NUM2INT(day), NUM2INT(hour), NUM2INT(min), NUM2INT(sec), p->codepage, buf);
92
+
93
+ return rb_str_new2(buf);
94
+ }
95
+
96
+ /* */
97
+ static VALUE jadtfmt_format_attrs(int argc, VALUE *argv, VALUE self) {
98
+ struct jadtfmt *p;
99
+ VALUE year, month, day, hour, min, sec;
100
+ char *buf;
101
+
102
+ rb_scan_args(argc, argv, "15", &year, &month, &day, &hour, &min, &sec);
103
+
104
+ Data_Get_Struct(self, struct jadtfmt, p);
105
+
106
+ if (p->pattern_len > 0) {
107
+ int len = p->pattern_len * 3 + 16;
108
+ buf = alloca(len);
109
+ memset(buf, 0, len);
110
+ } else {
111
+ rb_raise(rb_eRuntimeError, "pattern is not set");
112
+ }
113
+
114
+ jadtfmt_icu_format(p->fmt, NUM2INT(year), DEFAULT_INT(month), DEFAULT_INT(day), DEFAULT_INT(hour), DEFAULT_INT(min), DEFAULT_INT(sec), p->codepage, buf);
115
+
116
+ return rb_str_new2(buf);
117
+ }
118
+
119
+ /* */
120
+ static VALUE jadtfmt_apply_pattern(VALUE self, VALUE pattern) {
121
+ struct jadtfmt *p;
122
+
123
+ Check_Type(pattern, T_STRING);
124
+ Data_Get_Struct(self, struct jadtfmt, p);
125
+ jadtfmt_icu_apply_pattern(p->fmt, StringValuePtr(pattern), p->codepage);
126
+ p->pattern_len = RSTRING(pattern)->len;
127
+
128
+ return Qnil;
129
+ }
130
+
131
+ /* */
132
+ static VALUE jadtfmt_Time_jadtfmt(VALUE self, VALUE pattern) {
133
+ VALUE jadtfmt = rb_funcall(JapaneseDateFormat, rb_intern("new"), 1, pattern);
134
+ return rb_funcall(jadtfmt, rb_intern("format"), 1, self);
135
+ }
136
+
137
+ /* */
138
+ static VALUE jadtfmt_Time_jastrftime(VALUE self, VALUE format) {
139
+ VALUE jadtfmt, replace;
140
+
141
+ Check_Type(format, T_STRING);
142
+ jadtfmt = rb_funcall(JapaneseDateFormat, rb_intern("new"), 0);
143
+
144
+ if (rb_funcall(format, rb_intern("include?"), 1, pph_G)) {
145
+ rb_funcall(jadtfmt, rb_intern("apply_pattern"), 1, pph_G_pattern);
146
+ replace = rb_funcall(jadtfmt, rb_intern("format"), 1, self);
147
+ format = rb_funcall(format, rb_intern("gsub"), 2, pph_G, replace);
148
+ }
149
+
150
+ if (rb_funcall(format, rb_intern("include?"), 1, pph_N)) {
151
+ rb_funcall(jadtfmt, rb_intern("apply_pattern"), 1, pph_N_pattern);
152
+ replace = rb_funcall(jadtfmt, rb_intern("format"), 1, self);
153
+ format = rb_funcall(format, rb_intern("gsub"), 2, pph_N, replace);
154
+ }
155
+
156
+ if (rb_funcall(format, rb_intern("include?"), 1, pph_n)) {
157
+ rb_funcall(jadtfmt, rb_intern("apply_pattern"), 1, pph_n_pattern);
158
+ replace = rb_funcall(jadtfmt, rb_intern("format"), 1, self);
159
+ format = rb_funcall(format, rb_intern("gsub"), 2, pph_n, replace);
160
+ }
161
+
162
+ if (rb_funcall(format, rb_intern("include?"), 1, pph_E)) {
163
+ rb_funcall(jadtfmt, rb_intern("apply_pattern"), 1, pph_E_pattern);
164
+ replace = rb_funcall(jadtfmt, rb_intern("format"), 1, self);
165
+ format = rb_funcall(format, rb_intern("gsub"), 2, pph_E, replace);
166
+ }
167
+
168
+ return rb_funcall(self, rb_intern("strftime"), 1, format);
169
+ }
170
+
171
+ static void jadtfmt_free(struct jadtfmt *p) {
172
+ jadtfmt_icu_delete(p->fmt);
173
+ xfree(p);
174
+ }
175
+
176
+ static VALUE jadtfmt_alloc(VALUE klass) {
177
+ struct jadtfmt *p = ALLOC(struct jadtfmt);
178
+
179
+ p->fmt = NULL;
180
+ p->pattern_len = 0;
181
+ p->codepage = NULL;
182
+
183
+ return Data_Wrap_Struct(klass, 0, jadtfmt_free, p);
184
+ }
185
+
186
+ void Init_jadtfmt() {
187
+ JapaneseDateFormat = rb_define_class("JapaneseDateFormat", rb_cObject);
188
+ rb_define_const(JapaneseDateFormat, "VERSION", rb_str_new2(VERSION));
189
+ rb_define_alloc_func(JapaneseDateFormat, jadtfmt_alloc);
190
+ rb_define_private_method(JapaneseDateFormat, "initialize", jadtfmt_initialize, -1);
191
+ rb_define_method(JapaneseDateFormat, "format", jadtfmt_format, 1);
192
+ rb_define_method(JapaneseDateFormat, "format_attrs", jadtfmt_format_attrs, -1);
193
+ rb_define_method(JapaneseDateFormat, "apply_pattern", jadtfmt_apply_pattern, 1);
194
+
195
+ rb_define_method(rb_cTime, "jadtfmt", jadtfmt_Time_jadtfmt, 1);
196
+ rb_define_method(rb_cTime, "jastrftime", jadtfmt_Time_jastrftime, 1);
197
+
198
+ pph_G = rb_str_new2("%G");
199
+ pph_N = rb_str_new2("%N");
200
+ pph_n = rb_str_new2("%n");
201
+ pph_E = rb_str_new2("%E");
202
+ pph_G_pattern = rb_str_new2("G");
203
+ pph_N_pattern = rb_str_new2("yy");
204
+ pph_n_pattern = rb_str_new2("y");
205
+ pph_E_pattern = rb_str_new2("E");
206
+
207
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_G", pph_G);
208
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_N", pph_N);
209
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_n", pph_n);
210
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_E", pph_E);
211
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_G_pattern", pph_G_pattern);
212
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_N_pattern", pph_N_pattern);
213
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_n_pattern", pph_n_pattern);
214
+ rb_define_class_variable(JapaneseDateFormat, "@@pph_W_pattern", pph_E_pattern);
215
+ }
Binary file
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jadtfmt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: mswin32
6
+ authors:
7
+ - winebarrel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-18 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: sgwr_dts@yahoo.co.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - ext/jadtfmt.c
24
+ files:
25
+ - lib/i386-mswin32/jadtfmt.so
26
+ - ext/jadtfmt.c
27
+ has_rdoc: true
28
+ homepage: http://jadtfmt.rubyforge.org
29
+ post_install_message:
30
+ rdoc_options:
31
+ - --title
32
+ - JapaneseDateFormat - formatting a Time to the japanese time string using ICU.
33
+ require_paths:
34
+ - lib/i386-mswin32
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project: jadtfmt
50
+ rubygems_version: 1.0.1
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: formatting a Time to the japanese time string using ICU.
54
+ test_files: []
55
+