1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
#include <stdio.h> |
21 |
#include <string.h> |
22 |
|
23 |
#include <utilities/ascConfig.h> |
24 |
#include <utilities/error.h> |
25 |
#include <general/ospath.h> |
26 |
|
27 |
#include <compiler/importhandler.h> |
28 |
|
29 |
ImportHandlerCreateFilenameFn extpy_filename; |
30 |
ImportHandlerImportFn extpy_import; |
31 |
|
32 |
#ifndef ASC_EXPORT |
33 |
# error "Where is ASC_EXPORT?" |
34 |
#endif |
35 |
|
36 |
/** |
37 |
This is the function called from "IMPORT extpy" |
38 |
|
39 |
It sets up the functions in this external function library |
40 |
*/ |
41 |
extern ASC_EXPORT(int) extpy_register(){ |
42 |
int result = 0; |
43 |
|
44 |
ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Hello from EXTPY..."); |
45 |
|
46 |
struct ImportHandler *handler; |
47 |
handler = ASC_NEW(struct ImportHandler); |
48 |
|
49 |
handler->name = "extpy"; |
50 |
handler->filenamefn = extpy_filename; |
51 |
handler->importfn = extpy_import; |
52 |
|
53 |
result = importhandler_add(handler); |
54 |
|
55 |
if(result){ |
56 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to register import handler (error = %d)",result); |
57 |
} |
58 |
return result; |
59 |
} |
60 |
|
61 |
/** |
62 |
Create a filename base on a partial filename. In that case of python, this |
63 |
just means adding '.py' to the end. |
64 |
|
65 |
@param partialname the filename without suffix, as specified in the user's "IMPORT" command |
66 |
@return new filename, or NULL on failure |
67 |
*/ |
68 |
char *extpy_filename(const char *partialname){ |
69 |
char *name; |
70 |
int len; |
71 |
if(partialname==NULL){ |
72 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Partial name is NULL, can't work out filename"); |
73 |
return NULL; |
74 |
} |
75 |
|
76 |
len = strlen(partialname); |
77 |
name = ASC_NEW_ARRAY_CLEAR(char,len+4); |
78 |
strcpy(name,partialname); |
79 |
strcat(name,".py"); |
80 |
CONSOLE_DEBUG("New filename is %s",name); |
81 |
return name; |
82 |
} |
83 |
|
84 |
/** |
85 |
Import a python script located at the path indicated. |
86 |
|
87 |
@return 0 on success, else error codes (TBD) |
88 |
*/ |
89 |
int extpy_import(struct FilePath *fp, void *user_data){ |
90 |
char *name; |
91 |
name = ospath_str(fp); |
92 |
CONSOLE_DEBUG("IMPORTING PYTHON SCRIPT %s",name); |
93 |
ASC_FREE(name); |
94 |
return 0; |
95 |
} |
96 |
|