chinwag 0.1.5 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  #include "rb_chinwag_ext.h"
2
2
 
3
3
  VALUE m_chinwag;
4
- VALUE c_cw_dict;
4
+ VALUE c_cwdict;
5
5
 
6
6
  VALUE default_dict;
7
7
  VALUE default_output_type;
@@ -31,7 +31,7 @@ cw_t get_cw_t_for_symbol(VALUE symbol)
31
31
 
32
32
  VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
33
33
  {
34
- dict_t* d;
34
+ cwdict_t* d;
35
35
  VALUE result;
36
36
  cw_t cw_type = get_cw_t_for_symbol(default_output_type);
37
37
  long min = NUM2LONG(default_min_output);
@@ -45,9 +45,9 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
45
45
  }
46
46
 
47
47
  // do stuff with 'em
48
- if(argc == 0) Data_Get_Struct(default_dict, dict_t, d);
48
+ if(argc == 0) Data_Get_Struct(default_dict, cwdict_t, d);
49
49
 
50
- if(argc >= 1) Data_Get_Struct(argv[0], dict_t, d);
50
+ if(argc >= 1) Data_Get_Struct(argv[0], cwdict_t, d);
51
51
 
52
52
  if(argc >= 2) cw_type = get_cw_t_for_symbol(argv[1]);
53
53
 
@@ -75,7 +75,7 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
75
75
  rb_raise(rb_eRangeError,"out of range (1..10000)");
76
76
  }
77
77
 
78
- if(!dict_valid(*d, &e))
78
+ if(!cwdict_valid(*d, &e))
79
79
  {
80
80
  rb_raise(rb_eException, "%s", e);
81
81
  free(e);
@@ -107,14 +107,14 @@ VALUE m_cw_generate(int argc, VALUE* argv, VALUE obj)
107
107
  return result;
108
108
  }
109
109
 
110
- VALUE c_cw_dict_clone(VALUE obj);
111
- VALUE c_cw_dict_close(VALUE obj);
110
+ VALUE c_cwdict_clone(VALUE obj);
111
+ VALUE c_cwdict_close(VALUE obj);
112
112
  VALUE m_set_d_dict(VALUE obj, VALUE new)
113
113
  {
114
- VALUE original = c_cw_dict_clone(default_dict);
114
+ VALUE original = c_cwdict_clone(default_dict);
115
115
 
116
- default_dict = c_cw_dict_close(default_dict);
117
- default_dict = c_cw_dict_clone(new);
116
+ default_dict = c_cwdict_close(default_dict);
117
+ default_dict = c_cwdict_clone(new);
118
118
 
119
119
  return original;
120
120
  }
@@ -190,17 +190,17 @@ VALUE m_s_max(VALUE obj, VALUE num)
190
190
  return original;
191
191
  }
192
192
 
193
- static void c_cw_dict_free(void* dict)
193
+ static void c_cwdict_free(void* dict)
194
194
  {
195
- dict_t* d = (dict_t*)dict;
196
- if(d->drows && d->count > 0) close_dict(*d);
195
+ cwdict_t* d = (cwdict_t*)dict;
196
+ if(d->drows && d->count > 0) cwdict_close(*d);
197
197
  }
198
198
 
199
- VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
199
+ VALUE c_cwdict_open(int argc, VALUE* argv, VALUE obj)
200
200
  {
201
201
  VALUE file_pathname;
202
202
  FILE* file_ptr = NULL;
203
- dict_t d = open_dict(), path_parts, file_parts;
203
+ cwdict_t d = cwdict_open(), path_parts, file_parts;
204
204
  char* tkns_ptr = NULL; char* name_ptr = NULL;
205
205
  char* path_name = NULL; char* file_name = NULL;
206
206
  char* name = NULL; bool used_file = false; char* file_buffer = NULL;
@@ -226,7 +226,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
226
226
 
227
227
  if(include(path_name, "/") || include(path_name, "\\"))
228
228
  {
229
- path_parts = split(path_name, "/\\");
229
+ path_parts = split_into_cwdict(path_name, "/\\");
230
230
 
231
231
  last_drow = path_parts.count - 1;
232
232
  last_word = path_parts.drows[last_drow].count - 1;
@@ -236,7 +236,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
236
236
  strcpy(file_name, path_parts.drows[last_drow].words[last_word]);
237
237
  file_name[len] = '\0';
238
238
 
239
- close_dict(path_parts);
239
+ cwdict_close(path_parts);
240
240
  }
241
241
  else
242
242
  {
@@ -247,7 +247,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
247
247
 
248
248
  if(include(file_name, "."))
249
249
  {
250
- file_parts = split(file_name, ".");
250
+ file_parts = split_into_cwdict(file_name, ".");
251
251
 
252
252
  size_t len = strlen(file_parts.drows[0].words[0]);
253
253
 
@@ -255,7 +255,7 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
255
255
  strcpy(name, file_parts.drows[0].words[0]);
256
256
  name[len] = '\0';
257
257
 
258
- close_dict(file_parts);
258
+ cwdict_close(file_parts);
259
259
  }
260
260
  else
261
261
  {
@@ -282,26 +282,26 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
282
282
  }
283
283
 
284
284
  // check if tkns references existing, embedded dictionary...
285
- if(!tkns_ptr && !name_ptr && !used_file) d = open_dict();
285
+ if(!tkns_ptr && !name_ptr && !used_file) d = cwdict_open();
286
286
  else if(tkns_ptr && !used_file)
287
287
  {
288
288
  if(strcmp(tkns_ptr, "seussian") == 0)
289
289
  {
290
290
  if(!name_ptr)
291
- d = open_dict_with_name_and_tokens("seussian",dict_seuss,DELIMITERS);
291
+ d=cwdict_open_with_name_and_tokens("seussian",dict_seuss,DELIMITERS);
292
292
  else
293
- d = open_dict_with_name_and_tokens(name_ptr,dict_seuss,DELIMITERS);
293
+ d=cwdict_open_with_name_and_tokens(name_ptr,dict_seuss,DELIMITERS);
294
294
  }
295
295
  else if(strcmp(tkns_ptr, "latin") == 0)
296
296
  {
297
297
  if(!name_ptr)
298
- d = open_dict_with_name_and_tokens("latin", dict_latin,DELIMITERS);
298
+ d=cwdict_open_with_name_and_tokens("latin", dict_latin,DELIMITERS);
299
299
  else
300
- d = open_dict_with_name_and_tokens(name_ptr, dict_latin,DELIMITERS);
300
+ d=cwdict_open_with_name_and_tokens(name_ptr, dict_latin,DELIMITERS);
301
301
  }
302
302
  }
303
303
  // ...else, if just a name was passed...
304
- else if(name_ptr && !used_file) d = open_dict_with_name(name_ptr);
304
+ else if(name_ptr && !used_file) d = cwdict_open_with_name(name_ptr);
305
305
  // ...else, see if file exists by passed name...
306
306
  else if(used_file && name && file_ptr)
307
307
  {
@@ -312,52 +312,52 @@ VALUE c_cw_dict_open(int argc, VALUE* argv, VALUE obj)
312
312
  file_name);
313
313
  }
314
314
 
315
- d = open_dict_with_name_and_tokens(name, file_buffer, DELIMITERS);
315
+ d=cwdict_open_with_name_and_tokens(name, file_buffer, DELIMITERS);
316
316
 
317
317
  free(file_buffer);
318
318
  }
319
319
  // ...else, return a blank dictionary
320
320
 
321
321
  // create a dictionary pointer
322
- dict_t* d_ptr = (dict_t*)malloc(sizeof(dict_t));
322
+ cwdict_t* d_ptr = (cwdict_t*)malloc(sizeof(cwdict_t));
323
323
  *d_ptr = d;
324
324
 
325
325
  if(name) free(name);
326
326
  if(file_name) free(file_name);
327
327
 
328
- return Data_Wrap_Struct(c_cw_dict, 0, c_cw_dict_free, d_ptr);
328
+ return Data_Wrap_Struct(c_cwdict, 0, c_cwdict_free, d_ptr);
329
329
  }
330
330
 
331
- VALUE c_cw_dict_close(VALUE obj)
331
+ VALUE c_cwdict_close(VALUE obj)
332
332
  {
333
- dict_t* d;
334
- dict_t empty;
333
+ cwdict_t* d;
334
+ cwdict_t empty;
335
335
 
336
336
  // get original pointer from Ruby VM and close
337
- Data_Get_Struct(obj, dict_t, d);
337
+ Data_Get_Struct(obj, cwdict_t, d);
338
338
 
339
- if(d->drows && d->count > 0) { *d = close_dict(*d); }
339
+ if(d->drows && d->count > 0) { *d = cwdict_close(*d); }
340
340
 
341
341
  return obj;
342
342
  }
343
343
 
344
- VALUE c_cw_dict_name_g(VALUE obj)
344
+ VALUE c_cwdict_name_g(VALUE obj)
345
345
  {
346
- dict_t* d;
346
+ cwdict_t* d;
347
347
 
348
348
  // get original pointer from Ruby VM
349
- Data_Get_Struct(obj, dict_t, d);
349
+ Data_Get_Struct(obj, cwdict_t, d);
350
350
 
351
351
  if(d->name && strlen(d->name) > 0) return rb_str_new2(d->name);
352
352
  return rb_str_new2("");
353
353
  }
354
354
 
355
- VALUE c_cw_dict_name_s(VALUE obj, VALUE name)
355
+ VALUE c_cwdict_name_s(VALUE obj, VALUE name)
356
356
  {
357
- dict_t* d; long len = RSTRING_LEN(name);
357
+ cwdict_t* d; long len = RSTRING_LEN(name);
358
358
 
359
359
  // get original pointer from Ruby VM
360
- Data_Get_Struct(obj, dict_t, d);
360
+ Data_Get_Struct(obj, cwdict_t, d);
361
361
 
362
362
  d->name = (char*)malloc(len + 1);
363
363
  strcpy(d->name, StringValueCStr(name));
@@ -366,155 +366,235 @@ VALUE c_cw_dict_name_s(VALUE obj, VALUE name)
366
366
  return obj;
367
367
  }
368
368
 
369
- VALUE c_cw_dict_length(VALUE obj)
369
+ VALUE c_cwdict_length(VALUE obj)
370
370
  {
371
- dict_t* d;
371
+ cwdict_t* d;
372
372
 
373
373
  // get original pointer from Ruby VM
374
- Data_Get_Struct(obj, dict_t, d);
374
+ Data_Get_Struct(obj, cwdict_t, d);
375
375
 
376
376
  if(d->count == 0) return INT2NUM(0);
377
- return LONG2NUM(total_dict(*d));
377
+ return LONG2NUM(cwdict_length(*d));
378
378
  }
379
379
 
380
- VALUE c_cw_dict_join(int argc, VALUE* argv, VALUE obj)
380
+ VALUE c_cwdict_enum_length(VALUE obj, VALUE args, VALUE eobj)
381
381
  {
382
- dict_t* d;
382
+ return c_cwdict_length(obj);
383
+ }
384
+
385
+ VALUE c_cwdict_join(int argc, VALUE* argv, VALUE obj)
386
+ {
387
+ cwdict_t* d;
383
388
 
384
389
  // raise exception if passed wrong number of arguments
385
390
  if(argc > 1) rb_raise(rb_eArgError, "wrong number of arguments");
386
391
  if(argc == 1) Check_Type(argv[0], T_STRING);
387
392
 
388
393
  // get original pointer from Ruby VM
389
- Data_Get_Struct(obj, dict_t, d);
394
+ Data_Get_Struct(obj, cwdict_t, d);
390
395
 
391
- if(argc == 0) return rb_str_new2(join_dict(*d, " "));
392
- return rb_str_new2(join_dict(*d, StringValueCStr(argv[0])));
396
+ if(argc == 0) return rb_str_new2(cwdict_join(*d, " "));
397
+ return rb_str_new2(cwdict_join(*d, StringValueCStr(argv[0])));
393
398
  }
394
399
 
395
- VALUE c_cw_dict_clone(VALUE obj)
400
+ VALUE c_cwdict_clone(VALUE obj)
396
401
  {
397
- dict_t* d, *new_p;
402
+ cwdict_t* d, *new_p;
398
403
  VALUE new;
399
404
 
400
405
  // open new dict for return value
401
406
  VALUE args[] = { rb_str_new2(""), rb_str_new2("") };
402
- new = c_cw_dict_open(2, args, new);
407
+ new = c_cwdict_open(2, args, new);
403
408
 
404
409
  // get original pointers from Ruby VM
405
- Data_Get_Struct(obj, dict_t, d);
406
- Data_Get_Struct(new, dict_t, new_p);
410
+ Data_Get_Struct(obj, cwdict_t, d);
411
+ Data_Get_Struct(new, cwdict_t, new_p);
407
412
 
408
413
  // get a copy of the original dictionary
409
- *new_p = deep_copy_dict(*d);
414
+ *new_p = cwdict_clone(*d);
410
415
 
411
416
  return new;
412
417
  }
413
418
 
414
- VALUE c_cw_dict_sample(VALUE obj)
419
+ VALUE c_cwdict_sample(VALUE obj)
415
420
  {
416
- dict_t* d;
421
+ cwdict_t* d;
417
422
 
418
423
  // get original pointer from Ruby VM
419
- Data_Get_Struct(obj, dict_t, d);
424
+ Data_Get_Struct(obj, cwdict_t, d);
425
+
426
+ return rb_str_new2(cwdict_sample(*d));
427
+ }
428
+
429
+ VALUE c_cwdict_each(VALUE obj)
430
+ {
431
+ cwdict_t *d;
432
+
433
+ // get original pointers from Ruby VM
434
+ Data_Get_Struct(obj, cwdict_t, d);
435
+
436
+ RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
437
+
438
+ for(U32 i = 0; i != d->count; ++i)
439
+ {
440
+ for(U32 j = 0; j != d->drows[i].count; ++j)
441
+ {
442
+ rb_yield(rb_str_new2(d->drows[i].words[j]));
443
+ }
444
+ }
445
+
446
+ return obj;
447
+ }
448
+
449
+ VALUE c_cwdict_each_index(VALUE obj)
450
+ {
451
+ long num = 0; cwdict_t *d;
452
+
453
+ // get original pointers from Ruby VM
454
+ Data_Get_Struct(obj, cwdict_t, d);
455
+
456
+ RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
457
+
458
+ for(U32 i = 0; i != d->count; ++i)
459
+ {
460
+ for(U32 j = 0; j != d->drows[i].count; ++j)
461
+ {
462
+ rb_yield(LONG2NUM(++num));
463
+ }
464
+ }
465
+
466
+ return obj;
467
+ }
468
+
469
+ VALUE c_cwdict_map_s(VALUE obj);
470
+ VALUE c_cwdict_map(VALUE obj)
471
+ {
472
+ VALUE new;
473
+
474
+ // get a new copy of the original dict
475
+ new = c_cwdict_clone(obj);
476
+ new = c_cwdict_map_s(new);
477
+
478
+ return new;
479
+ }
420
480
 
421
- return rb_str_new2(sample_dict(*d));
481
+ VALUE c_cwdict_map_s(VALUE obj)
482
+ {
483
+ cwdict_t *d;
484
+
485
+ // get original pointers from Ruby VM
486
+ Data_Get_Struct(obj, cwdict_t, d);
487
+
488
+ for(U32 i = 0; i != d->count; ++i)
489
+ {
490
+ for(U32 j = 0; j != d->drows[i].count; ++j)
491
+ {
492
+ VALUE ref = rb_yield(rb_str_new2(d->drows[i].words[j]));
493
+ long len = RSTRING_LEN(ref);
494
+
495
+ d->drows[i].words[j] = (char*)realloc(d->drows[i].words[j], len + 1);
496
+ strcpy(d->drows[i].words[j], StringValueCStr(ref));
497
+ d->drows[i].words[j][len] = '\0';
498
+ }
499
+ }
500
+
501
+ return obj;
422
502
  }
423
503
 
424
- VALUE c_cw_dict_sort(VALUE obj)
504
+ VALUE c_cwdict_sort(VALUE obj)
425
505
  {
426
- dict_t* d;
506
+ cwdict_t* d;
427
507
  VALUE new;
428
508
 
429
509
  // get a new copy of the original dict
430
- new = c_cw_dict_clone(obj);
510
+ new = c_cwdict_clone(obj);
431
511
 
432
512
  // get original pointer from Ruby VM
433
- Data_Get_Struct(new, dict_t, d);
513
+ Data_Get_Struct(new, cwdict_t, d);
434
514
 
435
- *d = bubble_dict(*d);
515
+ *d = cwdict_sort(*d);
436
516
 
437
517
  return new;
438
518
  }
439
519
 
440
- VALUE c_cw_dict_prune(VALUE obj)
520
+ VALUE c_cwdict_prune(VALUE obj)
441
521
  {
442
- dict_t* d;
522
+ cwdict_t* d;
443
523
  VALUE new;
444
524
 
445
525
  // get a new copy of the original dict
446
- new = c_cw_dict_clone(obj);
526
+ new = c_cwdict_clone(obj);
447
527
 
448
528
  // get original pointer from Ruby VM
449
- Data_Get_Struct(new, dict_t, d);
529
+ Data_Get_Struct(new, cwdict_t, d);
450
530
 
451
- *d = prune_dict(*d, false);
531
+ *d = cwdict_prune(*d, false);
452
532
 
453
533
  return new;
454
534
  }
455
535
 
456
- VALUE c_cw_dict_clean(VALUE obj)
536
+ VALUE c_cwdict_clean(VALUE obj)
457
537
  {
458
- dict_t* d;
538
+ cwdict_t* d;
459
539
  VALUE new;
460
540
 
461
541
  // get a new copy of the original dict
462
- new = c_cw_dict_clone(obj);
542
+ new = c_cwdict_clone(obj);
463
543
 
464
544
  // get original pointer from Ruby VM
465
- Data_Get_Struct(new, dict_t, d);
545
+ Data_Get_Struct(new, cwdict_t, d);
466
546
 
467
- *d = prune_dict(*d, true);
547
+ *d = cwdict_prune(*d, true);
468
548
 
469
549
  return new;
470
550
  }
471
551
 
472
- VALUE c_cw_dict_sort_s(VALUE obj)
552
+ VALUE c_cwdict_sort_s(VALUE obj)
473
553
  {
474
- dict_t* d;
554
+ cwdict_t* d;
475
555
 
476
556
  // get original pointer from Ruby VM
477
- Data_Get_Struct(obj, dict_t, d);
557
+ Data_Get_Struct(obj, cwdict_t, d);
478
558
 
479
- *d = bubble_dict(*d);
559
+ *d = cwdict_sort(*d);
480
560
 
481
561
  return obj;
482
562
  }
483
563
 
484
- VALUE c_cw_dict_prune_s(VALUE obj)
564
+ VALUE c_cwdict_prune_s(VALUE obj)
485
565
  {
486
- dict_t* d;
566
+ cwdict_t* d;
487
567
 
488
568
  // get original pointer from Ruby VM
489
- Data_Get_Struct(obj, dict_t, d);
569
+ Data_Get_Struct(obj, cwdict_t, d);
490
570
 
491
- *d = prune_dict(*d, false);
571
+ *d = cwdict_prune(*d, false);
492
572
 
493
573
  return obj;
494
574
  }
495
575
 
496
- VALUE c_cw_dict_clean_s(VALUE obj)
576
+ VALUE c_cwdict_clean_s(VALUE obj)
497
577
  {
498
- dict_t* d;
578
+ cwdict_t* d;
499
579
 
500
580
  // get original pointer from Ruby VM
501
- Data_Get_Struct(obj, dict_t, d);
581
+ Data_Get_Struct(obj, cwdict_t, d);
502
582
 
503
- *d = prune_dict(*d, true);
583
+ *d = cwdict_prune(*d, true);
504
584
 
505
585
  return obj;
506
586
  }
507
587
 
508
- VALUE c_cw_dict_validate_s(VALUE obj)
588
+ VALUE c_cwdict_validate_s(VALUE obj)
509
589
  {
510
590
  char* e;
511
- dict_t* d;
591
+ cwdict_t* d;
512
592
 
513
593
  // get original pointer from Ruby VM
514
- Data_Get_Struct(obj, dict_t, d);
594
+ Data_Get_Struct(obj, cwdict_t, d);
515
595
 
516
596
  // handle invalid state first (for error handling's sake)
517
- if(!dict_valid(*d, &e))
597
+ if(!cwdict_valid(*d, &e))
518
598
  {
519
599
  rb_raise(rb_eException, "%s", e);
520
600
  free(e);
@@ -523,27 +603,27 @@ VALUE c_cw_dict_validate_s(VALUE obj)
523
603
  return obj;
524
604
  }
525
605
 
526
- VALUE c_cw_dict_named_q(VALUE obj)
606
+ VALUE c_cwdict_named_q(VALUE obj)
527
607
  {
528
- dict_t* d;
608
+ cwdict_t* d;
529
609
 
530
610
  // get original pointer from Ruby VM
531
- Data_Get_Struct(obj, dict_t, d);
611
+ Data_Get_Struct(obj, cwdict_t, d);
532
612
 
533
613
  if(d->name && strlen(d->name) > 0) return Qtrue;
534
614
  return Qfalse;
535
615
  }
536
616
 
537
- VALUE c_cw_dict_valid_q(VALUE obj)
617
+ VALUE c_cwdict_valid_q(VALUE obj)
538
618
  {
539
619
  char* e;
540
- dict_t* d;
620
+ cwdict_t* d;
541
621
 
542
622
  // get original pointer from Ruby VM
543
- Data_Get_Struct(obj, dict_t, d);
623
+ Data_Get_Struct(obj, cwdict_t, d);
544
624
 
545
625
  // handle invalid state first (for error handling's sake)
546
- if(!dict_valid(*d, &e))
626
+ if(!cwdict_valid(*d, &e))
547
627
  {
548
628
  rb_raise(rb_eException, "%s", e);
549
629
  free(e);
@@ -554,47 +634,47 @@ VALUE c_cw_dict_valid_q(VALUE obj)
554
634
  return Qtrue;
555
635
  }
556
636
 
557
- VALUE c_cw_dict_sorted_q(VALUE obj)
637
+ VALUE c_cwdict_sorted_q(VALUE obj)
558
638
  {
559
- dict_t* d;
639
+ cwdict_t* d;
560
640
 
561
641
  // get original pointer from Ruby VM
562
- Data_Get_Struct(obj, dict_t, d);
642
+ Data_Get_Struct(obj, cwdict_t, d);
563
643
 
564
644
  if(d->sorted) return Qtrue;
565
645
  return Qfalse;
566
646
  }
567
647
 
568
- VALUE c_cw_dict_include_q(VALUE obj, VALUE string)
648
+ VALUE c_cwdict_include_q(VALUE obj, VALUE string)
569
649
  {
570
- dict_t* d;
650
+ cwdict_t* d;
571
651
 
572
652
  // get original pointer from Ruby VM
573
- Data_Get_Struct(obj, dict_t, d);
653
+ Data_Get_Struct(obj, cwdict_t, d);
574
654
 
575
- if(dict_include(*d, StringValueCStr(string))) return Qtrue;
655
+ if(cwdict_include(*d, StringValueCStr(string))) return Qtrue;
576
656
  return Qfalse;
577
657
  }
578
658
 
579
- VALUE c_cw_dict_exclude_q(VALUE obj, VALUE string)
659
+ VALUE c_cwdict_exclude_q(VALUE obj, VALUE string)
580
660
  {
581
- dict_t* d;
661
+ cwdict_t* d;
582
662
 
583
663
  // get original pointer from Ruby VM
584
- Data_Get_Struct(obj, dict_t, d);
664
+ Data_Get_Struct(obj, cwdict_t, d);
585
665
 
586
- if(dict_exclude(*d, StringValueCStr(string))) return Qtrue;
666
+ if(cwdict_exclude(*d, StringValueCStr(string))) return Qtrue;
587
667
  return Qfalse;
588
668
  }
589
669
 
590
- VALUE c_cw_dict_inspect(VALUE obj)
670
+ VALUE c_cwdict_inspect(VALUE obj)
591
671
  {
592
- dict_t* dict; VALUE str;
672
+ cwdict_t* dict; VALUE str;
593
673
  size_t count = 0; int multiplier = 1; int word_len = 0;
594
674
  char* result = (char*)malloc(LARGE_BUFFER * multiplier + 1);
595
675
 
596
676
  // get original pointer from Ruby VM
597
- Data_Get_Struct(obj, dict_t, dict);
677
+ Data_Get_Struct(obj, cwdict_t, dict);
598
678
 
599
679
  // add opening delimiter
600
680
  strcpy(result, "["); ++count;
@@ -657,29 +737,29 @@ VALUE c_cw_dict_inspect(VALUE obj)
657
737
  return str;
658
738
  }
659
739
 
660
- VALUE c_cw_dict_to_s(VALUE obj)
740
+ VALUE c_cwdict_to_s(VALUE obj)
661
741
  {
662
- dict_t* dict;
742
+ cwdict_t* dict;
663
743
  size_t count = 0; int multiplier = 1;
664
744
 
665
745
  // get original pointer from Ruby VM
666
- Data_Get_Struct(obj, dict_t, dict);
746
+ Data_Get_Struct(obj, cwdict_t, dict);
667
747
 
668
748
  // if(dict->drows && dict->count > 0) return Qnil;
669
- return c_cw_dict_inspect(obj);
749
+ return c_cwdict_inspect(obj);
670
750
  }
671
751
 
672
- VALUE c_cw_dict_append_op(VALUE obj, VALUE addend)
752
+ VALUE c_cwdict_append_op(VALUE obj, VALUE addend)
673
753
  {
674
- dict_t* d;
754
+ cwdict_t* d;
675
755
 
676
756
  // get original pointer from Ruby VM
677
- Data_Get_Struct(obj, dict_t, d);
757
+ Data_Get_Struct(obj, cwdict_t, d);
678
758
 
679
759
  switch(TYPE(addend))
680
760
  {
681
761
  case T_STRING:
682
- *d = place_word_in_dict_strict(*d, StringValueCStr(addend));
762
+ *d = cwdict_place_word_strict(*d, StringValueCStr(addend));
683
763
  break;
684
764
  case T_ARRAY:
685
765
  for(long i = 0; i != RARRAY_LEN(addend); ++i)
@@ -694,7 +774,7 @@ VALUE c_cw_dict_append_op(VALUE obj, VALUE addend)
694
774
  VALUE entry = rb_ary_entry(addend, i);
695
775
  char* entry_str = StringValueCStr(entry);
696
776
 
697
- *d = place_word_in_dict_strict(*d, entry_str);
777
+ *d = cwdict_place_word_strict(*d, entry_str);
698
778
  }
699
779
 
700
780
  break;
@@ -703,46 +783,46 @@ VALUE c_cw_dict_append_op(VALUE obj, VALUE addend)
703
783
  break;
704
784
  }
705
785
 
706
- if(d->sorted) return c_cw_dict_sort(obj);
786
+ if(d->sorted) return c_cwdict_sort(obj);
707
787
  return obj;
708
788
  }
709
789
 
710
- VALUE c_cw_dict_add_op(VALUE obj, VALUE addend)
790
+ VALUE c_cwdict_add_op(VALUE obj, VALUE addend)
711
791
  {
712
792
  VALUE new;
713
793
 
714
794
  // get a clone of the original
715
- new = c_cw_dict_clone(obj);
795
+ new = c_cwdict_clone(obj);
716
796
 
717
- return c_cw_dict_append_op(new, addend);
797
+ return c_cwdict_append_op(new, addend);
718
798
  }
719
799
 
720
- VALUE c_cw_dict_add_assign_op(VALUE obj, VALUE addend)
800
+ VALUE c_cwdict_add_assign_op(VALUE obj, VALUE addend)
721
801
  {
722
- return c_cw_dict_append_op(obj, addend);
802
+ return c_cwdict_append_op(obj, addend);
723
803
  }
724
804
 
725
- VALUE c_cw_dict_check_equality(VALUE obj, VALUE against)
805
+ VALUE c_cwdict_check_equality(VALUE obj, VALUE against)
726
806
  {
727
- dict_t* d, *comparison;
807
+ cwdict_t* d, *comparison;
728
808
 
729
809
  // get original pointers from Ruby VM
730
- Data_Get_Struct(obj, dict_t, d);
731
- Data_Get_Struct(against, dict_t, comparison);
810
+ Data_Get_Struct(obj, cwdict_t, d);
811
+ Data_Get_Struct(against, cwdict_t, comparison);
732
812
 
733
- if(dict_equal(*d, *comparison)) return Qtrue;
813
+ if(cwdict_equal(*d, *comparison)) return Qtrue;
734
814
  return Qfalse;
735
815
  }
736
816
 
737
- VALUE c_cw_dict_check_inequality(VALUE obj, VALUE against)
817
+ VALUE c_cwdict_check_inequality(VALUE obj, VALUE against)
738
818
  {
739
- dict_t* d, *comparison;
819
+ cwdict_t* d, *comparison;
740
820
 
741
821
  // get original pointers from Ruby VM
742
- Data_Get_Struct(obj, dict_t, d);
743
- Data_Get_Struct(against, dict_t, comparison);
822
+ Data_Get_Struct(obj, cwdict_t, d);
823
+ Data_Get_Struct(against, cwdict_t, comparison);
744
824
 
745
- if(dict_not_equal(*d, *comparison)) return Qtrue;
825
+ if(cwdict_inequal(*d, *comparison)) return Qtrue;
746
826
  return Qfalse;
747
827
  }
748
828
 
@@ -750,7 +830,10 @@ void Init_chinwag()
750
830
  {
751
831
  // setup module extension and containing class(es)
752
832
  m_chinwag = rb_define_module("Chinwag");
753
- c_cw_dict = rb_define_class_under(m_chinwag, "CWDict", rb_cObject);
833
+ c_cwdict = rb_define_class_under(m_chinwag, "CWDict", rb_cObject);
834
+
835
+ // note module inclusions
836
+ rb_include_module(c_cwdict, rb_mEnumerable);
754
837
 
755
838
  // sync up module generation functions
756
839
  rb_define_module_function(m_chinwag, "generate", m_cw_generate, -1);
@@ -760,50 +843,55 @@ void Init_chinwag()
760
843
  rb_define_module_function(m_chinwag, "set_default_max_output",m_s_max,1);
761
844
 
762
845
  // sync up class methods
763
- rb_define_singleton_method(c_cw_dict, "open", c_cw_dict_open, -1);
764
- rb_define_method(c_cw_dict, "close", c_cw_dict_close, 0);
846
+ rb_define_singleton_method(c_cwdict, "open", c_cwdict_open, -1);
847
+ rb_define_method(c_cwdict, "close", c_cwdict_close, 0);
848
+
849
+ rb_define_method(c_cwdict, "name", c_cwdict_name_g, 0);
850
+ rb_define_method(c_cwdict, "name=", c_cwdict_name_s, 1);
851
+ rb_define_method(c_cwdict, "length", c_cwdict_length, 0);
765
852
 
766
- rb_define_method(c_cw_dict, "name", c_cw_dict_name_g, 0);
767
- rb_define_method(c_cw_dict, "name=", c_cw_dict_name_s, 1);
768
- rb_define_method(c_cw_dict, "length", c_cw_dict_length, 0);
853
+ rb_define_method(c_cwdict, "join", c_cwdict_join, -1);
854
+ rb_define_method(c_cwdict, "clone", c_cwdict_clone, 0);
855
+ rb_define_method(c_cwdict, "sample", c_cwdict_sample, 0);
769
856
 
770
- rb_define_method(c_cw_dict, "join", c_cw_dict_join, -1);
771
- rb_define_method(c_cw_dict, "clone", c_cw_dict_clone, 0);
772
- rb_define_method(c_cw_dict, "sample", c_cw_dict_sample, 0);
857
+ rb_define_method(c_cwdict, "each", c_cwdict_each, 0);
858
+ rb_define_method(c_cwdict, "each_index", c_cwdict_each_index, 0);
859
+ rb_define_method(c_cwdict, "map", c_cwdict_map, 0);
860
+ rb_define_method(c_cwdict, "map!", c_cwdict_map_s, 0);
773
861
 
774
- rb_define_method(c_cw_dict, "sort", c_cw_dict_sort, 0);
775
- rb_define_method(c_cw_dict, "prune", c_cw_dict_prune, 0);
776
- rb_define_method(c_cw_dict, "clean", c_cw_dict_clean, 0);
862
+ rb_define_method(c_cwdict, "sort", c_cwdict_sort, 0);
863
+ rb_define_method(c_cwdict, "prune", c_cwdict_prune, 0);
864
+ rb_define_method(c_cwdict, "clean", c_cwdict_clean, 0);
777
865
 
778
- rb_define_method(c_cw_dict, "sort!", c_cw_dict_sort_s, 0);
779
- rb_define_method(c_cw_dict, "prune!", c_cw_dict_prune_s, 0);
780
- rb_define_method(c_cw_dict, "clean!", c_cw_dict_clean_s, 0);
781
- rb_define_method(c_cw_dict, "validate!", c_cw_dict_validate_s, 0);
866
+ rb_define_method(c_cwdict, "sort!", c_cwdict_sort_s, 0);
867
+ rb_define_method(c_cwdict, "prune!", c_cwdict_prune_s, 0);
868
+ rb_define_method(c_cwdict, "clean!", c_cwdict_clean_s, 0);
869
+ rb_define_method(c_cwdict, "validate!", c_cwdict_validate_s, 0);
782
870
 
783
- rb_define_method(c_cw_dict, "named?", c_cw_dict_named_q, 0);
784
- rb_define_method(c_cw_dict, "valid?", c_cw_dict_valid_q, 0);
785
- rb_define_method(c_cw_dict, "sorted?", c_cw_dict_sorted_q, 0);
786
- rb_define_method(c_cw_dict, "include?", c_cw_dict_include_q, 1);
787
- rb_define_method(c_cw_dict, "exclude?", c_cw_dict_exclude_q, 1);
871
+ rb_define_method(c_cwdict, "named?", c_cwdict_named_q, 0);
872
+ rb_define_method(c_cwdict, "valid?", c_cwdict_valid_q, 0);
873
+ rb_define_method(c_cwdict, "sorted?", c_cwdict_sorted_q, 0);
874
+ rb_define_method(c_cwdict, "include?", c_cwdict_include_q, 1);
875
+ rb_define_method(c_cwdict, "exclude?", c_cwdict_exclude_q, 1);
788
876
 
789
- rb_define_method(c_cw_dict, "inspect", c_cw_dict_inspect, 0);
790
- rb_define_method(c_cw_dict, "to_s", c_cw_dict_to_s, 0);
877
+ rb_define_method(c_cwdict, "inspect", c_cwdict_inspect, 0);
878
+ rb_define_method(c_cwdict, "to_s", c_cwdict_to_s, 0);
791
879
 
792
880
  // operator methods
793
- rb_define_method(c_cw_dict, "+", c_cw_dict_add_op, 1);
794
- rb_define_method(c_cw_dict, "+=", c_cw_dict_add_assign_op, 1);
795
- rb_define_method(c_cw_dict, "<<", c_cw_dict_append_op, 1);
796
- rb_define_method(c_cw_dict, "==", c_cw_dict_check_equality, 1);
797
- rb_define_method(c_cw_dict, "!=", c_cw_dict_check_inequality, 1);
881
+ rb_define_method(c_cwdict, "+", c_cwdict_add_op, 1);
882
+ rb_define_method(c_cwdict, "+=", c_cwdict_add_assign_op, 1);
883
+ rb_define_method(c_cwdict, "<<", c_cwdict_append_op, 1);
884
+ rb_define_method(c_cwdict, "==", c_cwdict_check_equality, 1);
885
+ rb_define_method(c_cwdict, "!=", c_cwdict_check_inequality, 1);
798
886
 
799
887
  // method aliases
800
- rb_define_alias(c_cw_dict, "dup", "clone");
801
- rb_define_alias(c_cw_dict, "size", "length");
802
- rb_define_alias(c_cw_dict, "count", "length");
888
+ rb_define_alias(c_cwdict, "dup", "clone");
889
+ rb_define_alias(c_cwdict, "size", "length");
890
+ rb_define_alias(c_cwdict, "count", "length");
803
891
 
804
892
  // open Seussian dict as default fall-back
805
893
  VALUE args[] = { rb_str_new2("seussian") };
806
- default_dict = c_cw_dict_open(1, args, default_dict);
894
+ default_dict = c_cwdict_open(1, args, default_dict);
807
895
 
808
896
  // set default output type
809
897
  default_output_type = ID2SYM(rb_intern("words"));