[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.8

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.8     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox_Xsample.c,v 1.7 2004/12/01 17:32:26 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.8     ! ohara      60: #define OX_XSAMPLE_VERSION "1999/12/14 15:25:00"
1.1       ohara      61:
                     62: int sm_mathcap()
                     63: {
1.8     ! ohara      64:     mathcap_init(OX_XSAMPLE_VERSION, "ox_Xsample");
1.6       ohara      65:     push(oxf_cmo_mathcap(fd_rw));
1.1       ohara      66:     return 0;
                     67: }
                     68:
                     69: int sm_popCMO()
                     70: {
                     71:     cmo* m = pop();
                     72:
                     73:     if (m != NULL) {
                     74:         send_ox_cmo(fd_rw, m);
                     75:         return 0;
                     76:     }
                     77:     return SM_popCMO;
                     78: }
                     79:
                     80: cmo_error2 *make_error2(int code)
                     81: {
1.3       ohara      82:     return new_cmo_int32(-1);
1.1       ohara      83: }
                     84:
                     85: int get_i()
                     86: {
1.3       ohara      87:     cmo *c = pop();
                     88:     if (c->tag == CMO_INT32) {
                     89:         return ((cmo_int32 *)c)->i;
                     90:     }else if (c->tag == CMO_ZZ) {
                     91:         return mpz_get_si(((cmo_zz *)c)->mpz);
                     92:     }
                     93:     make_error2(-1);
                     94:     return 0;
1.1       ohara      95: }
                     96:
                     97: int get_xy(int *x, int *y)
                     98: {
1.6       ohara      99:     pop();
1.3       ohara     100:     *x = get_i();
                    101:     *y = get_i();
1.1       ohara     102: }
                    103:
                    104: int my_setpixel()
                    105: {
1.3       ohara     106:     int x, y;
                    107:     get_xy(&x, &y);
                    108:     setpixel(x, y);
                    109:     push(new_cmo_int32(0));
1.1       ohara     110: }
                    111:
                    112: int my_moveto()
                    113: {
1.3       ohara     114:     int x, y;
                    115:     get_xy(&x, &y);
                    116:     moveto(x, y);
                    117:     push(new_cmo_int32(0));
1.1       ohara     118: }
                    119:
                    120: int my_lineto()
                    121: {
1.3       ohara     122:     int x, y;
                    123:     get_xy(&x, &y);
                    124:     lineto(x, y);
                    125:     push(new_cmo_int32(0));
1.1       ohara     126: }
                    127:
                    128: int my_clear()
                    129: {
1.3       ohara     130:     /* dummy */
                    131:     pop();
                    132:     push(new_cmo_int32(0));
1.1       ohara     133: }
                    134:
                    135: int sm_executeFunction()
                    136: {
1.3       ohara     137:     cmo_string *func = (cmo_string *)pop();
                    138:     if (func->tag != CMO_STRING) {
                    139:         push(make_error2(0));
                    140:         return -1;
                    141:     }
                    142:     if (strcmp(func->s, "setpixel") == 0) {
                    143:         my_setpixel();
                    144:     }else if (strcmp(func->s, "moveto") == 0) {
                    145:         my_moveto();
                    146:     }else if (strcmp(func->s, "lineto") == 0) {
                    147:         my_lineto();
                    148:     }else if (strcmp(func->s, "clear") == 0) {
                    149:         my_clear();
                    150:     }else {
                    151:         push(make_error2(0));
                    152:         return -1;
                    153:     }
1.1       ohara     154: }
                    155:
                    156:
                    157: int receive_and_execute_sm_command()
                    158: {
                    159:     int code = receive_int32(fd_rw);
                    160:     switch(code) {
                    161:     case SM_popCMO:
                    162:         sm_popCMO();
                    163:         break;
                    164:     case SM_executeFunction:
                    165:         sm_executeFunction();
                    166:         break;
                    167:     case SM_mathcap:
                    168:         sm_mathcap();
                    169:         break;
                    170:     case SM_setMathCap:
                    171:         pop();
                    172:         break;
                    173:     default:
1.7       ohara     174:                ;
1.1       ohara     175:     }
                    176: }
                    177:
                    178: int receive()
                    179: {
                    180:     int tag;
                    181:
                    182:     tag = receive_ox_tag(fd_rw);
                    183:     switch(tag) {
                    184:     case OX_DATA:
                    185:         push(receive_cmo(fd_rw));
                    186:         break;
                    187:     case OX_COMMAND:
                    188:         receive_and_execute_sm_command();
                    189:         break;
                    190:     default:
1.7       ohara     191:                ;
1.1       ohara     192:     }
                    193:     return 0;
                    194: }
                    195:
                    196: int main()
                    197: {
1.5       ohara     198:     ox_stderr_init(stderr);
1.1       ohara     199:     initialize_stack();
                    200:
1.3       ohara     201:        fd_rw = oxf_open(3);
                    202:        oxf_determine_byteorder_server(fd_rw);
                    203:
                    204:     gopen();
1.1       ohara     205:     while(1) {
                    206:         receive();
1.3       ohara     207:         gFlush();
1.1       ohara     208:     }
1.3       ohara     209:     gclose();
1.1       ohara     210: }

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