Text Variations

version 4 by Jon Ingold

  • Home page
  • Beginning
  • Previous
  • Next



  • Part 1 - I6 routines

    [for capturing text to the output buffer; also printing and computing a (very simple) hash]

    Include (-

    Array buffer_printed_text --> 256;
    Array buffer_ptrs --> 16;
    Global buffer_counter = 0;
    Global tip_buffer = 0;

    Constant BP_MEMORYFREF_ROCK 661;
    Constant BP_MEMORY_MAXNEST 16;
    Array bp_memory_buf --> BP_MEMORY_MAXNEST;
    Array bp_memory_str --> BP_MEMORY_MAXNEST;
    Global bp_memory_nest = 0;

    !Some code from Roger Firth, to create a biplatform output stream '3'

    [ Stream_To_Array buf;
    #ifdef TARGET_ZCODE;
    @output_stream 3 buf;
    #ifnot; ! TARGET_GLULX;
    if (bp_memory_nest == BP_MEMORY_MAXNEST) rfalse;
    bp_memory_buf-->bp_memory_nest = buf;
    bp_memory_str-->bp_memory_nest = glk_stream_get_current();
    glk_stream_set_current(glk_stream_open_memory(buf+WORDSIZE, 256, filemode_Write, BP_MEMORYFREF_ROCK+bp_memory_nest));
    bp_memory_nest++;

    #endif; ! TARGET_
    rtrue; ];

    [ _Unstream;
    #ifdef TARGET_ZCODE;
    @output_stream -3;
    #ifnot; ! TARGET_GLULX;
    if (bp_memory_nest == 0) rfalse;
    bp_memory_nest--;
    glk_stream_close(glk_stream_get_current(), gg_arguments);
    glk_stream_set_current(bp_memory_str-->bp_memory_nest);
    (bp_memory_buf-->bp_memory_nest)-->0 = gg_arguments-->1;
    bp_memory_buf-->bp_memory_nest = bp_memory_str-->bp_memory_nest = GLK_NULL;
    #endif;
    ];

    [ __bc; return buffer_counter; ];

    [ init_random_list;
    buffer_counter = 0;
    tip_buffer = 0;
    buffer_ptrs-->buffer_counter = 0;
    ];

    [ start_random_entry sofar ptr;
    sofar = buffer_ptrs-->buffer_counter;
    ptr = buffer_printed_text + sofar;
    Stream_To_Array(ptr);
    ];

    [ complete_random_entry sofar ptr len;
    sofar = buffer_ptrs-->buffer_counter;
    ptr = buffer_printed_text + sofar;
    _Unstream();
    len = WORDSIZE + ptr-->0;
    buffer_counter++;
    buffer_ptrs-->buffer_counter = sofar+len;
    ];

    [ print_line n sofar ptr len ix;
    sofar = buffer_ptrs-->(n-1);
    ptr = buffer_printed_text + sofar;
    len = WORDSIZE + ptr-->0;
    for (ix=WORDSIZE : ix<len : ix++)
    { if (ptr->ix == 10 or 13) print "^"; else print (char) ptr->ix; }
    ];

    [ hash_buffer n ix sofar ptr len tot;
    for (n=0:n<buffer_counter:n++)
    { sofar = buffer_ptrs-->0;
    ptr = buffer_printed_text + sofar;
    len = WORDSIZE + ptr-->0;
    for (ix=WORDSIZE : ix<len : ix++) tot = tot + (ix*((ptr->ix)-90));
    }
    return tot + tip_buffer;
    ];

    [ Tipbuffer n; tip_buffer = n; ];

    -)