Class: Chinwag::CWDict

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/chinwag/rb_chinwag_ext.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.open(*args) ⇒ Object

sync up class methods



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'ext/chinwag/rb_chinwag_ext.c', line 199

VALUE c_cwdict_open(int argc, VALUE* argv, VALUE obj)
{
  VALUE file_pathname;
  FILE* file_ptr = NULL;
  cwdict_t d = cwdict_open(), path_parts, file_parts;
  char* tkns_ptr = NULL; char* name_ptr = NULL;
  char* path_name = NULL; char* file_name = NULL;
  char* name = NULL; bool used_file = false; char* file_buffer = NULL;
  long last_drow = 0, last_word = 0; size_t len = 0;

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

  if(argc == 1)
  {
    switch(TYPE(argv[0]))
    {
      case T_FILE:
        #ifdef HAVE_RUBY_IO_H
        file_pathname = RFILE(argv[0])->fptr->pathv;
        path_name = StringValueCStr(file_pathname);
        file_ptr = rb_io_stdio_file(RFILE(argv[0])->fptr);
        #else
        path_name = RFILE(argv[0])->fptr->path;
        file_ptr = RFILE(argv[0])->fptr->f;
        #endif

        if(include(path_name, "/") || include(path_name, "\\"))
        {
          path_parts = split_into_cwdict(path_name, "/\\");

          last_drow = path_parts.count - 1;
          last_word = path_parts.drows[last_drow].count - 1;
          len = strlen(path_parts.drows[last_drow].words[last_word]);

          file_name = (char*)malloc(len + 1);
          strcpy(file_name, path_parts.drows[last_drow].words[last_word]);
          file_name[len] = '\0';

          cwdict_close(path_parts);
        }
        else
        {
          file_name = (char*)malloc(strlen(path_name) + 1);
          strcpy(file_name, path_name);
          file_name[strlen(path_name)] = '\0';
        }

        if(include(file_name, "."))
        {
          file_parts = split_into_cwdict(file_name, ".");

          size_t len = strlen(file_parts.drows[0].words[0]);

          name = (char*)malloc(len + 1);
          strcpy(name, file_parts.drows[0].words[0]);
          name[len] = '\0';

          cwdict_close(file_parts);
        }
        else
        {
          name = (char*)malloc(strlen(file_name) + 1);
          strcpy(name, file_name);
          name[strlen(file_name)] = '\0';
        }

        used_file = true;

        break;
      case T_STRING:
        tkns_ptr = StringValueCStr(argv[0]);
        break;
      default:
        rb_raise(rb_eTypeError, "invalid type (String or File)");
        break;
    }
  }
  if(argc == 2)
  {
    Check_Type(argv[1], T_STRING);
    name_ptr = StringValueCStr(argv[1]);
  }

  // check if tkns references existing, embedded dictionary...
  if(!tkns_ptr && !name_ptr && !used_file) d = cwdict_open();
  else if(tkns_ptr && !used_file)
  {
    if(strcmp(tkns_ptr, "seussian") == 0)
    {
      if(!name_ptr)
      d=cwdict_open_with_name_and_tokens("seussian",dict_seuss,DELIMITERS);
      else
      d=cwdict_open_with_name_and_tokens(name_ptr,dict_seuss,DELIMITERS);
    }
    else if(strcmp(tkns_ptr, "latin") == 0)
    {
      if(!name_ptr)
      d=cwdict_open_with_name_and_tokens("latin", dict_latin,DELIMITERS);
      else
      d=cwdict_open_with_name_and_tokens(name_ptr, dict_latin,DELIMITERS);
    }
  }
  // ...else, if just a name was passed...
  else if(name_ptr && !used_file) d = cwdict_open_with_name(name_ptr);
  // ...else, see if file exists by passed name...
  else if(used_file && name && file_ptr)
  {
    U32 stringify_result = stringify_file(&file_buffer, file_ptr);
    if(stringify_result == 0)
    {
      rb_raise(rb_eException, "unable to process passed file (%s)", 
      file_name);
    }

    d=cwdict_open_with_name_and_tokens(name, file_buffer, DELIMITERS);

    free(file_buffer);
  }
  // ...else, return a blank dictionary

  // create a dictionary pointer
  cwdict_t* d_ptr = (cwdict_t*)malloc(sizeof(cwdict_t));
  *d_ptr = d;

  if(name) free(name);
  if(file_name) free(file_name);

  return Data_Wrap_Struct(c_cwdict, 0, c_cwdict_free, d_ptr);
}

Instance Method Details

#!=(against) ⇒ Object



825
826
827
828
829
830
831
832
833
834
835
# File 'ext/chinwag/rb_chinwag_ext.c', line 825

VALUE c_cwdict_check_inequality(VALUE obj, VALUE against)
{
  cwdict_t* d, *comparison;

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);
  Data_Get_Struct(against, cwdict_t, comparison);

  if(cwdict_inequal(*d, *comparison)) return Qtrue;
  return Qfalse;
}

#+(addend) ⇒ Object

operator methods



798
799
800
801
802
803
804
805
806
# File 'ext/chinwag/rb_chinwag_ext.c', line 798

VALUE c_cwdict_add_op(VALUE obj, VALUE addend)
{
  VALUE new;

  // get a clone of the original
  new = c_cwdict_clone(obj);

  return c_cwdict_append_op(new, addend);
}

#+=(addend) ⇒ Object



808
809
810
811
# File 'ext/chinwag/rb_chinwag_ext.c', line 808

VALUE c_cwdict_add_assign_op(VALUE obj, VALUE addend)
{
  return c_cwdict_append_op(obj, addend);
}

#<<(addend) ⇒ Object



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'ext/chinwag/rb_chinwag_ext.c', line 760

VALUE c_cwdict_append_op(VALUE obj, VALUE addend)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  switch(TYPE(addend))
  {
    case T_STRING:
      *d = cwdict_place_word_strict(*d, StringValueCStr(addend));
      break;
    case T_ARRAY:
      for(long i = 0; i != RARRAY_LEN(addend); ++i)
      {
        if(TYPE(rb_ary_entry(addend, i)) != T_STRING)
        {
          rb_raise(rb_eTypeError,"not a valid value (passed Array can only"
          " contain String elements)");
          break;
        }

        VALUE entry = rb_ary_entry(addend, i);
        char* entry_str = StringValueCStr(entry);

        *d = cwdict_place_word_strict(*d, entry_str);
      }

      break;
    default:
      rb_raise(rb_eTypeError,"not a valid value (expected String or Array");
      break;
  }

  if(d->sorted) return c_cwdict_sort(obj);
  return obj;
}

#==(against) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
# File 'ext/chinwag/rb_chinwag_ext.c', line 813

VALUE c_cwdict_check_equality(VALUE obj, VALUE against)
{
  cwdict_t* d, *comparison;

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);
  Data_Get_Struct(against, cwdict_t, comparison);

  if(cwdict_equal(*d, *comparison)) return Qtrue;
  return Qfalse;
}

#cleanObject



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'ext/chinwag/rb_chinwag_ext.c', line 544

VALUE c_cwdict_clean(VALUE obj)
{
  cwdict_t* d;
  VALUE new;

  // get a new copy of the original dict
  new = c_cwdict_clone(obj);

  // get original pointer from Ruby VM
  Data_Get_Struct(new, cwdict_t, d);

  *d = cwdict_prune(*d, true);

  return new;
}

#clean!Object



584
585
586
587
588
589
590
591
592
593
594
# File 'ext/chinwag/rb_chinwag_ext.c', line 584

VALUE c_cwdict_clean_s(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  *d = cwdict_prune(*d, true);

  return obj;
}

#cloneObject Also known as: dup



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'ext/chinwag/rb_chinwag_ext.c', line 400

VALUE c_cwdict_clone(VALUE obj)
{
  cwdict_t* d, *new_p;
  VALUE new;

  // open new dict for return value
  VALUE args[] = { rb_str_new2(""), rb_str_new2("") };
  new = c_cwdict_open(2, args, new);

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);
  Data_Get_Struct(new, cwdict_t, new_p);

  // get a copy of the original dictionary
  *new_p = cwdict_clone(*d);

  return new;
}

#closeObject



331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/chinwag/rb_chinwag_ext.c', line 331

VALUE c_cwdict_close(VALUE obj)
{
  cwdict_t* d;
  cwdict_t empty;

  // get original pointer from Ruby VM and close
  Data_Get_Struct(obj, cwdict_t, d);

  if(d->drows && d->count > 0) { *d = cwdict_close(*d); }
  
  return obj;
}

#eachObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'ext/chinwag/rb_chinwag_ext.c', line 429

VALUE c_cwdict_each(VALUE obj)
{
  cwdict_t *d;

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  #ifdef RETURN_SIZE_ENUMERATOR
  RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
  #else
  RETURN_ENUMERATOR(obj, 0, 0);
  #endif

  for(U32 i = 0; i != d->count; ++i)
  {
    for(U32 j = 0; j != d->drows[i].count; ++j)
    {
      rb_yield(rb_str_new2(d->drows[i].words[j]));
    }
  }

  return obj;
}

#each_indexObject



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'ext/chinwag/rb_chinwag_ext.c', line 453

VALUE c_cwdict_each_index(VALUE obj)
{
  long num = 0; cwdict_t *d;

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  #ifdef RETURN_SIZE_ENUMERATOR
  RETURN_SIZED_ENUMERATOR(obj, 0, 0, c_cwdict_enum_length);
  #else
  RETURN_ENUMERATOR(obj, 0, 0);
  #endif

  for(U32 i = 0; i != d->count; ++i)
  {
    for(U32 j = 0; j != d->drows[i].count; ++j)
    {
      rb_yield(LONG2NUM(++num));
    }
  }

  return obj;
}

#exclude?(string) ⇒ Boolean

Returns:

  • (Boolean)


667
668
669
670
671
672
673
674
675
676
# File 'ext/chinwag/rb_chinwag_ext.c', line 667

VALUE c_cwdict_exclude_q(VALUE obj, VALUE string)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(cwdict_exclude(*d, StringValueCStr(string))) return Qtrue;
  return Qfalse;
}

#include?(string) ⇒ Boolean

Returns:

  • (Boolean)


656
657
658
659
660
661
662
663
664
665
# File 'ext/chinwag/rb_chinwag_ext.c', line 656

VALUE c_cwdict_include_q(VALUE obj, VALUE string)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(cwdict_include(*d, StringValueCStr(string))) return Qtrue;
  return Qfalse;
}

#inspectObject



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'ext/chinwag/rb_chinwag_ext.c', line 678

VALUE c_cwdict_inspect(VALUE obj)
{
  cwdict_t* dict; VALUE str;
  size_t count = 0; int multiplier = 1; int word_len = 0;
  char* result = (char*)malloc(LARGE_BUFFER * multiplier + 1);

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, dict);

  // add opening delimiter
  strcpy(result, "["); ++count;

  // add internal dictionary row elements
  for(unsigned long i = 0; i != dict->count; ++i)
  {
    // add opening row delimiter
    strcat(result, "["); ++count;

    for(unsigned long j = 0; j != dict->drows[i].count; ++j)
    {
      // get word length for count
      word_len = strlen(dict->drows[i].words[j]);

      // add word to resulting string
      strcat(result, dict->drows[i].words[j]);
      count += word_len;

      // add continuation delimiter (if applicable)
      if(j < dict->drows[i].count - 1){ strcat(result,", "); count += 2; }

      // resize result if necessary
      if(count >= LARGE_BUFFER * multiplier - 100)
      {
        // create temporary copy
        char* temp = (char*)malloc(LARGE_BUFFER * multiplier + 1);

        result[count] = '\0';
        strcpy(temp, result);
        temp[count] = '\0';

        // increase buffer size
        result = (char*)realloc(result,LARGE_BUFFER * ++multiplier + 1);

        // move back into resulting string and clear intermediary buffer
        strcpy(result, temp);
        result[count] = '\0';

        free(temp);
      }
    }

    // add closing row delimiter
    strcat(result, "]"); ++count;

    // add continuation delimiter (if applicable)
    if(i < dict->count - 1) { strcat(result, ", "); count += 2; }
  }

  // add closing delimiter
  strcat(result, "]"); ++count;

  // NULL terminator (just in case)
  result[count] = '\0';

  str = rb_str_new2(result);
  free(result);

  return str;
}

#join(*args) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/chinwag/rb_chinwag_ext.c', line 385

VALUE c_cwdict_join(int argc, VALUE* argv, VALUE obj)
{
  cwdict_t* d;

  // raise exception if passed wrong number of arguments
  if(argc > 1) rb_raise(rb_eArgError, "wrong number of arguments");
  if(argc == 1) Check_Type(argv[0], T_STRING);

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(argc == 0) return rb_str_new2(cwdict_join(*d, " "));
  return rb_str_new2(cwdict_join(*d, StringValueCStr(argv[0])));
}

#lengthObject Also known as: size, count



369
370
371
372
373
374
375
376
377
378
# File 'ext/chinwag/rb_chinwag_ext.c', line 369

VALUE c_cwdict_length(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(d->count == 0) return INT2NUM(0);
  return LONG2NUM(cwdict_length(*d));
}

#mapObject



478
479
480
481
482
483
484
485
486
487
# File 'ext/chinwag/rb_chinwag_ext.c', line 478

VALUE c_cwdict_map(VALUE obj)
{
  VALUE new;

  // get a new copy of the original dict
  new = c_cwdict_clone(obj);
  new = c_cwdict_map_s(new);

  return new;
}

#map!Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'ext/chinwag/rb_chinwag_ext.c', line 489

VALUE c_cwdict_map_s(VALUE obj)
{
  cwdict_t *d;

  // get original pointers from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  for(U32 i = 0; i != d->count; ++i)
  {
    for(U32 j = 0; j != d->drows[i].count; ++j)
    {
      VALUE ref = rb_yield(rb_str_new2(d->drows[i].words[j]));
      long len = RSTRING_LEN(ref);

      d->drows[i].words[j] = (char*)realloc(d->drows[i].words[j], len + 1);
      strcpy(d->drows[i].words[j], StringValueCStr(ref));
      d->drows[i].words[j][len] = '\0';
    }
  }

  return obj;
}

#nameObject



344
345
346
347
348
349
350
351
352
353
# File 'ext/chinwag/rb_chinwag_ext.c', line 344

VALUE c_cwdict_name_g(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(d->name && strlen(d->name) > 0) return rb_str_new2(d->name);
  return rb_str_new2("");
}

#name=(name) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'ext/chinwag/rb_chinwag_ext.c', line 355

VALUE c_cwdict_name_s(VALUE obj, VALUE name)
{
  cwdict_t* d; long len = RSTRING_LEN(name);

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  d->name = (char*)malloc(len + 1);
  strcpy(d->name, StringValueCStr(name));
  d->name[len] = '\0';

  return obj;
}

#named?Boolean

Returns:

  • (Boolean)


614
615
616
617
618
619
620
621
622
623
# File 'ext/chinwag/rb_chinwag_ext.c', line 614

VALUE c_cwdict_named_q(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(d->name && strlen(d->name) > 0) return Qtrue;
  return Qfalse;
}

#pruneObject



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'ext/chinwag/rb_chinwag_ext.c', line 528

VALUE c_cwdict_prune(VALUE obj)
{
  cwdict_t* d;
  VALUE new;

  // get a new copy of the original dict
  new = c_cwdict_clone(obj);

  // get original pointer from Ruby VM
  Data_Get_Struct(new, cwdict_t, d);

  *d = cwdict_prune(*d, false);

  return new;
}

#prune!Object



572
573
574
575
576
577
578
579
580
581
582
# File 'ext/chinwag/rb_chinwag_ext.c', line 572

VALUE c_cwdict_prune_s(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  *d = cwdict_prune(*d, false);

  return obj;
}

#sampleObject



419
420
421
422
423
424
425
426
427
# File 'ext/chinwag/rb_chinwag_ext.c', line 419

VALUE c_cwdict_sample(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  return rb_str_new2(cwdict_sample(*d));
}

#sortObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/chinwag/rb_chinwag_ext.c', line 512

VALUE c_cwdict_sort(VALUE obj)
{
  cwdict_t* d;
  VALUE new;

  // get a new copy of the original dict
  new = c_cwdict_clone(obj);

  // get original pointer from Ruby VM
  Data_Get_Struct(new, cwdict_t, d);

  *d = cwdict_sort(*d);

  return new;
}

#sort!Object



560
561
562
563
564
565
566
567
568
569
570
# File 'ext/chinwag/rb_chinwag_ext.c', line 560

VALUE c_cwdict_sort_s(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  *d = cwdict_sort(*d);

  return obj;
}

#sorted?Boolean

Returns:

  • (Boolean)


645
646
647
648
649
650
651
652
653
654
# File 'ext/chinwag/rb_chinwag_ext.c', line 645

VALUE c_cwdict_sorted_q(VALUE obj)
{
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  if(d->sorted) return Qtrue;
  return Qfalse;
}

#to_sObject



748
749
750
751
752
753
754
755
756
757
758
# File 'ext/chinwag/rb_chinwag_ext.c', line 748

VALUE c_cwdict_to_s(VALUE obj)
{
  cwdict_t* dict;
  size_t count = 0; int multiplier = 1;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, dict);

  // if(dict->drows && dict->count > 0) return Qnil;
  return c_cwdict_inspect(obj);
}

#valid?Boolean

Returns:

  • (Boolean)


625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'ext/chinwag/rb_chinwag_ext.c', line 625

VALUE c_cwdict_valid_q(VALUE obj)
{
  char* e;
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  // handle invalid state first (for error handling's sake)
  if(!cwdict_valid(*d, &e))
  {
    rb_raise(rb_eException, "%s", e);
    free(e);

    return Qfalse;
  }

  return Qtrue;
}

#validate!Object



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'ext/chinwag/rb_chinwag_ext.c', line 596

VALUE c_cwdict_validate_s(VALUE obj)
{
  char* e;
  cwdict_t* d;

  // get original pointer from Ruby VM
  Data_Get_Struct(obj, cwdict_t, d);

  // handle invalid state first (for error handling's sake)
  if(!cwdict_valid(*d, &e))
  {
    rb_raise(rb_eException, "%s", e);
    free(e);
  }

  return obj;
}