[BACK]Return to ox_Xsample.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/ox_Xsample.c, Revision 1.9

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.9     ! takayama    2: /* $OpenXM: OpenXM/src/ox_toolkit/ox_Xsample.c,v 1.8 2005/07/26 12:52:05 ohara Exp $ */
1.1       ohara       3:
                      4: #include <stdio.h>
1.6       ohara       5: #include <stdlib.h>
1.2       ohara       6: #include "ox_toolkit.h"
1.1       ohara       7:
1.3       ohara       8: OXFILE *fd_rw;
1.1       ohara       9:
                     10: #define INIT_S_SIZE 2048
                     11: #define EXT_S_SIZE  2048
                     12:
                     13: static int stack_size = 0;
                     14: static int stack_pointer = 0;
                     15: static cmo **stack = NULL;
                     16:
                     17: int initialize_stack()
                     18: {
                     19:     stack_pointer = 0;
1.3       ohara      20:     stack_size = INIT_S_SIZE;
                     21:     stack = malloc(stack_size*sizeof(cmo*));
1.1       ohara      22: }
                     23:
                     24: static int extend_stack()
                     25: {
1.3       ohara      26:     int size2 = stack_size + EXT_S_SIZE;
                     27:     cmo **stack2 = malloc(size2*sizeof(cmo*));
                     28:     memcpy(stack2, stack, stack_size*sizeof(cmo *));
                     29:     free(stack);
                     30:     stack = stack2;
                     31:     stack_size = size2;
1.1       ohara      32: }
                     33:
                     34: int push(cmo* m)
                     35: {
                     36:     stack[stack_pointer] = m;
                     37:     stack_pointer++;
                     38:     if (stack_pointer >= stack_size) {
1.3       ohara      39:         extend_stack();
1.1       ohara      40:     }
                     41: }
                     42:
                     43: cmo* pop()
                     44: {
                     45:     if (stack_pointer > 0) {
                     46:         stack_pointer--;
                     47:         return stack[stack_pointer];
                     48:     }
                     49:     return new_cmo_null();
                     50: }
                     51:
                     52: void pops(int n)
                     53: {
                     54:     stack_pointer -= n;
                     55:     if (stack_pointer < 0) {
                     56:         stack_pointer = 0;
                     57:     }
                     58: }
                     59:
1.9     ! takayama   60: #define OX_XSAMPLE_VERSION 0x11121400
        !            61: #define ID_STRING  "1999/12/14 15:25:00"
1.1       ohara      62:
                     63: int sm_mathcap()
                     64: {
1.9     ! takayama   65:     mathcap_init(OX_XSAMPLE_VERSION, ID_STRING, "ox_Xsample", NULL, NULL);
1.6       ohara      66:     push(oxf_cmo_mathcap(fd_rw));
1.1       ohara      67:     return 0;
                     68: }
                     69:
                     70: int sm_popCMO()
                     71: {
                     72:     cmo* m = pop();
                     73:
                     74:     if (m != NULL) {
                     75:         send_ox_cmo(fd_rw, m);
                     76:         return 0;
                     77:     }
                     78:     return SM_popCMO;
                     79: }
                     80:
                     81: cmo_error2 *make_error2(int code)
                     82: {
1.3       ohara      83:     return new_cmo_int32(-1);
1.1       ohara      84: }
                     85:
                     86: int get_i()
                     87: {
1.3       ohara      88:     cmo *c = pop();
                     89:     if (c->tag == CMO_INT32) {
                     90:         return ((cmo_int32 *)c)->i;
                     91:     }else if (c->tag == CMO_ZZ) {
                     92:         return mpz_get_si(((cmo_zz *)c)->mpz);
                     93:     }
                     94:     make_error2(-1);
                     95:     return 0;
1.1       ohara      96: }
                     97:
                     98: int get_xy(int *x, int *y)
                     99: {
1.6       ohara     100:     pop();
1.3       ohara     101:     *x = get_i();
                    102:     *y = get_i();
1.1       ohara     103: }
                    104:
                    105: int my_setpixel()
                    106: {
1.3       ohara     107:     int x, y;
                    108:     get_xy(&x, &y);
                    109:     setpixel(x, y);
                    110:     push(new_cmo_int32(0));
1.1       ohara     111: }
                    112:
                    113: int my_moveto()
                    114: {
1.3       ohara     115:     int x, y;
                    116:     get_xy(&x, &y);
                    117:     moveto(x, y);
                    118:     push(new_cmo_int32(0));
1.1       ohara     119: }
                    120:
                    121: int my_lineto()
                    122: {
1.3       ohara     123:     int x, y;
                    124:     get_xy(&x, &y);
                    125:     lineto(x, y);
                    126:     push(new_cmo_int32(0));
1.1       ohara     127: }
                    128:
                    129: int my_clear()
                    130: {
1.3       ohara     131:     /* dummy */
                    132:     pop();
                    133:     push(new_cmo_int32(0));
1.1       ohara     134: }
                    135:
                    136: int sm_executeFunction()
                    137: {
1.3       ohara     138:     cmo_string *func = (cmo_string *)pop();
                    139:     if (func->tag != CMO_STRING) {
                    140:         push(make_error2(0));
                    141:         return -1;
                    142:     }
                    143:     if (strcmp(func->s, "setpixel") == 0) {
                    144:         my_setpixel();
                    145:     }else if (strcmp(func->s, "moveto") == 0) {
                    146:         my_moveto();
                    147:     }else if (strcmp(func->s, "lineto") == 0) {
                    148:         my_lineto();
                    149:     }else if (strcmp(func->s, "clear") == 0) {
                    150:         my_clear();
                    151:     }else {
                    152:         push(make_error2(0));
                    153:         return -1;
                    154:     }
1.1       ohara     155: }
                    156:
                    157:
                    158: int receive_and_execute_sm_command()
                    159: {
                    160:     int code = receive_int32(fd_rw);
                    161:     switch(code) {
                    162:     case SM_popCMO:
                    163:         sm_popCMO();
                    164:         break;
                    165:     case SM_executeFunction:
                    166:         sm_executeFunction();
                    167:         break;
                    168:     case SM_mathcap:
                    169:         sm_mathcap();
                    170:         break;
                    171:     case SM_setMathCap:
                    172:         pop();
                    173:         break;
                    174:     default:
1.7       ohara     175:                ;
1.1       ohara     176:     }
                    177: }
                    178:
                    179: int receive()
                    180: {
                    181:     int tag;
                    182:
                    183:     tag = receive_ox_tag(fd_rw);
                    184:     switch(tag) {
                    185:     case OX_DATA:
                    186:         push(receive_cmo(fd_rw));
                    187:         break;
                    188:     case OX_COMMAND:
                    189:         receive_and_execute_sm_command();
                    190:         break;
                    191:     default:
1.7       ohara     192:                ;
1.1       ohara     193:     }
                    194:     return 0;
                    195: }
                    196:
                    197: int main()
                    198: {
1.5       ohara     199:     ox_stderr_init(stderr);
1.1       ohara     200:     initialize_stack();
                    201:
1.3       ohara     202:        fd_rw = oxf_open(3);
                    203:        oxf_determine_byteorder_server(fd_rw);
                    204:
                    205:     gopen();
1.1       ohara     206:     while(1) {
                    207:         receive();
1.3       ohara     208:         gFlush();
1.1       ohara     209:     }
1.3       ohara     210:     gclose();
1.1       ohara     211: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>