Annotation of OpenXM/doc/oxlib/test2.c, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$ */
! 2: #include <stdio.h>
! 3: #include <sys/param.h>
! 4: #include "oxasir.h"
! 5:
! 6: char *asir_ox_pop_string();
! 7:
! 8: main() {
! 9: char *s;
! 10: asir_ox_init(0); /* Use the network byte order */
! 11:
! 12: /* Loading the file xm */
! 13: /* This part generates a core dump. I'm trying to find a reason. */
! 14: asir_ox_push_string("xm");
! 15: asir_ox_push_int32(1);
! 16: asir_ox_push_string("load");
! 17: asir_ox_push_cmd(SM_executeFunction);
! 18:
! 19:
! 20: asir_ox_push_string("fctr(x^10-1);");
! 21: asir_ox_push_cmd(SM_executeStringByLocalParser);
! 22:
! 23: /*
! 24: asir_ox_push_string("x^10-1;");
! 25: asir_ox_push_cmd(SM_executeStringByLocalParser);
! 26: asir_ox_push_int32(1);
! 27: asir_ox_push_string("fctr");
! 28: asir_ox_push_cmd(SM_executeFunction);
! 29: */
! 30:
! 31: s = asir_ox_pop_string();
! 32: printf("%s\n",s);
! 33:
! 34: printf("------------------------------------------\n");
! 35: asir_ox_push_object_given_by_a_string("12345;");
! 36: s = asir_ox_pop_string();
! 37: printf("%s\n",s);
! 38:
! 39: }
! 40:
! 41: asir_ox_push_int32(int k) {
! 42: static unsigned char p[8];
! 43: *((int *)p) = htonl((int) CMO_INT32);
! 44: *((int *)(p+4)) = htonl(k);
! 45: debug_packet(p,8);
! 46: asir_ox_push_cmo(p);
! 47: }
! 48:
! 49: asir_ox_push_string(char *s) {
! 50: unsigned char *p;
! 51: p = (unsigned char *)GC_malloc(strlen(s)+8);
! 52: if (sizeof(int) != 4) {
! 53: fprintf(stderr,"sizeof(int) is not equal to 4.\n"); exit(1);
! 54: }
! 55: *((int *)p) = htonl((int) CMO_STRING);
! 56: *((int *)(p+4)) = htonl((int ) strlen(s));
! 57: strcpy(p+8,s);
! 58: debug_packet(p,strlen(s)+8);
! 59: asir_ox_push_cmo(p);
! 60: }
! 61:
! 62: asir_ox_push_object_given_by_a_string(char *s) {
! 63: asir_ox_push_string(s);
! 64: asir_ox_push_cmd(SM_executeStringByLocalParser);
! 65: }
! 66:
! 67: char *asir_ox_pop_string() {
! 68: int size;
! 69: char *s;
! 70: int tag;
! 71: asir_ox_push_int32(1);
! 72: asir_ox_push_string("rtostr");
! 73: asir_ox_push_cmd(SM_executeFunction);
! 74:
! 75: size = asir_ox_peek_cmo_size();
! 76: s = (char *)GC_malloc(size+1);
! 77: asir_ox_pop_cmo(s,size+1);
! 78: s[size] = 0;
! 79: tag = ntohl(*((int *)s));
! 80: if (tag != CMO_STRING) {
! 81: fprintf(stderr,"asir_ox_pop_string: the cmo object is not a string. Returns the empty string.\n");
! 82: return("");
! 83: }else{
! 84: return(s+8);
! 85: }
! 86: }
! 87:
! 88: debug_packet(char *p,int size) {
! 89: int i;
! 90: for (i=0; i<size; i++) {
! 91: printf(" %2x ",p[i]);
! 92: if ((p[i] >= ' ') && (p[i] < 0x7f)) {
! 93: printf("(%c) ",p[i]);
! 94: }
! 95: }
! 96: printf("\n");
! 97: }
! 98:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>