source: git/modules/python/python.cc @ 606831

fieker-DuValspielwiese
Last change on this file since 606831 was 9f4d5f, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: cygwin + inc paths git-svn-id: file:///usr/local/Singular/svn/trunk@8681 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * This was automatically generated by modgen
3 * version 0.2
4 * module python
5 * Don't edit this file
6 */
7
8
9#line 10 "python.cc"
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include <ctype.h>
14
15#include <unistd.h>
16#include <sys/stat.h>
17#include <boost/python.hpp>
18#include <Python.h>
19#include <mod2.h>
20#include <tok.h>
21#include <structs.h>
22#include <ipid.h>
23
24#include <locals.h>
25#include <omalloc.h>
26#include "python.h"
27#line 19 "python.cc"
28
29idhdl enter_id(char *name, char *value, idtyp t)
30{
31  idhdl h;
32
33  h=enterid(omStrDup(name),0, t, &(currPack->idroot), TRUE/*FALSE*/);
34  if(h!=NULL) {
35     switch(t) {
36         case STRING_CMD: 
37              omFree(IDSTRING(h));
38              IDSTRING(h) = omStrDup(value);
39              break;
40         case PACKAGE_CMD: break;
41         case PROC_CMD: break;
42     }
43  } else 
44      Warn("Cannot create '%s'\n", name);
45  return(h);
46}
47
48#line 37 "python.cc"
49
50idhdl add_singular_proc(FILE* binfp, char *procname,int line,
51                       long pos, long end, BOOLEAN pstatic)
52{
53  idhdl h;
54  procinfov pi;
55  char *tempstr;
56  h = enter_id(procname, NULL, PROC_CMD);
57  if(h == NULL) return NULL;
58
59  pi = IDPROC(h);
60
61  pi->libname = omStrDup(currPack->libname);
62  pi->procname = omStrDup(procname);
63  pi->language = LANG_SINGULAR;
64  pi->ref = 1;
65  pi->is_static = pstatic;
66  pi->data.s.proc_start = pos;
67  pi->data.s.def_end    = pos;
68  pi->data.s.help_start = 0L;
69  pi->data.s.help_end   = 0L;
70  pi->data.s.body_start = pos;
71  pi->data.s.body_end   = end;
72  pi->data.s.proc_end   = end;
73  pi->data.s.example_start = 0L;
74  pi->data.s.proc_lineno = line;
75  pi->data.s.body_lineno = line;
76  pi->data.s.example_lineno = 0;
77  pi->data.s.help_chksum = 0;
78 
79  rewind(binfp);
80  fseek(binfp,pos,SEEK_CUR);
81  tempstr=(char *)omAlloc(end-pos+2);
82  memset(tempstr,0,end-pos+2);
83  fread(tempstr,sizeof(char),end-pos+1,binfp);
84  pi->data.s.body = omStrDup(tempstr);
85  omFree(tempstr);
86
87  return(h);
88}
89
90unsigned long crccheck(
91  char *file
92  ) 
93{
94  unsigned char buf[BUFLEN1];
95  unsigned long crc = 0;
96  long length = 0;
97  long bytes_read;
98  register FILE *fp;
99
100  // open the file
101  fp = fopen (file, "rb");
102  if (fp == NULL) return 0;
103
104  // read the file chunk by chunk, determine the lenght and
105  // start computing the checksum
106  while ((bytes_read = fread (buf, 1, BUFLEN1, fp)) > 0)
107  {
108    unsigned char *cp = buf;
109    length += bytes_read;
110    while (bytes_read--)
111      crc = (crc << 8) ^ crctab[((crc >> 24) ^ *(cp++)) & 0xFF];
112    }
113
114  // check if something went wrong and close the file
115  if (ferror (fp)) return 0; 
116  if (fclose(fp)==EOF) return 0;
117
118  // second part of checksum computation
119  bytes_read = length;
120  while (bytes_read > 0) {
121    crc = (crc << 8) ^ crctab[((crc >> 24) ^ bytes_read) & 0xFF];
122    bytes_read >>= 8;
123  }
124
125  crc = ~crc & 0xFFFFFFFF;
126
127  return(crc);
128}
129
130void fill_help_package();
131void fill_example_package();
132
133#line 2 "python.mod"
134/*
135 *  $Id: python.cc,v 1.5 2005-10-05 06:46:59 bricken Exp $
136 *
137 *  Test mod fuer modgen
138 */
139
140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
143#include <time.h>
144#include <Python.h>
145#include "wrapper.h"
146
147void mbpython(char* in);
148#line 17 "python.mod"
149
150
151#line 137 "python.cc"
152extern "C" {
153int mod_init(
154  int (*iiAddCproc)(char *libname, char *procname, BOOLEAN pstatic,
155              BOOLEAN(*func)(leftv res, leftv v))
156  )
157{
158  idhdl h;
159  char * tempstr;
160  FILE * binfp; 
161  int ret;
162  struct stat sb; 
163
164  tempstr = (char *)omAlloc(strlen(currPack->libname)+5);
165  memset(tempstr,0,strlen(currPack->libname)+1);
166  memcpy(tempstr,currPack->libname,strlen(currPack->libname));
167  memcpy(tempstr+strlen(currPack->libname)-3,".bin",4);
168  ret=stat(tempstr,&sb);
169  if(ret==0) { 
170    if ((sb.st_mode & S_IFMT) == S_IFREG) { 
171      if (crccheck(tempstr)!=crcsum)
172      {   Warn("file %s does not agree with module version - ignoring file",tempstr);
173          ret=-1;
174      }
175      if ((binfp = fopen(tempstr,"r")) == NULL) return -1;
176    } 
177    else 
178      ret=-1; 
179  }
180
181  fill_help_package();
182  fill_example_package();
183
184#line 20 "python.mod"
185  enter_id("category","tests", STRING_CMD);
186#line 22 "python.mod"
187  enter_id("version","$Id: python.cc,v 1.5 2005-10-05 06:46:59 bricken Exp $", STRING_CMD);
188#line 23 "python.mod"
189  enter_id("info","LIBRARY: kernel.lib  PROCEDURES OF GENERAL TYPE WRITEN IN C python(input); eval a string  in python", STRING_CMD);
190#line 26 "python.mod"
191#line 27 "python.mod"
192  Py_Initialize();
193  initSingular();
194  init_Singular();
195
196#line 36 "python.mod"
197  iiAddCproc(currPack->libname,"python",FALSE, mod_python);
198
199  if(ret!=-1) fclose(binfp);
200  return 0;
201}
202}
203
204#line 187 "python.cc"
205/* Help section */
206void fill_help_package() {
207
208#line 192 "python.cc"
209}  /* End of Help section */
210
211/* Example section */
212void fill_example_package() {
213#line 43 "python.mod"
214  enter_id("python_example",
215 "\
216  python(\"print 1+1\");\
217", STRING_CMD);
218
219
220#line 206 "python.cc"
221} /* End of Example section */
222
223#line 36 "python.mod"
224BOOLEAN mod_python(leftv __res, leftv __h)
225{
226#line 37 "python.mod"
227#line 214 "python.cc"
228 
229#line 38 "python.mod"
230#line 217 "python.cc"
231  leftv __v = __h, __v_save;
232  int __tok = NONE, __index = 0;
233  sleftv __sa; leftv __za = &__sa;
234  char * a;
235#line 38 "python.mod"
236
237
238#line 39 "python.mod"
239#line 226 "python.cc"
240  if(__v==NULL) goto mod_python_error;
241  __tok = __v->Typ();
242  if((__index=iiTestConvert(__tok, STRING_CMD))==0)
243     goto mod_python_error;
244  __v_save = __v->next;
245  __v->next = NULL;
246  if(iiConvert(__tok, STRING_CMD, __index, __v, __za))
247     goto mod_python_error;
248  __v = __v_save;
249  a = (char *)__za->Data();
250  if(__v!=NULL) { __tok = __v->Typ(); goto mod_python_error; }
251
252#line 39 "python.mod"
253
254  mbpython(a);
255#line 41 "python.mod"
256#line 243 "python.cc"
257  __res->data = NULL;
258  __res->rtyp = END_RING;
259  return FALSE;
260
261  mod_python_error:
262    Werror("python(`%s`) is not supported", Tok2Cmdname(__tok));
263    Werror("expected python('string')");
264    return TRUE;
265}
266
267
268#line 254 "python.cc"
269#line 53 "python.mod"
270void mbpython(char* inp){
271  PyRun_SimpleString(inp);
272}
Note: See TracBrowser for help on using the repository browser.