/* ustream_getc - unnamed stream getc routine */ static int ustream_getc(xlValue fptr) { xlValue head = xlGetStrHead(fptr); xlFIXTYPE iptr; xlValue buf; int ch; /* check for no buffer */ if (head == xlNil) ch = EOF; else { buf = xlCar(head); iptr = xlGetFixnum(xlGetStrIPtr(fptr)); ch = xlGetString(buf)[iptr++]; if (head == xlGetStrTail(fptr)) { if (iptr >= xlGetFixnum(xlGetStrOPtr(fptr))) { xlSetStrHead(fptr,xlNil); xlSetStrTail(fptr,xlNil); xlSetStrOPtr(fptr,xlMakeSmallFixnum(0)); iptr = 0; } } else if (iptr >= xlGetSLength(buf)) { xlSetStrHead(fptr,xlCdr(head)); iptr = 0; } xlSetStrIPtr(fptr,xlMakeFixnum(iptr)); } return ch; }
/* xRichGetSelectedText - built-in function 'get-selected-text' */ static xlValue xRichGetSelectedText(void) { DWORD start,end,length; xlValue obj,str; Widget *widget; /* parse the arguments */ obj = xlGetArgInstance(c_basicEditText); xlLastArg(); /* get the widget structure */ widget = GetWidget(obj); /* get the selection */ SendMessage(widget->wnd,EM_GETSEL,(WPARAM)&start,(LPARAM)&end); /* compute the text length */ if ((length = end - start) < 0) return xlNil; /* allocate a string and get the selection */ xlCPush(obj); str = xlNewString((xlFIXTYPE)length); SendMessage(widget->wnd,EM_GETSELTEXT,0,(LPARAM)xlGetString(str)); xlDrop(1); /* return the string */ return str; }
/* ustream_putc - unnamed stream putc */ static void ustream_putc(xlValue fptr,int ch) { xlValue tail = xlGetStrTail(fptr); xlFIXTYPE optr; xlValue buf; /* check for no buffer */ if (tail == xlNil) { xlCPush(fptr); buf = xlNewString(xlUSTRBUFSIZE); xlSetStrHead(fptr,xlCons(buf,xlNil)); xlSetStrTail(fptr,xlGetStrHead(fptr)); xlSetStrIPtr(fptr,xlMakeSmallFixnum(0)); optr = 0; xlDrop(1); } else { buf = xlCar(tail); optr = xlGetFixnum(xlGetStrOPtr(fptr)); if (optr >= xlGetSLength(buf)) { xlCPush(fptr); buf = xlNewString(xlUSTRBUFSIZE); xlSetCdr(tail,xlCons(buf,xlNil)); xlSetStrTail(fptr,xlCdr(tail)); optr = 0; xlDrop(1); } } /* put the next character in the stream */ xlGetString(buf)[optr++] = ch; xlSetStrOPtr(fptr,xlMakeFixnum(optr)); }
/* xMComInitialize - built-in method 'initialize' */ static xlValue xMComInitialize(void) { char *port = NULL; CommConnection *c; xlValue obj; long addr; /* parse the arguments */ obj = xlGetArgInstance(c_connection); xlVal = xlGetArgFixnum(); addr = (long)xlGetFixnum(xlVal); if (xlMoreArgsP()) { xlVal = xlGetArgFixnum(); addr = afVirtualAddr(addr,(long)xlGetFixnum(xlVal)); if (xlMoreArgsP()) port = xlGetString(xlGetArgString()); } xlLastArg(); /* default the port to the value of the environment variable MD_PORT */ if (!port) port = getenv("MD_PORT"); /* try to open the connection */ if (!port || !(c = mcomOpen(port,addr))) return xlNil; /* return the connection */ SetConnection(obj,c); return obj; }
/* xlMakeCString - convert a c string to a string node */ xlEXPORT xlValue xlMakeCString(char *str) { size_t len = strlen(str); xlValue val = xlNewString((xlFIXTYPE)len); memcpy(xlGetString(val),str,len); return val; }
/* xlNewString - allocate and initialize a new string */ xlEXPORT xlValue xlNewString(xlFIXTYPE size) { xlValue val; if (size < 0) xlError("string length negative",xlMakeFixnum(size)); val = allocvector(xlSTRING,xlByteToWordSize(size + 1)); xlGetString(val)[size] = '\0'; /* in case we need to use it as a c string */ val->value.vector.size = size; return val; }
/* xRichSaveFile - built-in function 'save-file' */ static xlValue xRichSaveFile(void) { xlValue obj; /* parse the arguments */ obj = xlGetArgInstance(c_richEditText); xlVal = xlGetArgString(); xlLastArg(); /* save the edit text control into the file */ return RichSaveFile(GetWidget(obj),xlGetString(xlVal)) ? xlTrue : xlFalse; }
/* xRichLoadFile - built-in function 'load-file!' */ static xlValue xRichLoadFile(void) { xlValue obj; /* parse the arguments */ obj = xlGetArgInstance(c_richEditText); xlVal = xlGetArgString(); xlLastArg(); /* load the file */ return RichLoadFile(GetWidget(obj),xlGetString(xlVal)) ? xlTrue : xlFalse; }
/* xReplaceSelection - built-in function 'replace-selection!' */ static xlValue xReplaceSelection(void) { xlValue obj,str; int undoP; /* parse the arguments */ obj = xlGetArgInstance(c_basicEditText); str = xlGetArgString(); undoP = xlMoreArgsP() ? xlGetArg() != xlFalse : TRUE; xlLastArg(); /* get the selection */ SendMessage(GetWidget(obj)->wnd,EM_REPLACESEL,(WPARAM)undoP,(LPARAM)xlGetString(str)); /* return the object */ return obj; }
/* xGetSelectedText - built-in function 'get-selected-text' */ static xlValue xGetSelectedText(void) { DWORD start,end,length; xlValue obj,str; Widget *widget; HLOCAL hText; char *p; /* parse the arguments */ obj = xlGetArgInstance(c_basicEditText); xlLastArg(); /* get the widget structure */ widget = GetWidget(obj); /* get the selection */ SendMessage(widget->wnd,EM_GETSEL,(WPARAM)&start,(LPARAM)&end); /* compute the text length */ if ((length = end - start) < 0) return xlNil; /* get a handle to the edit text */ if ((hText = (HLOCAL)SendMessage(widget->wnd,EM_GETHANDLE,0,0)) == NULL) return xlNil; /* allocate a string and get the selection */ xlCPush(obj); str = xlNewString((xlFIXTYPE)length); if ((p = (char *)LocalLock(hText)) == NULL) { DisplayLastError(); str = xlNil; } else { memcpy(xlGetString(str),p,length); LocalUnlock(hText); } xlDrop(1); /* return the string */ return str; }
/* xlGetStrOutput - get the output stream string */ xlEXPORT xlValue xlGetStrOutput(xlValue stream) { xlFIXTYPE length; char *dst; xlValue val; /* compute the length of the stream */ length = xlGetStrLength(stream); /* create a new string */ xlCPush(stream); val = xlNewString(length); xlDrop(1); /* copy the characters into the new string */ dst = xlGetString(val); while (--length >= 0) *dst++ = xlGetC(stream); /* return the string */ return val; }
void xlDebug(const XLpath file, XLint line) { XLerror *error; error = xlGetError(); if(error->value != XL_ERROR_VALUE_NONE) xlLog(L"XL debug:%s:%i:XL error:%s:%s:%i:%i:%ls\n", file, line, error->file, error->function, error->line, error->value, xlGetString(error->value)); }
/* MakeEditText - make an edit text or rich edit text control */ static xlValue MakeEditText(xlValue obj,int type) { xlValue hscrollp,vscrollp,autohscrollp,autovscrollp; xlValue label,multilinep,justify,borderp; WidgetPosition pos; FrameWidget *parent; Widget *widget; DWORD style; HWND wnd; /* parse the arguments */ parent = GetArgFrameWidget(); xlGetKeyArg(k_label,xlNil,&label); xlGetKeyArg(k_hscrollp,xlFalse,&hscrollp); xlGetKeyArg(k_vscrollp,xlFalse,&vscrollp); xlGetKeyArg(k_autohscrollp,xlFalse,&autohscrollp); xlGetKeyArg(k_autovscrollp,xlFalse,&autovscrollp); xlGetKeyArg(k_multilinep,xlFalse,&multilinep); xlGetKeyArg(k_justify,s_left,&justify); xlGetKeyArg(k_borderp,xlTrue,&borderp); GetWidgetPosition(&pos); /* setup the style */ style = WS_CHILD | WS_VISIBLE | ES_NOHIDESEL; if (type == wtEditText) style |= DS_LOCALEDIT; if (hscrollp != xlFalse) style |= WS_HSCROLL; if (vscrollp != xlFalse) style |= WS_VSCROLL; if (autohscrollp != xlFalse) style |= ES_AUTOHSCROLL; if (autovscrollp != xlFalse) style |= ES_AUTOVSCROLL; if (multilinep != xlFalse) style |= ES_MULTILINE; if (justify == s_left) style |= ES_LEFT; else if (justify == s_right) style |= ES_RIGHT; else if (justify == s_center) style |= ES_CENTER; else xlError("expecting 'left, 'right or 'center",justify); if (borderp != xlFalse) style |= WS_BORDER; /* allocate the widget data structure */ if (!(widget = MakeWidget(parent,type,sizeof(Widget)))) return xlNil; widget->position = pos; /* make the button */ wnd = CreateWindow(type == wtEditText ? "edit" : "RichEdit", xlStringP(label) ? xlGetString(label) : "Edit Text", style, pos.x, /* horizontal position */ pos.y, /* vertical position */ pos.width, /* width */ pos.height, /* height */ GetParentWnd(parent), (HMENU)widget->childID, hInstance, NULL); /* make sure the widget was created */ if ((widget->wnd = wnd) == NULL) { FreeWidget(widget); return xlNil; } /* store the widget data structure pointer */ SetWindowLong(wnd,GWL_USERDATA,(long)widget); /* setup to filter messages */ widget->wndProc = (WNDPROC)SetWindowLong(widget->wnd,GWL_WNDPROC,(LONG)TextFilter); /* set the font */ SendMessage(wnd,WM_SETFONT,(WPARAM)GetStockObject(ANSI_FIXED_FONT),0); /* return the widget object */ SetWidget(obj,widget); return obj; }
/* xCollectExpression - built-in function 'collect-expression' */ static xlValue xCollectExpression(void) { int gotSomethingP = FALSE; int inStringP = FALSE; int parenLevel = 0; char *buffer,*p; xlValue str; int ch; /* parse the argument list */ str = xlGetArgString(); xlLastArg(); /* initialize the string pointers to start at the end */ buffer = xlGetString(str); p = buffer + strlen(buffer); /* skip initial spaces */ while (p > buffer && isspace(p[-1])) --p; while (p > buffer) { ch = *--p; if (p >= buffer && *p == '\\') gotSomethingP = TRUE; else { if (inStringP) { switch (ch) { case '"': gotSomethingP = TRUE; inStringP = FALSE; if (parenLevel == 0) goto done; break; } } else { switch (ch) { case ')': ++parenLevel; break; case '(': if (parenLevel == 0) { ++p; goto done; } else if (--parenLevel == 0) { gotSomethingP = TRUE; goto done; } break; case '"': inStringP = TRUE; break; default: if (isspace(ch)) { if (parenLevel == 0) goto done; } else if (parenLevel == 0) gotSomethingP = TRUE; break; } } } } done: /* return the expression */ return gotSomethingP ? xlMakeCString(p) : xlNil; }
void myModel(XLenum mode, const XLpath xlpath, XLint t) { XLmodel model; XLmetaheader *metaheader; XLmetadata *metadata; xlMemoryZero(&model, sizeof(XLmodel)); metaheader = &model.header.metaheader; metadata = &model.header.metadata; xlMetaHeaderSet(metaheader, XL_MODEL_METAHEADER_MAGIC, xlpath); if(mode == XL_DATA_META || mode == XL_DATA_METAHEADER) { xlMetHeadType(metaheader); if(mode == XL_DATA_METAHEADER) return; } switch(t) { case 0: xlStringCopy(metadata->name, L"Demo"); break; default: xlSetError(XL_ERROR_VALUE_INVALID_PARAM); break; } xlStringCopy(metadata->version, xlGetString(XL_VERSION)); xlStringCopy(metadata->author, xlGetString(XL_AUTHOR)); xlStringCopy(metadata->date, xlGetString(XL_DATE)); xlStringCopy(metadata->copyright, xlGetString(XL_COPYRIGHT)); xlStringCopy(metadata->license, xlGetString(XL_LICENSE)); xlStringCopy(metadata->url, xlGetString(XL_URL)); xlStringCopy(metadata->email, xlGetString(XL_EMAIL)); xlStringCopy(metadata->tool, xlGetString(XL_TOOL)); xlStringCopy(metadata->comment, L"Proceduralized"); xlStringCopy(metadata->xl, xlGetString(XL_VERSION)); if(mode == XL_DATA_META || mode == XL_DATA_METADATA) { xlMetDatType(metadata); if(mode == XL_DATA_METADATA) return; } if(mode == XL_DATA_META) return; switch(t) { case 0: { XLuint i; model.header.bones = 1; model.header.parents = 0; model.header.vertices = 8; model.header.tags = 0; model.header.weights = 1; model.header.frames = 100; model.header.actions = 1; model.body.object = xlAlloc(1 * sizeof(XLModelObject)); xlPathZero(model.body.object->path); xlPathCopy(model.body.object->path, "Cube.xlc"); model.body.bones = xlAlloc(model.header.bones * sizeof(XLModelBone)); xlStringZero(model.body.bones[0].name); xlStringCopy(model.body.bones[0].name, L"Bone"); xlRangeLoad(&model.body.bones[0].parents, 0, 0); model.body.vertices = xlAlloc(model.header.vertices * sizeof(XLModelVertex)); for(i = 0; i < model.header.vertices; i++) { xlRangeLoad(&model.body.vertices[i].weights, 0, 1); } model.body.weights = xlAlloc(model.header.weights * sizeof(XLModelWeight)); model.body.weights[0].bone = 0; model.body.weights[0].value = XL_REAL(1); model.body.frames = xlAlloc(model.header.frames * sizeof(XLModelFrame)); XLmatrix matrix; XLvector vector; xlMatrixAssignIdentity(&matrix); xlVectorAssignReals(&vector, XL_REAL(0.95), XL_REAL(0.95), XL_REAL(0.95)); model.body.frames[0].bones = xlAlloc(model.header.bones * sizeof(XLModelBoneFrame)); xlMatrixAssignIdentity(&model.body.frames[0].bones[0].matrix); xlMatrixAssignIdentity(&model.body.frames[0].matrix); model.body.frames[0].time = XL_REAL(1); for(i = 1; i < model.header.frames / 2; i++) { model.body.frames[i].bones = xlAlloc(model.header.bones * sizeof(XLModelBoneFrame)); xlMatrixAssignCopy(&model.body.frames[i].bones[0].matrix, &matrix); xlMatrixAssignIdentity(&model.body.frames[i].matrix); model.body.frames[i].time = XL_REAL(1) / model.header.frames; xlMatrixScale(&matrix, &vector); } xlVectorAssignReals(&vector, XL_REAL(1.05), XL_REAL(1.05), XL_REAL(1.05)); model.body.frames[i].bones = xlAlloc(model.header.bones * sizeof(XLModelBoneFrame)); xlMatrixAssignCopy(&model.body.frames[i].bones[0].matrix, &matrix); xlMatrixAssignIdentity(&model.body.frames[i].matrix); model.body.frames[i].time = XL_REAL(1); for(i = model.header.frames / 2 + 1; i < model.header.frames; i++) { model.body.frames[i].bones = xlAlloc(model.header.bones * sizeof(XLModelBoneFrame)); xlMatrixAssignCopy(&model.body.frames[i].bones[0].matrix, &matrix); xlMatrixAssignIdentity(&model.body.frames[i].matrix); model.body.frames[i].time = XL_REAL(1) / model.header.frames; xlMatrixScale(&matrix, &vector); } model.body.actions = xlAlloc(model.header.actions * sizeof(XLModelAction)); xlStringZero(model.body.actions[0].name); xlStringCopy(model.body.actions[0].name, L"sy"); xlRangeLoad(&model.body.actions[0].frames, 0, model.header.frames); model.runtime.morphfactor = XL_REAL(1); break; } default: xlSetError(XL_ERROR_VALUE_INVALID_PARAM); break; } xlModType(&model); }
/* xlCopyString - copy a string */ xlEXPORT xlValue xlCopyString(xlValue str) { return xlMakeString(xlGetString(str),xlGetSLength(str)); }
/* xlMakeString - convert a string to a string node */ xlEXPORT xlValue xlMakeString(char *str,xlFIXTYPE len) { xlValue val = xlNewString(len); memcpy(xlGetString(val),str,(size_t)len); return val; }