# Ruby Newbie: Simple C Wrappers?

**URL:** <https://www.ruby-forum.com/t/ruby-newbie-simple-c-wrappers/101609>\
**Category:** Ruby\
**Created:** [August 6, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-newbie-simple-c-wrappers/101609 "2006-08-06T19:32:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cooper\_Stevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Cooper\_Stevenson](https://www.ruby-forum.com/u/Cooper_Stevenson)\
**Post date:** [August 6, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/ruby-newbie-simple-c-wrappers/101609/1 "2006-08-06T19:32:10Z")

</div>

Hello,

My apologies if this has already been discussed in detail on the list  
but I’ve been working with the Pragmatics an O’Rielly’s for some time  
now and I think it’s time to call for a ‘help.’

I would like to interface what seems to be a straightforward main  
program (as I understand it that’s all I need to interface, not the rest  
of the .c files, please correct me if this is in error) and have  
achieved a compile with an empty Init function like so:

```
void
Init_jzipr()
{

}

```

irb works:

$ irb  
irb(main):001:0\> require ‘jzipr’  
=\> true

I am finding the next step–creating the class–difficult. Here’s  
jzip.c–it’s doesn’t look like much but doing it for the first time…

------------------Begin jzip.c------------------------

/\* $Id: jzip.c,v 1.3 2000/10/04 23:07:57 jholder Exp $

- 
* * *
- see doc/License.txt for License Information
- 
* * *
- 
- File name: $Id: jzip.c,v 1.3 2000/10/04 23:07:57 jholder Exp $
- 
- Description:
- 
- Modification history:
- $Log: jzip.c,v $
- Revision 1.3 2000/10/04 23:07:57 jholder
- fixed redirect problem with isolatin1 range chars
- 
- Revision 1.2 2000/05/25 22:28:56 jholder
- changes routine names to reflect zmachine opcode names per spec 1.0
- 
- Revision 1.1.1.1 2000/05/10 14:21:34 jholder
- 
- imported
- 
- 
- 
* * *

\*/

/\*

- jzip.c
- 
- Z code interpreter main routine.
- 
- Revisions list:
- Mark Howell 10-Mar-1993 V2.0 removed\_email\_address@domain.invalid
- 

```
                    V2.0.1(some of a-f)

```

- John Holder 22-Nov-1995 V2.0.1g removed\_email\_address@domain.invalid
- John Holder 06-Feb-1998 V2.0.2 removed\_email\_address@domain.invalid Zstrict & Quetzal
- 
- If you have problems with this interpreter and/or fix bugs in it,
- please notify John Holder (removed\_email\_address@domain.invalid) and he will add your
- fix to the official JZIP distribution.  
\*/

#include “ztypes.h”

extern int GLOBALVER;

static void configure( zbyte\_t, zbyte\_t );

/\*

- main
- 
- Initialise environment, start interpreter, clean up.  
\*/

int main( int argc, char \*argv[] )  
{  
process\_arguments( argc, argv );

```
configure( V1, V8 );

initialize_screen( );

load_cache( );

z_restart( );

( void ) interpret( );

unload_cache( );

close_story( );

close_script( );

reset_screen( );

exit( EXIT_SUCCESS );

return ( 0 );

```

} /\* main \*/

/\*

- configure
- 
- Initialise global and type specific variables.
- 

\*/

static void configure( zbyte\_t min\_version, zbyte\_t max\_version )  
{  
zbyte\_t header[PAGE\_SIZE], second;

```
read_page( 0, header );
datap = header;

h_type = get_byte( H_TYPE );

   GLOBALVER = h_type;

if ( h_type < min_version || h_type > max_version ||
     ( get_byte( H_CONFIG ) & CONFIG_BYTE_SWAPPED ) )
   fatal( "Wrong game or version" );
/*
 * if (h_type == V6 || h_type == V7)
 * fatal ("Unsupported zcode version.");
 */

if ( h_type < V4 )
{
   story_scaler = 2;
   story_shift = 1;
   property_mask = P3_MAX_PROPERTIES - 1;
   property_size_mask = 0xe0;
}
else if ( h_type < V8 )
{
   story_scaler = 4;
   story_shift = 2;
   property_mask = P4_MAX_PROPERTIES - 1;
   property_size_mask = 0x3f;
}
else
{
   story_scaler = 8;
   story_shift = 3;
   property_mask = P4_MAX_PROPERTIES - 1;
   property_size_mask = 0x3f;
}

h_config = get_byte( H_CONFIG );
h_version = get_word( H_VERSION );
h_data_size = get_word( H_DATA_SIZE );
h_start_pc = get_word( H_START_PC );
h_words_offset = get_word( H_WORDS_OFFSET );
h_objects_offset = get_word( H_OBJECTS_OFFSET );
h_globals_offset = get_word( H_GLOBALS_OFFSET );
h_restart_size = get_word( H_RESTART_SIZE );
h_flags = get_word( H_FLAGS );
h_synonyms_offset = get_word( H_SYNONYMS_OFFSET );
h_file_size = get_word( H_FILE_SIZE );
if ( h_file_size == 0 )
   h_file_size = get_story_size( );
h_checksum = get_word( H_CHECKSUM );
h_alternate_alphabet_offset = get_word( H_ALTERNATE_ALPHABET_OFFSET 

```

);

```
if ( h_type >= V5 )
{
   h_unicode_table = get_word( H_UNICODE_TABLE );
}
datap = NULL;

```

} /\* configure \*/

------------------End jzip.c------------------------

Any help is greatly appreciated.

-Cooper

---

<div class="post-metadata">

**Author:** ![Cooper\_Stevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Cooper\_Stevenson](https://www.ruby-forum.com/u/Cooper_Stevenson)\
**Post date:** [August 6, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/ruby-newbie-simple-c-wrappers/101609/2 "2006-08-06T20:59:17Z")

</div>

Cooper Stevenson wrote:

> {  
> jzip.c–it’s doesn’t look like much but doing it for the first time…  
> What are you trying to achieve?  
> Do you want to start the main function from ruby?  
> (I don’t see the benefit here, just shell out to the program)

If so, write a small wrapper like

VALUE rb\_main(VALUE self, VALUE args)  
{  
// you could/should use args here to build up a C array with parameters  
main(0, 0);  
return self;  
}

and call rb\_define\_method in your Init\_jzipr to make that method  
available  
in ruby.

void  
Init\_jzipr()  
{  
rb\_define\_method(rb\_cObject, “main”, rb\_main, -2)  
}

cheers

Simon

---

<div class="post-metadata">

**Author:** ![Cooper\_Stevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Cooper\_Stevenson](https://www.ruby-forum.com/u/Cooper_Stevenson)\
**Post date:** [August 7, 2006, 12:08am UTC](https://www.ruby-forum.com/t/ruby-newbie-simple-c-wrappers/101609/3 "2006-08-07T00:08:35Z")

</div>

> If so, write a small wrapper like
> 
> VALUE rb\_main(VALUE self, VALUE args)  
> [snip]

Thanks, Simon. I’m working on this now!

I’m trying to run this C application and capture the user’s input and  
output to a Ruby string array. This way, I can use the values via Ruby  
on Rails to display content based on what’s going on with the C  
application.

Thanks again!

-Cooper
