| 1 |
/* --------------------------------------------------------------------- */ |
| 2 |
/* NeuroManager - A spike train analysis tool */ |
| 3 |
/* Copyright (c) 2005-2006 RIKEN, Japan. All rights reserved. */ |
| 4 |
/* http://satellite.sourceforge.jp/ */ |
| 5 |
/* --------------------------------------------------------------------- */ |
| 6 |
/* This program is free software; you can redistribute it and/or */ |
| 7 |
/* modify it under the terms of the GNU General Public License */ |
| 8 |
/* as published by the Free Software Foundation; either version 2 */ |
| 9 |
/* of the License, or (at your option) any later version. */ |
| 10 |
/* */ |
| 11 |
/* This program is distributed in the hope that it will be useful, */ |
| 12 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 13 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 14 |
/* GNU General Public License for more details. */ |
| 15 |
/* */ |
| 16 |
/* You should have received a copy of the GNU General Public License */ |
| 17 |
/* along with this program; see the file COPYING.txt. If not, write */ |
| 18 |
/* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth */ |
| 19 |
/* Floor, Boston, MA 02110-1301, USA. */ |
| 20 |
/* --------------------------------------------------------------------- */ |
| 21 |
|
| 22 |
/* $Id: MCDataManager.cpp,v 1.5 2006/03/20 09:09:43 orrisroot Exp $ */ |
| 23 |
|
| 24 |
// MCDataManager.cpp : implementation of the CMCDataManager class |
| 25 |
// |
| 26 |
|
| 27 |
#include "stdafx.h" |
| 28 |
#include "MCDataManager.h" |
| 29 |
|
| 30 |
#define MCDATA_PLUGINS "\\plugins\\mcdata" |
| 31 |
#define MCDATA_TITLE_FUNCNAME "GetDataTypeTitle" |
| 32 |
#define MCDATA_FILTER_FUNCNAME "GetDataTypeFilter" |
| 33 |
#define MCDATA_CREATE_FUNCNAME "CreateDataTypeInstance" |
| 34 |
static sl4_list_t *get_data_plugins(); |
| 35 |
static sl4_string_t *get_data_plugin_path(); |
| 36 |
static sl4_string_t *get_module_path(); |
| 37 |
static int is_directory( const char *path ); |
| 38 |
|
| 39 |
#define MAX_FILE_TYPES 100 |
| 40 |
|
| 41 |
CMCDataManager::CMCDataManager( void ) |
| 42 |
{ |
| 43 |
m_arDataType = NULL; |
| 44 |
m_dirPlugin = NULL; |
| 45 |
} |
| 46 |
|
| 47 |
CMCDataManager::~CMCDataManager( void ) |
| 48 |
{ |
| 49 |
DeleteDataTypeArray(); |
| 50 |
CleanPluginDirectory(); |
| 51 |
} |
| 52 |
|
| 53 |
CMCDataManager::plugin_info *CMCDataManager::pinfo_new( const char *path ) |
| 54 |
{ |
| 55 |
plugin_info * pinfo = new plugin_info; |
| 56 |
if ( pinfo == NULL ) |
| 57 |
return NULL; |
| 58 |
/* resolve file path */ |
| 59 |
pinfo->file = sl4_file_new( path ); |
| 60 |
if ( pinfo->file == NULL ) |
| 61 |
{ |
| 62 |
delete pinfo; |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
/* load library */ |
| 66 |
path = sl4_file_get_path( pinfo->file ); |
| 67 |
pinfo->library = LoadLibrary( path ); |
| 68 |
if ( pinfo->library == NULL ) |
| 69 |
{ |
| 70 |
sl4_file_delete( pinfo->file ); |
| 71 |
delete pinfo; |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
/* get address pointer of GetDataTypeTitle() function */ |
| 75 |
pinfo->title_func = ( mcdata_title_func_t ) GetProcAddress( pinfo->library, MCDATA_TITLE_FUNCNAME ); |
| 76 |
/* get address pointer of GetDataTypeFilter() function */ |
| 77 |
pinfo->filter_func = ( mcdata_filter_func_t ) GetProcAddress( pinfo->library, MCDATA_FILTER_FUNCNAME ); |
| 78 |
/* get address pointer of instance creation function */ |
| 79 |
pinfo->create_func = ( mcdata_create_func_t ) GetProcAddress( pinfo->library, MCDATA_CREATE_FUNCNAME ); |
| 80 |
if ( pinfo->title_func == NULL || pinfo->filter_func == NULL || pinfo->create_func == NULL ) |
| 81 |
{ |
| 82 |
FreeLibrary( pinfo->library ); |
| 83 |
sl4_file_delete( pinfo->file ); |
| 84 |
delete pinfo; |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
return pinfo; |
| 88 |
} |
| 89 |
|
| 90 |
BOOL CMCDataManager::pinfo_delete( plugin_info *pinfo ) |
| 91 |
{ |
| 92 |
if ( pinfo->file != NULL ) |
| 93 |
sl4_file_delete( pinfo->file ); |
| 94 |
if ( pinfo->library != NULL ) |
| 95 |
FreeLibrary( pinfo->library ); |
| 96 |
delete pinfo; |
| 97 |
return TRUE; |
| 98 |
} |
| 99 |
|
| 100 |
BOOL CMCDataManager::CreateDataTypeArray() |
| 101 |
{ |
| 102 |
m_arDataType = sl4_array_new( sizeof( plugin_info * ) ); |
| 103 |
if ( m_arDataType == NULL ) |
| 104 |
return FALSE; |
| 105 |
return TRUE; |
| 106 |
} |
| 107 |
|
| 108 |
BOOL CMCDataManager::DeleteDataTypeArray() |
| 109 |
{ |
| 110 |
if ( m_arDataType != NULL ) |
| 111 |
{ |
| 112 |
size_t i, len; |
| 113 |
len = sl4_array_length( m_arDataType ); |
| 114 |
for ( i = 0; i < len; i++ ) |
| 115 |
{ |
| 116 |
plugin_info *pinfo = sl4_array_index( m_arDataType, plugin_info *, i ); |
| 117 |
pinfo_delete( pinfo ); |
| 118 |
} |
| 119 |
sl4_array_delete( m_arDataType ); |
| 120 |
m_arDataType = NULL; |
| 121 |
} |
| 122 |
return TRUE; |
| 123 |
} |
| 124 |
|
| 125 |
BOOL CMCDataManager::AddDataTypeArray( const char *path ) |
| 126 |
{ |
| 127 |
plugin_info * pinfo; |
| 128 |
pinfo = pinfo_new( path ); |
| 129 |
if ( pinfo == NULL ) |
| 130 |
return FALSE; |
| 131 |
if ( sl4_array_add( m_arDataType, &pinfo ) != 0 ) |
| 132 |
return FALSE; |
| 133 |
return TRUE; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
BOOL CMCDataManager::InitPluginDirectory() |
| 138 |
{ |
| 139 |
/* get module path */ |
| 140 |
sl4_string_t * path; |
| 141 |
char buf[ MAX_PATH ]; |
| 142 |
size_t len; |
| 143 |
if ( GetModuleFileName( NULL, buf, MAX_PATH ) == 0 ) |
| 144 |
return NULL; |
| 145 |
len = strlen( buf ); |
| 146 |
for ( size_t i = len - 1; i > 0; i-- ) |
| 147 |
{ |
| 148 |
if ( buf[ i ] == '\\' ) |
| 149 |
{ |
| 150 |
buf[ i ] = '\0'; |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
path = sl4_string_new( buf ); |
| 155 |
if ( path == NULL ) |
| 156 |
return FALSE; |
| 157 |
|
| 158 |
/* get data plugins path */ |
| 159 |
if ( sl4_string_append( path, MCDATA_PLUGINS ) != 0 ) |
| 160 |
{ |
| 161 |
sl4_string_delete( path ); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
/* create sl4_file_t instance */ |
| 166 |
m_dirPlugin = sl4_file_new( sl4_string_get( path ) ); |
| 167 |
sl4_string_delete( path ); |
| 168 |
if ( m_dirPlugin == NULL ) |
| 169 |
return FALSE; |
| 170 |
/* directory check */ |
| 171 |
if ( sl4_file_is_directory( m_dirPlugin ) == 0 ) |
| 172 |
{ |
| 173 |
sl4_file_delete( m_dirPlugin ); |
| 174 |
m_dirPlugin = NULL; |
| 175 |
return FALSE; |
| 176 |
} |
| 177 |
return TRUE; |
| 178 |
} |
| 179 |
|
| 180 |
BOOL CMCDataManager::CleanPluginDirectory() |
| 181 |
{ |
| 182 |
if ( m_dirPlugin == NULL ) |
| 183 |
{ |
| 184 |
sl4_file_delete( m_dirPlugin ); |
| 185 |
m_dirPlugin = NULL; |
| 186 |
} |
| 187 |
return TRUE; |
| 188 |
} |
| 189 |
|
| 190 |
BOOL CMCDataManager::FindDataPlugins() |
| 191 |
{ |
| 192 |
int num = 0; |
| 193 |
/* get file lists */ |
| 194 |
HANDLE hFile; |
| 195 |
WIN32_FIND_DATA fData; |
| 196 |
sl4_string_t *pattern = sl4_string_new( sl4_file_get_path( m_dirPlugin ) ); |
| 197 |
if ( pattern == NULL ) |
| 198 |
return FALSE; |
| 199 |
sl4_string_append( pattern, sl4_file_get_separator() ); |
| 200 |
sl4_string_append( pattern, "*" ); |
| 201 |
hFile = FindFirstFile( sl4_string_get( pattern ), &fData ); |
| 202 |
sl4_string_delete( pattern ); |
| 203 |
if ( hFile == INVALID_HANDLE_VALUE ) |
| 204 |
{ |
| 205 |
return FALSE; |
| 206 |
} |
| 207 |
do |
| 208 |
{ |
| 209 |
if ( !( fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) |
| 210 |
{ |
| 211 |
sl4_string_t * fname = sl4_string_new( sl4_file_get_path( m_dirPlugin ) ); |
| 212 |
if ( fname != NULL ) |
| 213 |
{ |
| 214 |
if ( sl4_string_append( fname, sl4_file_get_separator() ) == 0 && |
| 215 |
sl4_string_append( fname, fData.cFileName ) == 0 ) |
| 216 |
if ( AddDataTypeArray( sl4_string_get( fname ) ) ) |
| 217 |
num++; /* increment number of plugins */ |
| 218 |
sl4_string_delete( fname ); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
while ( FindNextFile( hFile, &fData ) ); |
| 223 |
FindClose( hFile ); |
| 224 |
if ( num == 0 ) |
| 225 |
return FALSE; |
| 226 |
return TRUE; |
| 227 |
} |
| 228 |
|
| 229 |
BOOL CMCDataManager::Initialize() |
| 230 |
{ |
| 231 |
if ( !CreateDataTypeArray() ) |
| 232 |
return FALSE; |
| 233 |
if ( !InitPluginDirectory() ) |
| 234 |
return FALSE; |
| 235 |
if ( !FindDataPlugins() ) |
| 236 |
return FALSE; |
| 237 |
return TRUE; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
CMultiChannelData *CMCDataManager::CreateInstance( size_t n ) |
| 242 |
{ |
| 243 |
size_t len; |
| 244 |
len = sl4_array_length( this->m_arDataType ); |
| 245 |
if ( n >= len ) |
| 246 |
return NULL; |
| 247 |
plugin_info *pinfo = sl4_array_index( m_arDataType, plugin_info *, 0 ); |
| 248 |
return pinfo->create_func(); |
| 249 |
} |
| 250 |
|
| 251 |
BOOL CMCDataManager::InstallFileOpenMenu( CMenu *ParentMenu ) |
| 252 |
{ |
| 253 |
BOOL bReturn = FALSE ; |
| 254 |
if ( ParentMenu != NULL ) |
| 255 |
{ |
| 256 |
CMenu * FileMenu = NULL; |
| 257 |
CMenu *OpenMenu = NULL; |
| 258 |
UINT FilePosition = 0; |
| 259 |
UINT OpenPosition = 1; |
| 260 |
// get file menu. |
| 261 |
FileMenu = ParentMenu->GetSubMenu( FilePosition ); |
| 262 |
if ( FileMenu != NULL ) |
| 263 |
{ |
| 264 |
// get open menu. |
| 265 |
OpenMenu = FileMenu->GetSubMenu( OpenPosition ); |
| 266 |
} |
| 267 |
if ( OpenMenu != NULL ) |
| 268 |
{ |
| 269 |
size_t i, len; |
| 270 |
len = sl4_array_length( this->m_arDataType ); |
| 271 |
for ( i = 0; i < len; i++ ) |
| 272 |
{ |
| 273 |
plugin_info *pinfo = sl4_array_index( m_arDataType, plugin_info *, i ); |
| 274 |
const char *title = pinfo->title_func(); |
| 275 |
if ( i == 0 ) |
| 276 |
{ |
| 277 |
OpenMenu->ModifyMenu( 0, MF_BYPOSITION, ID_MCDATAFILE_OPEN_CMD01 + i, _T( title ) ) ; |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
OpenMenu->AppendMenu( MF_STRING, ID_MCDATAFILE_OPEN_CMD01 + i, _T( title ) ) ; |
| 282 |
} |
| 283 |
} |
| 284 |
bReturn = TRUE; |
| 285 |
} |
| 286 |
} |
| 287 |
return bReturn ; |
| 288 |
} |
| 289 |
|
| 290 |
BOOL CMCDataManager::GetPluginTitle( size_t n, char *buf, size_t buflen ) |
| 291 |
{ |
| 292 |
size_t len; |
| 293 |
len = sl4_array_length( this->m_arDataType ); |
| 294 |
if ( n >= len ) |
| 295 |
return FALSE; |
| 296 |
plugin_info *pinfo = sl4_array_index( m_arDataType, plugin_info *, n ); |
| 297 |
const char *title = pinfo->title_func(); |
| 298 |
len = strlen( title ); |
| 299 |
if ( len >= buflen ) |
| 300 |
return FALSE; |
| 301 |
strcpy( buf, title ); |
| 302 |
return TRUE; |
| 303 |
} |
| 304 |
|
| 305 |
int CMCDataManager::m_iMaxFileTypes = MAX_FILE_TYPES; |