PIKEFUN void create(string input){}
This is where the implementation of the create() method would be
written. What it would do is:
PIKEFUN void write(int nr_of_times){}
This is then where the implementation of write() would be
written. What it would do is:
/**
*File: reversed.c
*Description: The code of the Rev module */
#include "global.h"
#include "interpret.h"
#include "svalue.h"
#include "stralloc.h"
#include "array.h"
#include "pike_macros.h"
#include "program.h"
#include "stralloc.h"
#include "object.h"
#include "pike_types.h"
#include "threads.h"
#include "dynamic_buffer.h"
/* This must be included last! */
#include "module_magic.h"
/*PLEASE NOTE THAT this code is not that good from a programming point
of view, it is only intended to serve as an example of a simple
C module*/
struct data{
char buf[50];
};
static void reversed_string_create(INT32 args){
struct pike_string *input;
char *s;
char tmpbuf[50];
char retbuf[50];
int i;
int j = 0;
if (args != 1){
Pike_error("Method ReversedString->create() takes ONE argument");
}
/* a little bit messy, assigns s a pointer to the storage of the reversed string*/
s = (char *)&(((struct data *)Pike_fp->current_storage)->buf);
input = Pike_sp[-args].u.string;
strncpy((char*)&tmpbuf, (char *)(&input->str),49); /* copy the input string to a buffer*/
tmpbuf[49]='\0';
/* find the terminating null-character*/
for(i = 0; i < 50; i++){
if (tmpbuf[i]=='\0'){
break;
}
}
/*reverse the string, result in retbuf[]*/
while (i >= 0 && j < 50){
i--;
retbuf[j] = tmpbuf[i];
j++;
}
retbuf[j] = '\0';
/* store the reversed string, and pop arguments from stack*/
strcpy(s, (char *)&retbuf);
pop_n_elems(args);
}
static void reversed_string_write(INT32 args){
int i = 0;
int nr_of_times;
if (args != 1){
Pike_error("Method ReversedString->create() takes ONE argument");
}
nr_of_times = Pike_sp[-args].u.integer;
for(i = 0; i < nr_of_times; i++){
printf("%s",(char *)&(((struct data *)Pike_fp->current_storage)->buf));
}
pop_n_elems(args);
}
static void init_reversed_string(struct object *o){}
static void exit_reversed_string(struct object *o){}
void pike_module_exit(void) {}
void pike_module_init(void)
{
start_new_program(); /*begin of class*/
ADD_STORAGE(struct data);
/*add the methods create and write*/
ADD_FUNCTION("create", reversed_string_create, tFunc(tString, tVoid), 0);
ADD_FUNCTION("write", reversed_string_write, tFunc(tInt, tVoid), 0);
set_init_callback(init_reversed_string);
set_exit_callback(exit_reversed_string);
end_class("ReversedString",0);
}