|
| 1 | +// LibGAP API - API for using GAP as shared library. |
| 2 | + |
| 3 | +#include "libgap-api.h" |
| 4 | + |
| 5 | +#include "bool.h" |
| 6 | +#include "opers.h" |
| 7 | +#include "calls.h" |
| 8 | +#include "gapstate.h" |
| 9 | +#include "gvars.h" |
| 10 | +#include "lists.h" |
| 11 | +#include "streams.h" |
| 12 | +#include "stringobj.h" |
| 13 | + |
| 14 | +// |
| 15 | +// Setup and initialisation |
| 16 | +// |
| 17 | +void GAP_Initialize(int argc, |
| 18 | + char ** argv, |
| 19 | + char ** env, |
| 20 | + CallbackFunc markBagsCallback, |
| 21 | + CallbackFunc errorCallback) |
| 22 | +{ |
| 23 | + InitializeGap(&argc, argv, env); |
| 24 | + SetExtraMarkFuncBags(markBagsCallback); |
| 25 | + STATE(JumpToCatchCallback) = errorCallback; |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +UInt GAP_List_Length(Obj list) |
| 30 | +{ |
| 31 | + return LEN_LIST(list); |
| 32 | +} |
| 33 | + |
| 34 | +Obj GAP_List_AtPosition(Obj list, Int pos) |
| 35 | +{ |
| 36 | + return ELM_LIST(list, pos); |
| 37 | +} |
| 38 | + |
| 39 | +UInt GAP_String_Length(Obj string) |
| 40 | +{ |
| 41 | + return GET_LEN_STRING(string); |
| 42 | +} |
| 43 | + |
| 44 | +Int GAP_String_GetCString(Obj string, Char * buffer, UInt n) |
| 45 | +{ |
| 46 | + Obj copy; |
| 47 | + UInt len; |
| 48 | + |
| 49 | + if (IS_STRING(string)) { |
| 50 | + copy = CopyToStringRep(string); |
| 51 | + len = GET_LEN_STRING(copy) + 1; |
| 52 | + if (len >= n) |
| 53 | + len = n-1; |
| 54 | + // Have to use mempcy because GAP strings can contain |
| 55 | + // \0. |
| 56 | + memcpy(buffer, CSTR_STRING(copy), len); |
| 57 | + if (len == n-1) |
| 58 | + buffer[n] = '\0'; |
| 59 | + return 1; |
| 60 | + } |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// Combines GVarName and ValGVar. For a given string, it returns the value |
| 66 | +// of the gvar with name <name>, or NULL if the global variable is not |
| 67 | +// defined. |
| 68 | +Obj GAP_ValueGlobalVariable(const char * name) |
| 69 | +{ |
| 70 | + UInt gvar = GVarName(name); |
| 71 | + // TODO: GVarName should never return 0? |
| 72 | + if (gvar != 0) { |
| 73 | + return ValGVar(gvar); |
| 74 | + } |
| 75 | + else { |
| 76 | + return NULL; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// |
| 81 | +// Evaluate a string of GAP commands |
| 82 | +// |
| 83 | +Obj GAP_EvalString(const char * cmd) |
| 84 | +{ |
| 85 | + Obj instream; |
| 86 | + Obj res; |
| 87 | + Obj viewObjFunc, streamFunc; |
| 88 | + |
| 89 | + streamFunc = GAP_ValueGlobalVariable("InputTextString"); |
| 90 | + viewObjFunc = GAP_ValueGlobalVariable("ViewObj"); |
| 91 | + |
| 92 | + instream = DoOperation1Args(streamFunc, MakeString(cmd)); |
| 93 | + res = READ_ALL_COMMANDS(instream, False, True, viewObjFunc); |
| 94 | + return res; |
| 95 | +} |
0 commit comments