1 | #define INCL_DOS |
---|
2 | #include <os2.h> |
---|
3 | |
---|
4 | #include "pluginman.h" |
---|
5 | #include "luutils.h" |
---|
6 | #include <dos.h> |
---|
7 | #include <ludoc.xh> |
---|
8 | |
---|
9 | |
---|
10 | PluginManager::PluginManager() |
---|
11 | { |
---|
12 | plugins = new PluginInfoList; |
---|
13 | |
---|
14 | // Find exe path |
---|
15 | char buffer[ _MAX_PATH ]; |
---|
16 | char drive[ _MAX_DRIVE ]; |
---|
17 | char dir[ _MAX_DIR ]; |
---|
18 | _splitpath( __argv[0], drive, dir, NULL, NULL ); |
---|
19 | _makepath( buffer, drive, dir, NULL, NULL ); |
---|
20 | strcat( buffer, "\\LU*.DLL" ); |
---|
21 | |
---|
22 | // enum plugins, except for LUDOC.DLL, which is 'null' plugin |
---|
23 | struct find_t ffblk; |
---|
24 | unsigned done = _dos_findfirst( buffer, _A_RDONLY | _A_NORMAL, &ffblk ); |
---|
25 | while ( done == 0 ) |
---|
26 | { |
---|
27 | if ( stricmp( ffblk.name, "LUDOC.DLL" ) != 0 ) { |
---|
28 | loadPlugin( ffblk.name ); |
---|
29 | } |
---|
30 | done = _dos_findnext( &ffblk ); |
---|
31 | } |
---|
32 | _dos_findclose( &ffblk ); |
---|
33 | } |
---|
34 | |
---|
35 | PluginManager::~PluginManager() |
---|
36 | { |
---|
37 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
38 | { |
---|
39 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
40 | DosFreeModule( pi->handle ); |
---|
41 | //printf( "NAME: %s EXT: %s DESC: %s\n", pi->name.c_str(), |
---|
42 | // pi->extensions.c_str(), pi->description.c_str() ); |
---|
43 | } |
---|
44 | delete plugins; |
---|
45 | } |
---|
46 | |
---|
47 | void PluginManager::loadPlugin( const char *dllname ) |
---|
48 | { |
---|
49 | // Function pointer variables |
---|
50 | LuDocument * APIENTRY (*pCreateObject)(); |
---|
51 | char * APIENTRY (*pGetSupportedExtensions)(); |
---|
52 | char * APIENTRY (*pGetDescription)(); |
---|
53 | |
---|
54 | // cut DLL name at last point |
---|
55 | char *lpoint = strrchr( dllname, '.' ); |
---|
56 | *lpoint = '\0'; |
---|
57 | |
---|
58 | // load a DLL |
---|
59 | HMODULE h = NULLHANDLE; |
---|
60 | bool res = false; |
---|
61 | do |
---|
62 | { |
---|
63 | if ( DosLoadModule( NULL, 0, dllname, &h ) != 0 ) |
---|
64 | break; |
---|
65 | if ( DosQueryProcAddr( h, 0, "createObject", (PFN *)&pCreateObject ) != 0 ) |
---|
66 | break; |
---|
67 | if ( DosQueryProcAddr( h, 0, "getSupportedExtensions", (PFN *)&pGetSupportedExtensions ) != 0 ) |
---|
68 | break; |
---|
69 | if ( DosQueryProcAddr( h, 0, "getDescription", (PFN *)&pGetDescription ) != 0 ) |
---|
70 | break; |
---|
71 | |
---|
72 | res = true; |
---|
73 | } while (0); |
---|
74 | |
---|
75 | if ( res ) |
---|
76 | { |
---|
77 | PluginInfo pi; |
---|
78 | pi.handle = h; |
---|
79 | pi.name = dllname; |
---|
80 | pi.extensions = pGetSupportedExtensions(); |
---|
81 | pi.description = pGetDescription(); |
---|
82 | |
---|
83 | plugins->push_back( pi ); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | // returns NULL if not suitable plugin found |
---|
89 | LuDocument *PluginManager::createDocumentForExt( const char *ext ) |
---|
90 | { |
---|
91 | LuDocument * APIENTRY (*pCreateObject)(); |
---|
92 | |
---|
93 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
94 | { |
---|
95 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
96 | |
---|
97 | char *cExt = new char[ strlen( ext ) + 5 ]; |
---|
98 | strcpy( cExt, ";" ); |
---|
99 | strcat( cExt, ext ); |
---|
100 | strcat( cExt, ";" ); |
---|
101 | strupr( cExt ); |
---|
102 | |
---|
103 | char *cExts = new char[ pi->extensions.length() + 5 ]; |
---|
104 | strcpy( cExts, ";" ); |
---|
105 | strcat( cExts, pi->extensions.c_str() ); |
---|
106 | strcat( cExts, ";" ); |
---|
107 | strupr( cExts ); |
---|
108 | |
---|
109 | if ( strstr( cExts, cExt ) != NULL ) |
---|
110 | { |
---|
111 | if ( DosQueryProcAddr( pi->handle, 0, "createObject", |
---|
112 | (PFN *)&pCreateObject ) == 0 ) |
---|
113 | { |
---|
114 | delete cExt; |
---|
115 | delete cExts; |
---|
116 | return pCreateObject(); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | delete cExt; |
---|
121 | delete cExts; |
---|
122 | } |
---|
123 | |
---|
124 | return NULL; |
---|
125 | } |
---|
126 | |
---|
127 | std::string PluginManager::getExtsMask() |
---|
128 | { |
---|
129 | std::string cRet = ""; |
---|
130 | |
---|
131 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
132 | { |
---|
133 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
134 | char *tmpexts = newstrdup( pi->extensions.c_str() ); |
---|
135 | char *p = strtok( tmpexts, ";" ); |
---|
136 | while( p != NULL ) { |
---|
137 | cRet += "*."; |
---|
138 | cRet += p; |
---|
139 | cRet += ";"; |
---|
140 | p = strtok( NULL, ";" ); |
---|
141 | } |
---|
142 | } |
---|
143 | return cRet; |
---|
144 | } |
---|
145 | |
---|