Module: Chinwag

Defined in:
lib/chinwag.rb,
ext/chinwag/rb_chinwag_ext.c

Defined Under Namespace

Classes: CWDict

Class Method Summary collapse

Class Method Details

.generate(*args) ⇒ Object

sync up module generation functions



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/chinwag/rb_chinwag_ext.c', line 32

VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
{
  cwdict_t* d;
  VALUE result;
  cw_t cw_type = get_cw_t_for_symbol(default_output_type);
  long min = NUM2LONG(default_min_output);
  long max = NUM2LONG(default_max_output);
  char* output, *e;

  // raise exception if passed wrong number of arguments
  if(argc > 4)
  {
    rb_raise(rb_eArgError, "wrong number of arguments (0..4)");
  }

  // do stuff with 'em
  if(argc == 0) Data_Get_Struct(default_dict, cwdict_t, d);

  if(argc >= 1) Data_Get_Struct(argv[0], cwdict_t, d);
  
  if(argc >= 2) cw_type = get_cw_t_for_symbol(argv[1]);

  if(argc >= 3)
  {
    Check_Type(argv[2], T_FIXNUM);
    long temp_a = FIX2LONG(argv[2]);
    min = temp_a; max = temp_a;
  }

  if(argc == 4)
  {
    Check_Type(argv[3], T_FIXNUM);
    max = FIX2LONG(argv[3]);
  }

  if(max < min)
  {
    rb_raise(rb_eException,"upper threshold must be more than lower "
    "threshold (min : %lu, max : %lu)", min, max);
  }

  if(min < 1 || max > 10000)
  {
    rb_raise(rb_eRangeError,"out of range (1..10000)");
  }

  if(!cwdict_valid(*d, &e))
  {
    rb_raise(rb_eException, "%s", e);
    free(e);
  }

  switch(cw_type)
  {
    case CW_LETTERS:
      output = chinwag(cw_type, min, max, *d);
      break;
    case CW_WORDS:
      output = chinwag(cw_type, min, max, *d);
      break;
    case CW_SENTENCES:
      output = chinwag(cw_type, min, max, *d);
      break;
    case CW_PARAGRAPHS:
      output = chinwag(cw_type, min, max, *d);
      break;
    default:
      rb_raise(rb_eTypeError, "invalid type (expected LETTERS, WORDS, "
      "SENTENCES, or PARAGRAPHS)");
      break;
  }

  result = rb_str_new2(output);
  // free(output);

  return result;
}

.set_default_dict(new) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'ext/chinwag/rb_chinwag_ext.c', line 112

VALUE m_set_d_dict(VALUE obj, VALUE new)
{
  VALUE original = c_cwdict_clone(default_dict);

  default_dict = c_cwdict_close(default_dict);
  default_dict = c_cwdict_clone(new);

  return original;
}

.set_default_max_output(num) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'ext/chinwag/rb_chinwag_ext.c', line 168

VALUE m_s_max(VALUE obj, VALUE num)
{
  VALUE original = default_max_output;

  Check_Type(num, T_FIXNUM);

  default_max_output = num;

  long min = NUM2LONG(default_min_output);
  long max = NUM2LONG(default_max_output);

  if(max < min)
  {
    rb_raise(rb_eException,"upper threshold must be more than lower "
    "threshold (min : %lu, max : %lu)", min, max);
  }

  if(min < 1 || max > 10000)
  {
    rb_raise(rb_eRangeError,"out of range (1..10000)");
  }

  return original;
}

.set_default_min_output(num) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'ext/chinwag/rb_chinwag_ext.c', line 143

VALUE m_s_min(VALUE obj, VALUE num)
{
  VALUE original = default_min_output;

  Check_Type(num, T_FIXNUM);

  default_min_output = num;

  long min = NUM2LONG(default_min_output);
  long max = NUM2LONG(default_max_output);

  if(max < min)
  {
    rb_raise(rb_eException,"upper threshold must be more than lower "
    "threshold (min : %lu, max : %lu)", min, max);
  }

  if(min < 1 || max > 10000)
  {
    rb_raise(rb_eRangeError,"out of range (1..10000)");
  }

  return original;
}

.set_default_type(sym) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/chinwag/rb_chinwag_ext.c', line 122

VALUE m_set_d_type(VALUE obj, VALUE sym)
{
  VALUE original = default_output_type;

  Check_Type(sym, T_SYMBOL);

  VALUE id = rb_to_id(sym);

  if(strcmp(rb_id2name(id), "letters") == 0) default_output_type = sym;
  else if(strcmp(rb_id2name(id), "words") == 0) default_output_type = sym;
  else if(strcmp(rb_id2name(id), "sentences") == 0)default_output_type=sym;
  else if(strcmp(rb_id2name(id), "paragraphs") == 0)default_output_type=sym;
  else
  {
    rb_raise(rb_eTypeError, "invalid type (expected :letters, :words, "
    ":sentences, or :paragraphs)");
  }

  return original;
}